crypt32: Fix a test that fails in Vista.
[wine/multimedia.git] / dlls / crypt32 / tests / main.c
blob87ac7116ce9ae8426a4fa76c65e8ce385733bd06
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 buf = CryptMemRealloc(NULL, 0);
204 ok(!buf, "Expected NULL\n");
205 buf = CryptMemAlloc(0);
206 buf = CryptMemRealloc(buf, 1);
207 ok(buf != NULL, "CryptMemRealloc failed: %08x\n", GetLastError());
208 CryptMemFree(buf);
211 typedef DWORD (WINAPI *I_CryptAllocTlsFunc)(void);
212 typedef LPVOID (WINAPI *I_CryptDetachTlsFunc)(DWORD dwTlsIndex);
213 typedef LPVOID (WINAPI *I_CryptGetTlsFunc)(DWORD dwTlsIndex);
214 typedef BOOL (WINAPI *I_CryptSetTlsFunc)(DWORD dwTlsIndex, LPVOID lpTlsValue);
215 typedef BOOL (WINAPI *I_CryptFreeTlsFunc)(DWORD dwTlsIndex, DWORD unknown);
217 static I_CryptAllocTlsFunc pI_CryptAllocTls;
218 static I_CryptDetachTlsFunc pI_CryptDetachTls;
219 static I_CryptGetTlsFunc pI_CryptGetTls;
220 static I_CryptSetTlsFunc pI_CryptSetTls;
221 static I_CryptFreeTlsFunc pI_CryptFreeTls;
223 static void test_cryptTls(void)
225 DWORD index;
226 BOOL ret;
228 pI_CryptAllocTls = (I_CryptAllocTlsFunc)GetProcAddress(hCrypt,
229 "I_CryptAllocTls");
230 pI_CryptDetachTls = (I_CryptDetachTlsFunc)GetProcAddress(hCrypt,
231 "I_CryptDetachTls");
232 pI_CryptGetTls = (I_CryptGetTlsFunc)GetProcAddress(hCrypt,
233 "I_CryptGetTls");
234 pI_CryptSetTls = (I_CryptSetTlsFunc)GetProcAddress(hCrypt,
235 "I_CryptSetTls");
236 pI_CryptFreeTls = (I_CryptFreeTlsFunc)GetProcAddress(hCrypt,
237 "I_CryptFreeTls");
239 /* One normal pass */
240 index = pI_CryptAllocTls();
241 ok(index, "I_CryptAllocTls failed: %08x\n", GetLastError());
242 if (index)
244 LPVOID ptr;
246 ptr = pI_CryptGetTls(index);
247 ok(!ptr, "Expected NULL\n");
248 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
249 ok(ret, "I_CryptSetTls failed: %08x\n", GetLastError());
250 ptr = pI_CryptGetTls(index);
251 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
252 /* This crashes
253 ret = pI_CryptFreeTls(index, 1);
255 ret = pI_CryptFreeTls(index, 0);
256 ok(ret, "I_CryptFreeTls failed: %08x\n", GetLastError());
257 ret = pI_CryptFreeTls(index, 0);
258 /* Not sure if this fails because TlsFree should fail, so leave as
259 * todo for now.
261 todo_wine ok(!ret && GetLastError() == E_INVALIDARG,
262 "Expected E_INVALIDARG, got %08x\n", GetLastError());
264 /* Similar pass, check I_CryptDetachTls */
265 index = pI_CryptAllocTls();
266 ok(index, "I_CryptAllocTls failed: %08x\n", GetLastError());
267 if (index)
269 LPVOID ptr;
271 ptr = pI_CryptGetTls(index);
272 ok(!ptr, "Expected NULL\n");
273 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
274 ok(ret, "I_CryptSetTls failed: %08x\n", GetLastError());
275 ptr = pI_CryptGetTls(index);
276 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
277 ptr = pI_CryptDetachTls(index);
278 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
279 ptr = pI_CryptGetTls(index);
280 ok(!ptr, "Expected NULL\n");
284 typedef BOOL (WINAPI *I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)
285 (LPCWSTR, DWORD *);
287 static void test_readTrustedPublisherDWORD(void)
289 I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc pReadDWORD;
291 pReadDWORD =
292 (I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)GetProcAddress(
293 hCrypt, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
294 if (pReadDWORD)
296 static const WCHAR safer[] = {
297 'S','o','f','t','w','a','r','e','\\',
298 'P','o','l','i','c','i','e','s','\\',
299 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
300 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
301 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
302 '\\','S','a','f','e','r',0 };
303 static const WCHAR authenticodeFlags[] = { 'A','u','t','h','e','n',
304 't','i','c','o','d','e','F','l','a','g','s',0 };
305 BOOL ret, exists = FALSE;
306 DWORD size, readFlags = 0, returnedFlags;
307 HKEY key;
308 LONG rc;
310 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, safer, &key);
311 if (rc == ERROR_SUCCESS)
313 size = sizeof(readFlags);
314 rc = RegQueryValueExW(key, authenticodeFlags, NULL, NULL,
315 (LPBYTE)&readFlags, &size);
316 if (rc == ERROR_SUCCESS)
317 exists = TRUE;
319 returnedFlags = 0xdeadbeef;
320 ret = pReadDWORD(authenticodeFlags, &returnedFlags);
321 ok(ret == exists, "Unexpected return value\n");
322 ok(readFlags == returnedFlags,
323 "Expected flags %08x, got %08x\n", readFlags, returnedFlags);
327 typedef HCRYPTPROV (WINAPI *I_CryptGetDefaultCryptProvFunc)(DWORD w);
329 static void test_getDefaultCryptProv(void)
331 I_CryptGetDefaultCryptProvFunc pI_CryptGetDefaultCryptProv;
332 HCRYPTPROV prov;
334 pI_CryptGetDefaultCryptProv = (I_CryptGetDefaultCryptProvFunc)
335 GetProcAddress(hCrypt, "I_CryptGetDefaultCryptProv");
336 if (!pI_CryptGetDefaultCryptProv) return;
338 prov = pI_CryptGetDefaultCryptProv(0xdeadbeef);
339 ok(prov == 0 && GetLastError() == E_INVALIDARG,
340 "Expected E_INVALIDARG, got %08x\n", GetLastError());
341 prov = pI_CryptGetDefaultCryptProv(PROV_RSA_FULL);
342 ok(prov == 0 && GetLastError() == E_INVALIDARG,
343 "Expected E_INVALIDARG, got %08x\n", GetLastError());
344 prov = pI_CryptGetDefaultCryptProv(1);
345 ok(prov == 0 && GetLastError() == E_INVALIDARG,
346 "Expected E_INVALIDARG, got %08x\n", GetLastError());
347 prov = pI_CryptGetDefaultCryptProv(0);
348 ok(prov != 0, "I_CryptGetDefaultCryptProv failed: %08x\n", GetLastError());
349 CryptReleaseContext(prov, 0);
352 typedef int (WINAPI *I_CryptInstallOssGlobal)(DWORD,DWORD,DWORD);
354 static void test_CryptInstallOssGlobal(void)
356 int ret,i;
357 I_CryptInstallOssGlobal pI_CryptInstallOssGlobal;
359 pI_CryptInstallOssGlobal= (I_CryptInstallOssGlobal)GetProcAddress(hCrypt,"I_CryptInstallOssGlobal");
360 /* passing in some random values to I_CryptInstallOssGlobal, it always returns 9 the first time, then 10, 11 etc.*/
361 for(i=0;i<30;i++)
363 ret = pI_CryptInstallOssGlobal(rand(),rand(),rand());
364 ok((9+i) == ret ||
365 ret == 0, /* Vista */
366 "Expected %d or 0, got %d\n",(9+i),ret);
370 START_TEST(main)
372 hCrypt = GetModuleHandleA("crypt32.dll");
374 test_findAttribute();
375 test_findExtension();
376 test_findRDNAttr();
377 test_verifyTimeValidity();
378 test_cryptAllocate();
379 test_cryptTls();
380 test_readTrustedPublisherDWORD();
381 test_getDefaultCryptProv();
382 test_CryptInstallOssGlobal();