winex11: Register clipboard formats only once they are actually used.
[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     ok(ret, "SetFileAttributesA failed %u\n", GetLastError());
620     DeleteFileA(tmpfile);
621 }
622
623 static void test_catalog_properties(CHAR *catfile, int attributes, int members)
624 {
625     static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
626
627     HANDLE hcat;
628     CRYPTCATMEMBER *m;
629     CRYPTCATATTRIBUTE *attr;
630     char catalog[MAX_PATH];
631     WCHAR catalogW[MAX_PATH];
632     DWORD attrs;
633     BOOL ret;
634     int attrcount = 0, membercount = 0;
635
636     /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
637      * once wine is fixed
638      */
639     attrs = GetFileAttributesA(catfile);
640     if (attrs == INVALID_FILE_ATTRIBUTES)
641     {
642         HANDLE file;
643         DWORD written;
644
645         trace("Creating the catalog file\n");
646         if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
647         file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
648         ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
649         WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
650         CloseHandle(file);
651
652         attributes = 2;
653         members = 1;
654         MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
655     }
656     else
657     {
658         MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
659         catalog[0] = 0;
660     }
661
662     hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
663     if (hcat == INVALID_HANDLE_VALUE && members == 0)
664     {
665         win_skip("CryptCATOpen on W2K can't handle catalog files with no members\n");
666         return;
667     }
668     ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
669
670     m = pCryptCATEnumerateMember(NULL, NULL);
671     ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
672
673     m = NULL;
674     while ((m = pCryptCATEnumerateMember(hcat, m)))
675     {
676         ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
677         todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
678         ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
679         ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
680         ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
681         ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
682         ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
683
684         attr = pCryptCATEnumerateAttr(hcat, m, NULL);
685         ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
686
687         membercount++;
688     }
689     ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
690
691     attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
692     ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
693
694     attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
695     ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
696
697     attr = NULL;
698     while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
699     {
700         ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
701            !lstrcmpW(attr->pwszReferenceTag, attr2W),
702            "Expected 'attr1' or 'attr2'\n");
703
704         attrcount++;
705     }
706     todo_wine
707     ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
708
709     ret = pCryptCATClose(hcat);
710     ok(ret, "CryptCATClose failed\n");
711     if (catalog[0]) DeleteFileA( catalog );
712 }
713
714 static void test_create_catalog_file(void)
715 {
716     static CHAR  catfileA[] = "winetest.cat";
717     static CHAR  cdffileA[] = "winetest.cdf";
718     static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
719     CRYPTCATCDF *catcdf;
720     CRYPTCATATTRIBUTE *catattr;
721     CRYPTCATMEMBER *catmember;
722     WCHAR  *catmembertag;
723     DWORD written, attrs;
724     HANDLE file;
725     BOOL ret;
726     int attrcount, membercount;
727
728     if (!pCryptCATCDFOpen)
729     {
730         win_skip("CryptCATCDFOpen is not available\n");
731         return;
732     }
733
734     /* Create the cdf file */
735     file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
736     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
737     WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
738     CloseHandle(file);
739
740     /* Don't enumerate attributes and members */
741     trace("No attribs and members\n");
742     SetLastError(0xdeadbeef);
743     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
744     todo_wine
745     {
746     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
747     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
748     }
749
750     ret = pCryptCATCDFClose(catcdf);
751     todo_wine
752     {
753     ok(ret, "Expected success\n");
754     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
755     }
756
757     attrs = GetFileAttributesA(catfileA);
758     todo_wine
759     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");
760
761     test_catalog_properties(catfileA, 0, 0);
762     DeleteFileA(catfileA);
763
764     /* Only enumerate the attributes */
765     trace("Only attributes\n");
766     attrcount = membercount = 0;
767     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
768
769     catattr = NULL;
770     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
771     {
772         ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
773            !lstrcmpW(catattr->pwszReferenceTag, attr2W),
774            "Expected 'attr1' or 'attr2'\n");
775
776         attrcount++;
777     }
778     todo_wine
779     ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
780
781     pCryptCATCDFClose(catcdf);
782     /* Even though the resulting catalog file shows the attributes, they will not be enumerated */
783     test_catalog_properties(catfileA, 0, 0);
784     DeleteFileA(catfileA);
785
786     /* Only enumerate the members */
787     trace("Only members\n");
788     membercount = 0;
789     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
790
791     catmember = NULL;
792     catmembertag = NULL;
793     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
794     {
795         ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
796         membercount++;
797     }
798     todo_wine
799     ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
800
801     pCryptCATCDFClose(catcdf);
802     test_catalog_properties(catfileA, 0, 1);
803     DeleteFileA(catfileA);
804
805     /* Enumerate members and attributes */
806     trace("Attributes and members\n");
807     attrcount = membercount = 0;
808     catcdf = pCryptCATCDFOpen(cdffileW, NULL);
809
810     catattr = NULL;
811     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
812         attrcount++;
813     todo_wine
814     ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
815
816     catmember = NULL;
817     catmembertag = NULL;
818     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
819         membercount++;
820     todo_wine
821     ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
822
823     pCryptCATCDFClose(catcdf);
824     test_catalog_properties(catfileA, 2, 1);
825     DeleteFileA(catfileA);
826
827     DeleteFileA(cdffileA);
828 }
829
830 static void create_cdf_file(const CHAR *filename, const CHAR *contents)
831 {
832     HANDLE file;
833     DWORD written;
834
835     file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
836     ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
837     WriteFile(file, contents, lstrlenA(contents), &written, NULL);
838     CloseHandle(file);
839 }
840
841 #define CHECK_EXPECT(a, b) \
842     do { \
843         ok(a == error_area, "Expected %08x, got %08x\n", a, error_area); \
844         ok(b == local_error, "Expected %08x, got %08x\n", b, local_error); \
845     } while (0)
846
847 /* Clear the variables (can't use 0) */
848 #define CLEAR_EXPECT \
849     error_area = local_error = -1
850
851 /* Set both variables so the callback routine can check if a call to it was unexpected */
852 #define SET_UNEXPECTED \
853     error_area = local_error = -2
854
855 static void test_cdf_parsing(void)
856 {
857     static CHAR  catfileA[] = "tempfile.cat";
858     static CHAR  cdffileA[] = "tempfile.cdf";
859     static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
860     CHAR cdf_contents[4096];
861     CRYPTCATCDF *catcdf;
862     CRYPTCATATTRIBUTE *catattr;
863     CRYPTCATMEMBER *catmember;
864     WCHAR  *catmembertag;
865
866     if (!pCryptCATCDFOpen)
867     {
868         win_skip("CryptCATCDFOpen is not available\n");
869         return;
870     }
871
872     /* Empty file */
873     DeleteFileA(cdffileA);
874     create_cdf_file(cdffileA, "");
875
876     CLEAR_EXPECT;
877     SetLastError(0xdeadbeef);
878     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
879     todo_wine
880     CHECK_EXPECT(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND);
881     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
882     todo_wine
883     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
884     DeleteFileA(cdffileA);
885     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
886
887     /* Just the header */
888     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
889     create_cdf_file(cdffileA, cdf_contents);
890
891     SET_UNEXPECTED;
892     SetLastError(0xdeadbeef);
893     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
894     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
895     todo_wine
896     ok(GetLastError() == ERROR_SHARING_VIOLATION,
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         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
913     DeleteFileA(cdffileA);
914     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
915
916     /* Header and Name (no value) */
917     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
918     lstrcatA(cdf_contents, "Name=\r\n");
919     create_cdf_file(cdffileA, cdf_contents);
920
921     SET_UNEXPECTED;
922     SetLastError(0xdeadbeef);
923     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
924     ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
925     todo_wine
926     ok(GetLastError() == ERROR_SHARING_VIOLATION,
927         "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
928     DeleteFileA(cdffileA);
929     ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
930
931     /* Header and Name */
932     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
933     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
934     create_cdf_file(cdffileA, cdf_contents);
935
936     SET_UNEXPECTED;
937     SetLastError(0xdeadbeef);
938     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
939     todo_wine
940     {
941     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
942     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
943     }
944     pCryptCATCDFClose(catcdf);
945     DeleteFileA(cdffileA);
946     todo_wine
947     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
948
949     /* Header and nonexistent member */
950     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
951     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
952     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
953     lstrcatA(cdf_contents, "hashme=.\\deadbeef.cdf\r\n");
954     create_cdf_file(cdffileA, cdf_contents);
955
956     SET_UNEXPECTED;
957     SetLastError(0xdeadbeef);
958     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
959     todo_wine
960     {
961     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
962     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
963     }
964     /* Loop through the members */
965     CLEAR_EXPECT;
966     catmember = NULL;
967     catmembertag = NULL;
968     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
969     todo_wine
970     CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND);
971     pCryptCATCDFClose(catcdf);
972     DeleteFileA(cdffileA);
973     todo_wine
974     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
975
976     /* Header, correct member but no explicit newline */
977     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
978     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
979     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
980     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r");
981     create_cdf_file(cdffileA, cdf_contents);
982
983     SET_UNEXPECTED;
984     SetLastError(0xdeadbeef);
985     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
986     todo_wine
987     {
988     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
989     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
990     }
991     /* Loop through the members */
992     CLEAR_EXPECT;
993     catmember = NULL;
994     catmembertag = NULL;
995     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
996     todo_wine
997     CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH);
998     pCryptCATCDFClose(catcdf);
999     DeleteFileA(cdffileA);
1000     todo_wine
1001     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1002
1003     /* Header and 2 duplicate members */
1004     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1005     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1006     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1007     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1008     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1009     create_cdf_file(cdffileA, cdf_contents);
1010
1011     SET_UNEXPECTED;
1012     SetLastError(0xdeadbeef);
1013     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1014     todo_wine
1015     {
1016     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1017     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1018     }
1019     /* Loop through the members */
1020     SET_UNEXPECTED;
1021     catmember = NULL;
1022     catmembertag = NULL;
1023     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1024     pCryptCATCDFClose(catcdf);
1025     test_catalog_properties(catfileA, 0, 1);
1026     DeleteFileA(cdffileA);
1027     todo_wine
1028     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1029
1030     /* Wrong attribute */
1031     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1032     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1033     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1\r\n");
1034     create_cdf_file(cdffileA, cdf_contents);
1035
1036     SET_UNEXPECTED;
1037     SetLastError(0xdeadbeef);
1038     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1039     todo_wine
1040     {
1041     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1042     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1043     }
1044     /* Loop through the attributes */
1045     CLEAR_EXPECT;
1046     catattr = NULL;
1047     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1048     todo_wine
1049     CHECK_EXPECT(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES);
1050     pCryptCATCDFClose(catcdf);
1051     DeleteFileA(cdffileA);
1052     todo_wine
1053     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1054
1055     /* Two identical attributes */
1056     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1057     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1058     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1059     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1060     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1061     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1062     create_cdf_file(cdffileA, cdf_contents);
1063
1064     SET_UNEXPECTED;
1065     SetLastError(0xdeadbeef);
1066     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1067     todo_wine
1068     {
1069     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1070     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1071     }
1072     /* Loop through the members */
1073     SET_UNEXPECTED;
1074     catmember = NULL;
1075     catmembertag = NULL;
1076     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1077     /* Loop through the attributes */
1078     SET_UNEXPECTED;
1079     catattr = NULL;
1080     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1081     pCryptCATCDFClose(catcdf);
1082     test_catalog_properties(catfileA, 1, 1);
1083     DeleteFileA(cdffileA);
1084     todo_wine
1085     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1086
1087     /* Two different attribute values with the same tag */
1088     lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1089     lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1090     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1091     lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr2:value2\r\n");
1092     lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1093     lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1094     create_cdf_file(cdffileA, cdf_contents);
1095
1096     SET_UNEXPECTED;
1097     SetLastError(0xdeadbeef);
1098     catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1099     todo_wine
1100     {
1101     ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1102     ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1103     }
1104     /* Loop through the members */
1105     SET_UNEXPECTED;
1106     catmember = NULL;
1107     catmembertag = NULL;
1108     while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1109     /* Loop through the attributes */
1110     SET_UNEXPECTED;
1111     catattr = NULL;
1112     while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1113     pCryptCATCDFClose(catcdf);
1114     test_catalog_properties(catfileA, 1, 1);
1115     DeleteFileA(cdffileA);
1116     todo_wine
1117     ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1118 }
1119
1120 START_TEST(crypt)
1121 {
1122     char** myARGV;
1123     char sysdir[MAX_PATH];
1124
1125     InitFunctionPtrs();
1126
1127     if (!pCryptCATAdminAcquireContext)
1128     {
1129         win_skip("CryptCATAdmin functions are not available\n");
1130         return;
1131     }
1132
1133     GetSystemDirectoryA(sysdir, MAX_PATH);
1134     lstrcpyA(catroot, sysdir);
1135     lstrcatA(catroot, "\\CatRoot");
1136     lstrcpyA(catroot2, sysdir);
1137     lstrcatA(catroot2, "\\CatRoot2");
1138
1139     winetest_get_mainargs(&myARGV);
1140     strcpy(selfname, myARGV[0]);
1141
1142     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1143    
1144     test_context();
1145     test_calchash();
1146     test_CryptCATOpen();
1147     /* Parameter checking only */
1148     test_CryptCATCDF_params();
1149     /* Test the parsing of a cdf file */
1150     test_cdf_parsing();
1151     /* Create a catalog file out of our own catalog definition file */
1152     test_create_catalog_file();
1153     test_CryptCATAdminAddRemoveCatalog();
1154 }