summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnna Karas <aka@semihalf.com>2020-06-08 15:16:19 +0200
committerJulius Werner <jwerner@chromium.org>2020-07-06 23:47:40 +0000
commit072a9b96eb2036718db1212c9f35b9cfe72e3f78 (patch)
tree3224606eda9cc9e6a65577f59d00add5a2564f83 /tests
parent907ddc3e07240b17e19e2ab83c4a68e0eca9a32f (diff)
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>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Makefile.inc4
-rw-r--r--tests/lib/b64_decode-test.c67
2 files changed, 71 insertions, 0 deletions
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc
index c490317644..5d47fea887 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 0000000000..452c67f3ae
--- /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);
+}