2007-06-09 17:49:19 +00:00
|
|
|
/*
|
2014-06-01 06:46:42 +00:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
2007-06-09 17:49:19 +00:00
|
|
|
*
|
2014-08-07 19:14:10 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2007-06-09 17:49:19 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <check.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "encoding.h"
|
|
|
|
#include "base64.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
2009-02-09 18:46:17 +00:00
|
|
|
#define TUPLES 5
|
|
|
|
|
2007-06-09 17:49:19 +00:00
|
|
|
static struct tuple
|
|
|
|
{
|
|
|
|
char *a;
|
|
|
|
char *b;
|
2009-02-09 18:46:17 +00:00
|
|
|
} testpairs[TUPLES] = {
|
2007-06-09 17:49:19 +00:00
|
|
|
{ "iodinetestingtesting", "Aw8KAw4LDgvZDgLUz2rLC2rPBMC" },
|
2008-09-14 11:32:31 +00:00
|
|
|
{ "abc1231", "ywjJmtiZmq" },
|
2008-07-12 20:57:30 +00:00
|
|
|
{
|
2007-06-10 18:55:18 +00:00
|
|
|
"\xFF\xEF\x7C\xEF\xAE\x78\xDF\x6D\x74\xCF\x2C\x70\xBE\xEB\x6C\xAE\xAA\x68"
|
|
|
|
"\x9E\x69\x64\x8E\x28\x60\x7D\xE7\x5C\x6D\xA6\x58\x5D\x65\x54\x4D\x24\x50"
|
|
|
|
"\x3C\xE3\x4C\x2C\xA2\x48\x1C\x61\x44\x0C\x20\x40\x3F\x3F\x3C\xEF\xAE\x78"
|
|
|
|
"\xDF\x6D\x74\xCF\x2C\x70\xBE\xEB\x6C\xAE\xAA\x68\x9E\x69\x64\x8E\x28\x60"
|
|
|
|
"\x7D\xE7\x5C\x6D\xA6\x58\x5D\x65\x54\x4D\x24\x50\x3C\xE3\x4C\x2C\xA2\x48"
|
|
|
|
"\x1C\x61\x44\x0C\x20\x40\xFF\xEF\x7C\xEF\xAE\x78\xDF\x6D\x74\xCF\x2C\x70"
|
|
|
|
"\xBE\xEB\x6C\xAE\xAA\x68\x9E\x69\x64\x8E\x28\x60\x7D\xE7\x5C\x6D\xA6\x58"
|
|
|
|
"\x5D\x65\x54\x4D\x24\x50\x3C\xE3\x4C\x2C\xA2\x48\x1C\x61\x44\x0C\x20\x40",
|
|
|
|
|
2008-09-14 11:32:31 +00:00
|
|
|
"+9876543210-ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcbapZ"
|
|
|
|
"776543210-ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba+987654"
|
|
|
|
"3210-ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
|
2007-06-10 18:55:18 +00:00
|
|
|
},
|
2008-07-12 20:57:30 +00:00
|
|
|
{
|
2007-06-10 18:55:18 +00:00
|
|
|
"\xFF\xEF\x7C\xEF\xAE\x78\xDF\x6D\x74\xCF\x2C\x70\xBE\xEB\x6C\xAE\xAA\x68"
|
|
|
|
"\x9E\x69\x64\x8E\x28\x60\x7D\xE7\x5C\x6D\xA6\x58\x5D\x65\x54\x4D\x24\x50"
|
|
|
|
"\x3C\xE3\x4C\x2C\xA2\x48\x1C\x61\x44\x0C\x20\x40\x3F\x3F\x3C\xEF\xAE\x78"
|
|
|
|
"\xDF\x6D\x74\xCF\x2C\x70\xBE\xEB\x6C\xAE\xA1\x61\x91\x61\x61\x81\x28\x60"
|
|
|
|
"\x7D\xE7\x5C\x6D\xA6\x58\x5D\x65\x54\x4D\x24\x50\x3C\xE3\x4C\x2C\xA2\x48"
|
|
|
|
"\x1C\x61\x44\x0C\x20\x40\xFF\xEF\x7C\xEF\xAE\x78\xDF\x6D\x74\xCF\x2C\x70"
|
|
|
|
"\xBE\xEB\x6C\xAE\xA1\x61\x91\x61\x61\x81\x28\x60\x7D\xE7\x5C\x6D\xA6\x58"
|
|
|
|
"\x5D\x65\x54\x4D\x24\x50\x3C\xE3\x4C\x2C\xA2\x48\x1C\x61\x44\x0C\x20\x40",
|
|
|
|
|
2008-09-14 11:32:31 +00:00
|
|
|
"+9876543210-ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcbapZ"
|
|
|
|
"776543210-ZYXWVUTSRQfHKwfHGsHGFEDCBAzyxwvutsrqponmlkjihgfedcba+987654321"
|
2007-06-10 18:55:18 +00:00
|
|
|
"0-ZYXWVUTSRQfHKwfHGsHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
|
|
|
|
},
|
2009-02-09 18:46:17 +00:00
|
|
|
{ "", "" }
|
2007-06-09 17:49:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
START_TEST(test_base64_encode)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char buf[4096];
|
2008-12-06 19:08:14 +00:00
|
|
|
struct encoder *b64;
|
2007-06-09 17:49:19 +00:00
|
|
|
int val;
|
|
|
|
|
2008-12-06 19:08:14 +00:00
|
|
|
b64 = get_base64_encoder();
|
|
|
|
|
2009-02-09 18:46:17 +00:00
|
|
|
len = sizeof(buf);
|
|
|
|
val = b64->encode(buf, &len, testpairs[_i].a, strlen(testpairs[_i].a));
|
2007-06-09 17:49:19 +00:00
|
|
|
|
2014-01-29 21:41:24 +00:00
|
|
|
fail_unless(val == strlen(testpairs[_i].b));
|
2009-02-09 18:46:17 +00:00
|
|
|
fail_unless(strcmp(buf, testpairs[_i].b) == 0,
|
|
|
|
"'%s' != '%s'", buf, testpairs[_i].b);
|
2007-06-09 17:49:19 +00:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_base64_decode)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char buf[4096];
|
2008-12-06 19:08:14 +00:00
|
|
|
struct encoder *b64;
|
2007-06-09 17:49:19 +00:00
|
|
|
int val;
|
|
|
|
|
2008-12-06 19:08:14 +00:00
|
|
|
b64 = get_base64_encoder();
|
|
|
|
|
2009-02-09 18:46:17 +00:00
|
|
|
len = sizeof(buf);
|
|
|
|
val = b64->decode(buf, &len, testpairs[_i].b, strlen(testpairs[_i].b));
|
2007-06-09 17:49:19 +00:00
|
|
|
|
2014-01-29 21:41:24 +00:00
|
|
|
fail_unless(val == strlen(testpairs[_i].a));
|
2009-02-09 18:46:17 +00:00
|
|
|
fail_unless(strcmp(buf, testpairs[_i].a) == 0,
|
|
|
|
"'%s' != '%s'", buf, testpairs[_i].a);
|
2007-06-09 17:49:19 +00:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2009-02-17 20:22:24 +00:00
|
|
|
START_TEST(test_base64_blksize)
|
|
|
|
{
|
|
|
|
size_t rawlen;
|
|
|
|
size_t enclen;
|
|
|
|
char *rawbuf;
|
|
|
|
char *encbuf;
|
|
|
|
struct encoder *b64;
|
|
|
|
int i;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
b64 = get_base64_encoder();
|
|
|
|
|
|
|
|
rawlen = b64->blocksize_raw();
|
|
|
|
enclen = b64->blocksize_encoded();
|
|
|
|
|
|
|
|
rawbuf = malloc(rawlen + 16);
|
|
|
|
encbuf = malloc(enclen + 16);
|
|
|
|
|
|
|
|
for (i = 0; i < rawlen; i++) {
|
|
|
|
rawbuf[i] = 'A';
|
|
|
|
}
|
|
|
|
rawbuf[i] = 0;
|
|
|
|
|
|
|
|
val = b64->encode(encbuf, &enclen, rawbuf, rawlen);
|
|
|
|
|
|
|
|
fail_unless(rawlen == 3, "raw length was %d not 3", rawlen);
|
|
|
|
fail_unless(enclen == 3, "encoded %d bytes, not 3", enclen);
|
|
|
|
fail_unless(val == 4, "encoded string %s was length %d", encbuf, val);
|
|
|
|
|
|
|
|
memset(rawbuf, 0, rawlen + 16);
|
|
|
|
|
|
|
|
enclen = val;
|
|
|
|
val = b64->decode(rawbuf, &rawlen, encbuf, enclen);
|
|
|
|
|
|
|
|
fail_unless(rawlen == 3, "raw length was %d not 3", rawlen);
|
|
|
|
fail_unless(val == 3);
|
|
|
|
for (i = 0; i < rawlen; i++) {
|
|
|
|
fail_unless(rawbuf[i] == 'A');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2007-06-09 17:49:19 +00:00
|
|
|
TCase *
|
|
|
|
test_base64_create_tests()
|
|
|
|
{
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
tc = tcase_create("Base64");
|
2009-02-09 18:46:17 +00:00
|
|
|
tcase_add_loop_test(tc, test_base64_encode, 0, TUPLES);
|
|
|
|
tcase_add_loop_test(tc, test_base64_decode, 0, TUPLES);
|
2009-02-17 20:22:24 +00:00
|
|
|
tcase_add_test(tc, test_base64_blksize);
|
2007-06-09 17:49:19 +00:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|