kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / crypt32 / tests / main.c
blob7c11e8e2ee3cc98a3b2e43ace130751d3d581142
1 /*
2 * Miscellaneous crypt32 tests
4 * Copyright 2005 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <wincrypt.h>
27 #include <winreg.h>
29 #include "wine/test.h"
31 HMODULE hCrypt;
33 static void test_findAttribute(void)
35 PCRYPT_ATTRIBUTE ret;
36 BYTE blobbin[] = {0x02,0x01,0x01};
37 static CHAR oid[] = "1.2.3";
38 CRYPT_ATTR_BLOB blobs[] = { { sizeof blobbin, blobbin }, };
39 CRYPT_ATTRIBUTE attr = { oid, sizeof(blobs) / sizeof(blobs[0]), blobs };
41 /* returns NULL, last error not set */
42 SetLastError(0xdeadbeef);
43 ret = CertFindAttribute(NULL, 0, NULL);
44 ok(ret == NULL, "Expected failure\n");
45 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
46 GetLastError());
47 if (0)
49 /* crashes */
50 SetLastError(0xdeadbeef);
51 ret = CertFindAttribute(NULL, 1, NULL);
52 /* returns NULL, last error is ERROR_INVALID_PARAMETER
53 * crashes on Vista
55 SetLastError(0xdeadbeef);
56 ret = CertFindAttribute(NULL, 1, &attr);
57 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
58 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
59 GetLastError());
61 /* returns NULL, last error not set */
62 SetLastError(0xdeadbeef);
63 ret = CertFindAttribute("bogus", 1, &attr);
64 ok(ret == NULL, "Expected failure\n");
65 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
66 GetLastError());
67 /* returns NULL, last error not set */
68 SetLastError(0xdeadbeef);
69 ret = CertFindAttribute("1.2.4", 1, &attr);
70 ok(ret == NULL, "Expected failure\n");
71 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
72 GetLastError());
73 /* succeeds, last error not set */
74 SetLastError(0xdeadbeef);
75 ret = CertFindAttribute("1.2.3", 1, &attr);
76 ok(ret != NULL, "CertFindAttribute failed: %08x\n", GetLastError());
79 static void test_findExtension(void)
81 PCERT_EXTENSION ret;
82 static CHAR oid[] = "1.2.3";
83 BYTE blobbin[] = {0x02,0x01,0x01};
84 CERT_EXTENSION ext = { oid, TRUE, { sizeof blobbin, blobbin } };
86 /* returns NULL, last error not set */
87 SetLastError(0xdeadbeef);
88 ret = CertFindExtension(NULL, 0, NULL);
89 ok(ret == NULL, "Expected failure\n");
90 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
91 GetLastError());
92 if (0)
94 /* crashes */
95 SetLastError(0xdeadbeef);
96 ret = CertFindExtension(NULL, 1, NULL);
97 /* returns NULL, last error is ERROR_INVALID_PARAMETER
98 * crashes on Vista
100 SetLastError(0xdeadbeef);
101 ret = CertFindExtension(NULL, 1, &ext);
102 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
103 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
104 GetLastError());
106 /* returns NULL, last error not set */
107 SetLastError(0xdeadbeef);
108 ret = CertFindExtension("bogus", 1, &ext);
109 ok(ret == NULL, "Expected failure\n");
110 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
111 GetLastError());
112 /* returns NULL, last error not set */
113 SetLastError(0xdeadbeef);
114 ret = CertFindExtension("1.2.4", 1, &ext);
115 ok(ret == NULL, "Expected failure\n");
116 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
117 GetLastError());
118 /* succeeds, last error not set */
119 SetLastError(0xdeadbeef);
120 ret = CertFindExtension("1.2.3", 1, &ext);
121 ok(ret != NULL, "CertFindExtension failed: %08x\n", GetLastError());
124 static void test_findRDNAttr(void)
126 PCERT_RDN_ATTR ret;
127 static CHAR oid[] = "1.2.3";
128 BYTE bin[] = { 0x16,0x09,'J','u','a','n',' ','L','a','n','g' };
129 CERT_RDN_ATTR attrs[] = {
130 { oid, CERT_RDN_IA5_STRING, { sizeof bin, bin } },
132 CERT_RDN rdns[] = {
133 { sizeof(attrs) / sizeof(attrs[0]), attrs },
135 CERT_NAME_INFO nameInfo = { sizeof(rdns) / sizeof(rdns[0]), rdns };
137 if (0)
139 /* crashes */
140 SetLastError(0xdeadbeef);
141 ret = CertFindRDNAttr(NULL, NULL);
142 /* returns NULL, last error is ERROR_INVALID_PARAMETER
143 * crashes on Vista
145 SetLastError(0xdeadbeef);
146 ret = CertFindRDNAttr(NULL, &nameInfo);
147 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
148 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
149 GetLastError());
151 /* returns NULL, last error not set */
152 SetLastError(0xdeadbeef);
153 ret = CertFindRDNAttr("bogus", &nameInfo);
154 ok(ret == NULL, "Expected failure\n");
155 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
156 GetLastError());
157 /* returns NULL, last error not set */
158 SetLastError(0xdeadbeef);
159 ret = CertFindRDNAttr("1.2.4", &nameInfo);
160 ok(ret == NULL, "Expected failure\n");
161 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
162 GetLastError());
163 /* succeeds, last error not set */
164 SetLastError(0xdeadbeef);
165 ret = CertFindRDNAttr("1.2.3", &nameInfo);
166 ok(ret != NULL, "CertFindRDNAttr failed: %08x\n", GetLastError());
169 static void test_verifyTimeValidity(void)
171 SYSTEMTIME sysTime;
172 FILETIME fileTime;
173 CERT_INFO info = { 0 };
174 LONG ret;
176 GetSystemTime(&sysTime);
177 SystemTimeToFileTime(&sysTime, &fileTime);
178 /* crashes
179 ret = CertVerifyTimeValidity(NULL, NULL);
180 ret = CertVerifyTimeValidity(&fileTime, NULL);
182 /* Check with 0 NotBefore and NotAfter */
183 ret = CertVerifyTimeValidity(&fileTime, &info);
184 ok(ret == 1, "Expected 1, got %d\n", ret);
185 memcpy(&info.NotAfter, &fileTime, sizeof(info.NotAfter));
186 /* Check with NotAfter equal to comparison time */
187 ret = CertVerifyTimeValidity(&fileTime, &info);
188 ok(ret == 0, "Expected 0, got %d\n", ret);
189 /* Check with NotBefore after comparison time */
190 memcpy(&info.NotBefore, &fileTime, sizeof(info.NotBefore));
191 info.NotBefore.dwLowDateTime += 5000;
192 ret = CertVerifyTimeValidity(&fileTime, &info);
193 ok(ret == -1, "Expected -1, got %d\n", ret);
196 static void test_cryptAllocate(void)
198 LPVOID buf;
200 buf = CryptMemAlloc(0);
201 ok(buf != NULL, "CryptMemAlloc failed: %08x\n", GetLastError());
202 CryptMemFree(buf);
203 /* CryptMemRealloc(NULL, 0) fails pre-Vista */
204 buf = CryptMemAlloc(0);
205 buf = CryptMemRealloc(buf, 1);
206 ok(buf != NULL, "CryptMemRealloc failed: %08x\n", GetLastError());
207 CryptMemFree(buf);
210 typedef DWORD (WINAPI *I_CryptAllocTlsFunc)(void);
211 typedef LPVOID (WINAPI *I_CryptDetachTlsFunc)(DWORD dwTlsIndex);
212 typedef LPVOID (WINAPI *I_CryptGetTlsFunc)(DWORD dwTlsIndex);
213 typedef BOOL (WINAPI *I_CryptSetTlsFunc)(DWORD dwTlsIndex, LPVOID lpTlsValue);
214 typedef BOOL (WINAPI *I_CryptFreeTlsFunc)(DWORD dwTlsIndex, DWORD unknown);
216 static I_CryptAllocTlsFunc pI_CryptAllocTls;
217 static I_CryptDetachTlsFunc pI_CryptDetachTls;
218 static I_CryptGetTlsFunc pI_CryptGetTls;
219 static I_CryptSetTlsFunc pI_CryptSetTls;
220 static I_CryptFreeTlsFunc pI_CryptFreeTls;
222 static void test_cryptTls(void)
224 DWORD index;
225 BOOL ret;
227 pI_CryptAllocTls = (I_CryptAllocTlsFunc)GetProcAddress(hCrypt,
228 "I_CryptAllocTls");
229 pI_CryptDetachTls = (I_CryptDetachTlsFunc)GetProcAddress(hCrypt,
230 "I_CryptDetachTls");
231 pI_CryptGetTls = (I_CryptGetTlsFunc)GetProcAddress(hCrypt,
232 "I_CryptGetTls");
233 pI_CryptSetTls = (I_CryptSetTlsFunc)GetProcAddress(hCrypt,
234 "I_CryptSetTls");
235 pI_CryptFreeTls = (I_CryptFreeTlsFunc)GetProcAddress(hCrypt,
236 "I_CryptFreeTls");
238 /* One normal pass */
239 index = pI_CryptAllocTls();
240 ok(index, "I_CryptAllocTls failed: %08x\n", GetLastError());
241 if (index)
243 LPVOID ptr;
245 ptr = pI_CryptGetTls(index);
246 ok(!ptr, "Expected NULL\n");
247 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
248 ok(ret, "I_CryptSetTls failed: %08x\n", GetLastError());
249 ptr = pI_CryptGetTls(index);
250 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
251 /* This crashes
252 ret = pI_CryptFreeTls(index, 1);
254 ret = pI_CryptFreeTls(index, 0);
255 ok(ret, "I_CryptFreeTls failed: %08x\n", GetLastError());
256 ret = pI_CryptFreeTls(index, 0);
257 ok(!ret, "I_CryptFreeTls succeeded\n");
258 ok(GetLastError() == E_INVALIDARG,
259 "Expected E_INVALIDARG, got %08x\n", GetLastError());
261 /* Similar pass, check I_CryptDetachTls */
262 index = pI_CryptAllocTls();
263 ok(index, "I_CryptAllocTls failed: %08x\n", GetLastError());
264 if (index)
266 LPVOID ptr;
268 ptr = pI_CryptGetTls(index);
269 ok(!ptr, "Expected NULL\n");
270 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
271 ok(ret, "I_CryptSetTls failed: %08x\n", GetLastError());
272 ptr = pI_CryptGetTls(index);
273 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
274 ptr = pI_CryptDetachTls(index);
275 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
276 ptr = pI_CryptGetTls(index);
277 ok(!ptr, "Expected NULL\n");
281 typedef BOOL (WINAPI *I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)
282 (LPCWSTR, DWORD *);
284 static void test_readTrustedPublisherDWORD(void)
286 I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc pReadDWORD;
288 pReadDWORD =
289 (I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)GetProcAddress(
290 hCrypt, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
291 if (pReadDWORD)
293 static const WCHAR safer[] = {
294 'S','o','f','t','w','a','r','e','\\',
295 'P','o','l','i','c','i','e','s','\\',
296 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
297 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
298 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
299 '\\','S','a','f','e','r',0 };
300 static const WCHAR authenticodeFlags[] = { 'A','u','t','h','e','n',
301 't','i','c','o','d','e','F','l','a','g','s',0 };
302 BOOL ret, exists = FALSE;
303 DWORD size, readFlags = 0, returnedFlags;
304 HKEY key;
305 LONG rc;
307 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, safer, &key);
308 if (rc == ERROR_SUCCESS)
310 size = sizeof(readFlags);
311 rc = RegQueryValueExW(key, authenticodeFlags, NULL, NULL,
312 (LPBYTE)&readFlags, &size);
313 if (rc == ERROR_SUCCESS)
314 exists = TRUE;
316 returnedFlags = 0xdeadbeef;
317 ret = pReadDWORD(authenticodeFlags, &returnedFlags);
318 ok(ret == exists, "Unexpected return value\n");
319 ok(readFlags == returnedFlags,
320 "Expected flags %08x, got %08x\n", readFlags, returnedFlags);
324 typedef HCRYPTPROV (WINAPI *I_CryptGetDefaultCryptProvFunc)(DWORD w);
326 static void test_getDefaultCryptProv(void)
328 I_CryptGetDefaultCryptProvFunc pI_CryptGetDefaultCryptProv;
329 HCRYPTPROV prov;
331 pI_CryptGetDefaultCryptProv = (I_CryptGetDefaultCryptProvFunc)
332 GetProcAddress(hCrypt, "I_CryptGetDefaultCryptProv");
333 if (!pI_CryptGetDefaultCryptProv) return;
335 prov = pI_CryptGetDefaultCryptProv(0xdeadbeef);
336 ok(prov == 0 && GetLastError() == E_INVALIDARG,
337 "Expected E_INVALIDARG, got %08x\n", GetLastError());
338 prov = pI_CryptGetDefaultCryptProv(PROV_RSA_FULL);
339 ok(prov == 0 && GetLastError() == E_INVALIDARG,
340 "Expected E_INVALIDARG, got %08x\n", GetLastError());
341 prov = pI_CryptGetDefaultCryptProv(1);
342 ok(prov == 0 && GetLastError() == E_INVALIDARG,
343 "Expected E_INVALIDARG, got %08x\n", GetLastError());
344 prov = pI_CryptGetDefaultCryptProv(0);
345 ok(prov != 0, "I_CryptGetDefaultCryptProv failed: %08x\n", GetLastError());
346 CryptReleaseContext(prov, 0);
349 typedef int (WINAPI *I_CryptInstallOssGlobal)(DWORD,DWORD,DWORD);
351 static void test_CryptInstallOssGlobal(void)
353 int ret,i;
354 I_CryptInstallOssGlobal pI_CryptInstallOssGlobal;
356 pI_CryptInstallOssGlobal= (I_CryptInstallOssGlobal)GetProcAddress(hCrypt,"I_CryptInstallOssGlobal");
357 /* passing in some random values to I_CryptInstallOssGlobal, it always returns 9 the first time, then 10, 11 etc.*/
358 for(i=0;i<30;i++)
360 ret = pI_CryptInstallOssGlobal(rand(),rand(),rand());
361 ok((9+i) == ret ||
362 ret == 0, /* Vista */
363 "Expected %d or 0, got %d\n",(9+i),ret);
367 static BOOL (WINAPI *pCryptFormatObject)(DWORD dwEncoding, DWORD dwFormatType,
368 DWORD dwFormatStrType, void *pFormatStruct, LPCSTR lpszStructType,
369 const BYTE *pbEncoded, DWORD dwEncoded, void *pbFormat, DWORD *pcbFormat);
371 static const BYTE encodedInt[] = { 0x02,0x01,0x01 };
372 static const WCHAR encodedIntStr[] = { '0','2',' ','0','1',' ','0','1',0 };
373 static const BYTE encodedBigInt[] = { 0x02,0x1f,0x01,0x02,0x03,0x04,0x05,0x06,
374 0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,
375 0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f };
376 static const WCHAR encodedBigIntStr[] = { '0','2',' ','1','f',' ','0','1',' ',
377 '0','2',' ','0','3',' ','0','4',' ','0','5',' ','0','6',' ','0','7',' ','0',
378 '8',' ','0','9',' ','0','a',' ','0','b',' ','0','c',' ','0','d',' ','0','e',
379 ' ','0','f',' ','1','0',' ','1','1',' ','1','2',' ','1','3',' ','1','4',' ',
380 '1','5',' ','1','6',' ','1','7',' ','1','8',' ','1','9',' ','1','a',' ','1',
381 'b',' ','1','c',' ','1','d',' ','1','e',' ','1','f',0 };
383 static void test_format_object(void)
385 BOOL ret;
386 DWORD size;
387 LPWSTR str;
389 pCryptFormatObject = (void *)GetProcAddress(hCrypt, "CryptFormatObject");
390 if (!pCryptFormatObject)
392 skip("No CryptFormatObject\n");
393 return;
395 /* Crash */
396 if (0)
398 ret = pCryptFormatObject(0, 0, 0, NULL, NULL, NULL, 0, NULL, NULL);
400 /* When called with any but the default encoding, it fails to find a
401 * formatting function.
403 SetLastError(0xdeadbeef);
404 ret = pCryptFormatObject(0, 0, 0, NULL, NULL, NULL, 0, NULL, &size);
405 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
406 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
407 /* When called with the default encoding type for any undefined struct type
408 * (including none), it succeeds: the default encoding is a hex string
409 * encoding.
411 SetLastError(0xdeadbeef);
412 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL, NULL, 0,
413 NULL, &size);
414 ok(ret, "CryptFormatObject failed: %d\n", GetLastError());
415 if (ret)
417 if (size == 0 && GetLastError() == ERROR_FILE_NOT_FOUND)
419 win_skip("CryptFormatObject has no default implementation\n");
420 return;
422 ok(size == sizeof(WCHAR), "unexpected size %d\n", size);
423 str = HeapAlloc(GetProcessHeap(), 0, size);
424 SetLastError(0xdeadbeef);
425 size = 0;
426 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL, NULL, 0,
427 str, &size);
428 ok(!ret && GetLastError() == ERROR_MORE_DATA,
429 "expected ERROR_MORE_DATA, got %d\n", GetLastError());
430 size = sizeof(WCHAR);
431 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL, NULL, 0,
432 str, &size);
433 ok(ret, "CryptFormatObject failed: %d\n", GetLastError());
434 ok(!str[0], "expected empty string\n");
435 HeapFree(GetProcessHeap(), 0, str);
437 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL, encodedInt,
438 sizeof(encodedInt), NULL, &size);
439 ok(ret, "CryptFormatObject failed: %d\n", GetLastError());
440 if (ret)
442 str = HeapAlloc(GetProcessHeap(), 0, size);
443 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL,
444 encodedInt, sizeof(encodedInt), str, &size);
445 ok(ret, "CryptFormatObject failed: %d\n", GetLastError());
446 ok(!lstrcmpW(str, encodedIntStr), "unexpected format string\n");
447 HeapFree(GetProcessHeap(), 0, str);
449 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL,
450 encodedBigInt, sizeof(encodedBigInt), NULL, &size);
451 ok(ret, "CryptFormatObject failed: %d\n", GetLastError());
452 if (ret)
454 str = HeapAlloc(GetProcessHeap(), 0, size);
455 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL, NULL,
456 encodedBigInt, sizeof(encodedBigInt), str, &size);
457 ok(ret, "CryptFormatObject failed: %d\n", GetLastError());
458 ok(!lstrcmpiW(str, encodedBigIntStr), "unexpected format string\n");
459 HeapFree(GetProcessHeap(), 0, str);
461 /* When called with the default encoding type for any undefined struct
462 * type but CRYPT_FORMAT_STR_NO_HEX specified, it fails to find a
463 * formatting function.
465 SetLastError(0xdeadbeef);
466 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, CRYPT_FORMAT_STR_NO_HEX,
467 NULL, NULL, NULL, 0, NULL, &size);
468 ok(!ret, "CryptFormatObject succeeded\n");
469 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
470 GetLastError() == 0xdeadbeef, /* Vista, W2K8 */
471 "expected ERROR_FILE_NOT_FOUND or no change, got %d\n", GetLastError());
472 /* When called to format an AUTHORITY_KEY_ID2_INFO, it fails when no
473 * data are given.
475 SetLastError(0xdeadbeef);
476 ret = pCryptFormatObject(X509_ASN_ENCODING, 0, 0, NULL,
477 szOID_AUTHORITY_KEY_IDENTIFIER2, NULL, 0, NULL, &size);
478 ok(!ret && GetLastError() == E_INVALIDARG,
479 "expected E_INVALIDARG, got %d\n", GetLastError());
482 START_TEST(main)
484 hCrypt = GetModuleHandleA("crypt32.dll");
486 test_findAttribute();
487 test_findExtension();
488 test_findRDNAttr();
489 test_verifyTimeValidity();
490 test_cryptAllocate();
491 test_cryptTls();
492 test_readTrustedPublisherDWORD();
493 test_getDefaultCryptProv();
494 test_CryptInstallOssGlobal();
495 test_format_object();