comctl32: Update Polish translation.
[wine] / dlls / crypt32 / tests / sip.c
1 /*
2  * Subject Interface Package tests
3  *
4  * Copyright 2006 Paul Vriens
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 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <winnls.h>
27 #include <wincrypt.h>
28 #include <mssip.h>
29
30 #include "wine/test.h"
31
32 static char *show_guid(const GUID *guid)
33 {
34     static char guidstring[39];
35
36     sprintf(guidstring,
37         "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
38         guid->Data1, guid->Data2, guid->Data3,
39         guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
40         guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
41
42     return guidstring;
43 }
44
45 static void test_AddRemoveProvider(void)
46 {
47     BOOL ret;
48     SIP_ADD_NEWPROVIDER newprov;
49     GUID actionid = { 0xdeadbe, 0xefde, 0xadbe, { 0xef,0xde,0xad,0xbe,0xef,0xde,0xad,0xbe }};
50     static WCHAR dummydll[]      = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
51     static WCHAR dummyfunction[] = {'d','u','m','m','y','f','u','n','c','t','i','o','n',0 };
52
53     /* NULL check */
54     SetLastError(0xdeadbeef);
55     ret = CryptSIPRemoveProvider(NULL);
56     ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
57     ok (GetLastError() == ERROR_INVALID_PARAMETER,
58         "Expected ERROR_INVALID_PARAMETER, got %ld.\n", GetLastError());
59
60     /* nonexistent provider should result in a registry error */
61     SetLastError(0xdeadbeef);
62     ret = CryptSIPRemoveProvider(&actionid);
63     ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
64     ok (GetLastError() == ERROR_FILE_NOT_FOUND,
65         "Expected ERROR_FILE_NOT_FOUND, got %ld.\n", GetLastError());
66
67     /* Everything OK, pwszIsFunctionName and pwszIsFunctionNameFmt2 are left NULL
68      * as allowed */
69
70     memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
71     newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
72     newprov.pgSubject = &actionid;
73     newprov.pwszDLLFileName = dummydll;
74     newprov.pwszGetFuncName = dummyfunction;
75     newprov.pwszPutFuncName = dummyfunction;
76     newprov.pwszCreateFuncName = dummyfunction;
77     newprov.pwszVerifyFuncName = dummyfunction;
78     newprov.pwszRemoveFuncName = dummyfunction;
79     SetLastError(0xdeadbeef);
80     ret = CryptSIPAddProvider(&newprov);
81     ok ( ret, "CryptSIPAddProvider should have succeeded\n");
82     ok ( GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %ld\n",
83      GetLastError());
84
85     /* Dummy provider will be deleted, but the function still fails because
86      * pwszIsFunctionName and pwszIsFunctionNameFmt2 are not present in the
87      * registry.
88      */
89     SetLastError(0xdeadbeef);
90     ret = CryptSIPRemoveProvider(&actionid);
91     ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
92     ok (GetLastError() == ERROR_FILE_NOT_FOUND,
93         "Expected ERROR_FILE_NOT_FOUND, got %ld.\n", GetLastError());
94
95     /* Everything OK */
96     memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
97     newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
98     newprov.pgSubject = &actionid;
99     newprov.pwszDLLFileName = dummydll;
100     newprov.pwszGetFuncName = dummyfunction;
101     newprov.pwszPutFuncName = dummyfunction;
102     newprov.pwszCreateFuncName = dummyfunction;
103     newprov.pwszVerifyFuncName = dummyfunction;
104     newprov.pwszRemoveFuncName = dummyfunction;
105     newprov.pwszIsFunctionNameFmt2 = dummyfunction;
106     newprov.pwszIsFunctionName = dummyfunction;
107     SetLastError(0xdeadbeef);
108     ret = CryptSIPAddProvider(&newprov);
109     ok ( ret, "CryptSIPAddProvider should have succeeded\n");
110     ok ( GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %ld\n",
111      GetLastError());
112
113     /* Dummy provider should be deleted */
114     SetLastError(0xdeadbeef);
115     ret = CryptSIPRemoveProvider(&actionid);
116     ok ( ret, "CryptSIPRemoveProvider should have succeeded\n");
117     ok ( GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %ld\n",
118      GetLastError());
119 }
120
121 static void test_SIPRetrieveSubjectGUID(void)
122 {
123     BOOL ret;
124     GUID subject;
125     HANDLE file;
126     static const CHAR windir[] = "windir";
127     static const CHAR regeditExe[] = "regedit.exe";
128     static const GUID nullSubject  = { 0x0, 0x0, 0x0, { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 }};
129     static const WCHAR deadbeef[]  = { 'c',':','\\','d','e','a','d','b','e','e','f','.','d','b','f',0 };
130     /* Couldn't find a name for this GUID, it's the one used for 95% of the files */
131     static const GUID unknownGUID = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
132     static CHAR  regeditPath[MAX_PATH];
133     static WCHAR regeditPathW[MAX_PATH];
134     static CHAR path[MAX_PATH];
135     static CHAR tempfile[MAX_PATH];
136     static WCHAR tempfileW[MAX_PATH];
137     DWORD written;
138
139     /* NULL check */
140     SetLastError(0xdeadbeef);
141     ret = CryptSIPRetrieveSubjectGuid(NULL, NULL, NULL);
142     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
143     todo_wine
144         ok (GetLastError() == ERROR_INVALID_PARAMETER,
145             "Expected ERROR_INVALID_PARAMETER, got %ld.\n", GetLastError());
146
147     /* Test with a non-existent file (hopefully) */
148     SetLastError(0xdeadbeef);
149     /* Set subject to something other than zero's */
150     memset(&subject, 1, sizeof(GUID));
151     ret = CryptSIPRetrieveSubjectGuid(deadbeef, NULL, &subject);
152     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
153     todo_wine
154     {
155         ok (GetLastError() == ERROR_FILE_NOT_FOUND,
156             "Expected ERROR_FILE_NOT_FOUND, got %ld.\n", GetLastError());
157         ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
158             "Expected a NULL GUID for c:\\deadbeef.dbf, not %s\n", show_guid(&subject));
159     }
160
161     /* Now with an executable that should exist
162      *
163      * Use A-functions where possible as that should be available on all platforms
164      */
165     GetEnvironmentVariableA(windir, regeditPath, MAX_PATH);
166     sprintf(regeditPath, "%s\\%s", regeditPath, regeditExe);
167     MultiByteToWideChar( CP_ACP, 0, regeditPath,
168                          strlen(regeditPath)+1, regeditPathW,
169                          sizeof(regeditPathW)/sizeof(regeditPathW[0]) );
170
171     SetLastError(0xdeadbeef);
172     memset(&subject, 1, sizeof(GUID));
173     ret = CryptSIPRetrieveSubjectGuid(regeditPathW, NULL, &subject);
174     todo_wine
175     {
176         ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
177         ok ( GetLastError() == ERROR_SUCCESS,
178             "Expected ERROR_SUCCESS, got 0x%08lx\n", GetLastError());
179         ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
180             "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
181     }
182
183     /* The same thing but now with a handle instead of a filename */
184     file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
185     SetLastError(0xdeadbeef);
186     memset(&subject, 1, sizeof(GUID));
187     ret = CryptSIPRetrieveSubjectGuid(NULL, file, &subject);
188     todo_wine
189     {
190         ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
191         ok ( GetLastError() == ERROR_SUCCESS,
192             "Expected ERROR_SUCCESS, got 0x%08lx\n", GetLastError());
193         ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
194             "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
195     }
196     CloseHandle(file);
197
198     /* And both */
199     file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
200     SetLastError(0xdeadbeef);
201     memset(&subject, 1, sizeof(GUID));
202     ret = CryptSIPRetrieveSubjectGuid(regeditPathW, file, &subject);
203     todo_wine
204     {
205         ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
206         ok ( GetLastError() == ERROR_SUCCESS,
207             "Expected ERROR_SUCCESS, got 0x%08lx\n", GetLastError());
208         ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
209             "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
210     }
211     CloseHandle(file);
212
213     /* Now with an empty file */
214     GetTempPathA(sizeof(path), path);
215     GetTempFileNameA(path, "sip", 0 , tempfile);
216     MultiByteToWideChar( CP_ACP, 0, tempfile,
217                          strlen(tempfile)+1, tempfileW,
218                          sizeof(tempfileW)/sizeof(tempfileW[0]) );
219
220     SetLastError(0xdeadbeef);
221     memset(&subject, 1, sizeof(GUID));
222     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
223     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
224     todo_wine
225     {
226         ok ( GetLastError() == ERROR_FILE_INVALID ||
227              GetLastError() == S_OK /* Win98 */,
228             "Expected ERROR_FILE_INVALID or S_OK, got 0x%08lx\n", GetLastError());
229         ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
230             "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
231     }
232
233     /* Use a file with a size of 3 (at least < 4) */
234     file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
235     WriteFile(file, "123", 3, &written, NULL);
236     CloseHandle(file);
237
238     SetLastError(0xdeadbeef);
239     memset(&subject, 1, sizeof(GUID));
240     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
241     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
242     todo_wine
243     {
244         ok ( GetLastError() == ERROR_INVALID_PARAMETER ||
245              GetLastError() == S_OK /* Win98 */,
246             "Expected ERROR_INVALID_PARAMETER or S_OK, got 0x%08lx\n", GetLastError());
247         ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
248             "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
249     }
250
251     /* And now >= 4 */
252     file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
253     WriteFile(file, "1234", 4, &written, NULL);
254     CloseHandle(file);
255
256     SetLastError(0xdeadbeef);
257     memset(&subject, 1, sizeof(GUID));
258     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
259     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
260     todo_wine
261     {
262         ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN ||
263              GetLastError() == S_OK /* Win98 */,
264             "Expected TRUST_E_SUBJECT_FORM_UNKNOWN or S_OK, got 0x%08lx\n", GetLastError());
265         ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
266             "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
267     }
268
269     /* Clean up */
270     DeleteFileA(tempfile);
271 }
272
273 static void test_SIPLoad(void)
274 {
275     BOOL ret;
276     GUID subject;
277     static GUID dummySubject = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
278     static GUID unknown      = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
279     SIP_DISPATCH_INFO sdi;
280
281     /* All NULL */
282     SetLastError(0xdeadbeef);
283     ret = CryptSIPLoad(NULL, 0, NULL);
284     ok ( !ret, "Expected CryptSIPLoad to fail\n");
285     todo_wine
286         ok ( GetLastError() == ERROR_INVALID_PARAMETER,
287             "Expected ERROR_INVALID_PARAMETER, got 0x%08lx\n", GetLastError());
288
289     /* Only pSipDispatch NULL */
290     SetLastError(0xdeadbeef);
291     ret = CryptSIPLoad(&subject, 0, NULL);
292     ok ( !ret, "Expected CryptSIPLoad to fail\n");
293     todo_wine
294         ok ( GetLastError() == ERROR_INVALID_PARAMETER,
295             "Expected ERROR_INVALID_PARAMETER, got 0x%08lx\n", GetLastError());
296
297     /* No NULLs, but nonexistent pgSubject */
298     SetLastError(0xdeadbeef);
299     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
300     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
301     ret = CryptSIPLoad(&dummySubject, 0, &sdi);
302     ok ( !ret, "Expected CryptSIPLoad to fail\n");
303     todo_wine
304         ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
305             "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08lx\n", GetLastError());
306
307     /* cbSize not initialized */
308     SetLastError(0xdeadbeef);
309     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
310     ret = CryptSIPLoad(&dummySubject, 0, &sdi);
311     ok ( !ret, "Expected CryptSIPLoad to fail\n");
312     todo_wine
313         ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
314             "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08lx\n", GetLastError());
315
316     /* cbSize not initialized, but valid subject (named unknown but registered by wintrust) */
317     SetLastError(0xdeadbeef);
318     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
319     ret = CryptSIPLoad(&unknown, 0, &sdi);
320     todo_wine
321     {
322         ok ( ret, "Expected CryptSIPLoad to succeed\n");
323         ok ( GetLastError() == ERROR_PROC_NOT_FOUND,
324             "Expected ERROR_PROC_NOT_FOUND, got 0x%08lx\n", GetLastError());
325     }
326
327     /* All OK */
328     SetLastError(0xdeadbeef);
329     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
330     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
331     ret = CryptSIPLoad(&unknown, 0, &sdi);
332     todo_wine
333         ok ( ret, "Expected CryptSIPLoad to succeed\n");
334     ok ( GetLastError() == 0xdeadbeef,
335         "Expected 0xdeadbeef, got 0x%08lx\n", GetLastError());
336
337     /* Reserved parameter not 0 */
338     SetLastError(0xdeadbeef);
339     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
340     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
341     ret = CryptSIPLoad(&unknown, 1, &sdi);
342     ok ( !ret, "Expected CryptSIPLoad to fail\n");
343     todo_wine
344         ok ( GetLastError() == ERROR_INVALID_PARAMETER,
345             "Expected ERROR_INVALID_PARAMETER, got 0x%08lx\n", GetLastError());
346 }
347
348 START_TEST(sip)
349 {
350     test_AddRemoveProvider();
351     test_SIPRetrieveSubjectGUID();
352     test_SIPLoad();
353 }