diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/base64.c | 57 | ||||
-rw-r--r-- | test/base64.tasklist | 9 | ||||
-rw-r--r-- | test/build.mk | 2 | ||||
-rw-r--r-- | test/test_config.h | 4 |
4 files changed, 72 insertions, 0 deletions
diff --git a/test/base64.c b/test/base64.c new file mode 100644 index 0000000000..9ded346c5d --- /dev/null +++ b/test/base64.c @@ -0,0 +1,57 @@ +/* Copyright 2022 The Chromium OS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Test Base-64 encoding. + */ + +#include <stdio.h> +#include "base64.h" +#include "common.h" +#include "test_util.h" +#include "util.h" + +static char results[100]; +static size_t index; + +static void printer(char c) +{ + if (index < sizeof(results)) + results[index++] = c; +} + +static int test_encode(void) +{ + const char *data = "\x00\x01\x02\x80\xf0\ffabcdefghij " + "random text to wrap at 64 characters at most"; + const struct { + size_t len; /* Length of input. */ + const char *encoding; /* Expected encoding. */ + } cases[] = { + { 1, "AA==\n" }, + { 2, "AAE=\n" }, + { 3, "AAEC\n" }, + { 4, "AAECgA==\n" }, + { 60, "AAECgPAMZmFiY2RlZmdoaWogcmFuZG9tIHRleHQg" + "dG8gd3JhcCBhdCA2NCBjaGFy\nYWN0ZXJzIGF0IG1v\n" }, + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + index = 0; + base64_encode_to_console(data, cases[i].len, printer); + TEST_ASSERT(index == strlen(cases[i].encoding)); + TEST_ASSERT(memcmp(cases[i].encoding, results, index) == 0); + } + + return EC_SUCCESS; +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_encode); + + test_print_result(); +} diff --git a/test/base64.tasklist b/test/base64.tasklist new file mode 100644 index 0000000000..243a3b683f --- /dev/null +++ b/test/base64.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2022 The Chromium OS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ diff --git a/test/build.mk b/test/build.mk index 4072459707..16638acaae 100644 --- a/test/build.mk +++ b/test/build.mk @@ -13,6 +13,7 @@ test-list-host=$(TEST_LIST_HOST) else test-list-host = aes test-list-host += base32 +test-list-host += base64 test-list-host += button test-list-host += cec test-list-host += console_edit @@ -58,6 +59,7 @@ endif aes-y=aes.o base32-y=base32.o +base64-y=base64.o button-y=button.o cec-y=cec.o console_edit-y=console_edit.o diff --git a/test/test_config.h b/test/test_config.h index f7b222302f..493d255626 100644 --- a/test/test_config.h +++ b/test/test_config.h @@ -27,6 +27,10 @@ #define CONFIG_BASE32 #endif +#ifdef TEST_BASE64 +#define CONFIG_BASE64 +#endif + #ifdef TEST_FLASH_LOG #define CONFIG_CRC8 #define CONFIG_FLASH_ERASED_VALUE32 (-1U) |