diff options
author | Anna Karas <aka@semihalf.com> | 2020-07-02 10:09:47 +0200 |
---|---|---|
committer | Patrick Georgi <pgeorgi@google.com> | 2020-07-07 17:26:42 +0000 |
commit | 5664bc8cf9414ac4e0f08a2b5cafb5e9f5b3eecc (patch) | |
tree | 95faa54be543ad721b7176ba235705c458b55f4c | |
parent | c130687e1e177adcd4b58e5050328ebd577eff28 (diff) |
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
Reviewed-on: https://review.coreboot.org/c/coreboot/+/43088
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r-- | tests/lib/Makefile.inc | 4 | ||||
-rw-r--r-- | tests/lib/hexstrtobin-test.c | 48 |
2 files changed, 52 insertions, 0 deletions
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 5d47fea887..594c68ec06 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 0000000000..46ad3dffe1 --- /dev/null +++ b/tests/lib/hexstrtobin-test.c @@ -0,0 +1,48 @@ +/* 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; + size_t res; +} hexstr[] = { + {.str = "A", .res = 0}, + {.str = "AB", .val = (int[]) {171}, .res = 1}, + {.str = "277a", .val = (int[]) {39, 122}, .res = 2}, + {.str = "277ab", .val = (int[]) {39, 122}, .res = 2}, + {.str = "\n\rx1234567ijkl", .val = (int[]) {18, 52, 86}, .res = 3}, + {.str = "\nB*e/ef-", .val = (int[]) {190, 239}, .res = 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); +} |