Anna Karas has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
tests: Add lib/b64_decode-test test case
Implement unit tests for lib/b64_decode module.
Signed-off-by: Anna Karas aka@semihalf.com Change-Id: Id5fe9272e30eaff3d086a95241b3819101089c2b --- M tests/lib/Makefile.inc A tests/lib/b64_decode-test.c 2 files changed, 70 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/13/42313/1
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 61f99bf..abb279c 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -4,6 +4,10 @@ # two examples - first should be simply string.c, second should use -wrap
tests-y += string-test +tests-y += b64_decode-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 diff --git a/tests/lib/b64_decode-test.c b/tests/lib/b64_decode-test.c new file mode 100644 index 0000000..9426236 --- /dev/null +++ b/tests/lib/b64_decode-test.c @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <b64_decode.h> +#include <tests/test.h> + +struct messages_t { + const char *enc; + const char *dec; +} messages[] = { + {"QQ==", "A"}, + {"QUI=", "AB"}, + {"QUJD", "ABC"}, + {"QUJDRA==", "ABCD"}, + {"SGVsbG8=", "Hello"}, + {"SGVsbG8h", "Hello!"} +}; + +const char *invalid[] = { + "QQ=-=", + "SGVsbG-8=" +}; + +/* Provide necessary definition in order to satisfy dependencies. */ +int do_printk(int msg_level, const char *fmt, ...) { + return 0; +} + +static void test_b64_decode(void **state) +{ + uint8_t *decoded; + size_t res; + + for (int i = 0; i < ARRAY_SIZE(messages); i++) { + decoded = malloc(strlen(messages[i].enc) * sizeof(char)); + + res = b64_decode((uint8_t *)messages[i].enc, strlen(messages[i].enc), decoded); + + assert_int_equal(res, (strlen(messages[i].dec))); + assert_string_equal(decoded, messages[i].dec); + + free(decoded); + } + + for (int i = 0; i < ARRAY_SIZE(invalid); i++) { + decoded = malloc(strlen(invalid[i]) * sizeof(char)); + + res = b64_decode((uint8_t *)invalid[i], strlen(invalid[i]), decoded); + + assert_int_equal(res, 0); + + free(decoded); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_b64_decode), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}
build bot (Jenkins) has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 1:
(1 comment)
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c File tests/lib/b64_decode-test.c:
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 28: int do_printk(int msg_level, const char *fmt, ...) { open brace '{' following function definitions go on the next line
Paul Fagerburg has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 1: Code-Review+1
Paul Menzel has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 1: Code-Review+1
Welcome to coreboot!
Julius Werner has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 1: Code-Review+2
(2 comments)
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c File tests/lib/b64_decode-test.c:
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 19: {"SGVsbG8h", "Hello!"} nit: How about adding one with line breaks since the API explicitly supports that?
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 27: /* Provide necessary definition in order to satisfy dependencies. */ This is something that many tests will need, so it might be worth factoring out somewhere? We could create something like tests/stubs/console-stub.c and put this in there (and related stuff like do_vprintk()).
Jan Dabros has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 1:
(1 comment)
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c File tests/lib/b64_decode-test.c:
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 27: /* Provide necessary definition in order to satisfy dependencies. */
This is something that many tests will need, so it might be worth factoring out somewhere? We could […]
You may take a look here - https://review.coreboot.org/c/coreboot/+/42612/5, thee is an attempt to add such a stub.
Hello build bot (Jenkins), Patrick Georgi, Martin Roth, Paul Menzel, Paul Fagerburg, Julius Werner, Jan Dabros, Paul Fagerburg,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/42313
to look at the new patch set (#2).
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
tests: Add lib/b64_decode-test test case
Implement unit tests for lib/b64_decode module.
Signed-off-by: Anna Karas aka@semihalf.com Change-Id: Id5fe9272e30eaff3d086a95241b3819101089c2b --- M tests/lib/Makefile.inc A tests/lib/b64_decode-test.c 2 files changed, 71 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/13/42313/2
Anna Karaś has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 2:
(3 comments)
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c File tests/lib/b64_decode-test.c:
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 19: {"SGVsbG8h", "Hello!"}
nit: How about adding one with line breaks since the API explicitly supports that?
Done.
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 27: /* Provide necessary definition in order to satisfy dependencies. */
You may take a look here - https://review.coreboot. […]
I see that this commit has not been merged yet, so I decided to leave it as it is for now.
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 28: int do_printk(int msg_level, const char *fmt, ...) {
open brace '{' following function definitions go on the next line
Done.
Paul Fagerburg has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 2: Code-Review+1
For any comments that you have resolved, you should mark them resolved so that we can see which ones are still outstanding.
Anna Karaś has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 2:
(2 comments)
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c File tests/lib/b64_decode-test.c:
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 19: {"SGVsbG8h", "Hello!"}
Done.
Done
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 27: /* Provide necessary definition in order to satisfy dependencies. */
I see that this commit has not been merged yet, so I decided to leave it as it is for now.
Done
Anna Karaś has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 2:
I have already updated this patch, could you please take a look at it? 😊
Paul Fagerburg has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 3: Code-Review+2
Julius Werner has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
Patch Set 3:
(1 comment)
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c File tests/lib/b64_decode-test.c:
https://review.coreboot.org/c/coreboot/+/42313/1/tests/lib/b64_decode-test.c... PS1, Line 27: /* Provide necessary definition in order to satisfy dependencies. */
Done
Can you please still do this? Who knows when that SPI flash patch is going to move forward, you shouldn't wait for that. Whoever can merge first wins.
I'm okay with doing this in a separate patch but I would like to have that stub concept (or whatever else we decide to do about this recurring boilerplate) to be in place before we start adding a slew of new tests that need it.
Julius Werner has submitted this change. ( https://review.coreboot.org/c/coreboot/+/42313 )
Change subject: tests: Add lib/b64_decode-test test case ......................................................................
tests: Add lib/b64_decode-test test case
Implement unit tests for lib/b64_decode module.
Signed-off-by: Anna Karas aka@semihalf.com Change-Id: Id5fe9272e30eaff3d086a95241b3819101089c2b Reviewed-on: https://review.coreboot.org/c/coreboot/+/42313 Reviewed-by: Paul Fagerburg pfagerburg@chromium.org Tested-by: build bot (Jenkins) no-reply@coreboot.org --- M tests/lib/Makefile.inc A tests/lib/b64_decode-test.c 2 files changed, 71 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 c490317..5d47fea 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -1,6 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only
tests-y += string-test +tests-y += b64_decode-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 diff --git a/tests/lib/b64_decode-test.c b/tests/lib/b64_decode-test.c new file mode 100644 index 0000000..452c67f --- /dev/null +++ b/tests/lib/b64_decode-test.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <b64_decode.h> +#include <tests/test.h> + +struct messages_t { + const char *enc; + const char *dec; +} messages[] = { + {"QQ==", "A"}, + {"Q\r\nUI=", "AB"}, + {"QUJD", "ABC"}, + {"\nQUJDRA==", "ABCD"}, + {"SGVsbG8\r=", "Hello"}, + {"SGVsbG8h", "Hello!"} +}; + +const char *invalid[] = { + "QQ=-=", + "SGVsbG-8=" +}; + +/* Provide necessary definition in order to satisfy dependencies. */ +int do_printk(int msg_level, const char *fmt, ...) +{ + return 0; +} + +static void test_b64_decode(void **state) +{ + uint8_t *decoded; + size_t res; + + for (int i = 0; i < ARRAY_SIZE(messages); i++) { + decoded = malloc(strlen(messages[i].enc) * sizeof(char)); + + res = b64_decode((uint8_t *)messages[i].enc, strlen(messages[i].enc), decoded); + + assert_int_equal(res, (strlen(messages[i].dec))); + assert_string_equal(decoded, messages[i].dec); + + free(decoded); + } + + for (int i = 0; i < ARRAY_SIZE(invalid); i++) { + decoded = malloc(strlen(invalid[i]) * sizeof(char)); + + res = b64_decode((uint8_t *)invalid[i], strlen(invalid[i]), decoded); + + assert_int_equal(res, 0); + + free(decoded); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_b64_decode), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}