Skip to content

Commit

Permalink
MdePkg/Test: Add google tests for BaseLib
Browse files Browse the repository at this point in the history
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.

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
  • Loading branch information
heatd authored and mergify[bot] committed Dec 3, 2023
1 parent e2d4f75 commit 3e133f7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
31 changes: 31 additions & 0 deletions MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLib.inf
Original file line number Diff line number Diff line change
@@ -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 = 0x00010005
BASE_NAME = GoogleTestBaseLib
FILE_GUID = 34D8CBBA-2442-455F-8454-5B06B12A8B62
MODULE_TYPE = HOST_APPLICATION
VERSION_STRING = 1.0

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64
#

[Sources]
TestCheckSum.cpp
TestBaseLibMain.cpp

[Packages]
MdePkg/MdePkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec

[LibraryClasses]
GoogleTestLib
BaseLib
18 changes: 18 additions & 0 deletions MdePkg/Test/GoogleTest/Library/BaseLib/TestBaseLibMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @file
Main routine for BaseLib google tests.
Copyright (c) 2023 Pedro Falcato. All rights reserved<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <gtest/gtest.h>

int
main (
int argc,
char *argv[]
)
{
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
}
64 changes: 64 additions & 0 deletions MdePkg/Test/GoogleTest/Library/BaseLib/TestCheckSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/** @file
Unit tests for BaseLib's checksum capabilities.
Copyright (c) 2023 Pedro Falcato. All rights reserved<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <gtest/gtest.h>
extern "C" {
#include <Base.h>
#include <Library/BaseLib.h>
}

// Precomputed crc32c and crc16-ansi for "hello" (without the null byte)
constexpr STATIC UINT32 mHelloCrc32c = 0x9A71BB4C;
constexpr STATIC UINT16 mHelloCrc16 = 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) == crc32c(s[N - 1], 1, crc32c(s, N - 1, 0))
// and all other sorts of combinations one might imagine.
UINT32 val;

val = 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 ("longlonglonglonglong") - 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) == crc16(s[N - 1], 1, crc16(s, N - 1, 0))
// and all other sorts of combinations one might imagine.
UINT16 val;

val = CalculateCrc16Ansi ("hel", 3, CRC16ANSI_INIT);
EXPECT_EQ (CalculateCrc16Ansi (&"hello"[3], 2, val), mHelloCrc16);
}
5 changes: 5 additions & 0 deletions MdePkg/Test/MdePkgHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc

[LibraryClasses]
BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibBase.inf

Expand All @@ -31,6 +32,10 @@
MdePkg/Test/UnitTest/Library/BaseLib/BaseLibUnitTestsHost.inf
MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/GoogleTestBaseSafeIntLib.inf
MdePkg/Test/UnitTest/Library/DevicePathLib/TestDevicePathLibHost.inf
#
# BaseLib tests
#
MdePkg/Test/GoogleTest/Library/BaseLib/GoogleTestBaseLib.inf

#
# Build HOST_APPLICATION Libraries
Expand Down

0 comments on commit 3e133f7

Please sign in to comment.