Jan Dabros | 2d0ee36 | 2020-04-20 15:19:45 +0200 | [diff] [blame] | 1 | #include <stdarg.h> |
| 2 | #include <stddef.h> |
| 3 | #include <setjmp.h> |
| 4 | #include <cmocka.h> |
| 5 | |
| 6 | #include <string.h> |
| 7 | |
| 8 | /* |
| 9 | * Important note: In every particular test, don't use any string-related |
| 10 | * functions other than function under test. We are linking against |
| 11 | * src/lib/string.c not the standard library. This is important for proper test |
| 12 | * isolation. One can use __builtin_xxx for many of the most simple str*() |
| 13 | * functions, when non-coreboot one is required. |
| 14 | */ |
| 15 | |
| 16 | struct strings_t { |
| 17 | char *str; |
| 18 | size_t size; |
| 19 | } strings[] = { |
| 20 | {"coreboot", 8}, |
| 21 | {"is\0very", 2}, /* strlen should be 2 because of the embedded \0 */ |
| 22 | {"nice\n", 5} |
| 23 | }; |
| 24 | |
| 25 | static void test_strlen_strings(void **state) |
| 26 | { |
| 27 | int i; |
| 28 | |
| 29 | for (i = 0; i < ARRAY_SIZE(strings); i++) |
| 30 | assert_int_equal(strings[i].size, strlen(strings[i].str)); |
| 31 | } |
| 32 | |
| 33 | static void test_strdup(void **state) |
| 34 | { |
| 35 | char str[] = "Hello coreboot\n"; |
| 36 | char *duplicate; |
| 37 | |
| 38 | duplicate = strdup(str); |
| 39 | |
| 40 | /* There is a more suitable Cmocka's function 'assert_string_equal()', but it |
| 41 | is using strcmp() internally. */ |
| 42 | assert_int_equal(0, memcmp(str, duplicate, __builtin_strlen(str))); |
| 43 | } |
| 44 | |
| 45 | int main(void) |
| 46 | { |
| 47 | const struct CMUnitTest tests[] = { |
| 48 | cmocka_unit_test(test_strlen_strings), |
| 49 | cmocka_unit_test(test_strdup), |
| 50 | }; |
| 51 | |
| 52 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 53 | } |