2006-12-16 03:42:13 +03:00
|
|
|
/*
|
2007-06-09 22:15:29 +04:00
|
|
|
* Copyright (c) 2006-2007 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
|
2006-12-16 03:42:13 +03:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-12-16 03:09:16 +03:00
|
|
|
#include <check.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-01-27 19:32:44 +03:00
|
|
|
#include <errno.h>
|
2006-12-16 03:09:16 +03:00
|
|
|
|
2007-06-09 20:57:33 +04:00
|
|
|
#include "encoding.h"
|
2006-12-16 03:09:16 +03:00
|
|
|
#include "base32.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
2007-06-09 21:48:28 +04:00
|
|
|
static struct tuple
|
2006-12-16 03:09:16 +03:00
|
|
|
{
|
|
|
|
char *a;
|
|
|
|
char *b;
|
|
|
|
} testpairs[] = {
|
2007-06-09 20:57:33 +04:00
|
|
|
{ "iodinetestingtesting", "nfxwi0lomv0gk21unfxgo3dfon0gs1th" },
|
2006-12-16 03:09:16 +03:00
|
|
|
{ "abc123", "mfrggmjsgm" },
|
2008-07-13 00:57:30 +04:00
|
|
|
{ NULL, NULL }
|
2006-12-16 03:09:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
START_TEST(test_base32_encode)
|
|
|
|
{
|
|
|
|
size_t len;
|
2007-06-09 20:57:33 +04:00
|
|
|
char buf[4096];
|
2008-12-06 22:08:14 +03:00
|
|
|
struct encoder *b32;
|
2007-01-27 19:32:44 +03:00
|
|
|
int val;
|
2006-12-16 03:09:16 +03:00
|
|
|
int i;
|
|
|
|
|
2008-12-06 22:08:14 +03:00
|
|
|
b32 = get_base32_encoder();
|
|
|
|
|
2006-12-16 03:09:16 +03:00
|
|
|
for (i = 0; testpairs[i].a != NULL; i++) {
|
2007-06-09 21:48:28 +04:00
|
|
|
len = sizeof(buf);
|
2008-12-06 22:08:14 +03:00
|
|
|
val = b32->encode(buf, &len, testpairs[i].a, strlen(testpairs[i].a));
|
2006-12-16 03:09:16 +03:00
|
|
|
|
2007-01-27 19:32:44 +03:00
|
|
|
fail_unless(val > 0, strerror(errno));
|
2008-07-13 00:57:30 +04:00
|
|
|
fail_unless(strcmp(buf, testpairs[i].b) == 0,
|
|
|
|
"'%s' != '%s'", buf, testpairs[i].b);
|
2006-12-16 03:09:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_base32_decode)
|
|
|
|
{
|
|
|
|
size_t len;
|
2007-06-09 20:57:33 +04:00
|
|
|
char buf[4096];
|
2008-12-06 22:08:14 +03:00
|
|
|
struct encoder *b32;
|
2007-01-27 19:32:44 +03:00
|
|
|
int val;
|
2006-12-16 03:09:16 +03:00
|
|
|
int i;
|
2008-12-06 22:08:14 +03:00
|
|
|
|
|
|
|
b32 = get_base32_encoder();
|
2006-12-16 03:09:16 +03:00
|
|
|
|
|
|
|
for (i = 0; testpairs[i].a != NULL; i++) {
|
2007-06-09 21:48:28 +04:00
|
|
|
len = sizeof(buf);
|
2008-12-06 22:08:14 +03:00
|
|
|
val = b32->decode(buf, &len, testpairs[i].b, strlen(testpairs[i].b));
|
2006-12-16 03:09:16 +03:00
|
|
|
|
2007-01-27 19:32:44 +03:00
|
|
|
fail_unless(val > 0, strerror(errno));
|
|
|
|
fail_unless(buf != NULL, "buf == NULL");
|
2008-07-13 00:57:30 +04:00
|
|
|
fail_unless(strcmp(buf, testpairs[i].a) == 0,
|
|
|
|
"'%s' != '%s'", buf, testpairs[i].a);
|
2006-12-16 03:09:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2008-12-11 22:26:11 +03:00
|
|
|
START_TEST(test_base32_5to8_8to5)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
c = b32_5to8(i);
|
|
|
|
fail_unless(b32_8to5(c) == i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2006-12-16 03:09:16 +03:00
|
|
|
TCase *
|
|
|
|
test_base32_create_tests()
|
|
|
|
{
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
tc = tcase_create("Base32");
|
|
|
|
tcase_add_test(tc, test_base32_encode);
|
|
|
|
tcase_add_test(tc, test_base32_decode);
|
2008-12-11 22:26:11 +03:00
|
|
|
tcase_add_test(tc, test_base32_5to8_8to5);
|
2006-12-16 03:09:16 +03:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|