From f5c0021e058ecba3570b1743f1d1077b2eb86565 Mon Sep 17 00:00:00 2001 From: Jakub Czapiga Date: Wed, 31 Mar 2021 12:41:29 +0200 Subject: tests: Add lib/uuid-test test case Signed-off-by: Jakub Czapiga Change-Id: I7f8498ad3d9d0d68b34aa0b48daca60545ec3f4e Reviewed-on: https://review.coreboot.org/c/coreboot/+/51976 Tested-by: build bot (Jenkins) Reviewed-by: Paul Fagerburg --- tests/lib/Makefile.inc | 4 +++ tests/lib/uuid-test.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/lib/uuid-test.c (limited to 'tests') diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 613f8e5aac..cba436118c 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -23,6 +23,7 @@ tests-y += memmove-test tests-y += crc_byte-test tests-y += compute_ip_checksum-test tests-y += memrange-test +tests-y += uuid-test string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c @@ -112,3 +113,6 @@ memrange-test-srcs += src/lib/memrange.c memrange-test-srcs += tests/stubs/console.c memrange-test-srcs += src/device/device_util.c +uuid-test-srcs += tests/lib/uuid-test.c +uuid-test-srcs += src/lib/hexstrtobin.c +uuid-test-srcs += src/lib/uuid.c diff --git a/tests/lib/uuid-test.c b/tests/lib/uuid-test.c new file mode 100644 index 0000000000..c6e40f58b1 --- /dev/null +++ b/tests/lib/uuid-test.c @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include + +/* This test checks if parse_uuid() correctly parses UUID strings with correct format. + lib/uuid does not check UUID version nor variant. + If that changes tests should be updated as appropriate. */ +static void test_parse_uuid_correct(void **state) +{ + /* lib/uuid does not check UUID version nor variant. + If that changes tests should be updated as appropriate. */ + uint8_t uuid_buf[UUID_LEN] = { 0 }; + const char *uuid_correct = "00112233-4455-6677-8899-aabbccddeeff"; + const uint8_t uuid_correct_bin[UUID_LEN] = { + 0x33, 0x22, 0x11, 0x00, 0x55, 0x44, 0x77, 0x66, + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + }; + const char *uuid_zereos = "00000000-0000-0000-0000-000000000000"; + const uint8_t uuid_zeros_bin[UUID_LEN] = { 0 }; + const char *uuid_ff = "ffffffff-ffff-ffff-ffff-ffffffffffff"; + const uint8_t uuid_ff_bin[UUID_LEN] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + }; + + assert_int_equal(0, parse_uuid(uuid_buf, uuid_correct)); + assert_memory_equal(uuid_correct_bin, uuid_buf, UUID_LEN); + + assert_int_equal(0, parse_uuid(uuid_buf, uuid_zereos)); + assert_memory_equal(uuid_zeros_bin, uuid_buf, UUID_LEN); + + assert_int_equal(0, parse_uuid(uuid_buf, uuid_ff)); + assert_memory_equal(uuid_ff_bin, uuid_buf, UUID_LEN); +} + +/* This test validates parse_uuid() feeding it with incorrectly-formatted UUID strings. + No version/variant tests are performed due to lack of handling these properties + by lib/uuid. If that changes tests should be updated as appropriate.*/ +static void test_parse_uuid_incorrect(void **state) +{ + uint8_t uuid_buf[UUID_LEN] = { 0 }; + /* First separator at incorrect index */ + const char *uuid_incorrect_1 = "ab372d0-34cc1-1337-688b-2211337777ee"; + /* Incorrect characters */ + const char *uuid_incorrect_2 = "abbce711-5@51-7777-ffff-ahchbh999911"; + /* Too long uuid string */ + const char *uuid_incorrect_3 = "a7a38502-7465-9cbd-98ea-73003906dad5dd"; + /* Too short uuid string */ + const char *uuid_incorrect_4 = "72abd839-7109-64bd-cfef-a8266"; + + assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_1)); + assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_2)); + assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_3)); + assert_int_equal(-1, parse_uuid(uuid_buf, uuid_incorrect_4)); +} + +static void test_guid(void **state) +{ + guid_t guid = GUID_INIT(0x1F4D66BB, 0xDEAD, 0xBEEF, + 0x08, 0xAB, 0x21, 0x37, 0xBB, 0x07, 0xDD, 0x4C); + guid_t guid_copy; + const guid_t guid_bin = { { + 0xBB, 0x66, 0x4D, 0x1F, + 0xAD, 0xDE, + 0xEF, 0xBE, + 0x08, 0xAB, 0x21, 0x37, 0xBB, 0x07, 0xDD, 0x4C + } }; + + /* GUID produced using GUID_INIT() macro should be identical to guid_bin + Check it by comparing memory directly and using provided comparison function */ + assert_memory_equal(guid.b, guid_bin.b, UUID_LEN); + assert_int_equal(0, guidcmp(&guid, &guid_bin)); + + /* Copy GUID and compare it with original */ + guidcpy(&guid_copy, &guid); + assert_memory_equal(guid.b, guid_copy.b, UUID_LEN); + assert_int_equal(0, guidcmp(&guid, &guid_copy)); + + /* Binary negate bits of one of GUIDs bytes. + Expect comparison to fail as GUIDs should differ */ + guid_copy.b[7] = ~guid_copy.b[7]; + assert_int_not_equal(0, guidcmp(&guid, &guid_copy)); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_parse_uuid_correct), + cmocka_unit_test(test_parse_uuid_incorrect), + cmocka_unit_test(test_guid), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} -- cgit v1.2.3