add some testcases

This commit is contained in:
Steffen Jaeckel 2017-02-21 00:39:10 +01:00 committed by Karel Miko
parent eee936d752
commit abf0a18290

View File

@ -40,22 +40,24 @@ int base64_test(void)
{"vuiSPKIl8PiR5O-rC4z9_xTQKZ0=", 1}, {"vuiSPKIl8PiR5O-rC4z9_xTQKZ0=", 1},
{"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0", 0}, {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0", 0},
{"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0}, {"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
{"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0==", 0},
{"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0===", 0},
{"vuiS*PKIl8P*iR5O-rC4*z9_xTQKZ0====", 0},
{"vuiS*=PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
{"vuiS*==PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
{"vuiS*===PKIl8P*iR5O-rC4*z9_xTQKZ0=", 0},
}; };
for (x = 0; x < sizeof(cases)/sizeof(cases[0]); ++x) { for (x = 0; x < sizeof(cases)/sizeof(cases[0]); ++x) {
memset(out, 0, sizeof(out));
memset(tmp, 0, sizeof(tmp));
slen1 = strlen(cases[x].s); slen1 = strlen(cases[x].s);
l1 = sizeof(out); l1 = sizeof(out);
DO(base64_encode((unsigned char*)cases[x].s, slen1, out, &l1)); DO(base64_encode((unsigned char*)cases[x].s, slen1, out, &l1));
l2 = sizeof(tmp); l2 = sizeof(tmp);
DO(base64_decode(out, l1, tmp, &l2)); DO(base64_strict_decode(out, l1, tmp, &l2));
if (l2 != slen1 || l1 != strlen(cases[x].b64) || memcmp(tmp, cases[x].s, l2) || memcmp(out, cases[x].b64, l1)) { if (compare_testvector(out, l1, cases[x].b64, strlen(cases[x].b64), "base64 encode", x) ||
fprintf(stderr, "\nbase64 failed case %lu", x); compare_testvector(tmp, l2, cases[x].s, slen1, "base64 decode", x)) {
fprintf(stderr, "\nbase64 should: %s", cases[x].b64);
out[sizeof(out)-1] = '\0';
fprintf(stderr, "\nbase64 is: %s", out);
fprintf(stderr, "\nplain should: %s", cases[x].s);
tmp[sizeof(tmp)-1] = '\0';
fprintf(stderr, "\nplain is: %s\n", tmp);
return 1; return 1;
} }
} }
@ -67,15 +69,22 @@ int base64_test(void)
DO(base64url_strict_decode((unsigned char*)url_cases[x].s, slen1, out, &l1)); DO(base64url_strict_decode((unsigned char*)url_cases[x].s, slen1, out, &l1));
else else
DO(base64url_decode((unsigned char*)url_cases[x].s, slen1, out, &l1)); DO(base64url_decode((unsigned char*)url_cases[x].s, slen1, out, &l1));
if (l1 != strlen(special_case) || memcmp(out, special_case, l1)) { if (compare_testvector(out, l1, special_case, strlen(special_case), "base64url decode", x)) {
fprintf(stderr, "\nbase64url failed case %lu: %s", x, url_cases[x].s); return 1;
print_hex("\nbase64url should", special_case, strlen(special_case)); }
out[sizeof(out)-1] = '\0'; if(x < 2) {
print_hex("\nbase64url is", out, l1); l2 = sizeof(tmp);
if(x == 0)
DO(base64url_encode(out, l1, tmp, &l2));
else
DO(base64url_strict_encode(out, l1, tmp, &l2));
if (compare_testvector(tmp, l2, url_cases[x].s, strlen(url_cases[x].s), "base64url encode", x)) {
return 1; return 1;
} }
} }
}
DO(base64url_strict_decode((unsigned char*)url_cases[4].s, slen1, out, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_INVALID_PACKET);
for (x = 0; x < 64; x++) { for (x = 0; x < 64; x++) {
yarrow_read(in, x, &yarrow_prng); yarrow_read(in, x, &yarrow_prng);
@ -83,22 +92,18 @@ int base64_test(void)
DO(base64_encode(in, x, out, &l1)); DO(base64_encode(in, x, out, &l1));
l2 = sizeof(tmp); l2 = sizeof(tmp);
DO(base64_decode(out, l1, tmp, &l2)); DO(base64_decode(out, l1, tmp, &l2));
if (l2 != x || memcmp(tmp, in, x)) { if (compare_testvector(tmp, x, in, x, "random base64", x)) {
fprintf(stderr, "base64 failed %lu %lu %lu", x, l1, l2);
return 1; return 1;
} }
} }
x--; x--;
memmove(&out[11], &out[10], l1 - 10); memmove(&out[11], &out[10], l1 - 10);
out[10] = '\0'; out[10] = '=';
l1++; l1++;
l2 = sizeof(tmp); l2 = sizeof(tmp);
DO(base64_decode(out, l1, tmp, &l2)); DO(base64_decode(out, l1, tmp, &l2));
if (l2 != x || memcmp(tmp, in, x)) { if (compare_testvector(tmp, l2, in, l2, "relaxed base64 decoding", -1)) {
fprintf(stderr, "relaxed base64 decoding failed %lu %lu %lu", x, l1, l2);
print_hex("is ", tmp, l2);
print_hex("should", in, x);
print_hex("input ", out, l1); print_hex("input ", out, l1);
return 1; return 1;
} }