ReactOS  0.4.14-dev-554-g2f8d847
base64.c
Go to the documentation of this file.
1 /*
2  * Unit test suite for crypt32.dll's CryptStringToBinary and CryptBinaryToString
3  * functions.
4  *
5  * Copyright 2006 Juan Lang
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windows.h>
24 #include <wincrypt.h>
25 
26 #include "wine/heap.h"
27 #include "wine/test.h"
28 
29 #define CERT_HEADER "-----BEGIN CERTIFICATE-----\r\n"
30 #define ALT_CERT_HEADER "-----BEGIN This is some arbitrary text that goes on and on-----\r\n"
31 #define CERT_TRAILER "-----END CERTIFICATE-----\r\n"
32 #define ALT_CERT_TRAILER "-----END More arbitrary text------\r\n"
33 #define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----\r\n"
34 #define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----\r\n"
35 #define X509_HEADER "-----BEGIN X509 CRL-----\r\n"
36 #define X509_TRAILER "-----END X509 CRL-----\r\n"
37 #define CERT_HEADER_NOCR "-----BEGIN CERTIFICATE-----\n"
38 #define CERT_TRAILER_NOCR "-----END CERTIFICATE-----\n"
39 #define CERT_REQUEST_HEADER_NOCR "-----BEGIN NEW CERTIFICATE REQUEST-----\n"
40 #define CERT_REQUEST_TRAILER_NOCR "-----END NEW CERTIFICATE REQUEST-----\n"
41 #define X509_HEADER_NOCR "-----BEGIN X509 CRL-----\n"
42 #define X509_TRAILER_NOCR "-----END X509 CRL-----\n"
43 
44 struct BinTests
45 {
46  const BYTE *toEncode;
48  const char *base64;
49 };
50 
51 static const BYTE toEncode1[] = { 0 };
52 static const BYTE toEncode2[] = { 1,2 };
53 /* static const BYTE toEncode3[] = { 1,2,3 }; */
54 static const BYTE toEncode4[] =
55  "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
56  "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
57  "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
58 static const BYTE toEncode5[] =
59  "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHI";
60 
61 static const struct BinTests tests[] = {
62  { toEncode1, sizeof(toEncode1), "AA==\r\n", },
63  { toEncode2, sizeof(toEncode2), "AQI=\r\n", },
64  /* { toEncode3, sizeof(toEncode3), "AQID\r\n", }, This test fails on Vista. */
65  { toEncode4, sizeof(toEncode4),
66  "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\r\n"
67  "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\r\n"
68  "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\r\n"
69  "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\r\n"
70  "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\r\n" },
71  { toEncode5, sizeof(toEncode5),
72  "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISQA=\r\n" },
73 };
74 
75 static const struct BinTests testsNoCR[] = {
76  { toEncode1, sizeof(toEncode1), "AA==\n", },
77  { toEncode2, sizeof(toEncode2), "AQI=\n", },
78  /* { toEncode3, sizeof(toEncode3), "AQID\n", }, This test fails on Vista. */
79  { toEncode4, sizeof(toEncode4),
80  "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\n"
81  "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\n"
82  "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\n"
83  "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\n"
84  "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\n" },
85  { toEncode5, sizeof(toEncode5),
86  "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISQA=\n" },
87 };
88 
89 static WCHAR *strdupAtoW(const char *str)
90 {
91  WCHAR *ret = NULL;
92  DWORD len;
93 
94  if (!str) return ret;
95  len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
96  ret = heap_alloc(len * sizeof(WCHAR));
97  if (ret)
99  return ret;
100 }
101 
103  DWORD format, const char *expected, const char *header, const char *trailer)
104 {
105  DWORD strLen, strLen2, required;
106  const char *ptr;
107  LPSTR str = NULL;
108  BOOL ret;
109 
110  required = strlen(expected) + 1;
111  if (header)
112  required += strlen(header);
113  if (trailer)
114  required += strlen(trailer);
115 
116  strLen = 0;
118  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
119  ok(strLen == required, "Unexpected required length %u, expected %u.\n", required, strLen);
120 
121  strLen2 = strLen;
123  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
124  ok(strLen == strLen2, "Unexpected required length.\n");
125 
126  strLen2 = strLen - 1;
128  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
129  ok(strLen == strLen2, "Unexpected required length.\n");
130 
131  str = heap_alloc(strLen);
132 
133  /* Partially filled output buffer. */
134  strLen2 = strLen - 1;
135  str[0] = 0x12;
137 todo_wine
138  ok((!ret && GetLastError() == ERROR_MORE_DATA) || broken(ret) /* XP */, "CryptBinaryToStringA failed %d, error %d.\n",
139  ret, GetLastError());
140  ok(strLen2 == strLen || broken(strLen2 == strLen - 1), "Expected length %d, got %d\n", strLen - 1, strLen);
141 todo_wine {
142  if (header)
143  ok(str[0] == header[0], "Unexpected buffer contents %#x.\n", str[0]);
144  else
145  ok(str[0] == expected[0], "Unexpected buffer contents %#x.\n", str[0]);
146 }
147  strLen2 = strLen;
149  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
150  ok(strLen2 == strLen - 1, "Expected length %d, got %d\n", strLen - 1, strLen);
151 
152  ptr = str;
153  if (header)
154  {
155  ok(!strncmp(header, ptr, strlen(header)), "Expected header %s, got %s\n", header, ptr);
156  ptr += strlen(header);
157  }
158  ok(!strncmp(expected, ptr, strlen(expected)), "Expected %s, got %s\n", expected, ptr);
159  ptr += strlen(expected);
160  if (trailer)
161  ok(!strncmp(trailer, ptr, strlen(trailer)), "Expected trailer %s, got %s\n", trailer, ptr);
162 
163  heap_free(str);
164 }
165 
167  const WCHAR *expected, const char *header, const char *trailer)
168 {
169  WCHAR *headerW, *trailerW, required;
170  DWORD strLen, strLen2;
171  WCHAR *strW = NULL;
172  const WCHAR *ptr;
173  BOOL ret;
174 
175  required = lstrlenW(expected) + 1;
176  if (header)
177  required += strlen(header);
178  if (trailer)
179  required += strlen(trailer);
180 
181  strLen = 0;
183  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
184  ok(strLen == required, "Unexpected required length %u, expected %u.\n", strLen, required);
185 
186  /* Same call with non-zero length value. */
187  strLen2 = strLen;
189  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
190  ok(strLen == strLen2, "Unexpected required length.\n");
191 
192  strLen2 = strLen - 1;
194  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
195  ok(strLen == strLen2, "Unexpected required length.\n");
196 
197  strLen2 = strLen - 1;
199  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
200  ok(strLen == strLen2, "Unexpected required length.\n");
201 
202  strW = heap_alloc(strLen * sizeof(WCHAR));
203 
204  headerW = strdupAtoW(header);
205  trailerW = strdupAtoW(trailer);
206 
207  strLen2 = strLen - 1;
208  strW[0] = 0x1234;
210 todo_wine
211  ok((!ret && GetLastError() == ERROR_MORE_DATA) || broken(ret) /* XP */, "CryptBinaryToStringW failed, %d, error %d\n",
212  ret, GetLastError());
213  if (headerW)
214  ok(strW[0] == 0x1234, "Unexpected buffer contents %#x.\n", strW[0]);
215  else
216  ok(strW[0] == 0x1234 || broken(strW[0] != 0x1234) /* XP */, "Unexpected buffer contents %#x.\n", strW[0]);
217 
218  strLen2 = strLen;
220  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
221 
222  ok(strLen2 == strLen - 1, "Expected length %d, got %d\n", strLen - 1, strLen);
223 
224  ptr = strW;
225  if (headerW)
226  {
227  ok(!memcmp(headerW, ptr, lstrlenW(headerW)), "Expected header %s, got %s.\n", wine_dbgstr_w(headerW),
228  wine_dbgstr_w(ptr));
229  ptr += lstrlenW(headerW);
230  }
231  ok(!memcmp(expected, ptr, lstrlenW(expected)), "Expected %s, got %s.\n", wine_dbgstr_w(expected),
232  wine_dbgstr_w(ptr));
233  ptr += lstrlenW(expected);
234  if (trailerW)
235  ok(!memcmp(trailerW, ptr, lstrlenW(trailerW)), "Expected trailer %s, got %s.\n", wine_dbgstr_w(trailerW),
236  wine_dbgstr_w(ptr));
237 
238  heap_free(strW);
239  heap_free(headerW);
240  heap_free(trailerW);
241 }
242 
243 static void test_CryptBinaryToString(void)
244 {
245  DWORD strLen, strLen2, i;
246  BOOL ret;
247 
250  "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
251 
252  strLen = 123;
253  ret = CryptBinaryToStringA(NULL, 0, 0, NULL, &strLen);
255  "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
256  ok(strLen == 123, "Unexpected length.\n");
257 
258  if (0)
260 
261  strLen = 123;
262  ret = CryptBinaryToStringW(NULL, 0, 0, NULL, &strLen);
263  ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "Unexpected error %d\n", GetLastError());
264  ok(strLen == 123, "Unexpected length.\n");
265 
266  for (i = 0; i < ARRAY_SIZE(tests); i++)
267  {
268  WCHAR *strW, *encodedW;
269  LPSTR str = NULL;
270  BOOL ret;
271 
272  strLen = 0;
274  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
275  ok(strLen == tests[i].toEncodeLen, "Unexpected required length %u.\n", strLen);
276 
277  strLen2 = strLen;
278  str = heap_alloc(strLen);
280  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
281  ok(strLen == strLen2, "Expected length %u, got %u\n", strLen, strLen2);
282  ok(!memcmp(str, tests[i].toEncode, tests[i].toEncodeLen), "Unexpected value\n");
283  heap_free(str);
284 
285  strLen = 0;
287  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
288  ok(strLen == tests[i].toEncodeLen, "Unexpected required length %u.\n", strLen);
289 
290  strLen2 = strLen;
291  strW = heap_alloc(strLen);
293  ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
294  ok(strLen == strLen2, "Expected length %u, got %u\n", strLen, strLen2);
295  ok(!memcmp(strW, tests[i].toEncode, tests[i].toEncodeLen), "Unexpected value\n");
296  heap_free(strW);
297 
299  tests[i].base64, NULL, NULL);
306 
307  encodedW = strdupAtoW(tests[i].base64);
308 
316 
317  heap_free(encodedW);
318  }
319 
320  for (i = 0; i < ARRAY_SIZE(testsNoCR); i++)
321  {
322  LPSTR str = NULL;
323  WCHAR *encodedW;
324  BOOL ret;
325 
328  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
329 
330  strLen2 = strLen;
331  str = heap_alloc(strLen);
334  ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
335  ok(strLen == strLen2, "Expected length %d, got %d\n", strLen, strLen2);
336  ok(!memcmp(str, testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen), "Unexpected value\n");
337  heap_free(str);
338 
340  testsNoCR[i].base64, NULL, NULL);
348 
349  encodedW = strdupAtoW(testsNoCR[i].base64);
350 
361 
362  heap_free(encodedW);
363  }
364 }
365 
367  LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected,
368  DWORD expectedLen)
369 {
370  static const char garbage[] = "garbage\r\n";
371  LPSTR str;
372  DWORD len = strlen(toDecode) + strlen(garbage) + 1;
373 
374  if (header)
375  len += strlen(header);
376  if (trailer)
377  len += strlen(trailer);
378  str = HeapAlloc(GetProcessHeap(), 0, len);
379  if (str)
380  {
381  LPBYTE buf;
382  DWORD bufLen = 0;
383  BOOL ret;
384 
385  if (header)
386  strcpy(str, header);
387  else
388  *str = 0;
389  strcat(str, toDecode);
390  if (trailer)
391  strcat(str, trailer);
392  ret = CryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
393  NULL);
394  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
395  buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
396  if (buf)
397  {
398  DWORD skipped, usedFormat;
399 
400  /* check as normal, make sure last two parameters are optional */
401  ret = CryptStringToBinaryA(str, 0, useFormat, buf, &bufLen, NULL,
402  NULL);
403  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
404  ok(bufLen == expectedLen,
405  "Expected length %d, got %d\n", expectedLen, bufLen);
406  ok(!memcmp(buf, expected, bufLen), "Unexpected value\n");
407  /* check last two params */
408  ret = CryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
409  &skipped, &usedFormat);
410  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
411  ok(skipped == 0, "Expected skipped 0, got %d\n", skipped);
412  ok(usedFormat == expectedFormat, "Expected format %d, got %d\n",
413  expectedFormat, usedFormat);
414  HeapFree(GetProcessHeap(), 0, buf);
415  }
416 
417  /* Check again, but with garbage up front */
418  strcpy(str, garbage);
419  if (header)
420  strcat(str, header);
421  strcat(str, toDecode);
422  if (trailer)
423  strcat(str, trailer);
424  ret = CryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
425  NULL);
426  /* expect failure with no header, and success with one */
427  if (header)
428  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
429  else
431  "Expected !ret and last error ERROR_INVALID_DATA, got ret=%d, error=%d\n", ret, GetLastError());
432  if (ret)
433  {
434  buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
435  if (buf)
436  {
437  DWORD skipped, usedFormat;
438 
439  ret = CryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
440  &skipped, &usedFormat);
441  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
442  ok(skipped == strlen(garbage),
443  "Expected %d characters of \"%s\" skipped when trying format %08x, got %d (used format is %08x)\n",
444  lstrlenA(garbage), str, useFormat, skipped, usedFormat);
445  HeapFree(GetProcessHeap(), 0, buf);
446  }
447  }
448  HeapFree(GetProcessHeap(), 0, str);
449  }
450 }
451 
453  const BYTE *bufA, DWORD bufLenA, DWORD fmtUsedA)
454 {
455  BYTE buf[8] = {0};
456  DWORD bufLen = sizeof(buf)-1, fmtUsed = 0xdeadbeef;
457  BOOL ret;
458  WCHAR strW[64];
459  int i;
460  for (i = 0; (strW[i] = strA[i]) != 0; ++i);
461  ret = CryptStringToBinaryW(strW, len, fmt, buf, &bufLen, NULL, &fmtUsed);
462  ok(ret == retA && bufLen == bufLenA && memcmp(bufA, buf, bufLen) == 0
463  && fmtUsed == fmtUsedA, "base64 \"%s\" len %d: W and A differ\n", strA, len);
464 }
465 
466 static void decodeBase64WithLenFmt(LPCSTR str, int len, DWORD fmt, LPCSTR expected, int le, BOOL isBroken)
467 {
468  BYTE buf[8] = {0};
469  DWORD bufLen = sizeof(buf)-1, fmtUsed = 0xdeadbeef;
470  BOOL ret;
471  SetLastError(0xdeadbeef);
472  ret = CryptStringToBinaryA(str, len, fmt, buf, &bufLen, NULL, &fmtUsed);
473  buf[bufLen] = 0;
474  if (expected) {
475  BOOL correct = ret && strcmp(expected, (char*)buf) == 0;
476  ok(correct || (isBroken && broken(!ret)),
477  "base64 \"%s\" len %d: expected \"%s\", got \"%s\" (ret %d, le %d)\n",
478  str, len, expected, (char*)buf, ret, GetLastError());
479  if (correct)
480  ok(fmtUsed == fmt, "base64 \"%s\" len %d: expected fmt %d, used %d\n",
481  str, len, fmt, fmtUsed);
482  } else {
483  ok(!ret && GetLastError() == le,
484  "base64 \"%s\" len %d: expected failure, got \"%s\" (ret %d, le %d)\n",
485  str, len, (char*)buf, ret, GetLastError());
486  }
487 
488  decodeBase64WithLenFmtW(str, len, fmt, ret, buf, bufLen, fmtUsed);
489 }
490 
492 {
494 }
495 
496 static void decodeBase64WithLen(LPCSTR str, int len, LPCSTR expected, int le)
497 {
499 }
500 
502 {
504 }
505 
506 struct BadString
507 {
508  const char *str;
510 };
511 
512 static const struct BadString badStrings[] = {
513  { "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER },
514 };
515 
516 static void testStringToBinaryA(void)
517 {
518  BOOL ret;
519  DWORD bufLen = 0, i;
520  BYTE buf[8];
521 
524  "Expected ERROR_INVALID_PARAMETER, got ret=%d le=%u\n", ret, GetLastError());
525  ret = CryptStringToBinaryA(NULL, 0, 0, NULL, &bufLen, NULL, NULL);
527  "Expected ERROR_INVALID_PARAMETER, got ret=%d le=%u\n", ret, GetLastError());
528  /* Bogus format */
529  ret = CryptStringToBinaryA(tests[0].base64, 0, 0, NULL, &bufLen, NULL,
530  NULL);
532  "Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", ret, GetLastError());
533  /* Decoding doesn't expect the NOCR flag to be specified */
534  ret = CryptStringToBinaryA(tests[0].base64, 1,
537  "Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", ret, GetLastError());
538  /* Bad strings */
539  for (i = 0; i < ARRAY_SIZE(badStrings); i++)
540  {
541  bufLen = 0;
543  NULL, &bufLen, NULL, NULL);
545  "%d: Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", i, ret, GetLastError());
546  }
547  /* Weird base64 strings (invalid padding, extra white-space etc.) */
554  decodeBase64WithLenBroken("V", 0, "T", 0);
555  decodeBase64WithLenBroken("VV", 0, "U", 0);
556  decodeBase64WithLenBroken("VVV", 0, "UU", 0);
557  decodeBase64WithLen("V", 1, "T", 0);
558  decodeBase64WithLen("VV", 2, "U", 0);
559  decodeBase64WithLen("VVV", 3, "UU", 0);
560  decodeBase64WithLen("V===", 0, "T", 0);
561  decodeBase64WithLen("V========", 0, "T", 0);
562  decodeBase64WithLen("V===", 4, "T", 0);
563  decodeBase64WithLen("V\nVVV", 0, "UUU", 0);
564  decodeBase64WithLen("VV\nVV", 0, "UUU", 0);
565  decodeBase64WithLen("VVV\nV", 0, "UUU", 0);
566  decodeBase64WithLen("V\nVVV", 5, "UUU", 0);
567  decodeBase64WithLen("VV\nVV", 5, "UUU", 0);
568  decodeBase64WithLen("VVV\nV", 5, "UUU", 0);
569  decodeBase64WithLen("VV VV", 0, "UUU", 0);
570  decodeBase64WithLen("V===VVVV", 0, "T", 0);
571  decodeBase64WithLen("VV==VVVV", 0, "U", 0);
572  decodeBase64WithLen("VVV=VVVV", 0, "UU", 0);
573  decodeBase64WithLen("VVVV=VVVV", 0, "UUU", 0);
574  decodeBase64WithLen("V===VVVV", 8, "T", 0);
575  decodeBase64WithLen("VV==VVVV", 8, "U", 0);
576  decodeBase64WithLen("VVV=VVVV", 8, "UU", 0);
577  decodeBase64WithLen("VVVV=VVVV", 8, "UUU", 0);
578 
579  decodeBase64WithFmt("-----BEGIN-----VVVV-----END-----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
580  decodeBase64WithFmt("-----BEGIN-----VVVV-----END -----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
581  decodeBase64WithFmt("-----BEGIN -----VVVV-----END-----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
582  decodeBase64WithFmt("-----BEGIN -----VVVV-----END -----", CRYPT_STRING_BASE64HEADER, "UUU", 0);
583 
584  decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64HEADER, "T", 0);
585  decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64HEADER, "T", 0);
586  decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64HEADER, "T", 0);
587  decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64HEADER, "T", 0);
588  decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
589  decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
590  decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
591  decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
592  decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
593  decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
594  decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
595  decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
596 
597  /* Too small buffer */
598  buf[0] = 0;
599  bufLen = 4;
600  ret = CryptStringToBinaryA("VVVVVVVV", 8, CRYPT_STRING_BASE64, (BYTE*)buf, &bufLen, NULL, NULL);
601  ok(!ret && bufLen == 4 && buf[0] == 0,
602  "Expected ret 0, bufLen 4, buf[0] '\\0', got ret %d, bufLen %d, buf[0] '%c'\n",
603  ret, bufLen, buf[0]);
604 
605  /* Good strings */
606  for (i = 0; i < ARRAY_SIZE(tests); i++)
607  {
608  bufLen = 0;
609  /* Bogus length--oddly enough, that succeeds, even though it's not
610  * properly padded.
611  */
613  NULL, &bufLen, NULL, NULL);
614  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
615  /* Check with the precise format */
618  tests[i].toEncodeLen);
621  tests[i].toEncode, tests[i].toEncodeLen);
624  tests[i].toEncode, tests[i].toEncodeLen);
628  tests[i].toEncodeLen);
631  tests[i].toEncode, tests[i].toEncodeLen);
632  /* And check with the "any" formats */
635  tests[i].toEncodeLen);
636  /* Don't check with no header and the string_any format, that'll
637  * always succeed.
638  */
641  tests[i].toEncodeLen);
644  tests[i].toEncodeLen);
645  /* oddly, these seem to decode using the wrong format
646  decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
647  CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
648  CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
649  tests[i].toEncodeLen);
650  decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
651  CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
652  CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
653  tests[i].toEncodeLen);
654  decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
655  CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
656  tests[i].toEncode, tests[i].toEncodeLen);
657  decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
658  CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
659  tests[i].toEncodeLen);
660  */
661  }
662  /* And again, with no CR--decoding handles this automatically */
663  for (i = 0; i < ARRAY_SIZE(testsNoCR); i++)
664  {
665  bufLen = 0;
666  /* Bogus length--oddly enough, that succeeds, even though it's not
667  * properly padded.
668  */
670  NULL, &bufLen, NULL, NULL);
671  ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
672  /* Check with the precise format */
675  testsNoCR[i].toEncodeLen);
678  testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
682  testsNoCR[i].toEncodeLen);
685  testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
686  /* And check with the "any" formats */
689  testsNoCR[i].toEncodeLen);
690  /* Don't check with no header and the string_any format, that'll
691  * always succeed.
692  */
695  testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
698  testsNoCR[i].toEncodeLen);
699  }
700 }
701 
702 START_TEST(base64)
703 {
706 }
const char * str
Definition: base64.c:508
static void decodeBase64WithLenFmt(LPCSTR str, int len, DWORD fmt, LPCSTR expected, int le, BOOL isBroken)
Definition: base64.c:466
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
BOOL WINAPI CryptBinaryToStringW(const BYTE *pbBinary, DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
Definition: base64.c:473
#define CRYPT_STRING_BASE64X509CRLHEADER
Definition: wincrypt.h:2993
char strA[12]
Definition: clipboard.c:2028
#define CERT_TRAILER_NOCR
Definition: base64.c:38
#define TRUE
Definition: types.h:120
#define CERT_REQUEST_TRAILER
Definition: base64.c:34
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
#define CERT_HEADER_NOCR
Definition: base64.c:37
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define CRYPT_STRING_BASE64_ANY
Definition: wincrypt.h:2990
char * wine_dbgstr_w(const wchar_t *wstr)
Definition: atltest.h:87
#define ALT_CERT_TRAILER
Definition: base64.c:32
#define CP_ACP
Definition: compat.h:99
DWORD toEncodeLen
Definition: base64.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
static void encodeAndCompareBase64_A(const BYTE *toEncode, DWORD toEncodeLen, DWORD format, const char *expected, const char *header, const char *trailer)
Definition: base64.c:102
#define CRYPT_STRING_BASE64REQUESTHEADER
Definition: wincrypt.h:2987
char * LPSTR
Definition: xmlstorage.h:182
static const struct BadString badStrings[]
Definition: base64.c:512
#define lstrlenW
Definition: compat.h:415
static const struct BinTests testsNoCR[]
Definition: base64.c:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
static void * heap_alloc(size_t len)
Definition: appwiz.h:65
#define CERT_HEADER
Definition: base64.c:29
const BYTE * toEncode
Definition: base64.c:46
static const struct BinTests tests[]
Definition: base64.c:61
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned char * LPBYTE
Definition: typedefs.h:52
static void decodeBase64WithLenBroken(LPCSTR str, int len, LPCSTR expected, int le)
Definition: base64.c:491
BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString, DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
Definition: base64.c:983
unsigned int BOOL
Definition: ntddk_ex.h:94
WCHAR strW[12]
Definition: clipboard.c:2029
static void decodeBase64WithLen(LPCSTR str, int len, LPCSTR expected, int le)
Definition: base64.c:496
static void decodeBase64WithFmt(LPCSTR str, DWORD fmt, LPCSTR expected, int le)
Definition: base64.c:501
static const BYTE toEncode4[]
Definition: base64.c:54
static PVOID ptr
Definition: dispmode.c:27
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
#define CRYPT_STRING_BINARY
Definition: wincrypt.h:2986
#define CRYPT_STRING_ANY
Definition: wincrypt.h:2991
const char * LPCSTR
Definition: xmlstorage.h:183
static void encode_compare_base64_W(const BYTE *toEncode, DWORD toEncodeLen, DWORD format, const WCHAR *expected, const char *header, const char *trailer)
Definition: base64.c:166
#define X509_TRAILER
Definition: base64.c:36
static const BYTE toEncode2[]
Definition: base64.c:52
static WCHAR * strdupAtoW(const char *str)
Definition: base64.c:89
#define CERT_REQUEST_HEADER
Definition: base64.c:33
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
BOOL WINAPI CryptStringToBinaryA(LPCSTR pszString, DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
Definition: base64.c:772
#define X509_HEADER
Definition: base64.c:35
__wchar_t WCHAR
Definition: xmlstorage.h:180
static const BYTE toEncode1[]
Definition: base64.c:51
#define CRYPT_STRING_BASE64
Definition: wincrypt.h:2985
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
unsigned long DWORD
Definition: ntddk_ex.h:95
#define CERT_TRAILER
Definition: base64.c:31
#define SetLastError(x)
Definition: compat.h:417
const char * base64
Definition: base64.c:48
int ret
#define todo_wine
Definition: test.h:163
#define CERT_REQUEST_TRAILER_NOCR
Definition: base64.c:40
GLenum GLsizei len
Definition: glext.h:6722
unsigned char BYTE
Definition: mem.h:68
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define broken(x)
Definition: _sntprintf.h:21
#define CERT_REQUEST_HEADER_NOCR
Definition: base64.c:39
#define ERROR_INVALID_DATA
Definition: winerror.h:116
#define X509_TRAILER_NOCR
Definition: base64.c:42
#define ALT_CERT_HEADER
Definition: base64.c:30
static const BYTE toEncode5[]
Definition: base64.c:58
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
static void testStringToBinaryA(void)
Definition: base64.c:516
#define ARRAY_SIZE(a)
Definition: main.h:24
#define ok(value,...)
Definition: atltest.h:57
DWORD format
Definition: base64.c:509
#define CRYPT_STRING_NOCR
Definition: wincrypt.h:2997
#define MultiByteToWideChar
Definition: compat.h:100
static void decodeBase64WithLenFmtW(LPCSTR strA, int len, DWORD fmt, BOOL retA, const BYTE *bufA, DWORD bufLenA, DWORD fmtUsedA)
Definition: base64.c:452
static void test_CryptBinaryToString(void)
Definition: base64.c:243
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define CRYPT_STRING_BASE64HEADER
Definition: wincrypt.h:2984
#define X509_HEADER_NOCR
Definition: base64.c:41
BOOL WINAPI CryptBinaryToStringA(const BYTE *pbBinary, DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
Definition: base64.c:253
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
Definition: dsound.c:943
#define HeapFree(x, y, z)
Definition: compat.h:402
struct CFHEADER header
Definition: fdi.c:101
BOOL expected
Definition: store.c:2063
static void decodeAndCompareBase64_A(LPCSTR toDecode, LPCSTR header, LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected, DWORD expectedLen)
Definition: base64.c:366
START_TEST(base64)
Definition: base64.c:702
static BOOL heap_free(void *mem)
Definition: appwiz.h:75