wintrust/tests: Add a few tests.
[wine/multimedia.git] / dlls / wintrust / tests / crypt.c
blobb1b7bc5bd68e253f765f7bef5edf7054365dc881
1 /* Unit test suite for wintrust crypt functions
3 * Copyright 2007 Paul Vriens
4 * Copyright 2008 Hans Leidekker for CodeWeavers
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
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "windows.h"
26 #include "mscat.h"
28 #include "wine/test.h"
30 static char selfname[MAX_PATH];
31 static CHAR CURR_DIR[MAX_PATH];
32 static CHAR catroot[MAX_PATH];
33 static CHAR catroot2[MAX_PATH];
36 * Minimalistic catalog file. To reconstruct, save text below as winetest.cdf,
37 * convert to DOS line endings and run 'makecat /cat winetest.cdf'
41 [CatalogHeader]
42 Name=winetest.cat
43 ResultDir=.\
44 PublicVersion=0x00000001
45 EncodingType=
46 CATATTR1=0x10010001:attr1:value1
47 CATATTR2=0x10010001:attr2:value2
49 [CatalogFiles]
50 hashme=.\winetest.cdf
53 static const BYTE test_catalog[] = {
54 0x30, 0x82, 0x01, 0xbc, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
55 0x82, 0x01, 0xad, 0x30, 0x82, 0x01, 0xa9, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x82, 0x01, 0x9e,
56 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0a, 0x01, 0xa0, 0x82, 0x01, 0x8f, 0x30,
57 0x82, 0x01, 0x8b, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01,
58 0x01, 0x04, 0x10, 0xfa, 0x55, 0x2c, 0xc2, 0xf6, 0xcc, 0xdd, 0x11, 0x2a, 0x9c, 0x00, 0x14, 0x22,
59 0xec, 0x8f, 0x3b, 0x17, 0x0d, 0x30, 0x38, 0x31, 0x32, 0x31, 0x38, 0x31, 0x31, 0x32, 0x36, 0x34,
60 0x38, 0x5a, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01, 0x02,
61 0x05, 0x00, 0x30, 0x81, 0xdd, 0x30, 0x81, 0xda, 0x04, 0x0e, 0x68, 0x00, 0x61, 0x00, 0x73, 0x00,
62 0x68, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x00, 0x00, 0x31, 0x81, 0xc7, 0x30, 0x61, 0x06, 0x0a, 0x2b,
63 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x31, 0x53, 0x30, 0x51, 0x30, 0x2c, 0x06,
64 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x19, 0xa2, 0x1e, 0x80, 0x1c, 0x00,
65 0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c, 0x00,
66 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x3e, 0x30, 0x21, 0x30, 0x09, 0x06,
67 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xed, 0xd6, 0x9c, 0x9c, 0xb2, 0xfc,
68 0xaa, 0x03, 0xe8, 0xd3, 0x20, 0xf6, 0xab, 0x28, 0xc3, 0xff, 0xbd, 0x07, 0x36, 0xf5, 0x30, 0x62,
69 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x02, 0x31, 0x54, 0x30, 0x52,
70 0x1e, 0x4c, 0x00, 0x7b, 0x00, 0x44, 0x00, 0x45, 0x00, 0x33, 0x00, 0x35, 0x00, 0x31, 0x00, 0x41,
71 0x00, 0x34, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x45, 0x00, 0x35, 0x00, 0x39, 0x00, 0x2d,
72 0x00, 0x31, 0x00, 0x31, 0x00, 0x44, 0x00, 0x30, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x43, 0x00, 0x34,
73 0x00, 0x37, 0x00, 0x2d, 0x00, 0x30, 0x00, 0x30, 0x00, 0x43, 0x00, 0x30, 0x00, 0x34, 0x00, 0x46,
74 0x00, 0x43, 0x00, 0x32, 0x00, 0x39, 0x00, 0x35, 0x00, 0x45, 0x00, 0x45, 0x00, 0x7d, 0x02, 0x02,
75 0x02, 0x00, 0xa0, 0x6a, 0x30, 0x68, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
76 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61, 0x00, 0x74, 0x00, 0x74,
77 0x00, 0x72, 0x00, 0x32, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e, 0x76, 0x00, 0x61, 0x00,
78 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x32, 0x00, 0x00, 0x00, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06,
79 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61,
80 0x00, 0x74, 0x00, 0x74, 0x00, 0x72, 0x00, 0x31, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e,
81 0x76, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x31, 0x00, 0x00, 0x00, 0x31, 0x00,
84 static BOOL (WINAPI * pCryptCATAdminAcquireContext)(HCATADMIN*, const GUID*, DWORD);
85 static BOOL (WINAPI * pCryptCATAdminReleaseContext)(HCATADMIN, DWORD);
86 static BOOL (WINAPI * pCryptCATAdminCalcHashFromFileHandle)(HANDLE hFile, DWORD*, BYTE*, DWORD);
87 static HCATINFO (WINAPI * pCryptCATAdminAddCatalog)(HCATADMIN, PWSTR, PWSTR, DWORD);
88 static BOOL (WINAPI * pCryptCATAdminRemoveCatalog)(HCATADMIN, LPCWSTR, DWORD);
89 static BOOL (WINAPI * pCryptCATAdminReleaseCatalogContext)(HCATADMIN, HCATINFO, DWORD);
90 static HANDLE (WINAPI * pCryptCATOpen)(LPWSTR, DWORD, HCRYPTPROV, DWORD, DWORD);
91 static BOOL (WINAPI * pCryptCATCatalogInfoFromContext)(HCATINFO, CATALOG_INFO *, DWORD);
92 static CRYPTCATMEMBER * (WINAPI * pCryptCATEnumerateMember)(HANDLE, CRYPTCATMEMBER *);
93 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateAttr)(HANDLE, CRYPTCATMEMBER *, CRYPTCATATTRIBUTE *);
94 static BOOL (WINAPI * pCryptCATClose)(HANDLE);
96 static void InitFunctionPtrs(void)
98 HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
100 #define WINTRUST_GET_PROC(func) \
101 p ## func = (void*)GetProcAddress(hWintrust, #func); \
102 if(!p ## func) { \
103 trace("GetProcAddress(%s) failed\n", #func); \
106 WINTRUST_GET_PROC(CryptCATAdminAcquireContext)
107 WINTRUST_GET_PROC(CryptCATAdminReleaseContext)
108 WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle)
109 WINTRUST_GET_PROC(CryptCATAdminAddCatalog)
110 WINTRUST_GET_PROC(CryptCATAdminRemoveCatalog)
111 WINTRUST_GET_PROC(CryptCATAdminReleaseCatalogContext)
112 WINTRUST_GET_PROC(CryptCATOpen)
113 WINTRUST_GET_PROC(CryptCATCatalogInfoFromContext)
114 WINTRUST_GET_PROC(CryptCATEnumerateMember)
115 WINTRUST_GET_PROC(CryptCATEnumerateAttr)
116 WINTRUST_GET_PROC(CryptCATClose)
118 #undef WINTRUST_GET_PROC
121 static GUID dummy = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
123 static void test_context(void)
125 BOOL ret;
126 HCATADMIN hca;
127 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
128 CHAR dummydir[MAX_PATH];
129 DWORD attrs;
131 /* When CryptCATAdminAcquireContext is successful it will create
132 * several directories if they don't exist:
134 * ...\system32\CatRoot\{GUID}, this directory holds the .cat files
135 * ...\system32\CatRoot2\{GUID} (WinXP and up), here we find the catalog database for that GUID
137 * Windows Vista uses lowercase catroot and catroot2.
139 * When passed a NULL GUID it will create the following directories although on
140 * WinXP and up these directories are already present when Windows is installed:
142 * ...\system32\CatRoot\{127D0A1D-4EF2-11D1-8608-00C04FC295EE}
143 * ...\system32\CatRoot2\{127D0A1D-4EF2-11D1-8608-00C04FC295EE} (WinXP up)
145 * TODO: Find out what this GUID is/does.
147 * On WinXP and up there is also a TimeStamp file in some of directories that
148 * seem to indicate the last change to the catalog database for that GUID.
150 * On Windows 2000 some files are created/updated:
152 * ...\system32\CatRoot\SYSMAST.cbk
153 * ...\system32\CatRoot\SYSMAST.cbd
154 * ...\system32\CatRoot\{GUID}\CATMAST.cbk
155 * ...\system32\CatRoot\{GUID}\CATMAST.cbd
159 /* All NULL */
160 SetLastError(0xdeadbeef);
161 ret = pCryptCATAdminAcquireContext(NULL, NULL, 0);
162 ok(!ret, "Expected failure\n");
163 ok(GetLastError() == ERROR_INVALID_PARAMETER,
164 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
166 /* NULL GUID */
167 ret = pCryptCATAdminAcquireContext(&hca, NULL, 0);
168 ok(ret, "Expected success\n");
169 ok(hca != NULL, "Expected a context handle, got NULL\n");
171 /* All NULL */
172 SetLastError(0xdeadbeef);
173 ret = pCryptCATAdminReleaseContext(NULL, 0);
174 ok(!ret, "Expected failure\n");
175 ok(GetLastError() == ERROR_INVALID_PARAMETER,
176 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
178 /* Proper release */
179 SetLastError(0xdeadbeef);
180 ret = pCryptCATAdminReleaseContext(hca, 0);
181 ok(ret, "Expected success\n");
182 ok(GetLastError() == 0xdeadbeef,
183 "Expected no change in last error, got %d\n", GetLastError());
185 /* Try to release a second time */
186 SetLastError(0xdeadbeef);
187 ret = pCryptCATAdminReleaseContext(hca, 0);
188 ok(!ret, "Expected failure\n");
189 ok(GetLastError() == ERROR_INVALID_PARAMETER,
190 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
192 /* NULL context handle and dummy GUID */
193 SetLastError(0xdeadbeef);
194 ret = pCryptCATAdminAcquireContext(NULL, &dummy, 0);
195 ok(!ret, "Expected failure\n");
196 ok(GetLastError() == ERROR_INVALID_PARAMETER,
197 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
199 /* Correct context handle and dummy GUID
201 * The tests run in the past unfortunately made sure that some directories were created.
203 * FIXME:
204 * We don't want to mess too much with these for now so we should delete only the ones
205 * that shouldn't be there like the deadbeef ones. We first have to figure out if it's
206 * save to remove files and directories from CatRoot/CatRoot2.
209 ret = pCryptCATAdminAcquireContext(&hca, &dummy, 0);
210 ok(ret, "Expected success\n");
211 ok(hca != NULL, "Expected a context handle, got NULL\n");
213 attrs = GetFileAttributes(catroot);
214 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
216 /* Windows creates the GUID directory in capitals */
217 lstrcpyA(dummydir, catroot);
218 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
219 attrs = GetFileAttributes(dummydir);
220 ok(attrs != INVALID_FILE_ATTRIBUTES,
221 "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
223 /* Only present on XP or higher. */
224 attrs = GetFileAttributes(catroot2);
225 if (attrs != INVALID_FILE_ATTRIBUTES)
227 lstrcpyA(dummydir, catroot2);
228 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
229 attrs = GetFileAttributes(dummydir);
230 ok(attrs != INVALID_FILE_ATTRIBUTES,
231 "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
234 ret = pCryptCATAdminReleaseContext(hca, 0);
235 ok(ret, "Expected success\n");
237 /* Correct context handle and GUID */
238 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
239 ok(ret, "Expected success\n");
240 ok(hca != NULL, "Expected a context handle, got NULL\n");
242 ret = pCryptCATAdminReleaseContext(hca, 0);
243 ok(ret, "Expected success\n");
245 /* Flags not equal to 0 */
246 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
247 ok(ret, "Expected success\n");
248 ok(hca != NULL, "Expected a context handle, got NULL\n");
250 ret = pCryptCATAdminReleaseContext(hca, 0);
251 ok(ret, "Expected success\n");
254 /* TODO: Check whether SHA-1 is the algorithm that's always used */
255 static void test_calchash(void)
257 BOOL ret;
258 HANDLE file;
259 DWORD hashsize = 0;
260 BYTE* hash;
261 BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
262 CHAR temp[MAX_PATH];
263 DWORD written;
265 /* All NULL */
266 SetLastError(0xdeadbeef);
267 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
268 ok(!ret, "Expected failure\n");
269 ok(GetLastError() == ERROR_INVALID_PARAMETER,
270 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
272 /* NULL filehandle, rest is legal */
273 SetLastError(0xdeadbeef);
274 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
275 ok(!ret, "Expected failure\n");
276 ok(GetLastError() == ERROR_INVALID_PARAMETER,
277 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
279 /* Correct filehandle, rest is NULL */
280 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
281 SetLastError(0xdeadbeef);
282 ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
283 ok(!ret, "Expected failure\n");
284 ok(GetLastError() == ERROR_INVALID_PARAMETER,
285 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
286 CloseHandle(file);
288 /* All OK, but dwFlags set to 1 */
289 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
290 SetLastError(0xdeadbeef);
291 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
292 ok(!ret, "Expected failure\n");
293 ok(GetLastError() == ERROR_INVALID_PARAMETER,
294 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
295 CloseHandle(file);
297 /* All OK, requesting the size of the hash */
298 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
299 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
300 SetLastError(0xdeadbeef);
301 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
302 ok(ret, "Expected success %u\n", GetLastError());
303 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
304 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
305 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
306 CloseHandle(file);
308 /* All OK, retrieve the hash
309 * Double the hash buffer to see what happens to the size parameter
311 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
312 hashsize *= 2;
313 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
314 SetLastError(0xdeadbeef);
315 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
316 ok(ret, "Expected success %u\n", GetLastError());
317 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
318 ok(GetLastError() == ERROR_SUCCESS,
319 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
320 CloseHandle(file);
321 HeapFree(GetProcessHeap(), 0, hash);
323 /* Do the same test with a file created and filled by ourselves (and we thus
324 * have a known hash for).
326 GetTempFileNameA(CURR_DIR, "hsh", 0, temp);
327 file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
328 WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
329 CloseHandle(file);
331 /* All OK, first request the size and then retrieve the hash */
332 file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
333 hashsize = 0;
334 pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
335 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
336 SetLastError(0xdeadbeef);
337 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
338 ok(ret, "Expected success\n");
339 ok(GetLastError() == ERROR_SUCCESS,
340 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
341 ok(hashsize == sizeof(expectedhash) &&
342 !memcmp(hash, expectedhash, sizeof(expectedhash)),
343 "Hashes didn't match\n");
344 CloseHandle(file);
346 HeapFree(GetProcessHeap(), 0, hash);
347 DeleteFileA(temp);
350 static void test_CryptCATAdminAddRemoveCatalog(void)
352 static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
353 static CHAR basename[] = "winetest.cat";
354 HCATADMIN hcatadmin;
355 HCATINFO hcatinfo;
356 CATALOG_INFO info;
357 WCHAR tmpfileW[MAX_PATH];
358 char tmpfile[MAX_PATH];
359 char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
360 WCHAR catfileW[MAX_PATH];
361 HANDLE file;
362 DWORD error, written;
363 BOOL ret;
364 DWORD attrs;
366 if (!pCryptCATAdminRemoveCatalog)
368 /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
369 win_skip("CryptCATAdminRemoveCatalog is not available\n");
370 return;
373 if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
374 DeleteFileA(tmpfile);
375 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
376 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
377 CloseHandle(file);
379 ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
380 ok(ret, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
382 SetLastError(0xdeadbeef);
383 hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
384 error = GetLastError();
385 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
386 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMTER\n", GetLastError());
388 SetLastError(0xdeadbeef);
389 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
390 error = GetLastError();
391 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
392 ok(error == ERROR_INVALID_PARAMETER, "got %u expected INVALID_PARAMTER\n", GetLastError());
394 MultiByteToWideChar(0, 0, tmpfile, -1, tmpfileW, MAX_PATH);
396 SetLastError(0xdeadbeef);
397 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
398 error = GetLastError();
399 todo_wine {
400 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
401 ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
404 SetLastError(0xdeadbeef);
405 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
406 error = GetLastError();
407 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
408 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMTER\n", GetLastError());
410 SetLastError(0xdeadbeef);
411 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
412 error = GetLastError();
413 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
414 todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
416 DeleteFileA(tmpfile);
417 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
418 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
419 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
420 CloseHandle(file);
422 /* Unique name will be created */
423 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
424 todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
426 info.cbStruct = sizeof(info);
427 info.wszCatalogFile[0] = 0;
428 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
429 todo_wine
431 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
432 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
434 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
435 if ((p = strrchr(catfile, '\\'))) p++;
436 MultiByteToWideChar(0, 0, p, -1, catfileW, MAX_PATH);
438 /* winetest.cat will be created */
439 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
440 ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
442 lstrcpyA(catfilepath, catroot);
443 lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
444 attrs = GetFileAttributes(catfilepath);
445 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
447 info.cbStruct = sizeof(info);
448 info.wszCatalogFile[0] = 0;
449 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
450 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
451 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
452 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
453 if ((p = strrchr(catfile, '\\'))) p++;
454 ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
456 ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
457 ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());
459 /* Remove the catalog file with the unique name */
460 ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
461 todo_wine
462 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
464 /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
465 * according to MSDN */
466 ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
467 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
468 /* The call succeeds but the file is not removed */
469 attrs = GetFileAttributes(catfilepath);
470 todo_wine
471 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
472 ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
473 todo_wine
474 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
475 attrs = GetFileAttributes(catfilepath);
476 ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
478 ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
479 ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());
481 DeleteFileA(tmpfile);
484 static void test_catalog_properties(void)
486 static const WCHAR hashmeW[] = {'h','a','s','h','m','e',0};
487 static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
489 HANDLE hcat;
490 CRYPTCATMEMBER *m;
491 CRYPTCATATTRIBUTE *attr;
492 char catalog[MAX_PATH];
493 WCHAR catalogW[MAX_PATH];
494 DWORD written;
495 HANDLE file;
496 BOOL ret;
498 if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
499 file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
500 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
501 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
502 CloseHandle(file);
504 hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
505 ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
507 MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
509 hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
510 ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
512 m = pCryptCATEnumerateMember(NULL, NULL);
513 ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
515 m = pCryptCATEnumerateMember(hcat, NULL);
516 ok(m != NULL, "CryptCATEnumerateMember failed %u\n", GetLastError());
518 ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
519 todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
520 ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
521 ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
522 ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
523 ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
524 ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
526 attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
527 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
529 attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
530 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
532 attr = pCryptCATEnumerateAttr(hcat, m, NULL);
533 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
535 m = pCryptCATEnumerateMember(hcat, m);
536 ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
538 ret = pCryptCATClose(hcat);
539 ok(ret, "CryptCATClose failed\n");
541 DeleteFileA(catalog);
544 START_TEST(crypt)
546 int myARGC;
547 char** myARGV;
548 char windir[MAX_PATH];
550 InitFunctionPtrs();
552 if (!pCryptCATAdminAcquireContext)
554 win_skip("CryptCATAdmin functions are not available\n");
555 return;
558 GetWindowsDirectoryA(windir, MAX_PATH);
559 lstrcpyA(catroot, windir);
560 lstrcatA(catroot, "\\system32\\CatRoot");
561 lstrcpyA(catroot2, windir);
562 lstrcatA(catroot2, "\\system32\\CatRoot2");
564 myARGC = winetest_get_mainargs(&myARGV);
565 strcpy(selfname, myARGV[0]);
567 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
569 test_context();
570 test_calchash();
571 test_CryptCATAdminAddRemoveCatalog();
572 test_catalog_properties();