diff options
Diffstat (limited to 'test/lib')
-rw-r--r-- | test/lib/Makefile | 1 | ||||
-rw-r--r-- | test/lib/alist.c | 242 | ||||
-rw-r--r-- | test/lib/asn1.c | 3 | ||||
-rw-r--r-- | test/lib/efi_device_path.c | 1 | ||||
-rw-r--r-- | test/lib/efi_image_region.c | 2 | ||||
-rw-r--r-- | test/lib/hexdump.c | 3 | ||||
-rw-r--r-- | test/lib/lmb.c | 1 | ||||
-rw-r--r-- | test/lib/rsa.c | 2 | ||||
-rw-r--r-- | test/lib/sscanf.c | 1 | ||||
-rw-r--r-- | test/lib/string.c | 3 | ||||
-rw-r--r-- | test/lib/test_aes.c | 1 | ||||
-rw-r--r-- | test/lib/test_crc8.c | 1 | ||||
-rw-r--r-- | test/lib/test_crypt.c | 1 | ||||
-rw-r--r-- | test/lib/test_errno_str.c | 1 | ||||
-rw-r--r-- | test/lib/test_print.c | 16 | ||||
-rw-r--r-- | test/lib/uuid.c | 1 |
16 files changed, 247 insertions, 33 deletions
diff --git a/test/lib/Makefile b/test/lib/Makefile index e75a263e6a4..70f14c46b1e 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -5,6 +5,7 @@ ifeq ($(CONFIG_SPL_BUILD),) obj-y += cmd_ut_lib.o obj-y += abuf.o +obj-y += alist.o obj-$(CONFIG_EFI_LOADER) += efi_device_path.o obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o obj-y += hexdump.o diff --git a/test/lib/alist.c b/test/lib/alist.c new file mode 100644 index 00000000000..d41845c7e6c --- /dev/null +++ b/test/lib/alist.c @@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2023 Google LLC + * Written by Simon Glass <sjg@chromium.org> + */ + +#include <alist.h> +#include <string.h> +#include <test/lib.h> +#include <test/test.h> +#include <test/ut.h> + +struct my_struct { + uint val; + uint other_val; +}; + +enum { + obj_size = sizeof(struct my_struct), +}; + +/* Test alist_init() */ +static int lib_test_alist_init(struct unit_test_state *uts) +{ + struct alist lst; + ulong start; + + start = ut_check_free(); + + /* with a size of 0, the fields should be inited, with no memory used */ + memset(&lst, '\xff', sizeof(lst)); + ut_assert(alist_init_struct(&lst, struct my_struct)); + ut_asserteq_ptr(NULL, lst.data); + ut_asserteq(0, lst.count); + ut_asserteq(0, lst.alloc); + ut_assertok(ut_check_delta(start)); + alist_uninit(&lst); + ut_asserteq_ptr(NULL, lst.data); + ut_asserteq(0, lst.count); + ut_asserteq(0, lst.alloc); + + /* use an impossible size */ + ut_asserteq(false, alist_init(&lst, obj_size, + CONFIG_SYS_MALLOC_LEN)); + ut_assertnull(lst.data); + ut_asserteq(0, lst.count); + ut_asserteq(0, lst.alloc); + + /* use a small size */ + ut_assert(alist_init(&lst, obj_size, 4)); + ut_assertnonnull(lst.data); + ut_asserteq(0, lst.count); + ut_asserteq(4, lst.alloc); + + /* free it */ + alist_uninit(&lst); + ut_asserteq_ptr(NULL, lst.data); + ut_asserteq(0, lst.count); + ut_asserteq(0, lst.alloc); + ut_assertok(ut_check_delta(start)); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_alist_init, 0); + +/* Test alist_get() and alist_getd() */ +static int lib_test_alist_get(struct unit_test_state *uts) +{ + struct alist lst; + ulong start; + void *ptr; + + start = ut_check_free(); + + ut_assert(alist_init(&lst, obj_size, 3)); + ut_asserteq(0, lst.count); + ut_asserteq(3, lst.alloc); + + ut_assertnull(alist_get_ptr(&lst, 2)); + ut_assertnull(alist_get_ptr(&lst, 3)); + + ptr = alist_ensure_ptr(&lst, 1); + ut_assertnonnull(ptr); + ut_asserteq(2, lst.count); + ptr = alist_ensure_ptr(&lst, 2); + ut_asserteq(3, lst.count); + ut_assertnonnull(ptr); + + ptr = alist_ensure_ptr(&lst, 3); + ut_assertnonnull(ptr); + ut_asserteq(4, lst.count); + ut_asserteq(6, lst.alloc); + + ut_assertnull(alist_get_ptr(&lst, 4)); + + alist_uninit(&lst); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_alist_get, 0); + +/* Test alist_has() */ +static int lib_test_alist_has(struct unit_test_state *uts) +{ + struct alist lst; + ulong start; + void *ptr; + + start = ut_check_free(); + + ut_assert(alist_init(&lst, obj_size, 3)); + + ut_assert(!alist_has(&lst, 0)); + ut_assert(!alist_has(&lst, 1)); + ut_assert(!alist_has(&lst, 2)); + ut_assert(!alist_has(&lst, 3)); + + /* create a new one to force expansion */ + ptr = alist_ensure_ptr(&lst, 4); + ut_assertnonnull(ptr); + + ut_assert(alist_has(&lst, 0)); + ut_assert(alist_has(&lst, 1)); + ut_assert(alist_has(&lst, 2)); + ut_assert(alist_has(&lst, 3)); + ut_assert(alist_has(&lst, 4)); + ut_assert(!alist_has(&lst, 5)); + + alist_uninit(&lst); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_alist_has, 0); + +/* Test alist_ensure() */ +static int lib_test_alist_ensure(struct unit_test_state *uts) +{ + struct my_struct *ptr3, *ptr4; + struct alist lst; + ulong start; + + start = ut_check_free(); + + ut_assert(alist_init_struct(&lst, struct my_struct)); + ut_asserteq(obj_size, lst.obj_size); + ut_asserteq(0, lst.count); + ut_asserteq(0, lst.alloc); + ptr3 = alist_ensure_ptr(&lst, 3); + ut_asserteq(4, lst.count); + ut_asserteq(4, lst.alloc); + ut_assertnonnull(ptr3); + ptr3->val = 3; + + ptr4 = alist_ensure_ptr(&lst, 4); + ut_asserteq(8, lst.alloc); + ut_asserteq(5, lst.count); + ut_assertnonnull(ptr4); + ptr4->val = 4; + ut_asserteq(4, alist_get(&lst, 4, struct my_struct)->val); + + ut_asserteq_ptr(ptr4, alist_ensure(&lst, 4, struct my_struct)); + + alist_ensure(&lst, 4, struct my_struct)->val = 44; + ut_asserteq(44, alist_get(&lst, 4, struct my_struct)->val); + ut_asserteq(3, alist_get(&lst, 3, struct my_struct)->val); + ut_assertnull(alist_get(&lst, 7, struct my_struct)); + ut_asserteq(8, lst.alloc); + ut_asserteq(5, lst.count); + + /* add some more, checking handling of malloc() failure */ + malloc_enable_testing(0); + ut_assertnonnull(alist_ensure(&lst, 7, struct my_struct)); + ut_assertnull(alist_ensure(&lst, 8, struct my_struct)); + malloc_disable_testing(); + + lst.flags &= ~ALISTF_FAIL; + ut_assertnonnull(alist_ensure(&lst, 8, struct my_struct)); + ut_asserteq(16, lst.alloc); + ut_asserteq(9, lst.count); + + alist_uninit(&lst); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_alist_ensure, 0); + +/* Test alist_add() bits not tested by lib_test_alist_ensure() */ +static int lib_test_alist_add(struct unit_test_state *uts) +{ + struct my_struct data, *ptr, *ptr2; + const struct my_struct *chk; + struct alist lst; + ulong start; + + start = ut_check_free(); + + ut_assert(alist_init_struct(&lst, struct my_struct)); + + data.val = 123; + data.other_val = 456; + ptr = alist_add(&lst, data); + ut_assertnonnull(ptr); + ut_asserteq(4, lst.alloc); + ut_asserteq(1, lst.count); + + ut_asserteq(123, ptr->val); + ut_asserteq(456, ptr->other_val); + + ptr2 = alist_add_placeholder(&lst); + ut_assertnonnull(ptr2); + + ptr2->val = 321; + ptr2->other_val = 654; + + chk = alist_get(&lst, 1, struct my_struct); + ut_asserteq(321, chk->val); + ut_asserteq(654, chk->other_val); + + ptr2 = alist_getw(&lst, 1, struct my_struct); + ut_asserteq(321, ptr2->val); + ut_asserteq(654, ptr2->other_val); + + alist_uninit(&lst); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_alist_add, 0); diff --git a/test/lib/asn1.c b/test/lib/asn1.c index 4842b7058ac..f0c7819e408 100644 --- a/test/lib/asn1.c +++ b/test/lib/asn1.c @@ -135,7 +135,6 @@ static int lib_asn1_x509(struct unit_test_state *uts) return CMD_RET_SUCCESS; } - LIB_TEST(lib_asn1_x509, 0); #endif /* CONFIG_X509_CERTIFICATE_PARSER */ @@ -324,7 +323,6 @@ static int lib_asn1_pkcs7(struct unit_test_state *uts) return CMD_RET_SUCCESS; } - LIB_TEST(lib_asn1_pkcs7, 0); #endif /* CONFIG_PKCS7_MESSAGE_PARSER */ @@ -386,6 +384,5 @@ static int lib_asn1_pkey(struct unit_test_state *uts) return CMD_RET_SUCCESS; } - LIB_TEST(lib_asn1_pkey, 0); #endif /* CONFIG_RSA_PUBLIC_KEY_PARSER */ diff --git a/test/lib/efi_device_path.c b/test/lib/efi_device_path.c index 290c8768fa4..5cc001e209e 100644 --- a/test/lib/efi_device_path.c +++ b/test/lib/efi_device_path.c @@ -45,5 +45,4 @@ static int lib_test_efi_dp_check_length(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_efi_dp_check_length, 0); diff --git a/test/lib/efi_image_region.c b/test/lib/efi_image_region.c index 3ca49dc4a2e..2102539ea70 100644 --- a/test/lib/efi_image_region.c +++ b/test/lib/efi_image_region.c @@ -65,7 +65,6 @@ static int lib_test_efi_image_region_add(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_efi_image_region_add, 0); static int lib_test_efi_image_region_sort(struct unit_test_state *uts) @@ -158,5 +157,4 @@ static int lib_test_efi_image_region_sort(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_efi_image_region_sort, 0); diff --git a/test/lib/hexdump.c b/test/lib/hexdump.c index d531a830398..7b4592d175f 100644 --- a/test/lib/hexdump.c +++ b/test/lib/hexdump.c @@ -31,7 +31,6 @@ static int lib_test_hex_to_bin(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_hex_to_bin, 0); static int lib_test_hex2bin(struct unit_test_state *uts) @@ -61,7 +60,6 @@ static int lib_test_hex2bin(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_hex2bin, 0); static int lib_test_bin2hex(struct unit_test_state *uts) @@ -91,5 +89,4 @@ static int lib_test_bin2hex(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_bin2hex, 0); diff --git a/test/lib/lmb.c b/test/lib/lmb.c index 4b5b6e5e209..3c66138f732 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -360,7 +360,6 @@ static int lib_test_lmb_noreserved(struct unit_test_state *uts) /* simulate 512 MiB RAM beginning at 1.5GiB */ return test_noreserved(uts, 0xE0000000, 4, 1); } - LIB_TEST(lib_test_lmb_noreserved, 0); static int lib_test_lmb_unaligned_size(struct unit_test_state *uts) diff --git a/test/lib/rsa.c b/test/lib/rsa.c index 40f70010c78..129d03ab7dd 100644 --- a/test/lib/rsa.c +++ b/test/lib/rsa.c @@ -158,7 +158,6 @@ static int lib_rsa_verify_valid(struct unit_test_state *uts) return CMD_RET_SUCCESS; } - LIB_TEST(lib_rsa_verify_valid, 0); /** @@ -200,6 +199,5 @@ static int lib_rsa_verify_invalid(struct unit_test_state *uts) return CMD_RET_SUCCESS; } - LIB_TEST(lib_rsa_verify_invalid, 0); #endif /* RSA_VERIFY_WITH_PKEY */ diff --git a/test/lib/sscanf.c b/test/lib/sscanf.c index 9fe5521749f..3a2ec8ffa5f 100644 --- a/test/lib/sscanf.c +++ b/test/lib/sscanf.c @@ -169,5 +169,4 @@ static int lib_sscanf(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_sscanf, 0); diff --git a/test/lib/string.c b/test/lib/string.c index d08dbca9291..8d22f3fd68f 100644 --- a/test/lib/string.c +++ b/test/lib/string.c @@ -93,7 +93,6 @@ static int lib_memset(struct unit_test_state *uts) } return 0; } - LIB_TEST(lib_memset, 0); /** @@ -157,7 +156,6 @@ static int lib_memcpy(struct unit_test_state *uts) } return 0; } - LIB_TEST(lib_memcpy, 0); /** @@ -192,7 +190,6 @@ static int lib_memmove(struct unit_test_state *uts) } return 0; } - LIB_TEST(lib_memmove, 0); /** lib_memdup() - unit test for memdup() */ diff --git a/test/lib/test_aes.c b/test/lib/test_aes.c index cfd9d8ca5a9..6d9068c4f79 100644 --- a/test/lib/test_aes.c +++ b/test/lib/test_aes.c @@ -163,5 +163,4 @@ static int lib_test_aes(struct unit_test_state *uts) return ret; } - LIB_TEST(lib_test_aes, 0); diff --git a/test/lib/test_crc8.c b/test/lib/test_crc8.c index 0dac97bc5bf..52be2dc06c4 100644 --- a/test/lib/test_crc8.c +++ b/test/lib/test_crc8.c @@ -25,5 +25,4 @@ static int lib_crc8(struct unit_test_state *uts) { return 0; } - LIB_TEST(lib_crc8, 0); diff --git a/test/lib/test_crypt.c b/test/lib/test_crypt.c index dcdadd992c1..b6dd5f07b86 100644 --- a/test/lib/test_crypt.c +++ b/test/lib/test_crypt.c @@ -59,5 +59,4 @@ static int lib_crypt(struct unit_test_state *uts) return CMD_RET_SUCCESS; } - LIB_TEST(lib_crypt, 0); diff --git a/test/lib/test_errno_str.c b/test/lib/test_errno_str.c index 67f76442b27..967ecfd56f1 100644 --- a/test/lib/test_errno_str.c +++ b/test/lib/test_errno_str.c @@ -41,5 +41,4 @@ static int lib_errno_str(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_errno_str, 0); diff --git a/test/lib/test_print.c b/test/lib/test_print.c index c7fc50a1de1..cd7f3f85769 100644 --- a/test/lib/test_print.c +++ b/test/lib/test_print.c @@ -17,13 +17,10 @@ DECLARE_GLOBAL_DATA_PTR; static int test_print_freq(struct unit_test_state *uts, uint64_t freq, char *expected) { - ut_silence_console(uts); - console_record_reset_enable(); print_freq(freq, ";\n"); - ut_unsilence_console(uts); console_record_readline(uts->actual_str, sizeof(uts->actual_str)); ut_asserteq_str(expected, uts->actual_str); - ut_assertok(ut_check_console_end(uts)); + ut_assert_console_end(); return 0; } @@ -41,19 +38,15 @@ static int lib_test_print_freq(struct unit_test_state *uts) ut_assertok(test_print_freq(uts, 54321987654321, "54321.99 GHz;")); return 0; } - -LIB_TEST(lib_test_print_freq, 0); +LIB_TEST(lib_test_print_freq, UTF_CONSOLE); static int test_print_size(struct unit_test_state *uts, uint64_t freq, char *expected) { - ut_silence_console(uts); - console_record_reset_enable(); print_size(freq, ";\n"); - ut_unsilence_console(uts); console_record_readline(uts->actual_str, sizeof(uts->actual_str)); ut_asserteq_str(expected, uts->actual_str); - ut_assertok(ut_check_console_end(uts)); + ut_assert_console_end(); return 0; } @@ -74,5 +67,4 @@ static int lib_test_print_size(struct unit_test_state *uts) ut_assertok(test_print_size(uts, 54321987654321, "49.4 TiB;")); return 0; } - -LIB_TEST(lib_test_print_size, 0); +LIB_TEST(lib_test_print_size, UTF_CONSOLE); diff --git a/test/lib/uuid.c b/test/lib/uuid.c index 0914f2c47e7..8fe65dbf78b 100644 --- a/test/lib/uuid.c +++ b/test/lib/uuid.c @@ -36,5 +36,4 @@ static int lib_test_uuid_to_le(struct unit_test_state *uts) return 0; } - LIB_TEST(lib_test_uuid_to_le, 0); |