Undefined behavior in unit-tests is no fun. assert_string_equal()
expects properly zero-terminated strings. None of the encoded test
strings contain a termination, hence add it manually.
Without this change, the test was often failing with a wrong error
message:
[==========] tests_lib_b64_decode-test(tests): Running 1 test(s).
[ RUN ] test_b64_decode
[ ERROR ] --- "AB" != "AB"
[ LINE ] --- tests/lib/b64_decode-test.c:38: error: Failure!
[ FAILED ] test_b64_decode
[==========] tests_lib_b64_decode-test(tests): 1 test(s) run.
Probably due to unprintable characters in the string. No idea why
my system is more susceptible to this issue.
Change-Id: Id1bd2c3ff06bc1d4e5aa21ddd0f1d5802540999d
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/84088
Reviewed-by: Jakub Czapiga <czapiga@google.com>
Reviewed-by: Nicholas Sudsgaard <devel+coreboot@nsudsgaard.com>
Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
Reviewed-by: Matt DeVillier <matt.devillier@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
64 lines
1.2 KiB
C
64 lines
1.2 KiB
C
/* 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="
|
|
};
|
|
|
|
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)));
|
|
|
|
decoded[res] = 0x00;
|
|
|
|
assert_string_equal((const char *)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 cb_run_group_tests(tests, NULL, NULL);
|
|
}
|