20 #include <freerdp/crypto/crypto.h>
29 static const struct Encode64test encodeTests_base64[] = {
30 {
"\x00", 1,
"AA==" },
31 {
"\x00\x00", 2,
"AAA=" },
32 {
"\x00\x00\x00", 3,
"AAAA" },
33 {
"0123456", 7,
"MDEyMzQ1Ng==" },
34 {
"90123456", 8,
"OTAxMjM0NTY=" },
35 {
"890123456", 9,
"ODkwMTIzNDU2" },
36 {
"7890123456", 10,
"Nzg5MDEyMzQ1Ng==" },
41 static const struct Encode64test encodeTests_base64url[] = {
43 {
"\x00\x00", 2,
"AAA" },
44 {
"\x00\x00\x00", 3,
"AAAA" },
45 {
"01?34>6", 7,
"MDE_MzQ-Ng" },
46 {
"90123456", 8,
"OTAxMjM0NTY" },
47 {
"890123456", 9,
"ODkwMTIzNDU2" },
48 {
"78?01>3456", 10,
"Nzg_MDE-MzQ1Ng" },
53 int TestBase64(
int argc,
char* argv[])
61 (void)fprintf(stderr,
"%d:encode base64...", testNb);
63 for (
int i = 0; encodeTests_base64[i].input; i++)
65 char* encoded = crypto_base64_encode((
const BYTE*)encodeTests_base64[i].input,
66 encodeTests_base64[i].len);
68 if (strcmp(encodeTests_base64[i].output, encoded) != 0)
70 (void)fprintf(stderr,
"ko, error for string %d\n", i);
77 (void)fprintf(stderr,
"ok\n");
79 (void)fprintf(stderr,
"%d:encode base64url...", testNb);
81 for (
int i = 0; encodeTests_base64url[i].input; i++)
83 char* encoded = crypto_base64url_encode((
const BYTE*)encodeTests_base64url[i].input,
84 encodeTests_base64url[i].len);
86 if (strcmp(encodeTests_base64url[i].output, encoded) != 0)
88 (void)fprintf(stderr,
"ko, error for string %d\n", i);
95 (void)fprintf(stderr,
"ok\n");
97 (void)fprintf(stderr,
"%d:decode base64...", testNb);
99 for (
int i = 0; encodeTests_base64[i].input; i++)
101 crypto_base64_decode(encodeTests_base64[i].output, strlen(encodeTests_base64[i].output),
104 if (!decoded || (outLen != encodeTests_base64[i].len) ||
105 memcmp(encodeTests_base64[i].input, decoded, outLen) != 0)
107 (void)fprintf(stderr,
"ko, error for string %d\n", i);
114 (void)fprintf(stderr,
"ok\n");
116 (void)fprintf(stderr,
"%d:decode base64url...", testNb);
118 for (
int i = 0; encodeTests_base64url[i].input; i++)
120 crypto_base64url_decode(encodeTests_base64url[i].output,
121 strlen(encodeTests_base64url[i].output), &decoded, &outLen);
123 if (!decoded || (outLen != encodeTests_base64url[i].len) ||
124 memcmp(encodeTests_base64url[i].input, decoded, outLen) != 0)
126 (void)fprintf(stderr,
"ko, error for string %d\n", i);
133 (void)fprintf(stderr,
"ok\n");
135 (void)fprintf(stderr,
"%d:decode base64 errors...", testNb);
136 crypto_base64_decode(
"000", 3, &decoded, &outLen);
140 (void)fprintf(stderr,
"ko, badly padded string\n");
144 crypto_base64_decode(
"0=00", 4, &decoded, &outLen);
148 (void)fprintf(stderr,
"ko, = in a wrong place\n");
152 crypto_base64_decode(
"00=0", 4, &decoded, &outLen);
156 (void)fprintf(stderr,
"ko, = in a wrong place\n");
159 (void)fprintf(stderr,
"ok\n");
163 (void)fprintf(stderr,
"%d:encode base64 with crLf...", testNb);
164 const char* longStr =
"01234567890123456789012345678901234567890123456789";
165 const char* longStrExpected =
166 "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3\r\nODk=\r\n";
168 char* encoded = crypto_base64_encode_ex((
const BYTE*)longStr, strlen(longStr), TRUE);
169 if (!encoded || strcmp(encoded, longStrExpected) != 0)
171 (void)fprintf(stderr,
"problem with encode with CRLF\n");
175 (void)fprintf(stderr,
"ok\n");