kernel32: Add a 64-bit flag to the MODULE_GetBinaryType return value.
[wine] / dlls / wintrust / tests / crypt.c
1 /* Unit test suite for wintrust crypt functions
2  *
3  * Copyright 2007 Paul Vriens
4  * Copyright 2008 Hans Leidekker for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24
25 #include "windows.h"
26 #include "mscat.h"
27
28 #include "wine/test.h"
29
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];
34
35 static const WCHAR hashmeW[] = {'h','a','s','h','m','e',0};
36 static const WCHAR attr1W[] = {'a','t','t','r','1',0};
37 static const WCHAR attr2W[] = {'a','t','t','r','2',0};
38
39 /*
40  * Minimalistic catalog file. To reconstruct, save text below as winetest.cdf,
41  * convert to DOS line endings and run 'makecat /cat winetest.cdf'
42  */
43
44 /*
45 [CatalogHeader]
46 Name=winetest.cat
47 ResultDir=.\
48 PublicVersion=0x00000001
49 EncodingType=
50 CATATTR1=0x10010001:attr1:value1
51 CATATTR2=0x10010001:attr2:value2
52
53 [CatalogFiles]
54 hashme=.\winetest.cdf
55 */
56
57 static const CHAR test_cdf[] =
58     "[CatalogHeader]\r\n"
59     "Name=winetest.cat\r\n"
60     "ResultDir=.\\\r\n"
61     "PublicVersion=0x00000001\r\n"
62     "EncodingType=\r\n"
63     "CATATTR1=0x10010001:attr1:value1\r\n"
64     "CATATTR2=0x10010001:attr2:value2\r\n"
65     "\r\n"
66     "[CatalogFiles]\r\n"
67     "hashme=.\\winetest.cdf\r\n";
68
69 static const BYTE test_catalog[] = {
70     0x30, 0x82, 0x01, 0xbc, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
71     0x82, 0x01, 0xad, 0x30, 0x82, 0x01, 0xa9, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x82, 0x01, 0x9e,
72     0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0a, 0x01, 0xa0, 0x82, 0x01, 0x8f, 0x30,
73     0x82, 0x01, 0x8b, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01,
74     0x01, 0x04, 0x10, 0xfa, 0x55, 0x2c, 0xc2, 0xf6, 0xcc, 0xdd, 0x11, 0x2a, 0x9c, 0x00, 0x14, 0x22,
75     0xec, 0x8f, 0x3b, 0x17, 0x0d, 0x30, 0x38, 0x31, 0x32, 0x31, 0x38, 0x31, 0x31, 0x32, 0x36, 0x34,
76     0x38, 0x5a, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01, 0x02,
77     0x05, 0x00, 0x30, 0x81, 0xdd, 0x30, 0x81, 0xda, 0x04, 0x0e, 0x68, 0x00, 0x61, 0x00, 0x73, 0x00,
78     0x68, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x00, 0x00, 0x31, 0x81, 0xc7, 0x30, 0x61, 0x06, 0x0a, 0x2b,
79     0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x31, 0x53, 0x30, 0x51, 0x30, 0x2c, 0x06,
80     0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x19, 0xa2, 0x1e, 0x80, 0x1c, 0x00,
81     0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c, 0x00,
82     0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x3e, 0x30, 0x21, 0x30, 0x09, 0x06,
83     0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xed, 0xd6, 0x9c, 0x9c, 0xb2, 0xfc,
84     0xaa, 0x03, 0xe8, 0xd3, 0x20, 0xf6, 0xab, 0x28, 0xc3, 0xff, 0xbd, 0x07, 0x36, 0xf5, 0x30, 0x62,
85     0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x02, 0x31, 0x54, 0x30, 0x52,
86     0x1e, 0x4c, 0x00, 0x7b, 0x00, 0x44, 0x00, 0x45, 0x00, 0x33, 0x00, 0x35, 0x00, 0x31, 0x00, 0x41,
87     0x00, 0x34, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x45, 0x00, 0x35, 0x00, 0x39, 0x00, 0x2d,
88     0x00, 0x31, 0x00, 0x31, 0x00, 0x44, 0x00, 0x30, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x43, 0x00, 0x34,
89     0x00, 0x37, 0x00, 0x2d, 0x00, 0x30, 0x00, 0x30, 0x00, 0x43, 0x00, 0x30, 0x00, 0x34, 0x00, 0x46,
90     0x00, 0x43, 0x00, 0x32, 0x00, 0x39, 0x00, 0x35, 0x00, 0x45, 0x00, 0x45, 0x00, 0x7d, 0x02, 0x02,
91     0x02, 0x00, 0xa0, 0x6a, 0x30, 0x68, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
92     0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61, 0x00, 0x74, 0x00, 0x74,
93     0x00, 0x72, 0x00, 0x32, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e, 0x76, 0x00, 0x61, 0x00,
94     0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x32, 0x00, 0x00, 0x00, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06,
95     0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61,
96     0x00, 0x74, 0x00, 0x74, 0x00, 0x72, 0x00, 0x31, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e,
97     0x76, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x31, 0x00, 0x00, 0x00, 0x31, 0x00,
98 };
99
100 static BOOL (WINAPI * pCryptCATAdminAcquireContext)(HCATADMIN*, const GUID*, DWORD);
101 static BOOL (WINAPI * pCryptCATAdminReleaseContext)(HCATADMIN, DWORD);
102 static BOOL (WINAPI * pCryptCATAdminCalcHashFromFileHandle)(HANDLE hFile, DWORD*, BYTE*, DWORD);
103 static HCATINFO (WINAPI * pCryptCATAdminAddCatalog)(HCATADMIN, PWSTR, PWSTR, DWORD);
104 static BOOL (WINAPI * pCryptCATAdminRemoveCatalog)(HCATADMIN, LPCWSTR, DWORD);
105 static BOOL (WINAPI * pCryptCATAdminReleaseCatalogContext)(HCATADMIN, HCATINFO, DWORD);
106 static HANDLE (WINAPI * pCryptCATOpen)(LPWSTR, DWORD, HCRYPTPROV, DWORD, DWORD);
107 static BOOL (WINAPI * pCryptCATCatalogInfoFromContext)(HCATINFO, CATALOG_INFO *, DWORD);
108 static BOOL (WINAPI * pCryptCATCDFClose)(CRYPTCATCDF *);
109 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATCDFEnumCatAttributes)(CRYPTCATCDF *, CRYPTCATATTRIBUTE *,
110                                                                     PFN_CDF_PARSE_ERROR_CALLBACK);
111 static LPWSTR (WINAPI * pCryptCATCDFEnumMembersByCDFTagEx)(CRYPTCATCDF *, LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK,
112                                                            CRYPTCATMEMBER **, BOOL, LPVOID);
113 static CRYPTCATCDF * (WINAPI * pCryptCATCDFOpen)(LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK);
114 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateCatAttr)(HANDLE, CRYPTCATATTRIBUTE *);
115 static CRYPTCATMEMBER * (WINAPI * pCryptCATEnumerateMember)(HANDLE, CRYPTCATMEMBER *);
116 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateAttr)(HANDLE, CRYPTCATMEMBER *, CRYPTCATATTRIBUTE *);
117 static BOOL (WINAPI * pCryptCATClose)(HANDLE);
118
119 static void InitFunctionPtrs(void)
120 {
121     HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
122
123 #define WINTRUST_GET_PROC(func) \
124     p ## func = (void*)GetProcAddress(hWintrust, #func); \
125     if(!p ## func) { \
126       trace("GetProcAddress(%s) failed\n", #func); \
127     }
128
129     WINTRUST_GET_PROC(CryptCATAdminAcquireContext)
130     WINTRUST_GET_PROC(CryptCATAdminReleaseContext)
131     WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle)
132     WINTRUST_GET_PROC(CryptCATAdminAddCatalog)
133     WINTRUST_GET_PROC(CryptCATAdminRemoveCatalog)
134     WINTRUST_GET_PROC(CryptCATAdminReleaseCatalogContext)
135     WINTRUST_GET_PROC(CryptCATOpen)
136     WINTRUST_GET_PROC(CryptCATCatalogInfoFromContext)
137     WINTRUST_GET_PROC(CryptCATCDFClose)
138     WINTRUST_GET_PROC(CryptCATCDFEnumCatAttributes)
139     WINTRUST_GET_PROC(CryptCATCDFEnumMembersByCDFTagEx)
140     WINTRUST_GET_PROC(CryptCATCDFOpen)
141     WINTRUST_GET_PROC(CryptCATEnumerateCatAttr)
142     WINTRUST_GET_PROC(CryptCATEnumerateMember)
143     WINTRUST_GET_PROC(CryptCATEnumerateAttr)
144     WINTRUST_GET_PROC(CryptCATClose)
145
146 #undef WINTRUST_GET_PROC
147 }
148
149 static GUID dummy = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
150
151 static void test_context(void)
152 {
153     BOOL ret;
154     HCATADMIN hca;
155     static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
156     CHAR dummydir[MAX_PATH];
157     DWORD attrs;
158
159     /* When CryptCATAdminAcquireContext is successful it will create
160      * several directories if they don't exist:
161      *
162      * ...\system32\CatRoot\{GUID}, this directory holds the .cat files
163      * ...\system32\CatRoot2\{GUID}  (WinXP and up), here we find the catalog database for that GUID
164      *
165      * Windows Vista uses lowercase catroot and catroot2.
166      *
167      * When passed a NULL GUID it will create the following directories although on
168      * WinXP and up these directories are already present when Windows is installed:
169      *
170      * ...\system32\CatRoot\{127D0A1D-4EF2-11D1-8608-00C04FC295EE}
171      * ...\system32\CatRoot2\{127D0A1D-4EF2-11D1-8608-00C04FC295EE} (WinXP up)
172      *
173      * TODO: Find out what this GUID is/does.
174      *
175      * On WinXP and up there is also a TimeStamp file in some of directories that
176      * seem to indicate the last change to the catalog database for that GUID.
177      *
178      * On Windows 2000 some files are created/updated:
179      *
180      * ...\system32\CatRoot\SYSMAST.cbk
181      * ...\system32\CatRoot\SYSMAST.cbd
182      * ...\system32\CatRoot\{GUID}\CATMAST.cbk
183      * ...\system32\CatRoot\{GUID}\CATMAST.cbd
184      *
185      */
186
187     /* All NULL */
188     SetLastError(0xdeadbeef);
189     ret = pCryptCATAdminAcquireContext(NULL, NULL, 0);
190     ok(!ret, "Expected failure\n");
191     ok(GetLastError() == ERROR_INVALID_PARAMETER,
192        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
193
194     /* NULL GUID */
195     ret = pCryptCATAdminAcquireContext(&hca, NULL, 0);
196     ok(ret, "Expected success\n");
197     ok(hca != NULL, "Expected a context handle, got NULL\n");
198
199     /* All NULL */
200     SetLastError(0xdeadbeef);
201     ret = pCryptCATAdminReleaseContext(NULL, 0);
202     ok(!ret, "Expected failure\n");
203     ok(GetLastError() == ERROR_INVALID_PARAMETER,
204        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
205
206     /* Proper release */
207     SetLastError(0xdeadbeef);
208     ret = pCryptCATAdminReleaseContext(hca, 0);
209     ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
210
211     /* Try to release a second time */
212     SetLastError(0xdeadbeef);
213     ret = pCryptCATAdminReleaseContext(hca, 0);
214     ok(!ret, "Expected failure\n");
215     ok(GetLastError() == ERROR_INVALID_PARAMETER,
216        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
217
218     /* NULL context handle and dummy GUID */
219     SetLastError(0xdeadbeef);
220     ret = pCryptCATAdminAcquireContext(NULL, &dummy, 0);
221     ok(!ret, "Expected failure\n");
222     ok(GetLastError() == ERROR_INVALID_PARAMETER,
223        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
224
225     /* Correct context handle and dummy GUID
226      *
227      * The tests run in the past unfortunately made sure that some directories were created.
228      *
229      * FIXME:
230      * We don't want to mess too much with these for now so we should delete only the ones
231      * that shouldn't be there like the deadbeef ones. We first have to figure out if it's
232      * save to remove files and directories from CatRoot/CatRoot2.
233      */
234
235     ret = pCryptCATAdminAcquireContext(&hca, &dummy, 0);
236     ok(ret, "Expected success\n");
237     ok(hca != NULL, "Expected a context handle, got NULL\n");
238
239     attrs = GetFileAttributes(catroot);
240     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
241
242     /* Windows creates the GUID directory in capitals */
243     lstrcpyA(dummydir, catroot);
244     lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
245     attrs = GetFileAttributes(dummydir);
246     ok(attrs != INVALID_FILE_ATTRIBUTES,
247        "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
248
249     /* Only present on XP or higher. */
250     attrs = GetFileAttributes(catroot2);
251     if (attrs != INVALID_FILE_ATTRIBUTES)
252     {
253         lstrcpyA(dummydir, catroot2);
254         lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
255         attrs = GetFileAttributes(dummydir);
256         ok(attrs != INVALID_FILE_ATTRIBUTES,
257             "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
258     }
259
260     ret = pCryptCATAdminReleaseContext(hca, 0);
261     ok(ret, "Expected success\n");
262
263     /* Correct context handle and GUID */
264     ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
265     ok(ret, "Expected success\n");
266     ok(hca != NULL, "Expected a context handle, got NULL\n");
267
268     ret = pCryptCATAdminReleaseContext(hca, 0);
269     ok(ret, "Expected success\n");
270
271     /* Flags not equal to 0 */
272     ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
273     ok(ret, "Expected success\n");
274     ok(hca != NULL, "Expected a context handle, got NULL\n");
275
276     ret = pCryptCATAdminReleaseContext(hca, 0);
277     ok(ret, "Expected success\n");
278 }
279
280 /* TODO: Check whether SHA-1 is the algorithm that's always used */
281 static void test_calchash(void)
282 {
283     BOOL ret;
284     HANDLE file;
285     DWORD hashsize = 0;
286     BYTE* hash;
287     BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
288     CHAR temp[MAX_PATH];
289     DWORD written;
290
291     /* All NULL */
292     SetLastError(0xdeadbeef);
293     ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
294     ok(!ret, "Expected failure\n");
295     ok(GetLastError() == ERROR_INVALID_PARAMETER,
296        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
297
298     /* NULL filehandle, rest is legal */
299     SetLastError(0xdeadbeef);
300     ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
301     ok(!ret, "Expected failure\n");
302     ok(GetLastError() == ERROR_INVALID_PARAMETER,
303        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
304
305     /* Correct filehandle, rest is NULL */
306     file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
307     SetLastError(0xdeadbeef);
308     ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
309     ok(!ret, "Expected failure\n");
310     ok(GetLastError() == ERROR_INVALID_PARAMETER,
311        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
312     CloseHandle(file);
313
314     /* All OK, but dwFlags set to 1 */
315     file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
316     SetLastError(0xdeadbeef);
317     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
318     ok(!ret, "Expected failure\n");
319     ok(GetLastError() == ERROR_INVALID_PARAMETER,
320        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
321     CloseHandle(file);
322
323     /* All OK, requesting the size of the hash */
324     file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
325     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
326     SetLastError(0xdeadbeef);
327     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
328     ok(ret, "Expected success %u\n", GetLastError());
329     ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
330     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
331        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
332     CloseHandle(file);
333
334     /* All OK, retrieve the hash
335      * Double the hash buffer to see what happens to the size parameter
336      */
337     file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
338     hashsize *= 2;
339     hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
340     SetLastError(0xdeadbeef);
341     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
342     ok(ret, "Expected success %u\n", GetLastError());
343     ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
344     ok(GetLastError() == ERROR_SUCCESS,
345        "Expected ERROR_SUCCESS, got %d\n", GetLastError());
346     CloseHandle(file);
347     HeapFree(GetProcessHeap(), 0, hash);
348
349     /* Do the same test with a file created and filled by ourselves (and we thus
350      * have a known hash for).
351      */
352     GetTempFileNameA(CURR_DIR, "hsh", 0, temp); 
353     file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
354     WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
355     CloseHandle(file);
356
357     /* All OK, first request the size and then retrieve the hash */
358     file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
359     hashsize = 0;
360     pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
361     hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
362     SetLastError(0xdeadbeef);
363     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
364     ok(ret, "Expected success\n");
365     ok(GetLastError() == ERROR_SUCCESS,
366        "Expected ERROR_SUCCESS, got %d\n", GetLastError());
367     ok(hashsize == sizeof(expectedhash) &&
368        !memcmp(hash, expectedhash, sizeof(expectedhash)),
369        "Hashes didn't match\n");
370     CloseHandle(file);
371
372     HeapFree(GetProcessHeap(), 0, hash);
373     DeleteFileA(temp);
374 }
375
376 static void test_CryptCATOpen(void)
377 {
378     HANDLE hcat;
379     char empty[MAX_PATH];
380     WCHAR emptyW[MAX_PATH];
381     HANDLE file;
382     BOOL ret;
383
384     SetLastError(0xdeadbeef);
385     hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
386     ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
387     ok(GetLastError() == ERROR_INVALID_PARAMETER,
388        "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
389
390     if (!GetTempFileNameA(CURR_DIR, "cat", 0, empty)) return;
391
392     file = CreateFileA(empty, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
393     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
394     CloseHandle(file);
395     MultiByteToWideChar(CP_ACP, 0, empty, -1, emptyW, MAX_PATH);
396
397     hcat = pCryptCATOpen(emptyW, 0, 0, 0, 0);
398     todo_wine
399     ok(hcat != INVALID_HANDLE_VALUE, "Expected a correct handle\n");
400
401     ret = pCryptCATClose(hcat);
402     todo_wine
403     ok(ret, "CryptCATClose failed\n");
404     DeleteFileA(empty);
405 }
406
407 static DWORD error_area;
408 static DWORD local_error;
409
410 static void WINAPI cdf_callback(DWORD area, DWORD error, WCHAR* line)
411 {
412     ok(error_area != -2, "Didn't expect cdf_callback() to be called (%08x, %08x)\n",
413        area, error);
414
415     error_area = area;
416     local_error = error;
417 }
418
419 static void test_CryptCATCDF_params(void)
420 {
421     static WCHAR nonexistent[] = {'d','e','a','d','b','e','e','f','.','c','d','f',0};
422     CRYPTCATCDF *catcdf;
423     BOOL ret;
424
425     if (!pCryptCATCDFOpen)
426     {
427         win_skip("CryptCATCDFOpen is not available\n");
428         return;
429     }
430
431     SetLastError(0xdeadbeef);
432     catcdf = pCryptCATCDFOpen(NULL, NULL);
433     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
434     todo_wine
435     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
436
437     SetLastError(0xdeadbeef);
438     catcdf = pCryptCATCDFOpen(NULL, cdf_callback);
439     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
440     todo_wine
441     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
442
443     /* File doesn't exist */
444     SetLastError(0xdeadbeef);
445     catcdf = pCryptCATCDFOpen(nonexistent, cdf_callback);
446     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
447     todo_wine
448     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
449
450     SetLastError(0xdeadbeef);
451     ret = pCryptCATCDFClose(NULL);
452     ok(!ret, "Expected failure\n");
453     todo_wine
454     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
455
456     catcdf = NULL;
457     SetLastError(0xdeadbeef);
458     ret = pCryptCATCDFClose(catcdf);
459     ok(!ret, "Expected failure\n");
460     todo_wine
461     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
462 }
463
464 /* FIXME: Once Wine can create catalog files we should use the created catalog file in this test */
465 static void test_CryptCATAdminAddRemoveCatalog(void)
466 {
467     static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
468     static CHAR basename[] = "winetest.cat";
469     HCATADMIN hcatadmin;
470     HCATINFO hcatinfo;
471     CATALOG_INFO info;
472     WCHAR tmpfileW[MAX_PATH];
473     char tmpfile[MAX_PATH];
474     char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
475     WCHAR catfileW[MAX_PATH];
476     HANDLE file;
477     DWORD error, written;
478     BOOL ret;
479     DWORD attrs;
480
481     if (!pCryptCATAdminRemoveCatalog)
482     {
483         /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
484         win_skip("CryptCATAdminRemoveCatalog is not available\n");
485         return;
486     }
487
488     if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
489     DeleteFileA(tmpfile);
490     file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
491     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
492     CloseHandle(file);
493
494     ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
495     ok(ret, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
496
497     SetLastError(0xdeadbeef);
498     hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
499     error = GetLastError();
500     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
501     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
502
503     SetLastError(0xdeadbeef);
504     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
505     error = GetLastError();
506     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
507     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
508
509     MultiByteToWideChar(0, 0, tmpfile, -1, tmpfileW, MAX_PATH);
510
511     SetLastError(0xdeadbeef);
512     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
513     error = GetLastError();
514     todo_wine {
515     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
516     ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
517     }
518
519     SetLastError(0xdeadbeef);
520     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
521     error = GetLastError();
522     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
523     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
524
525     SetLastError(0xdeadbeef);
526     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
527     error = GetLastError();
528     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
529     todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
530
531     DeleteFileA(tmpfile);
532     file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
533     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
534     WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
535     CloseHandle(file);
536
537     /* Unique name will be created */
538     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
539     todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
540
541     info.cbStruct = sizeof(info);
542     info.wszCatalogFile[0] = 0;
543     ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
544     todo_wine
545     {
546     ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
547     ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
548     }
549     WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
550     if ((p = strrchr(catfile, '\\'))) p++;
551     memset(catfileW, 0, sizeof(catfileW));
552     MultiByteToWideChar(0, 0, p, -1, catfileW, MAX_PATH);
553
554     /* Set the file attributes so we can check what happens with them during the 'copy' */
555     attrs = FILE_ATTRIBUTE_READONLY;
556     ret = SetFileAttributesA(tmpfile, attrs);
557     ok(ret, "SetFileAttributesA failed : %u\n", GetLastError());
558
559     /* winetest.cat will be created */
560     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
561     ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
562
563     lstrcpyA(catfilepath, catroot);
564     lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
565     attrs = GetFileAttributes(catfilepath);
566     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
567     todo_wine
568     ok(attrs == FILE_ATTRIBUTE_SYSTEM ||
569        attrs == (FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_SYSTEM), /* Vista */
570        "File has wrong attributes : %08x\n", attrs);
571
572     info.cbStruct = sizeof(info);
573     info.wszCatalogFile[0] = 0;
574     ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
575     ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
576     ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
577     WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
578     if ((p = strrchr(catfile, '\\'))) p++;
579     ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
580
581     ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
582     ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());
583
584     /* Remove the catalog file with the unique name */
585     ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
586     ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
587
588     /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
589      * according to MSDN */
590     ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
591     ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
592     /* The call succeeded with the full path but the file is not removed */
593     attrs = GetFileAttributes(catfilepath);
594     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
595     /* Given only the filename the file is removed */
596     ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
597     ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
598     attrs = GetFileAttributes(catfilepath);
599     ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
600
601     ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
602     ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());
603
604     /* Set the attributes so we can delete the file */
605     attrs = FILE_ATTRIBUTE_NORMAL;
606     ret = SetFileAttributesA(tmpfile, attrs);
607     DeleteFileA(tmpfile);
608 }
609
610 static void test_catalog_properties(CHAR *catfile, int attributes, int members)
611 {
612     static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
613
614     HANDLE hcat;
615     CRYPTCATMEMBER *m;
616     CRYPTCATATTRIBUTE *attr;
617     char catalog[MAX_PATH];
618     WCHAR catalogW[MAX_PATH];
619     DWORD attrs;
620     BOOL ret;
621     int attrcount = 0, membercount = 0;
622
623     /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
624      * once wine is fixed
625      */
626     attrs = GetFileAttributesA(catfile);
627     if (attrs == INVALID_FILE_ATTRIBUTES)
628     {
629         HANDLE file;
630         DWORD written;
631
632         trace("Creating the catalog file\n");
633         if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
634         file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
635         ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
636         WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
637         CloseHandle(file);
638
639         attributes = 2;
640         members = 1;
641         MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
642     }
643     else
644     {
645         MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
646         catalog[0] = 0;
647     }
648
649     hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
650     if (hcat == INVALID_HANDLE_VALUE && members == 0)
651     {
652         win_skip("CryptCATOpen on W2K can't handle catalog files with no members\n");
653         return;
654     }
655     ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
656
657     m = pCryptCATEnumerateMember(NULL, NULL);
658     ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
659
660     m = NULL;
661     while ((m = pCryptCATEnumerateMember(hcat, m)))
662     {
663         ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
664         todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
665         ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
666         ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
667         ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
668         ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
669         ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
670
671         attr = pCryptCATEnumerateAttr(hcat, m, NULL);
672         ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
673
674         membercount++;
675     }
676     ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
677
678     attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
679     ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
680
681     attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
682     ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
683
684     attr = NULL;
685     while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
686     {
687         ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
688            !lstrcmpW(attr->pwszReferenceTag, attr2W),
689            "Expected 'attr1' or 'attr2'\n");
690
691         attrcount++;
692     }
693     todo_wine
694     ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
695
696     ret = pCryptCATClose(hcat);
697     ok(ret, "CryptCATClose failed\n");
698     if (catalog[0]) DeleteFileA( catalog );
699 }
700
701 static void test_create_catalog_file(void)
702 {
703     static CHAR  catfileA[] = "winetest.cat";
704     static CHAR  cdffileA[] = "winetest.cdf";
705     static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
706     CRYPTCATCDF *catcdf;
707     CRYPTCATATTRIBUTE *catattr;
708     CRYPTCATMEMBER *catmember;
709     WCHAR  *catmembertag;
710     DWORD written, attrs;
711     HANDLE file;
712     BOOL ret;
713     int attrcount, membercount;
714
715     if (!pCryptCATCDFOpen)
716     {
717         win_skip("CryptCATCDFOpen is not available\n");
718         return;
719     }
720
721     /* Create the cdf file */
722     file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
723     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
724     WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
725     CloseHandle(file);
726
727     /* Don't enumerate attributes and members */
728     trace("No attribs and members\n");
729     SetLastError(0xdeadbeef);
730     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
731     todo_wine
732     {
733     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
734     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
735     }
736
737     ret = pCryptCATCDFClose(catcdf);
738     todo_wine
739     {
740     ok(ret, "Expected success\n");
741     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
742     }
743
744     attrs = GetFileAttributesA(catfileA);
745     todo_wine
746     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");
747
748     test_catalog_properties(catfileA, 0, 0);
749     DeleteFileA(catfileA);
750
751     /* Only enumerate the attributes */
752     trace("Only attributes\n");
753     attrcount = membercount = 0;
754     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
755
756     catattr = NULL;
757     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
758     {
759         ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
760            !lstrcmpW(catattr->pwszReferenceTag, attr2W),
761            "Expected 'attr1' or 'attr2'\n");
762
763         attrcount++;
764     }
765     todo_wine
766     ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
767
768     pCryptCATCDFClose(catcdf);
769     /* Even though the resulting catalog file shows the attributes, they will not be enumerated */
770     test_catalog_properties(catfileA, 0, 0);
771     DeleteFileA(catfileA);
772
773     /* Only enumerate the members */
774     trace("Only members\n");
775     attrcount = membercount = 0;
776     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
777
778     catmember = NULL;
779     catmembertag = NULL;
780     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
781     {
782         ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
783         membercount++;
784     }
785     todo_wine
786     ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
787
788     pCryptCATCDFClose(catcdf);
789     test_catalog_properties(catfileA, 0, 1);
790     DeleteFileA(catfileA);
791
792     /* Enumerate members and attributes */
793     trace("Attributes and members\n");
794     attrcount = membercount = 0;
795     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
796
797     catattr = NULL;
798     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
799         attrcount++;
800     todo_wine
801     ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
802
803     catmember = NULL;
804     catmembertag = NULL;
805     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
806         membercount++;
807     todo_wine
808     ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
809
810     pCryptCATCDFClose(catcdf);
811     test_catalog_properties(catfileA, 2, 1);
812     DeleteFileA(catfileA);
813
814     DeleteFileA(cdffileA);
815 }
816
817 static void create_cdf_file(const CHAR *filename, const CHAR *contents)
818 {
819     HANDLE file;
820     DWORD written;
821
822     file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
823     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
824     WriteFile(file, contents, lstrlenA(contents), &written, NULL);
825     CloseHandle(file);
826 }
827
828 #define CHECK_EXPECT(a, b) \
829     do { \
830         ok(a == error_area, "Expected %08x, got %08x\n", a, error_area); \
831         ok(b == local_error, "Expected %08x, got %08x\n", b, local_error); \
832     } while (0)
833
834 /* Clear the variables (can't use 0) */
835 #define CLEAR_EXPECT \
836     error_area = local_error = -1
837
838 /* Set both variables so the callback routine can check if a call to it was unexpected */
839 #define SET_UNEXPECTED \
840     error_area = local_error = -2
841
842 static void test_cdf_parsing(void)
843 {
844     static CHAR  catfileA[] = "tempfile.cat";
845     static CHAR  cdffileA[] = "tempfile.cdf";
846     static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
847     CHAR cdf_contents[4096];
848     CRYPTCATCDF *catcdf;
849     CRYPTCATATTRIBUTE *catattr;
850     CRYPTCATMEMBER *catmember;
851     WCHAR  *catmembertag;
852
853     if (!pCryptCATCDFOpen)
854     {
855         win_skip("CryptCATCDFOpen is not available\n");
856         return;
857     }
858
859     /* Empty file */
860     DeleteFileA(cdffileA);
861     create_cdf_file(cdffileA, "");
862
863     CLEAR_EXPECT;
864     SetLastError(0xdeadbeef);
865     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
866     todo_wine
867     CHECK_EXPECT(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND);
868     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
869     todo_wine
870     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
871     DeleteFileA(cdffileA);
872     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
873
874     /* Just the header */
875     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
876     create_cdf_file(cdffileA, cdf_contents);
877
878     SET_UNEXPECTED;
879     SetLastError(0xdeadbeef);
880     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
881     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
882     todo_wine
883     ok(GetLastError() == ERROR_SHARING_VIOLATION ||
884         broken(GetLastError() == ERROR_SUCCESS),    /* win9x */
885         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
886     DeleteFileA(cdffileA);
887
888     /* Header and member only */
889     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
890     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
891     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
892     create_cdf_file(cdffileA, cdf_contents);
893
894     SET_UNEXPECTED;
895     SetLastError(0xdeadbeef);
896     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
897     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
898     todo_wine
899     ok(GetLastError() == ERROR_SHARING_VIOLATION ||
900         broken(GetLastError() == ERROR_SUCCESS),    /* win9x */
901         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
902     DeleteFileA(cdffileA);
903     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
904
905     /* Header and Name (no value) */
906     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
907     lstrcatA(cdf_contents, "Name=\r\n");
908     create_cdf_file(cdffileA, cdf_contents);
909
910     SET_UNEXPECTED;
911     SetLastError(0xdeadbeef);
912     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
913     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
914     todo_wine
915     ok(GetLastError() == ERROR_SHARING_VIOLATION ||
916         broken(GetLastError() == ERROR_SUCCESS),    /* win9x */
917         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
918     DeleteFileA(cdffileA);
919     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
920
921     /* Header and Name */
922     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
923     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
924     create_cdf_file(cdffileA, cdf_contents);
925
926     SET_UNEXPECTED;
927     SetLastError(0xdeadbeef);
928     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
929     todo_wine
930     {
931     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
932     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
933     }
934     pCryptCATCDFClose(catcdf);
935     DeleteFileA(cdffileA);
936     todo_wine
937     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
938
939     /* Header and nonexistent member */
940     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
941     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
942     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
943     lstrcatA(cdf_contents, "hashme=.\\deadbeef.cdf\r\n");
944     create_cdf_file(cdffileA, cdf_contents);
945
946     SET_UNEXPECTED;
947     SetLastError(0xdeadbeef);
948     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
949     todo_wine
950     {
951     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
952     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
953     }
954     /* Loop through the members */
955     CLEAR_EXPECT;
956     catmember = NULL;
957     catmembertag = NULL;
958     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
959     todo_wine
960     CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND);
961     pCryptCATCDFClose(catcdf);
962     DeleteFileA(cdffileA);
963     todo_wine
964     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
965
966     /* Header, correct member but no explicit newline */
967     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
968     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
969     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
970     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r");
971     create_cdf_file(cdffileA, cdf_contents);
972
973     SET_UNEXPECTED;
974     SetLastError(0xdeadbeef);
975     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
976     todo_wine
977     {
978     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
979     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
980     }
981     /* Loop through the members */
982     CLEAR_EXPECT;
983     catmember = NULL;
984     catmembertag = NULL;
985     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
986     todo_wine
987     CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH);
988     pCryptCATCDFClose(catcdf);
989     DeleteFileA(cdffileA);
990     todo_wine
991     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
992
993     /* Header and 2 duplicate members */
994     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
995     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
996     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
997     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
998     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
999     create_cdf_file(cdffileA, cdf_contents);
1000
1001     SET_UNEXPECTED;
1002     SetLastError(0xdeadbeef);
1003     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1004     todo_wine
1005     {
1006     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1007     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1008     }
1009     /* Loop through the members */
1010     SET_UNEXPECTED;
1011     catmember = NULL;
1012     catmembertag = NULL;
1013     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1014     pCryptCATCDFClose(catcdf);
1015     test_catalog_properties(catfileA, 0, 1);
1016     DeleteFileA(cdffileA);
1017     todo_wine
1018     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1019
1020     /* Wrong attribute */
1021     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1022     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1023     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1\r\n");
1024     create_cdf_file(cdffileA, cdf_contents);
1025
1026     SET_UNEXPECTED;
1027     SetLastError(0xdeadbeef);
1028     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1029     todo_wine
1030     {
1031     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1032     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1033     }
1034     /* Loop through the attributes */
1035     CLEAR_EXPECT;
1036     catattr = NULL;
1037     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1038     todo_wine
1039     CHECK_EXPECT(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES);
1040     pCryptCATCDFClose(catcdf);
1041     DeleteFileA(cdffileA);
1042     todo_wine
1043     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1044
1045     /* Two identical attributes */
1046     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1047     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1048     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1049     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1050     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1051     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1052     create_cdf_file(cdffileA, cdf_contents);
1053
1054     SET_UNEXPECTED;
1055     SetLastError(0xdeadbeef);
1056     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1057     todo_wine
1058     {
1059     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1060     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1061     }
1062     /* Loop through the members */
1063     SET_UNEXPECTED;
1064     catmember = NULL;
1065     catmembertag = NULL;
1066     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1067     /* Loop through the attributes */
1068     SET_UNEXPECTED;
1069     catattr = NULL;
1070     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1071     pCryptCATCDFClose(catcdf);
1072     test_catalog_properties(catfileA, 1, 1);
1073     DeleteFileA(cdffileA);
1074     todo_wine
1075     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1076
1077     /* Two different attribute values with the same tag */
1078     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1079     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1080     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1081     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr2:value2\r\n");
1082     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1083     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1084     create_cdf_file(cdffileA, cdf_contents);
1085
1086     SET_UNEXPECTED;
1087     SetLastError(0xdeadbeef);
1088     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1089     todo_wine
1090     {
1091     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1092     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1093     }
1094     /* Loop through the members */
1095     SET_UNEXPECTED;
1096     catmember = NULL;
1097     catmembertag = NULL;
1098     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1099     /* Loop through the attributes */
1100     SET_UNEXPECTED;
1101     catattr = NULL;
1102     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1103     pCryptCATCDFClose(catcdf);
1104     test_catalog_properties(catfileA, 1, 1);
1105     DeleteFileA(cdffileA);
1106     todo_wine
1107     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1108 }
1109
1110 START_TEST(crypt)
1111 {
1112     int myARGC;
1113     char** myARGV;
1114     char sysdir[MAX_PATH];
1115
1116     InitFunctionPtrs();
1117
1118     if (!pCryptCATAdminAcquireContext)
1119     {
1120         win_skip("CryptCATAdmin functions are not available\n");
1121         return;
1122     }
1123
1124     GetSystemDirectoryA(sysdir, MAX_PATH);
1125     lstrcpyA(catroot, sysdir);
1126     lstrcatA(catroot, "\\CatRoot");
1127     lstrcpyA(catroot2, sysdir);
1128     lstrcatA(catroot2, "\\CatRoot2");
1129
1130     myARGC = winetest_get_mainargs(&myARGV);
1131     strcpy(selfname, myARGV[0]);
1132
1133     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1134    
1135     test_context();
1136     test_calchash();
1137     test_CryptCATOpen();
1138     /* Parameter checking only */
1139     test_CryptCATCDF_params();
1140     /* Test the parsing of a cdf file */
1141     test_cdf_parsing();
1142     /* Create a catalog file out of our own catalog definition file */
1143     test_create_catalog_file();
1144     test_CryptCATAdminAddRemoveCatalog();
1145 }