1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

tests/unittests/base64: add test case for urlsafe_encode

This commit is contained in:
Benjamin Valentin 2020-03-30 15:49:46 +02:00 committed by Benjamin Valentin
parent fc89531c61
commit 64d4713301
2 changed files with 77 additions and 1 deletions

View File

@ -1 +1 @@
USEMODULE += base64
USEMODULE += base64url

View File

@ -11,6 +11,7 @@
#if (TEST_BASE64_SHOW_OUTPUT == 1)
#include <stdio.h>
#endif
#include <stdint.h>
#include <string.h>
#include "embUnit.h"
#include "tests-base64.h"
@ -409,6 +410,79 @@ static void test_base64_10_decode_empty(void)
TEST_ASSERT_EQUAL_INT(0, base64_out_size);
}
static void test_base64_11_urlsafe_encode_int(void)
{
uint32_t data_in = 4345;
unsigned char expected_encoding[] = "-RAAAA==";
size_t base64_out_size = 0;
unsigned char base64_out[ strlen((char *)expected_encoding) ];
/*
* @Note:
* The first encoding attempt fails, but reveals the required out size.
*
* This size is a lower bound estimation,
* thus it can require few more bytes then the actual used size for the output.
*/
int ret = base64url_encode((void *)&data_in, sizeof(data_in), NULL, &base64_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_ERROR_BUFFER_OUT_SIZE, ret);
ret = base64url_encode((void *)&data_in, sizeof(data_in), base64_out, &base64_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_SUCCESS, ret);
for (int i = 0; i < (int)base64_out_size; ++i) {
TEST_ASSERT_MESSAGE(base64_out[i] == expected_encoding[i], \
"encoding failed!(produced unexpected output)");
}
#if (TEST_BASE64_SHOW_OUTPUT == 1)
puts("Test 11 Encoded:");
for (int i = 0; i < (int)base64_out_size; ++i) {
printf("%c", base64_out[i]);
}
printf("\nFrom:\n%x\n", data_in);
#endif
}
static void test_base64_12_urlsafe_decode_int(void)
{
unsigned char encoded_base64[] = "_____wAA==";
unsigned char expected[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0};
size_t base64_size = strlen((char *)encoded_base64);
size_t data_out_size = 0;
unsigned char data_out[ sizeof(expected) ];
int ret = base64_decode(encoded_base64, base64_size, NULL, &data_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_ERROR_BUFFER_OUT_SIZE, ret);
ret = base64_decode(encoded_base64, base64_size, NULL, &data_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_ERROR_BUFFER_OUT, ret);
ret = base64_decode(encoded_base64, base64_size, data_out, &data_out_size);
TEST_ASSERT_EQUAL_INT(BASE64_SUCCESS, ret);
for (int i = 0; i < (int)data_out_size; ++i) {
TEST_ASSERT_MESSAGE(data_out[i] == expected[i], \
"decoding failed!(produced unexpected output)");
}
#if (TEST_BASE64_SHOW_OUTPUT == 1)
puts("Test 11 Decoded:");
for (int i = 0; i < (int)data_out_size; ++i) {
printf("%x", data_out[i]);
}
printf("\nFrom:\n%x\n", (char *)encoded_base64);
#endif
}
Test *tests_base64_tests(void)
{
@ -424,6 +498,8 @@ Test *tests_base64_tests(void)
new_TestFixture(test_base64_09_encode_size_determination),
new_TestFixture(test_base64_10_encode_empty),
new_TestFixture(test_base64_10_decode_empty),
new_TestFixture(test_base64_11_urlsafe_encode_int),
new_TestFixture(test_base64_12_urlsafe_decode_int),
};
EMB_UNIT_TESTCALLER(base64_tests, NULL, NULL, fixtures);