Test base64url en/decoding
Add tests for base64url and fix a bug discovered while doing that
This commit is contained in:
parent
429c361435
commit
304ce6d702
@ -119,6 +119,9 @@ static void* base64_decode(const char* alphabet, const char* s, size_t length, s
|
||||
if ((pad && remainder > 0) || (remainder == 1))
|
||||
return NULL;
|
||||
|
||||
if (!pad && remainder)
|
||||
length += 4 - remainder;
|
||||
|
||||
q = data = (BYTE*)malloc(length / 4 * 3 + 1);
|
||||
if (!q)
|
||||
return NULL;
|
||||
|
@ -26,7 +26,7 @@ struct Encode64test
|
||||
const char* output;
|
||||
};
|
||||
|
||||
static const struct Encode64test encodeTests[] = {
|
||||
static const struct Encode64test encodeTests_base64[] = {
|
||||
{ "\x00", 1, "AA==" },
|
||||
{ "\x00\x00", 2, "AAA=" },
|
||||
{ "\x00\x00\x00", 3, "AAAA" },
|
||||
@ -38,6 +38,18 @@ static const struct Encode64test encodeTests[] = {
|
||||
{ NULL, -1, NULL }, /* /!\ last one /!\ */
|
||||
};
|
||||
|
||||
static const struct Encode64test encodeTests_base64url[] = {
|
||||
{ "\x00", 1, "AA" },
|
||||
{ "\x00\x00", 2, "AAA" },
|
||||
{ "\x00\x00\x00", 3, "AAAA" },
|
||||
{ "01?34>6", 7, "MDE_MzQ-Ng" },
|
||||
{ "90123456", 8, "OTAxMjM0NTY" },
|
||||
{ "890123456", 9, "ODkwMTIzNDU2" },
|
||||
{ "78?01>3456", 10, "Nzg_MDE-MzQ1Ng" },
|
||||
|
||||
{ NULL, -1, NULL }, /* /!\ last one /!\ */
|
||||
};
|
||||
|
||||
int TestBase64(int argc, char* argv[])
|
||||
{
|
||||
int i, testNb = 0;
|
||||
@ -48,11 +60,30 @@ int TestBase64(int argc, char* argv[])
|
||||
testNb++;
|
||||
fprintf(stderr, "%d:encode base64...", testNb);
|
||||
|
||||
for (i = 0; encodeTests[i].input; i++)
|
||||
for (i = 0; encodeTests_base64[i].input; i++)
|
||||
{
|
||||
char* encoded = crypto_base64_encode((const BYTE*)encodeTests[i].input, encodeTests[i].len);
|
||||
char* encoded = crypto_base64_encode((const BYTE*)encodeTests_base64[i].input,
|
||||
encodeTests_base64[i].len);
|
||||
|
||||
if (strcmp(encodeTests[i].output, encoded))
|
||||
if (strcmp(encodeTests_base64[i].output, encoded))
|
||||
{
|
||||
fprintf(stderr, "ko, error for string %d\n", i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(encoded);
|
||||
}
|
||||
|
||||
fprintf(stderr, "ok\n");
|
||||
testNb++;
|
||||
fprintf(stderr, "%d:encode base64url...", testNb);
|
||||
|
||||
for (i = 0; encodeTests_base64url[i].input; i++)
|
||||
{
|
||||
char* encoded = crypto_base64url_encode((const BYTE*)encodeTests_base64url[i].input,
|
||||
encodeTests_base64url[i].len);
|
||||
|
||||
if (strcmp(encodeTests_base64url[i].output, encoded))
|
||||
{
|
||||
fprintf(stderr, "ko, error for string %d\n", i);
|
||||
return -1;
|
||||
@ -65,13 +96,32 @@ int TestBase64(int argc, char* argv[])
|
||||
testNb++;
|
||||
fprintf(stderr, "%d:decode base64...", testNb);
|
||||
|
||||
for (i = 0; encodeTests[i].input; i++)
|
||||
for (i = 0; encodeTests_base64[i].input; i++)
|
||||
{
|
||||
crypto_base64_decode(encodeTests[i].output, strlen(encodeTests[i].output), &decoded,
|
||||
&outLen);
|
||||
crypto_base64_decode(encodeTests_base64[i].output, strlen(encodeTests_base64[i].output),
|
||||
&decoded, &outLen);
|
||||
|
||||
if (!decoded || (outLen != encodeTests[i].len) ||
|
||||
memcmp(encodeTests[i].input, decoded, outLen))
|
||||
if (!decoded || (outLen != encodeTests_base64[i].len) ||
|
||||
memcmp(encodeTests_base64[i].input, decoded, outLen))
|
||||
{
|
||||
fprintf(stderr, "ko, error for string %d\n", i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(decoded);
|
||||
}
|
||||
|
||||
fprintf(stderr, "ok\n");
|
||||
testNb++;
|
||||
fprintf(stderr, "%d:decode base64url...", testNb);
|
||||
|
||||
for (i = 0; encodeTests_base64url[i].input; i++)
|
||||
{
|
||||
crypto_base64url_decode(encodeTests_base64url[i].output,
|
||||
strlen(encodeTests_base64url[i].output), &decoded, &outLen);
|
||||
|
||||
if (!decoded || (outLen != encodeTests_base64url[i].len) ||
|
||||
memcmp(encodeTests_base64url[i].input, decoded, outLen))
|
||||
{
|
||||
fprintf(stderr, "ko, error for string %d\n", i);
|
||||
return -1;
|
||||
|
Loading…
Reference in New Issue
Block a user