dsound/tests: Add IEEE float buffer tests for dsound8.
[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 || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
237     if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
238     {
239         win_skip("Not running as administrator\n");
240         return;
241     }
242     ok(hca != NULL, "Expected a context handle, got NULL\n");
243
244     attrs = GetFileAttributes(catroot);
245     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
246
247     /* Windows creates the GUID directory in capitals */
248     lstrcpyA(dummydir, catroot);
249     lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
250     attrs = GetFileAttributes(dummydir);
251     ok(attrs != INVALID_FILE_ATTRIBUTES,
252        "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
253
254     /* Only present on XP or higher. */
255     attrs = GetFileAttributes(catroot2);
256     if (attrs != INVALID_FILE_ATTRIBUTES)
257     {
258         lstrcpyA(dummydir, catroot2);
259         lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
260         attrs = GetFileAttributes(dummydir);
261         ok(attrs != INVALID_FILE_ATTRIBUTES,
262             "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
263     }
264
265     ret = pCryptCATAdminReleaseContext(hca, 0);
266     ok(ret, "Expected success\n");
267
268     /* Correct context handle and GUID */
269     ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
270     ok(ret, "Expected success\n");
271     ok(hca != NULL, "Expected a context handle, got NULL\n");
272
273     ret = pCryptCATAdminReleaseContext(hca, 0);
274     ok(ret, "Expected success\n");
275
276     /* Flags not equal to 0 */
277     ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
278     ok(ret, "Expected success\n");
279     ok(hca != NULL, "Expected a context handle, got NULL\n");
280
281     ret = pCryptCATAdminReleaseContext(hca, 0);
282     ok(ret, "Expected success\n");
283 }
284
285 /* TODO: Check whether SHA-1 is the algorithm that's always used */
286 static void test_calchash(void)
287 {
288     BOOL ret;
289     HANDLE file;
290     DWORD hashsize = 0;
291     BYTE* hash;
292     BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
293     CHAR temp[MAX_PATH];
294     DWORD written;
295
296     /* All NULL */
297     SetLastError(0xdeadbeef);
298     ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
299     ok(!ret, "Expected failure\n");
300     ok(GetLastError() == ERROR_INVALID_PARAMETER,
301        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
302
303     /* NULL filehandle, rest is legal */
304     SetLastError(0xdeadbeef);
305     ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
306     ok(!ret, "Expected failure\n");
307     ok(GetLastError() == ERROR_INVALID_PARAMETER,
308        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
309
310     /* Correct filehandle, rest is NULL */
311     file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
312     SetLastError(0xdeadbeef);
313     ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
314     ok(!ret, "Expected failure\n");
315     ok(GetLastError() == ERROR_INVALID_PARAMETER,
316        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
317     CloseHandle(file);
318
319     /* All OK, but dwFlags set to 1 */
320     file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
321     SetLastError(0xdeadbeef);
322     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
323     ok(!ret, "Expected failure\n");
324     ok(GetLastError() == ERROR_INVALID_PARAMETER,
325        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
326     CloseHandle(file);
327
328     /* All OK, requesting the size of the hash */
329     file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
330     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
331     SetLastError(0xdeadbeef);
332     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
333     ok(ret, "Expected success %u\n", GetLastError());
334     ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
335     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
336        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
337     CloseHandle(file);
338
339     /* All OK, retrieve the hash
340      * Double the hash buffer to see what happens to the size parameter
341      */
342     file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
343     hashsize *= 2;
344     hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
345     SetLastError(0xdeadbeef);
346     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
347     ok(ret, "Expected success %u\n", GetLastError());
348     ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
349     ok(GetLastError() == ERROR_SUCCESS,
350        "Expected ERROR_SUCCESS, got %d\n", GetLastError());
351     CloseHandle(file);
352     HeapFree(GetProcessHeap(), 0, hash);
353
354     /* Do the same test with a file created and filled by ourselves (and we thus
355      * have a known hash for).
356      */
357     GetTempFileNameA(CURR_DIR, "hsh", 0, temp); 
358     file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
359     WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
360     CloseHandle(file);
361
362     /* All OK, first request the size and then retrieve the hash */
363     file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
364     hashsize = 0;
365     pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
366     hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
367     SetLastError(0xdeadbeef);
368     ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
369     ok(ret, "Expected success\n");
370     ok(GetLastError() == ERROR_SUCCESS,
371        "Expected ERROR_SUCCESS, got %d\n", GetLastError());
372     ok(hashsize == sizeof(expectedhash) &&
373        !memcmp(hash, expectedhash, sizeof(expectedhash)),
374        "Hashes didn't match\n");
375     CloseHandle(file);
376
377     HeapFree(GetProcessHeap(), 0, hash);
378     DeleteFileA(temp);
379 }
380
381 static void test_CryptCATOpen(void)
382 {
383     HANDLE hcat;
384     char empty[MAX_PATH];
385     WCHAR emptyW[MAX_PATH];
386     HANDLE file;
387     BOOL ret;
388
389     SetLastError(0xdeadbeef);
390     hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
391     ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
392     ok(GetLastError() == ERROR_INVALID_PARAMETER,
393        "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
394
395     if (!GetTempFileNameA(CURR_DIR, "cat", 0, empty)) return;
396
397     file = CreateFileA(empty, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
398     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
399     CloseHandle(file);
400     MultiByteToWideChar(CP_ACP, 0, empty, -1, emptyW, MAX_PATH);
401
402     hcat = pCryptCATOpen(emptyW, 0, 0, 0, 0);
403     todo_wine
404     ok(hcat != INVALID_HANDLE_VALUE, "Expected a correct handle\n");
405
406     ret = pCryptCATClose(hcat);
407     todo_wine
408     ok(ret, "CryptCATClose failed\n");
409     DeleteFileA(empty);
410 }
411
412 static DWORD error_area;
413 static DWORD local_error;
414
415 static void WINAPI cdf_callback(DWORD area, DWORD error, WCHAR* line)
416 {
417     ok(error_area != -2, "Didn't expect cdf_callback() to be called (%08x, %08x)\n",
418        area, error);
419
420     error_area = area;
421     local_error = error;
422 }
423
424 static void test_CryptCATCDF_params(void)
425 {
426     static WCHAR nonexistent[] = {'d','e','a','d','b','e','e','f','.','c','d','f',0};
427     CRYPTCATCDF *catcdf;
428     BOOL ret;
429
430     if (!pCryptCATCDFOpen)
431     {
432         win_skip("CryptCATCDFOpen is not available\n");
433         return;
434     }
435
436     SetLastError(0xdeadbeef);
437     catcdf = pCryptCATCDFOpen(NULL, NULL);
438     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
439     todo_wine
440     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
441
442     SetLastError(0xdeadbeef);
443     catcdf = pCryptCATCDFOpen(NULL, cdf_callback);
444     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
445     todo_wine
446     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
447
448     /* File doesn't exist */
449     SetLastError(0xdeadbeef);
450     catcdf = pCryptCATCDFOpen(nonexistent, cdf_callback);
451     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
452     todo_wine
453     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
454
455     SetLastError(0xdeadbeef);
456     ret = pCryptCATCDFClose(NULL);
457     ok(!ret, "Expected failure\n");
458     todo_wine
459     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
460
461     catcdf = NULL;
462     SetLastError(0xdeadbeef);
463     ret = pCryptCATCDFClose(catcdf);
464     ok(!ret, "Expected failure\n");
465     todo_wine
466     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
467 }
468
469 /* FIXME: Once Wine can create catalog files we should use the created catalog file in this test */
470 static void test_CryptCATAdminAddRemoveCatalog(void)
471 {
472     static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
473     static CHAR basename[] = "winetest.cat";
474     HCATADMIN hcatadmin;
475     HCATINFO hcatinfo;
476     CATALOG_INFO info;
477     WCHAR tmpfileW[MAX_PATH];
478     char tmpfile[MAX_PATH];
479     char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
480     WCHAR catfileW[MAX_PATH];
481     HANDLE file;
482     DWORD error, written;
483     BOOL ret;
484     DWORD attrs;
485
486     if (!pCryptCATAdminRemoveCatalog)
487     {
488         /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
489         win_skip("CryptCATAdminRemoveCatalog is not available\n");
490         return;
491     }
492
493     if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
494     DeleteFileA(tmpfile);
495     file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
496     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
497     CloseHandle(file);
498
499     ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
500     ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
501     if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
502     {
503         win_skip("Not running as administrator\n");
504         return;
505     }
506
507     SetLastError(0xdeadbeef);
508     hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
509     error = GetLastError();
510     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
511     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
512
513     SetLastError(0xdeadbeef);
514     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
515     error = GetLastError();
516     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
517     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
518
519     MultiByteToWideChar(0, 0, tmpfile, -1, tmpfileW, MAX_PATH);
520
521     SetLastError(0xdeadbeef);
522     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
523     error = GetLastError();
524     todo_wine {
525     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
526     ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
527     }
528     if (hcatinfo != NULL)
529         pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
530
531     SetLastError(0xdeadbeef);
532     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
533     error = GetLastError();
534     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
535     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
536
537     SetLastError(0xdeadbeef);
538     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
539     error = GetLastError();
540     ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
541     todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
542
543     DeleteFileA(tmpfile);
544     file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
545     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
546     WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
547     CloseHandle(file);
548
549     /* Unique name will be created */
550     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
551     todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
552
553     info.cbStruct = sizeof(info);
554     info.wszCatalogFile[0] = 0;
555     ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
556     todo_wine
557     {
558     ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
559     ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
560     }
561     WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
562     if ((p = strrchr(catfile, '\\'))) p++;
563     memset(catfileW, 0, sizeof(catfileW));
564     MultiByteToWideChar(0, 0, p, -1, catfileW, MAX_PATH);
565
566     /* Set the file attributes so we can check what happens with them during the 'copy' */
567     attrs = FILE_ATTRIBUTE_READONLY;
568     ret = SetFileAttributesA(tmpfile, attrs);
569     ok(ret, "SetFileAttributesA failed : %u\n", GetLastError());
570
571     /* winetest.cat will be created */
572     hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
573     ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
574
575     lstrcpyA(catfilepath, catroot);
576     lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
577     attrs = GetFileAttributes(catfilepath);
578     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
579     todo_wine
580     ok(attrs == FILE_ATTRIBUTE_SYSTEM ||
581        attrs == (FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_SYSTEM), /* Vista */
582        "File has wrong attributes : %08x\n", attrs);
583
584     info.cbStruct = sizeof(info);
585     info.wszCatalogFile[0] = 0;
586     ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
587     ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
588     ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
589     WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
590     if ((p = strrchr(catfile, '\\'))) p++;
591     ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
592
593     ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
594     ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());
595
596     /* Remove the catalog file with the unique name */
597     ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
598     ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
599
600     /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
601      * according to MSDN */
602     ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
603     ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
604     /* The call succeeded with the full path but the file is not removed */
605     attrs = GetFileAttributes(catfilepath);
606     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
607     /* Given only the filename the file is removed */
608     ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
609     ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
610     attrs = GetFileAttributes(catfilepath);
611     ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
612
613     ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
614     ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());
615
616     /* Set the attributes so we can delete the file */
617     attrs = FILE_ATTRIBUTE_NORMAL;
618     ret = SetFileAttributesA(tmpfile, attrs);
619     DeleteFileA(tmpfile);
620 }
621
622 static void test_catalog_properties(CHAR *catfile, int attributes, int members)
623 {
624     static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
625
626     HANDLE hcat;
627     CRYPTCATMEMBER *m;
628     CRYPTCATATTRIBUTE *attr;
629     char catalog[MAX_PATH];
630     WCHAR catalogW[MAX_PATH];
631     DWORD attrs;
632     BOOL ret;
633     int attrcount = 0, membercount = 0;
634
635     /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
636      * once wine is fixed
637      */
638     attrs = GetFileAttributesA(catfile);
639     if (attrs == INVALID_FILE_ATTRIBUTES)
640     {
641         HANDLE file;
642         DWORD written;
643
644         trace("Creating the catalog file\n");
645         if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
646         file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
647         ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
648         WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
649         CloseHandle(file);
650
651         attributes = 2;
652         members = 1;
653         MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
654     }
655     else
656     {
657         MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
658         catalog[0] = 0;
659     }
660
661     hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
662     if (hcat == INVALID_HANDLE_VALUE && members == 0)
663     {
664         win_skip("CryptCATOpen on W2K can't handle catalog files with no members\n");
665         return;
666     }
667     ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
668
669     m = pCryptCATEnumerateMember(NULL, NULL);
670     ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
671
672     m = NULL;
673     while ((m = pCryptCATEnumerateMember(hcat, m)))
674     {
675         ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
676         todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
677         ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
678         ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
679         ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
680         ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
681         ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
682
683         attr = pCryptCATEnumerateAttr(hcat, m, NULL);
684         ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
685
686         membercount++;
687     }
688     ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
689
690     attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
691     ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
692
693     attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
694     ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
695
696     attr = NULL;
697     while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
698     {
699         ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
700            !lstrcmpW(attr->pwszReferenceTag, attr2W),
701            "Expected 'attr1' or 'attr2'\n");
702
703         attrcount++;
704     }
705     todo_wine
706     ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
707
708     ret = pCryptCATClose(hcat);
709     ok(ret, "CryptCATClose failed\n");
710     if (catalog[0]) DeleteFileA( catalog );
711 }
712
713 static void test_create_catalog_file(void)
714 {
715     static CHAR  catfileA[] = "winetest.cat";
716     static CHAR  cdffileA[] = "winetest.cdf";
717     static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
718     CRYPTCATCDF *catcdf;
719     CRYPTCATATTRIBUTE *catattr;
720     CRYPTCATMEMBER *catmember;
721     WCHAR  *catmembertag;
722     DWORD written, attrs;
723     HANDLE file;
724     BOOL ret;
725     int attrcount, membercount;
726
727     if (!pCryptCATCDFOpen)
728     {
729         win_skip("CryptCATCDFOpen is not available\n");
730         return;
731     }
732
733     /* Create the cdf file */
734     file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
735     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
736     WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
737     CloseHandle(file);
738
739     /* Don't enumerate attributes and members */
740     trace("No attribs and members\n");
741     SetLastError(0xdeadbeef);
742     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
743     todo_wine
744     {
745     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
746     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
747     }
748
749     ret = pCryptCATCDFClose(catcdf);
750     todo_wine
751     {
752     ok(ret, "Expected success\n");
753     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
754     }
755
756     attrs = GetFileAttributesA(catfileA);
757     todo_wine
758     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");
759
760     test_catalog_properties(catfileA, 0, 0);
761     DeleteFileA(catfileA);
762
763     /* Only enumerate the attributes */
764     trace("Only attributes\n");
765     attrcount = membercount = 0;
766     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
767
768     catattr = NULL;
769     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
770     {
771         ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
772            !lstrcmpW(catattr->pwszReferenceTag, attr2W),
773            "Expected 'attr1' or 'attr2'\n");
774
775         attrcount++;
776     }
777     todo_wine
778     ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
779
780     pCryptCATCDFClose(catcdf);
781     /* Even though the resulting catalog file shows the attributes, they will not be enumerated */
782     test_catalog_properties(catfileA, 0, 0);
783     DeleteFileA(catfileA);
784
785     /* Only enumerate the members */
786     trace("Only members\n");
787     attrcount = membercount = 0;
788     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
789
790     catmember = NULL;
791     catmembertag = NULL;
792     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
793     {
794         ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
795         membercount++;
796     }
797     todo_wine
798     ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
799
800     pCryptCATCDFClose(catcdf);
801     test_catalog_properties(catfileA, 0, 1);
802     DeleteFileA(catfileA);
803
804     /* Enumerate members and attributes */
805     trace("Attributes and members\n");
806     attrcount = membercount = 0;
807     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
808
809     catattr = NULL;
810     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
811         attrcount++;
812     todo_wine
813     ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
814
815     catmember = NULL;
816     catmembertag = NULL;
817     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
818         membercount++;
819     todo_wine
820     ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
821
822     pCryptCATCDFClose(catcdf);
823     test_catalog_properties(catfileA, 2, 1);
824     DeleteFileA(catfileA);
825
826     DeleteFileA(cdffileA);
827 }
828
829 static void create_cdf_file(const CHAR *filename, const CHAR *contents)
830 {
831     HANDLE file;
832     DWORD written;
833
834     file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
835     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
836     WriteFile(file, contents, lstrlenA(contents), &written, NULL);
837     CloseHandle(file);
838 }
839
840 #define CHECK_EXPECT(a, b) \
841     do { \
842         ok(a == error_area, "Expected %08x, got %08x\n", a, error_area); \
843         ok(b == local_error, "Expected %08x, got %08x\n", b, local_error); \
844     } while (0)
845
846 /* Clear the variables (can't use 0) */
847 #define CLEAR_EXPECT \
848     error_area = local_error = -1
849
850 /* Set both variables so the callback routine can check if a call to it was unexpected */
851 #define SET_UNEXPECTED \
852     error_area = local_error = -2
853
854 static void test_cdf_parsing(void)
855 {
856     static CHAR  catfileA[] = "tempfile.cat";
857     static CHAR  cdffileA[] = "tempfile.cdf";
858     static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
859     CHAR cdf_contents[4096];
860     CRYPTCATCDF *catcdf;
861     CRYPTCATATTRIBUTE *catattr;
862     CRYPTCATMEMBER *catmember;
863     WCHAR  *catmembertag;
864
865     if (!pCryptCATCDFOpen)
866     {
867         win_skip("CryptCATCDFOpen is not available\n");
868         return;
869     }
870
871     /* Empty file */
872     DeleteFileA(cdffileA);
873     create_cdf_file(cdffileA, "");
874
875     CLEAR_EXPECT;
876     SetLastError(0xdeadbeef);
877     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
878     todo_wine
879     CHECK_EXPECT(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND);
880     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
881     todo_wine
882     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
883     DeleteFileA(cdffileA);
884     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
885
886     /* Just the header */
887     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
888     create_cdf_file(cdffileA, cdf_contents);
889
890     SET_UNEXPECTED;
891     SetLastError(0xdeadbeef);
892     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
893     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
894     todo_wine
895     ok(GetLastError() == ERROR_SHARING_VIOLATION ||
896         broken(GetLastError() == ERROR_SUCCESS),    /* win9x */
897         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
898     DeleteFileA(cdffileA);
899
900     /* Header and member only */
901     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
902     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
903     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
904     create_cdf_file(cdffileA, cdf_contents);
905
906     SET_UNEXPECTED;
907     SetLastError(0xdeadbeef);
908     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
909     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
910     todo_wine
911     ok(GetLastError() == ERROR_SHARING_VIOLATION ||
912         broken(GetLastError() == ERROR_SUCCESS),    /* win9x */
913         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
914     DeleteFileA(cdffileA);
915     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
916
917     /* Header and Name (no value) */
918     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
919     lstrcatA(cdf_contents, "Name=\r\n");
920     create_cdf_file(cdffileA, cdf_contents);
921
922     SET_UNEXPECTED;
923     SetLastError(0xdeadbeef);
924     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
925     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
926     todo_wine
927     ok(GetLastError() == ERROR_SHARING_VIOLATION ||
928         broken(GetLastError() == ERROR_SUCCESS),    /* win9x */
929         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
930     DeleteFileA(cdffileA);
931     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
932
933     /* Header and Name */
934     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
935     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
936     create_cdf_file(cdffileA, cdf_contents);
937
938     SET_UNEXPECTED;
939     SetLastError(0xdeadbeef);
940     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
941     todo_wine
942     {
943     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
944     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
945     }
946     pCryptCATCDFClose(catcdf);
947     DeleteFileA(cdffileA);
948     todo_wine
949     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
950
951     /* Header and nonexistent member */
952     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
953     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
954     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
955     lstrcatA(cdf_contents, "hashme=.\\deadbeef.cdf\r\n");
956     create_cdf_file(cdffileA, cdf_contents);
957
958     SET_UNEXPECTED;
959     SetLastError(0xdeadbeef);
960     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
961     todo_wine
962     {
963     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
964     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
965     }
966     /* Loop through the members */
967     CLEAR_EXPECT;
968     catmember = NULL;
969     catmembertag = NULL;
970     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
971     todo_wine
972     CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND);
973     pCryptCATCDFClose(catcdf);
974     DeleteFileA(cdffileA);
975     todo_wine
976     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
977
978     /* Header, correct member but no explicit newline */
979     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
980     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
981     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
982     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r");
983     create_cdf_file(cdffileA, cdf_contents);
984
985     SET_UNEXPECTED;
986     SetLastError(0xdeadbeef);
987     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
988     todo_wine
989     {
990     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
991     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
992     }
993     /* Loop through the members */
994     CLEAR_EXPECT;
995     catmember = NULL;
996     catmembertag = NULL;
997     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
998     todo_wine
999     CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH);
1000     pCryptCATCDFClose(catcdf);
1001     DeleteFileA(cdffileA);
1002     todo_wine
1003     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1004
1005     /* Header and 2 duplicate members */
1006     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1007     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1008     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1009     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1010     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1011     create_cdf_file(cdffileA, cdf_contents);
1012
1013     SET_UNEXPECTED;
1014     SetLastError(0xdeadbeef);
1015     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1016     todo_wine
1017     {
1018     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1019     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1020     }
1021     /* Loop through the members */
1022     SET_UNEXPECTED;
1023     catmember = NULL;
1024     catmembertag = NULL;
1025     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1026     pCryptCATCDFClose(catcdf);
1027     test_catalog_properties(catfileA, 0, 1);
1028     DeleteFileA(cdffileA);
1029     todo_wine
1030     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1031
1032     /* Wrong attribute */
1033     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1034     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1035     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1\r\n");
1036     create_cdf_file(cdffileA, cdf_contents);
1037
1038     SET_UNEXPECTED;
1039     SetLastError(0xdeadbeef);
1040     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1041     todo_wine
1042     {
1043     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1044     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1045     }
1046     /* Loop through the attributes */
1047     CLEAR_EXPECT;
1048     catattr = NULL;
1049     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1050     todo_wine
1051     CHECK_EXPECT(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES);
1052     pCryptCATCDFClose(catcdf);
1053     DeleteFileA(cdffileA);
1054     todo_wine
1055     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1056
1057     /* Two identical attributes */
1058     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1059     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1060     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1061     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1062     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1063     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1064     create_cdf_file(cdffileA, cdf_contents);
1065
1066     SET_UNEXPECTED;
1067     SetLastError(0xdeadbeef);
1068     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1069     todo_wine
1070     {
1071     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1072     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1073     }
1074     /* Loop through the members */
1075     SET_UNEXPECTED;
1076     catmember = NULL;
1077     catmembertag = NULL;
1078     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1079     /* Loop through the attributes */
1080     SET_UNEXPECTED;
1081     catattr = NULL;
1082     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1083     pCryptCATCDFClose(catcdf);
1084     test_catalog_properties(catfileA, 1, 1);
1085     DeleteFileA(cdffileA);
1086     todo_wine
1087     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1088
1089     /* Two different attribute values with the same tag */
1090     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1091     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1092     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1093     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr2:value2\r\n");
1094     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1095     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1096     create_cdf_file(cdffileA, cdf_contents);
1097
1098     SET_UNEXPECTED;
1099     SetLastError(0xdeadbeef);
1100     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1101     todo_wine
1102     {
1103     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1104     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1105     }
1106     /* Loop through the members */
1107     SET_UNEXPECTED;
1108     catmember = NULL;
1109     catmembertag = NULL;
1110     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1111     /* Loop through the attributes */
1112     SET_UNEXPECTED;
1113     catattr = NULL;
1114     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1115     pCryptCATCDFClose(catcdf);
1116     test_catalog_properties(catfileA, 1, 1);
1117     DeleteFileA(cdffileA);
1118     todo_wine
1119     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1120 }
1121
1122 START_TEST(crypt)
1123 {
1124     char** myARGV;
1125     char sysdir[MAX_PATH];
1126
1127     InitFunctionPtrs();
1128
1129     if (!pCryptCATAdminAcquireContext)
1130     {
1131         win_skip("CryptCATAdmin functions are not available\n");
1132         return;
1133     }
1134
1135     GetSystemDirectoryA(sysdir, MAX_PATH);
1136     lstrcpyA(catroot, sysdir);
1137     lstrcatA(catroot, "\\CatRoot");
1138     lstrcpyA(catroot2, sysdir);
1139     lstrcatA(catroot2, "\\CatRoot2");
1140
1141     winetest_get_mainargs(&myARGV);
1142     strcpy(selfname, myARGV[0]);
1143
1144     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1145    
1146     test_context();
1147     test_calchash();
1148     test_CryptCATOpen();
1149     /* Parameter checking only */
1150     test_CryptCATCDF_params();
1151     /* Test the parsing of a cdf file */
1152     test_cdf_parsing();
1153     /* Create a catalog file out of our own catalog definition file */
1154     test_create_catalog_file();
1155     test_CryptCATAdminAddRemoveCatalog();
1156 }