FreeRDP
TestBase64.c
1 
20 #include <freerdp/crypto/crypto.h>
21 
22 struct Encode64test
23 {
24  const char* input;
25  size_t len;
26  const char* output;
27 };
28 
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==" },
37 
38  { NULL, -1, NULL }, /* /!\ last one /!\ */
39 };
40 
41 static const struct Encode64test encodeTests_base64url[] = {
42  { "\x00", 1, "AA" },
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" },
49 
50  { NULL, -1, NULL }, /* /!\ last one /!\ */
51 };
52 
53 int TestBase64(int argc, char* argv[])
54 {
55  int testNb = 0;
56  size_t outLen = 0;
57  BYTE* decoded = NULL;
58  WINPR_UNUSED(argc);
59  WINPR_UNUSED(argv);
60  testNb++;
61  (void)fprintf(stderr, "%d:encode base64...", testNb);
62 
63  for (int i = 0; encodeTests_base64[i].input; i++)
64  {
65  char* encoded = crypto_base64_encode((const BYTE*)encodeTests_base64[i].input,
66  encodeTests_base64[i].len);
67 
68  if (strcmp(encodeTests_base64[i].output, encoded) != 0)
69  {
70  (void)fprintf(stderr, "ko, error for string %d\n", i);
71  return -1;
72  }
73 
74  free(encoded);
75  }
76 
77  (void)fprintf(stderr, "ok\n");
78  testNb++;
79  (void)fprintf(stderr, "%d:encode base64url...", testNb);
80 
81  for (int i = 0; encodeTests_base64url[i].input; i++)
82  {
83  char* encoded = crypto_base64url_encode((const BYTE*)encodeTests_base64url[i].input,
84  encodeTests_base64url[i].len);
85 
86  if (strcmp(encodeTests_base64url[i].output, encoded) != 0)
87  {
88  (void)fprintf(stderr, "ko, error for string %d\n", i);
89  return -1;
90  }
91 
92  free(encoded);
93  }
94 
95  (void)fprintf(stderr, "ok\n");
96  testNb++;
97  (void)fprintf(stderr, "%d:decode base64...", testNb);
98 
99  for (int i = 0; encodeTests_base64[i].input; i++)
100  {
101  crypto_base64_decode(encodeTests_base64[i].output, strlen(encodeTests_base64[i].output),
102  &decoded, &outLen);
103 
104  if (!decoded || (outLen != encodeTests_base64[i].len) ||
105  memcmp(encodeTests_base64[i].input, decoded, outLen) != 0)
106  {
107  (void)fprintf(stderr, "ko, error for string %d\n", i);
108  return -1;
109  }
110 
111  free(decoded);
112  }
113 
114  (void)fprintf(stderr, "ok\n");
115  testNb++;
116  (void)fprintf(stderr, "%d:decode base64url...", testNb);
117 
118  for (int i = 0; encodeTests_base64url[i].input; i++)
119  {
120  crypto_base64url_decode(encodeTests_base64url[i].output,
121  strlen(encodeTests_base64url[i].output), &decoded, &outLen);
122 
123  if (!decoded || (outLen != encodeTests_base64url[i].len) ||
124  memcmp(encodeTests_base64url[i].input, decoded, outLen) != 0)
125  {
126  (void)fprintf(stderr, "ko, error for string %d\n", i);
127  return -1;
128  }
129 
130  free(decoded);
131  }
132 
133  (void)fprintf(stderr, "ok\n");
134  testNb++;
135  (void)fprintf(stderr, "%d:decode base64 errors...", testNb);
136  crypto_base64_decode("000", 3, &decoded, &outLen);
137 
138  if (decoded)
139  {
140  (void)fprintf(stderr, "ko, badly padded string\n");
141  return -1;
142  }
143 
144  crypto_base64_decode("0=00", 4, &decoded, &outLen);
145 
146  if (decoded)
147  {
148  (void)fprintf(stderr, "ko, = in a wrong place\n");
149  return -1;
150  }
151 
152  crypto_base64_decode("00=0", 4, &decoded, &outLen);
153 
154  if (decoded)
155  {
156  (void)fprintf(stderr, "ko, = in a wrong place\n");
157  return -1;
158  }
159  (void)fprintf(stderr, "ok\n");
160  testNb++;
161 
162  /* test the encode_ex version that will add \r\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";
167 
168  char* encoded = crypto_base64_encode_ex((const BYTE*)longStr, strlen(longStr), TRUE);
169  if (!encoded || strcmp(encoded, longStrExpected) != 0)
170  {
171  (void)fprintf(stderr, "problem with encode with CRLF\n");
172  return -1;
173  }
174  free(encoded);
175  (void)fprintf(stderr, "ok\n");
176 
177  return 0;
178 }