From nobody Tue Jul 28 02:48:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+111885+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+111885+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1701312388; cv=none; d=zohomail.com; s=zohoarc; b=io+6jKlxbVTOnGelz1XliwrU8U2kuMFTsJkB+13dh+8O+uZc2lBli00l02usRdUsk0R1UYIxYKCiDBveFXFXbtJrem0DK11Yk4cRbr7Y22h5w6kuSVhrPtE0L6HCdyOhiX4jzYuMgwzUb5vI75F2InI+yG2WtbniZ/HGzg1oic8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701312388; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=/ZLQhI0gEEpozaidbtLSA6NvMw76TUwH8v49KaWSUhI=; b=WTm3Mw2aQZzM5YiVwS8wJxHIQeWBt1K+uVyrmqScj6y/Zi9la5GKQfZoVPf0YY1p78PssTGx/lH37mNOQKPQIkcXUQZpEfbOKaIJL6pr/7XJQJrhmtcGbp3GNgqHoGwKrJaKMczU+pmsEIOdHvn0XUXncpJg+SvCUdO37pVR8ME= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+111885+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1701312388227458.83376216567206; Wed, 29 Nov 2023 18:46:28 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=+YJiFmkmXXI6gnSQTVQZPDGlCSsQ4W3TTfYmTMYiVAM=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1701312387; v=1; b=MuN+vagK5LNlL5ZEh5WYDmqOL3Xye7c6ACCiSAmPD+QOgicHiwNLdBgumUdQlAwzWg0BSi9P PyyAPAU4F+xdTFG5+q3TmK5Cx96K0T4s4NpduRus+Yt8/NNefnAcPFEye3QlbBfwLtxUtC/P/m3 roFucfBIt8OLBRGS73aiQljI= X-Received: by 127.0.0.2 with SMTP id 0a6FYY1788612xod9yQIV9PY; Wed, 29 Nov 2023 18:46:27 -0800 X-Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by mx.groups.io with SMTP id smtpd.web10.63603.1701312386996929105 for ; Wed, 29 Nov 2023 18:46:27 -0800 X-Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-40b4744d603so3550055e9.2 for ; Wed, 29 Nov 2023 18:46:26 -0800 (PST) X-Gm-Message-State: 4UJMDfcvtAJe4B1i72bkYarQx1787277AA= X-Google-Smtp-Source: AGHT+IG1L2D9ePfrt4UKQWPkqgPccwfeJEMmPULotv7wgODPKNrmxYjCFbjwCuD+UWGmerhdTkskkA== X-Received: by 2002:a05:600c:2a16:b0:40b:2b12:1463 with SMTP id w22-20020a05600c2a1600b0040b2b121463mr16156067wme.9.1701312384880; Wed, 29 Nov 2023 18:46:24 -0800 (PST) X-Received: from PC-PEDRO-ARCH.lan ([2001:8a0:7280:5801:9441:3dce:686c:bfc7]) by smtp.gmail.com with ESMTPSA id b19-20020a05600c4e1300b0040648217f4fsm3986232wmq.39.2023.11.29.18.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 18:46:22 -0800 (PST) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Savva Mitrofanov , Pedro Falcato , Liming Gao , Michael D Kinney , Zhiguang Liu Subject: [edk2-devel] [PATCH 2/2] MdePkg/Test: Add google tests for BaseLib Date: Thu, 30 Nov 2023 02:46:11 +0000 Message-ID: <20231130024611.67135-3-pedro.falcato@gmail.com> In-Reply-To: <20231130024611.67135-1-pedro.falcato@gmail.com> References: <20231130024611.67135-1-pedro.falcato@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,pedro.falcato@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1701312389670000003 Content-Type: text/plain; charset="utf-8" Add GoogleTestBaseLib, which contains gtest unit tests for BaseLib. For now, only add checksum tests for CRC32C and CRC16; these tests check for correctness on various inputs using precomputed hashes. Signed-off-by: Pedro Falcato Cc: Liming Gao Cc: Michael D Kinney Cc: Zhiguang Liu Reviewed-by: Michael D Kinney --- .../Library/BaseLib/GoogleTestBaseLib.inf | 31 +++++++++ .../Library/BaseLib/TestBaseLibMain.cpp | 23 +++++++ .../Library/BaseLib/TestCheckSum.cpp | 64 +++++++++++++++++++ .../SafeIntLibUintnIntnUnitTests64.cpp | 4 +- MdePkg/Test/MdePkgHostTest.dsc | 5 ++ 5 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLi= b.inf create mode 100644 MdePkg/Test/GoogleTest/Library/BaseLib/TestBaseLibMain.= cpp create mode 100644 MdePkg/Test/GoogleTest/Library/BaseLib/TestCheckSum.cpp diff --git a/MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLib.inf b= /MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLib.inf new file mode 100644 index 000000000000..c859e5f86b9e --- /dev/null +++ b/MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLib.inf @@ -0,0 +1,31 @@ +## @file +# Host OS based Application that unit tests BaseLib using Google Test +# +# Copyright (c) 2023, Pedro Falcato. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D GoogleTestBaseLib + FILE_GUID =3D 34D8CBBA-2442-455F-8454-5B06B12A8B62 + MODULE_TYPE =3D HOST_APPLICATION + VERSION_STRING =3D 1.0 + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + TestCheckSum.cpp + TestBaseLibMain.cpp + +[Packages] + MdePkg/MdePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + BaseLib diff --git a/MdePkg/Test/GoogleTest/Library/BaseLib/TestBaseLibMain.cpp b/M= dePkg/Test/GoogleTest/Library/BaseLib/TestBaseLibMain.cpp new file mode 100644 index 000000000000..1a9941492be6 --- /dev/null +++ b/MdePkg/Test/GoogleTest/Library/BaseLib/TestBaseLibMain.cpp @@ -0,0 +1,23 @@ +/** @file + Main routine for BaseLib google tests. + + Copyright (c) 2023 Pedro Falcato. All rights reserved
+ SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +// Note: Until we can --whole-archive libs, we're forced to include second= ary files from the main one. +// Yuck. + +#include "TestCheckSum.cpp" + +int +main ( + int argc, + char *argv[] + ) +{ + testing::InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/MdePkg/Test/GoogleTest/Library/BaseLib/TestCheckSum.cpp b/MdeP= kg/Test/GoogleTest/Library/BaseLib/TestCheckSum.cpp new file mode 100644 index 000000000000..fa97f11dfa9c --- /dev/null +++ b/MdePkg/Test/GoogleTest/Library/BaseLib/TestCheckSum.cpp @@ -0,0 +1,64 @@ +/** @file + Unit tests for BaseLib's checksum capabilities. + + Copyright (c) 2023 Pedro Falcato. All rights reserved
+ SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +extern "C" { +#include +#include +} + +// Precomputed crc32c and crc16-ansi for "hello" (without the null byte) +constexpr STATIC UINT32 mHelloCrc32c =3D 0x9A71BB4C; +constexpr STATIC UINT16 mHelloCrc16 =3D 0x34F6; + +TEST (Crc32c, BasicCheck) { + // Note: The magic numbers below are precomputed checksums + // Check for basic operation on even and odd numbers of bytes + EXPECT_EQ (CalculateCrc32c ("hello", 5, 0), mHelloCrc32c); + EXPECT_EQ ( + CalculateCrc32c ("longlonglonglonglong", sizeof ("longlonglonglonglong= ") - 1, 0), + 0xC50F869D + ); + EXPECT_EQ (CalculateCrc32c ("h", 1, 0), 0xB96298FC); + + // Check if a checksum with no bytes correctly yields 0 + EXPECT_EQ (CalculateCrc32c ("", 0, 0), 0U); +} + +TEST (Crc32c, MultipartCheck) { + // Test multi-part crc32c calculation. So that given a string of bytes + // s[N], crc32c(s, N, 0) =3D=3D crc32c(s[N - 1], 1, crc32c(s, N - 1, 0)) + // and all other sorts of combinations one might imagine. + UINT32 val; + + val =3D CalculateCrc32c ("hel", 3, 0); + EXPECT_EQ (CalculateCrc32c (&"hello"[3], 2, val), mHelloCrc32c); +} + +TEST (Crc16, BasicCheck) { + // Note: The magic numbers below are precomputed checksums + // Check for basic operation on even and odd numbers of bytes + EXPECT_EQ (CalculateCrc16Ansi ("hello", 5, CRC16ANSI_INIT), mHelloCrc16); + EXPECT_EQ ( + CalculateCrc16Ansi ("longlonglonglonglong", sizeof ("longlonglonglongl= ong") - 1, CRC16ANSI_INIT), + 0xF723 + ); + EXPECT_EQ (CalculateCrc16Ansi ("h", 1, CRC16ANSI_INIT), 0xAEBE); + + // Check if a checksum with no bytes correctly yields CRC16ANSI_INIT + EXPECT_EQ (CalculateCrc16Ansi ("", 0, CRC16ANSI_INIT), CRC16ANSI_INIT); +} + +TEST (Crc16, MultipartCheck) { + // Test multi-part crc16 calculation. So that given a string of bytes + // s[N], crc16(s, N, 0) =3D=3D crc16(s[N - 1], 1, crc16(s, N - 1, 0)) + // and all other sorts of combinations one might imagine. + UINT16 val; + + val =3D CalculateCrc16Ansi ("hel", 3, CRC16ANSI_INIT); + EXPECT_EQ (CalculateCrc16Ansi (&"hello"[3], 2, val), mHelloCrc16); +} diff --git a/MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/SafeIntLibUintnI= ntnUnitTests64.cpp b/MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/SafeIntL= ibUintnIntnUnitTests64.cpp index 6efdd3be7c5e..9fc60c5c71f3 100644 --- a/MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnit= Tests64.cpp +++ b/MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/SafeIntLibUintnIntnUnit= Tests64.cpp @@ -10,8 +10,8 @@ =20 #include extern "C" { - #include - #include +#include +#include } =20 TEST (ConversionTestSuite, TestSafeInt32ToUintn) { diff --git a/MdePkg/Test/MdePkgHostTest.dsc b/MdePkg/Test/MdePkgHostTest.dsc index b92b564d434a..583f8fc0ddd8 100644 --- a/MdePkg/Test/MdePkgHostTest.dsc +++ b/MdePkg/Test/MdePkgHostTest.dsc @@ -20,6 +20,7 @@ !include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc =20 [LibraryClasses] + BaseLib|MdePkg/Library/BaseLib/BaseLib.inf SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibBase.inf =20 @@ -31,6 +32,10 @@ MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsHost.inf MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/GoogleTestBaseSafeIntLib.i= nf MdePkg/Test/UnitTest/Library/DevicePathLib/TestDevicePathLibHost.inf + # + # BaseLib tests + # + MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLib.inf =20 # # Build HOST_APPLICATION Libraries --=20 2.43.0 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111885): https://edk2.groups.io/g/devel/message/111885 Mute This Topic: https://groups.io/mt/102886794/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-