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); +}
build bot (Jenkins) has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/43088 )
Change subject: tests: Add lib/hexstrtobin-test test case ......................................................................
Patch Set 1:
(1 comment)
https://review.coreboot.org/c/coreboot/+/43088/1/tests/lib/hexstrtobin-test.... File tests/lib/hexstrtobin-test.c:
https://review.coreboot.org/c/coreboot/+/43088/1/tests/lib/hexstrtobin-test.... PS1, Line 34: for (int j = 0; j < res; j++) { braces {} are not necessary for single statement blocks
Hello build bot (Jenkins), Patrick Georgi, Martin Roth,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/43088
to look at the new patch set (#2).
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, 52 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/88/43088/2
Paul Fagerburg has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/43088 )
Change subject: tests: Add lib/hexstrtobin-test test case ......................................................................
Patch Set 2:
(2 comments)
https://review.coreboot.org/c/coreboot/+/43088/2/tests/lib/hexstrtobin-test.... File tests/lib/hexstrtobin-test.c:
https://review.coreboot.org/c/coreboot/+/43088/2/tests/lib/hexstrtobin-test.... PS2, Line 11: int val[2]; This should not be hard-coded, because it restricts the test cases you can have. Also, if the size is hard-coded, then using `res` to say how many bytes are in the result is redundant.
https://review.coreboot.org/c/coreboot/+/43088/2/tests/lib/hexstrtobin-test.... PS2, Line 14: {"277a", {39, 122}, 2}, Add more test cases, such as two hex digits, an odd number of hex digits with non-hex values, and a case where there is only one hex digit, and so there is no result.
Hello build bot (Jenkins), Patrick Georgi, Martin Roth, Paul Fagerburg, Julius Werner,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/43088
to look at the new patch set (#3).
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, 52 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/88/43088/3
Anna Karaś has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/43088 )
Change subject: tests: Add lib/hexstrtobin-test test case ......................................................................
Patch Set 3:
(2 comments)
https://review.coreboot.org/c/coreboot/+/43088/2/tests/lib/hexstrtobin-test.... File tests/lib/hexstrtobin-test.c:
https://review.coreboot.org/c/coreboot/+/43088/2/tests/lib/hexstrtobin-test.... PS2, Line 11: int val[2];
This should not be hard-coded, because it restricts the test cases you can have. […]
Done
https://review.coreboot.org/c/coreboot/+/43088/2/tests/lib/hexstrtobin-test.... PS2, Line 14: {"277a", {39, 122}, 2},
Add more test cases, such as two hex digits, an odd number of hex digits with non-hex values, and a […]
Done
Paul Fagerburg has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/43088 )
Change subject: tests: Add lib/hexstrtobin-test test case ......................................................................
Patch Set 3: Code-Review+2
Patrick Georgi has submitted this change. ( 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 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 --- M tests/lib/Makefile.inc A tests/lib/hexstrtobin-test.c 2 files changed, 52 insertions(+), 0 deletions(-)
Approvals: build bot (Jenkins): Verified Paul Fagerburg: Looks good to me, approved
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..46ad3df --- /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); +}