2006-12-16 03:42:13 +03:00
|
|
|
/*
|
2014-06-01 10:46:42 +04:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
2006-12-16 03:42:13 +03:00
|
|
|
*
|
2014-08-07 23:14:10 +04:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2006-12-16 03:42:13 +03: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.
|
|
|
|
*/
|
|
|
|
|
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 "test.h"
|
|
|
|
|
2009-02-09 21:46:17 +03:00
|
|
|
#define TUPLES 5
|
|
|
|
|
2007-06-09 21:48:28 +04:00
|
|
|
static struct tuple
|
2006-12-16 03:09:16 +03:00
|
|
|
{
|
|
|
|
char *a;
|
|
|
|
char *b;
|
2009-02-09 21:46:17 +03:00
|
|
|
} testpairs[TUPLES] = {
|
2007-06-09 20:57:33 +04:00
|
|
|
{ "iodinetestingtesting", "nfxwi0lomv0gk21unfxgo3dfon0gs1th" },
|
2006-12-16 03:09:16 +03:00
|
|
|
{ "abc123", "mfrggmjsgm" },
|
2009-02-09 21:46:17 +03:00
|
|
|
{ "test", "orsxg3a" },
|
|
|
|
{ "tst", "orzxi" },
|
|
|
|
{ "", "" },
|
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];
|
2007-01-27 19:32:44 +03:00
|
|
|
int val;
|
2006-12-16 03:09:16 +03:00
|
|
|
|
2008-12-06 22:08:14 +03:00
|
|
|
|
2009-02-09 21:46:17 +03:00
|
|
|
len = sizeof(buf);
|
2017-03-12 07:44:08 +03:00
|
|
|
val = base32_ops.encode(buf, &len, testpairs[_i].a, strlen(testpairs[_i].a));
|
2006-12-16 03:09:16 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(val == strlen(testpairs[_i].b));
|
2021-08-25 00:07:36 +03:00
|
|
|
ck_assert_str_eq(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];
|
2007-01-27 19:32:44 +03:00
|
|
|
int val;
|
2014-06-01 10:34:18 +04:00
|
|
|
|
2009-02-09 21:46:17 +03:00
|
|
|
len = sizeof(buf);
|
2017-03-12 07:44:08 +03:00
|
|
|
val = base32_ops.decode(buf, &len, testpairs[_i].b, strlen(testpairs[_i].b));
|
2006-12-16 03:09:16 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(val == strlen(testpairs[_i].a));
|
2021-08-25 00:07:36 +03:00
|
|
|
ck_assert_str_eq(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++) {
|
2014-06-01 10:34:18 +04:00
|
|
|
c = b32_5to8(i);
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(b32_8to5(c) == i);
|
2008-12-11 22:26:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2009-02-17 23:40:40 +03:00
|
|
|
START_TEST(test_base32_blksize)
|
|
|
|
{
|
|
|
|
size_t rawlen;
|
|
|
|
size_t enclen;
|
|
|
|
char *rawbuf;
|
|
|
|
char *encbuf;
|
|
|
|
int i;
|
|
|
|
int val;
|
|
|
|
|
2017-03-12 07:44:08 +03:00
|
|
|
rawlen = base32_ops.blocksize_raw;
|
|
|
|
enclen = base32_ops.blocksize_encoded;
|
2009-02-17 23:40:40 +03:00
|
|
|
|
|
|
|
rawbuf = malloc(rawlen + 16);
|
|
|
|
encbuf = malloc(enclen + 16);
|
|
|
|
|
|
|
|
for (i = 0; i < rawlen; i++) {
|
|
|
|
rawbuf[i] = 'A';
|
|
|
|
}
|
|
|
|
rawbuf[i] = 0;
|
|
|
|
|
2017-03-12 07:44:08 +03:00
|
|
|
val = base32_ops.encode(encbuf, &enclen, rawbuf, rawlen);
|
2009-02-17 23:40:40 +03:00
|
|
|
|
2021-06-05 15:47:24 +03:00
|
|
|
ck_assert_msg(rawlen == 5, "raw length was %zu not 5", rawlen);
|
|
|
|
ck_assert_msg(enclen == 5, "encoded %zu bytes, not 5", enclen);
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert_msg(val == 8, "encoded string %s was length %d", encbuf, val);
|
2009-02-17 23:40:40 +03:00
|
|
|
|
|
|
|
memset(rawbuf, 0, rawlen + 16);
|
|
|
|
|
|
|
|
enclen = val;
|
2017-03-12 07:44:08 +03:00
|
|
|
val = base32_ops.decode(rawbuf, &rawlen, encbuf, enclen);
|
2009-02-17 23:40:40 +03:00
|
|
|
|
2021-06-05 15:47:24 +03:00
|
|
|
ck_assert_msg(rawlen == 5, "raw length was %zu not 5", rawlen);
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert_msg(val == 5, "val was not 5 but %d", val);
|
2009-02-17 23:40:40 +03:00
|
|
|
for (i = 0; i < rawlen; i++) {
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(rawbuf[i] == 'A');
|
2009-02-17 23:40:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2006-12-16 03:09:16 +03:00
|
|
|
TCase *
|
|
|
|
test_base32_create_tests()
|
|
|
|
{
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
tc = tcase_create("Base32");
|
2009-02-09 21:46:17 +03:00
|
|
|
tcase_add_loop_test(tc, test_base32_encode, 0, TUPLES);
|
|
|
|
tcase_add_loop_test(tc, test_base32_decode, 0, TUPLES);
|
2008-12-11 22:26:11 +03:00
|
|
|
tcase_add_test(tc, test_base32_5to8_8to5);
|
2009-02-17 23:40:40 +03:00
|
|
|
tcase_add_test(tc, test_base32_blksize);
|
2006-12-16 03:09:16 +03:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|