tests/base64: updated and added to Makefile.am

This commit is contained in:
Dmitry Podgorny
2015-10-13 23:19:25 +03:00
parent fc856f36a3
commit e0a13ecdaa
4 changed files with 127 additions and 135 deletions

View File

@@ -65,8 +65,8 @@ examples_uuid_LDADD = $(STROPHE_LIBS)
## Tests ## Tests
TESTS = tests/check_parser tests/test_rand tests/test_scram TESTS = tests/check_parser tests/test_rand tests/test_scram tests/test_base64
check_PROGRAMS = tests/check_parser tests/test_rand tests/test_scram check_PROGRAMS = $(TESTS)
tests_check_parser_SOURCES = tests/check_parser.c tests/test.h tests_check_parser_SOURCES = tests/check_parser.c tests/test.h
tests_check_parser_CFLAGS = @check_CFLAGS@ $(PARSER_CFLAGS) $(STROPHE_FLAGS) \ tests_check_parser_CFLAGS = @check_CFLAGS@ $(PARSER_CFLAGS) $(STROPHE_FLAGS) \
@@ -74,6 +74,11 @@ tests_check_parser_CFLAGS = @check_CFLAGS@ $(PARSER_CFLAGS) $(STROPHE_FLAGS) \
tests_check_parser_LDADD = @check_LIBS@ $(STROPHE_LIBS) tests_check_parser_LDADD = @check_LIBS@ $(STROPHE_LIBS)
tests_check_parser_LDFLAGS = -static tests_check_parser_LDFLAGS = -static
tests_test_base64_SOURCES = tests/test_base64.c tests/test.c tests/test.h
tests_test_base64_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
tests_test_base64_LDADD = $(STROPHE_LIBS)
tests_test_base64_LDFLAGS = -static
tests_test_rand_SOURCES = tests/test_rand.c tests/test.c src/sha1.c tests_test_rand_SOURCES = tests/test_rand.c tests/test.c src/sha1.c
tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src

View File

