Test odd-sized base64 decodes

Test base64_decode() with odd sized decoded lengths, including
unpadded encodings and padded encodings with "right-sized" output
buffers.  Convert calls to base64_decode_nopad() to base64_decode()
because base64_decode_nopad() is redundant.
This commit is contained in:
Taylor Yu 2017-04-07 16:33:22 -04:00
parent f15818f280
commit 00ffefb41b
2 changed files with 20 additions and 14 deletions

View File

@ -1470,7 +1470,7 @@ test_crypto_formats(void *arg)
tt_int_op(i, OP_GE, 0); tt_int_op(i, OP_GE, 0);
tt_int_op(i, OP_EQ, strlen(data2)); tt_int_op(i, OP_EQ, strlen(data2));
tt_assert(! strchr(data2, '=')); tt_assert(! strchr(data2, '='));
j = base64_decode_nopad((uint8_t*)data3, 1024, data2, i); j = base64_decode(data3, 1024, data2, i);
tt_int_op(j, OP_EQ, idx); tt_int_op(j, OP_EQ, idx);
tt_mem_op(data3,OP_EQ, data1, idx); tt_mem_op(data3,OP_EQ, data1, idx);
} }

View File

@ -133,48 +133,54 @@ test_util_format_base64_encode(void *ignored)
} }
static void static void
test_util_format_base64_decode_nopad(void *ignored) test_util_format_base64_decode_oddsize(void *ignored)
{ {
(void)ignored; (void)ignored;
int res; int res;
int i; int i;
char *src; char *src;
uint8_t *dst, *real_dst; char *dst, real_dst[7];
uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65}; char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
char real_src[] = "ZXhhbXBsZQ"; char real_src[] = "ZXhhbXBsZQ";
char expected40[] = "testing40characteroddsizebase64encoding!";
char src40[] = "dGVzdGluZzQwY2hhcmFjdGVyb2Rkc2l6ZWJhc2U2NGVuY29kaW5nIQ";
char pad40[] = "dGVzdGluZzQwY2hhcmFjdGVyb2Rkc2l6ZWJhc2U2NGVuY29kaW5nIQ==";
src = tor_malloc_zero(256); src = tor_malloc_zero(256);
dst = tor_malloc_zero(1000); dst = tor_malloc_zero(1000);
real_dst = tor_malloc_zero(10);
for (i=0;i<256;i++) { for (i=0;i<256;i++) {
src[i] = (char)i; src[i] = (char)i;
} }
res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING); res = base64_decode(dst, 1, src, 5);
tt_int_op(res, OP_EQ, -1);
res = base64_decode_nopad(dst, 1, src, 5);
tt_int_op(res, OP_EQ, -1); tt_int_op(res, OP_EQ, -1);
const char *s = "SGVsbG8gd29ybGQ"; const char *s = "SGVsbG8gd29ybGQ";
res = base64_decode_nopad(dst, 1000, s, strlen(s)); res = base64_decode(dst, 1000, s, strlen(s));
tt_int_op(res, OP_EQ, 11); tt_int_op(res, OP_EQ, 11);
tt_mem_op(dst, OP_EQ, "Hello world", 11); tt_mem_op(dst, OP_EQ, "Hello world", 11);
s = "T3BhIG11bmRv"; s = "T3BhIG11bmRv";
res = base64_decode_nopad(dst, 9, s, strlen(s)); res = base64_decode(dst, 9, s, strlen(s));
tt_int_op(res, OP_EQ, 9); tt_int_op(res, OP_EQ, 9);
tt_mem_op(dst, OP_EQ, "Opa mundo", 9); tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
res = base64_decode_nopad(real_dst, 10, real_src, 10); res = base64_decode(real_dst, sizeof(real_dst), real_src, 10);
tt_int_op(res, OP_EQ, 7); tt_int_op(res, OP_EQ, 7);
tt_mem_op(real_dst, OP_EQ, expected, 7); tt_mem_op(real_dst, OP_EQ, expected, 7);
res = base64_decode(dst, 40, src40, strlen(src40));
tt_int_op(res, OP_EQ, 40);
tt_mem_op(dst, OP_EQ, expected40, 40);
res = base64_decode(dst, 40, pad40, strlen(pad40));
tt_int_op(res, OP_EQ, 40);
tt_mem_op(dst, OP_EQ, expected40, 40);
done: done:
tor_free(src); tor_free(src);
tor_free(dst); tor_free(dst);
tor_free(real_dst);
} }
static void static void
@ -402,7 +408,7 @@ struct testcase_t util_format_tests[] = {
{ "unaligned_accessors", test_util_format_unaligned_accessors, 0, { "unaligned_accessors", test_util_format_unaligned_accessors, 0,
NULL, NULL }, NULL, NULL },
{ "base64_encode", test_util_format_base64_encode, 0, NULL, NULL }, { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
{ "base64_decode_nopad", test_util_format_base64_decode_nopad, 0, { "base64_decode_oddsize", test_util_format_base64_decode_oddsize, 0,
NULL, NULL }, NULL, NULL },
{ "base64_decode", test_util_format_base64_decode, 0, NULL, NULL }, { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
{ "base16_decode", test_util_format_base16_decode, 0, NULL, NULL }, { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },