Browse Source

[unittests] consolidate base64 test code

consolidate base64 test code

use char type for tables to reduce memory use
  (potentially increase cache hits)
personal/stbuehler/cleanup-build
Glenn Strauss 4 years ago
parent
commit
7aff5046ac
  1. 12
      src/base64.c
  2. 138
      src/test_base64.c

12
src/base64.c

@ -10,8 +10,8 @@
*/
/* BASE64_STANDARD: "A-Z a-z 0-9 + /" maps to 0-63, pad with "=" */
static const char base64_standard_table[66] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static const short base64_standard_reverse_table[128] = {
static const char base64_standard_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static const char base64_standard_reverse_table[] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
@ -24,8 +24,8 @@ static const short base64_standard_reverse_table[128] = {
};
/* BASE64_URL: "A-Z a-z 0-9 - _" maps to 0-63, pad with "." */
static const char base64_url_table[66] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
static const short base64_url_reverse_table[128] = {
static const char base64_url_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
static const char base64_url_reverse_table[] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
@ -42,7 +42,7 @@ unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t i
size_t out_pos = 0; /* current output character (position) that is decoded. can contain partial result */
unsigned int group = 0; /* how many base64 digits in the current group were decoded already. each group has up to 4 digits */
size_t i;
const short* base64_reverse_table;
const char *base64_reverse_table;
switch (charset) {
case BASE64_STANDARD:
@ -60,7 +60,7 @@ unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t i
/* run through the whole string, converting as we go */
for (i = 0; i < in_length; i++) {
unsigned char c = (unsigned char) in[i];
short ch;
int ch;
if (c == '\0') break;
if (c >= 128) return NULL; /* only 7-bit characters allowed */

138
src/test_base64.c

@ -2,145 +2,59 @@
#include "base64.h"
static void check_all_len_0() {
buffer *check = buffer_init();
const char empty[] = "";
static const base64_charset encs[] = { BASE64_STANDARD, BASE64_URL };
static buffer *check;
{
unsigned char* check_res;
inline
static void check_base64 (char *out, const size_t out_sz, const char *in, const size_t in_len, const base64_charset enc) {
force_assert(out_sz == li_to_base64_no_padding(out, out_sz, (const unsigned char *)in, in_len, enc));
force_assert(0 == li_to_base64_no_padding(NULL, 0, NULL, 0, BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, NULL, 0, BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, empty, 0));
}
{
unsigned char* check_res;
force_assert(0 == li_to_base64_no_padding(NULL, 0, NULL, 0, BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, NULL, 0, BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, empty, 0));
}
buffer_reset(check);
force_assert(NULL != buffer_append_base64_decode(check, out, out_sz, enc));
force_assert(buffer_is_equal_string(check, in, in_len));
}
buffer_free(check);
static void check_all_len_0 (const base64_charset enc) {
check_base64(NULL, 0, "", 0, enc);
}
static void check_all_len_1() {
static void check_all_len_1 (const base64_charset enc) {
unsigned int c1;
buffer *check = buffer_init();
for (c1 = 0; c1 < 256; ++c1) {
{
unsigned char in[] = { c1 };
char out[2] = { 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
{
unsigned char in[] = { c1 };
char out[2] = { 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
check_base64(out, sizeof(out), (char *)in, sizeof(in), enc);
}
buffer_free(check);
}
static void check_all_len_2() {
static void check_all_len_2 (const base64_charset enc) {
unsigned int c1, c2;
buffer *check = buffer_init();
for (c1 = 0; c1 < 256; ++c1) for (c2 = 0; c2 < 256; ++c2) {
{
unsigned char in[] = { c1, c2 };
char out[3] = { 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
{
unsigned char in[] = { c1, c2 };
char out[3] = { 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
check_base64(out, sizeof(out), (char *)in, sizeof(in), enc);
}
buffer_free(check);
}
static void check_all_len_3() {
static void check_all_len_3 (const base64_charset enc) {
unsigned int c1, c2, c3;
buffer *check = buffer_init();
for (c1 = 0; c1 < 256; ++c1) for (c2 = 0; c2 < 256; ++c2) for (c3 = 0; c3 < 256; ++c3) {
{
unsigned char in[] = { c1, c2, c3 };
char out[4] = { 0, 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
{
unsigned char in[] = { c1, c2, c3 };
char out[4] = { 0, 0, 0, 0 };
unsigned char* check_res;
force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
buffer_reset(check);
check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
}
check_base64(out, sizeof(out), (char *)in, sizeof(in), enc);
}
buffer_free(check);
}
int main() {
check_all_len_0();
check_all_len_1();
check_all_len_2();
check_all_len_3();
check = buffer_init();
for (unsigned int enc = 0; enc < sizeof(encs)/sizeof(*encs); ++enc) {
check_all_len_0(encs[enc]);
check_all_len_1(encs[enc]);
check_all_len_2(encs[enc]);
check_all_len_3(encs[enc]);
}
buffer_free(check);
return 0;
}
Loading…
Cancel
Save