@@ -34,7 +34,7 @@ void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len)
*bin_len = len / 2; *bin_len = len / 2;
} }
const char *test_bin_to_hex(uint8_t *bin, size_t len) const char *test_bin_to_hex(const uint8_t *bin, size_t len)
{ {
static char buf[2048]; static char buf[2048];
size_t i; size_t i;

View File

@@ -45,25 +45,25 @@ do { \
} \ } \
} while (0) } while (0)
#define COMPARE_BUF(v1, len1, v2, len2) \ #define COMPARE_BUF(v1, len1, v2, len2) \
do { \ do { \
const uint8_t *__v1 = (uint8_t *)(v1); \ const uint8_t *__v1 = (uint8_t *)(v1); \
const uint8_t *__v2 = (uint8_t *)(v2); \ const uint8_t *__v2 = (uint8_t *)(v2); \
size_t __len1 = len1; \ size_t __len1 = len1; \
size_t __len2 = len2; \ size_t __len2 = len2; \
if (__len1 != __len2 || \ if (__len1 != __len2 || \
memcmp(__v1, __v2, __len1) != 0) \ memcmp(__v1, __v2, __len1) != 0) \
{ \ { \
printf("%s differs!\n", #v1); \ printf("%s differs!\n", #v1); \
printf("expected: 0x%s\n", \ printf("expected: 0x%s\n", \
test_bin_to_hex(__v1, __len1); \ test_bin_to_hex(__v1, __len1)); \
printf("got: 0x%s\n", \ printf("got: 0x%s\n", \
test_bin_to_hex(__v2, __len2); \ test_bin_to_hex(__v2, __len2)); \
exit(1); \ exit(1); \
} \ } \
} while (0) } while (0)
void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len); void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len);
const char *test_bin_to_hex(uint8_t *bin, size_t len); const char *test_bin_to_hex(const uint8_t *bin, size_t len);
#endif /* __LIBSTROPHE_TEST_H__ */ #endif /* __LIBSTROPHE_TEST_H__ */

View File

@@ -9,6 +9,7 @@
** This program is dual licensed under the MIT and GPLv3 licenses. ** This program is dual licensed under the MIT and GPLv3 licenses.
*/ */
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -16,133 +17,119 @@
#include "common.h" #include "common.h"
#include "sasl.h" #include "sasl.h"
static const unsigned char text_1[] = "How now brown cow?"; #include "test.h"
static const char base64_1[] = "SG93IG5vdyBicm93biBjb3c/";
static const unsigned char text_2[] = {0x14,0xfb,0x9c,0x03,0xd9,0x7e,0x00}; static const unsigned char test_2_raw[] =
static const char base64_2[] = "FPucA9l+"; {0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e, 0x00};
static const unsigned char text_3[] = static const unsigned char test_4_raw[] =
"From rest and sleep, which but thy pictures be, "
"Much pleasure; then from thee much more must flow, "
"And soonest our best men with thee do go, "
"Rest of their bones, and soul's delivery.";
static const char base64_3[] =
"RnJvbSByZXN0IGFuZCBzbGVlcCwgd2hpY2ggYnV0IHRoeSBwaWN0dXJl"
"cyBiZSwgTXVjaCBwbGVhc3VyZTsgdGhlbiBmcm9tIHRoZWUgbXVjaCBt"
"b3JlIG11c3QgZmxvdywgQW5kIHNvb25lc3Qgb3VyIGJlc3QgbWVuIHdp"
"dGggdGhlZSBkbyBnbywgUmVzdCBvZiB0aGVpciBib25lcywgYW5kIHNv"
"dWwncyBkZWxpdmVyeS4=";
static const unsigned char text_4[] =
{0xd6, 0x2f, 0x27, 0x49, 0x7e, 0xdd, 0xf3, 0xd5, {0xd6, 0x2f, 0x27, 0x49, 0x7e, 0xdd, 0xf3, 0xd5,
0x41, 0xbc, 0x1b, 0xe9, 0xdf, 0xe9, 0xb3, 0x08, 0x00}; 0x41, 0xbc, 0x1b, 0xe9, 0xdf, 0xe9, 0xb3, 0x08, 0x00};
static const char base64_4[] = "1i8nSX7d89VBvBvp3+mzCA==";
static const char text_5[] =
"realm=\"chesspark.com\",nonce=\"b243c0d663257a9149999cef2f83a22116559e93\",qop=\"auth\",charset=utf-8,algorithm=md5-sess";
static const char base64_5[] =
"cmVhbG09ImNoZXNzcGFyay5jb20iLG5vbmNlPSJiMjQzYzBkNjYzMjU3"
"YTkxNDk5OTljZWYyZjgzYTIyMTE2NTU5ZTkzIixxb3A9ImF1dGgiLGNo"
"YXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz";
int test_encode(xmpp_ctx_t *ctx) static const struct {
{ char *raw;
char *result; char *base64;
result = base64_encode(ctx, text_1, strlen(text_1)); } tests[] = {
if (result == NULL) return 2; {
if (strlen(result) != strlen(base64_1)) return 1; .raw = "How now brown cow?",
if (strncmp(base64_1, result, strlen(base64_1))) return 1; .base64 = "SG93IG5vdyBicm93biBjb3c/",
xmpp_free(ctx,result); },
{
result = base64_encode(ctx, text_2, strlen(text_2)); .raw = (char *)test_2_raw,
if (result == NULL) return 2; .base64 = "FPucA9l+",
if (strlen(result) != strlen(base64_2)) return 1; },
if (strncmp(base64_2, result, strlen(base64_2))) return 1; {
xmpp_free(ctx,result); .raw =
"From rest and sleep, which but thy pictures be, "
result = base64_encode(ctx, text_3, strlen(text_3)); "Much pleasure; then from thee much more must flow, "
if (result == NULL) return 2; "And soonest our best men with thee do go, "
if (strlen(result) != strlen(base64_3)) return 1; "Rest of their bones, and soul's delivery.",
if (strncmp(base64_3, result, strlen(base64_3))) return 1; .base64 =
xmpp_free(ctx,result); "RnJvbSByZXN0IGFuZCBzbGVlcCwgd2hpY2ggYnV0IHRoeSBwaWN0dXJl"
"cyBiZSwgTXVjaCBwbGVhc3VyZTsgdGhlbiBmcm9tIHRoZWUgbXVjaCBt"
result = base64_encode(ctx, text_4, strlen(text_4)); "b3JlIG11c3QgZmxvdywgQW5kIHNvb25lc3Qgb3VyIGJlc3QgbWVuIHdp"
if (result == NULL) return 2; "dGggdGhlZSBkbyBnbywgUmVzdCBvZiB0aGVpciBib25lcywgYW5kIHNv"
if (strlen(result) != strlen(base64_4)) return 1; "dWwncyBkZWxpdmVyeS4=",
if (strncmp(base64_4, result, strlen(base64_4))) return 1; },
xmpp_free(ctx,result); {
.raw = (char *)test_4_raw,
result = base64_encode(ctx, text_5, strlen(text_5)); .base64 = "1i8nSX7d89VBvBvp3+mzCA==",
if (result == NULL) return 2; },
if (strlen(result) != strlen(base64_5)) return 1; {
if (strncmp(base64_5, result, strlen(base64_5))) return 1; .raw =
xmpp_free(ctx,result); "realm=\"chesspark.com\",nonce=\"b243c0d663257a9149999cef2f83"
"a22116559e93\",qop=\"auth\",charset=utf-8,algorithm=md5-sess",
return 0; .base64 =
} "cmVhbG09ImNoZXNzcGFyay5jb20iLG5vbmNlPSJiMjQzYzBkNjYzMjU3"
"YTkxNDk5OTljZWYyZjgzYTIyMTE2NTU5ZTkzIixxb3A9ImF1dGgiLGNo"
"YXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz",
int test_decode(xmpp_ctx_t *ctx) },
{
unsigned char *result;
result = base64_decode(ctx, base64_1, strlen(base64_1));
if (result == NULL) return 2;
if (strlen(result) != strlen(text_1)) return 1;
if (strncmp(text_1, result, strlen(text_1))) return 1;
xmpp_free(ctx,result);
result = base64_decode(ctx, base64_2, strlen(base64_2));
if (result == NULL) return 2;
if (strlen(result) != strlen(text_2)) return 1;
if (strncmp(text_2, result, strlen(text_2))) return 1;
xmpp_free(ctx,result);
result = base64_decode(ctx, base64_3, strlen(base64_3));
if (result == NULL) return 2;
if (strlen(result) != strlen(text_3)) return 1;
if (strncmp(text_3, result, strlen(text_3))) return 1;
xmpp_free(ctx,result);
result = base64_decode(ctx, base64_4, strlen(base64_4));
if (result == NULL) return 2;
if (strlen(result) != strlen(text_4)) return 1;
if (strncmp(text_4, result, strlen(text_4))) return 1;
xmpp_free(ctx,result);
result = base64_decode(ctx, base64_5, strlen(base64_5));
if (result == NULL) return 2;
if (strlen(result) != strlen(text_5)) return 1;
if (strncmp(text_5, result, strlen(text_5))) return 1;
xmpp_free(ctx,result);
return 0;
}
/* RFC4648 test vectors */
{
.raw = "",
.base64 = "",
},
{
.raw = "f",
.base64 = "Zg==",
},
{
.raw = "fo",
.base64 = "Zm8=",
},
{
.raw = "foo",
.base64 = "Zm9v",
},
{
.raw = "foob",
.base64 = "Zm9vYg==",
},
{
.raw = "fooba",
.base64 = "Zm9vYmE=",
},
{
.raw = "foobar",
.base64 = "Zm9vYmFy",
},
};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
xmpp_ctx_t *ctx; xmpp_ctx_t *ctx;
int ret; unsigned char *dec;
char *enc;
size_t len;
int ret = 0;
int i;
printf("BASE64 tests.\n");
printf("allocating context... ");
ctx = xmpp_ctx_new(NULL, NULL); ctx = xmpp_ctx_new(NULL, NULL);
if (ctx == NULL) printf("failed to create context\n"); if (ctx == NULL) {
if (ctx == NULL) return -1; fprintf(stderr, "failed to create context\n");
printf("ok.\n"); return 1;
}
printf("testing encode... "); for (i = 0; i < ARRAY_SIZE(tests); ++i) {
ret = test_encode(ctx); printf("Test #%d: ", (int)i + 1);
if (ret) printf("base64_encode failed!\n"); enc = base64_encode(ctx, (unsigned char *)tests[i].raw,
if (ret) return ret; strlen(tests[i].raw));
printf("ok.\n"); assert(enc != NULL);
COMPARE(tests[i].base64, enc);
xmpp_free(ctx, enc);
printf("testing decode... "); dec = base64_decode(ctx, tests[i].base64, strlen(tests[i].base64));
ret = test_decode(ctx); assert(dec != NULL);
if (ret) printf("base64_decode failed!\n"); len = (size_t)base64_decoded_len(ctx, tests[i].base64,
if (ret) return ret; strlen(tests[i].base64));
printf("ok.\n"); COMPARE_BUF(tests[i].raw, strlen(tests[i].raw), dec, len);
xmpp_free(ctx, dec);
printf("ok\n");
}
printf("freeing context... ");
xmpp_ctx_free(ctx); xmpp_ctx_free(ctx);
printf("ok.\n");
return ret; return ret;
} }