Anna Karaś has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/43088 )
Change subject: tests: Add lib/hexstrtobin-test test case ......................................................................
tests: Add lib/hexstrtobin-test test case
Implement unit tests for lib/hexstrtobin module.
Signed-off-by: Anna Karas aka@semihalf.com Change-Id: Id929b07936ea180a798309e5acb1dacf1b396e32 --- M tests/lib/Makefile.inc A tests/lib/hexstrtobin-test.c 2 files changed, 53 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/88/43088/1
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 5d47fea..594c68e 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -2,9 +2,13 @@
tests-y += string-test tests-y += b64_decode-test +tests-y += hexstrtobin-test
string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c
b64_decode-test-srcs += tests/lib/b64_decode-test.c b64_decode-test-srcs += src/lib/b64_decode.c + +hexstrtobin-test-srcs += tests/lib/hexstrtobin-test.c +hexstrtobin-test-srcs += src/lib/hexstrtobin.c diff --git a/tests/lib/hexstrtobin-test.c b/tests/lib/hexstrtobin-test.c new file mode 100644 index 0000000..419ffe9 --- /dev/null +++ b/tests/lib/hexstrtobin-test.c @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <stdlib.h> +#include <string.h> +#include <lib.h> +#include <stdint.h> +#include <tests/test.h> + +struct hexstr_t { + char *str; + int val[2]; + size_t res; +} hexstr[] = { + {"277a", {39, 122}, 2}, + + /* This example should give us similar result as the one above, + although it is one hex character longer. */ + {"277ab", {39, 122}, 2}, + {"\nB*e/ef-", {190, 239}, 2}, +}; + +static void test_hexstrtobin(void **state) +{ + uint8_t *buf; + size_t res, len; + + for (int i = 0; i < ARRAY_SIZE(hexstr); i++) { + len = strlen(hexstr[i].str) / 2 + 1; + buf = malloc(len); + res = hexstrtobin(hexstr[i].str, buf, len); + + assert_int_equal(hexstr[i].res, res); + + for (int j = 0; j < res; j++) { + assert_int_equal(hexstr[i].val[j], buf[j]); + } + + free(buf); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_hexstrtobin), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}