d3drm: Implement D3DRMCreateColorRGB.
[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 BOOL (WINAPI * funcCryptSIPGetSignedDataMsg)(SIP_SUBJECTINFO *,DWORD *,DWORD,DWORD *,BYTE *);
33 static BOOL (WINAPI * funcCryptSIPPutSignedDataMsg)(SIP_SUBJECTINFO *,DWORD,DWORD *,DWORD,BYTE *);
34 static BOOL (WINAPI * funcCryptSIPCreateIndirectData)(SIP_SUBJECTINFO *,DWORD *,SIP_INDIRECT_DATA *);
35 static BOOL (WINAPI * funcCryptSIPVerifyIndirectData)(SIP_SUBJECTINFO *,SIP_INDIRECT_DATA *);
36 static BOOL (WINAPI * funcCryptSIPRemoveSignedDataMsg)(SIP_SUBJECTINFO *,DWORD);
37
38 static char *show_guid(const GUID *guid)
39 {
40     static char guidstring[39];
41
42     sprintf(guidstring,
43         "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
44         guid->Data1, guid->Data2, guid->Data3,
45         guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
46         guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
47
48     return guidstring;
49 }
50
51 static void test_AddRemoveProvider(void)
52 {
53     BOOL ret;
54     SIP_ADD_NEWPROVIDER newprov;
55     GUID actionid = { 0xdeadbe, 0xefde, 0xadbe, { 0xef,0xde,0xad,0xbe,0xef,0xde,0xad,0xbe }};
56     static WCHAR dummydll[]      = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
57     static WCHAR dummyfunction[] = {'d','u','m','m','y','f','u','n','c','t','i','o','n',0 };
58
59     /* NULL check */
60     SetLastError(0xdeadbeef);
61     ret = CryptSIPRemoveProvider(NULL);
62     ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
63     ok (GetLastError() == ERROR_INVALID_PARAMETER,
64         "Expected ERROR_INVALID_PARAMETER, got %d.\n", GetLastError());
65
66     /* nonexistent provider should result in a registry error */
67     SetLastError(0xdeadbeef);
68     ret = CryptSIPRemoveProvider(&actionid);
69     ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
70     ok (GetLastError() == ERROR_FILE_NOT_FOUND,
71         "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
72
73     /* Everything OK, pwszIsFunctionName and pwszIsFunctionNameFmt2 are left NULL
74      * as allowed */
75
76     memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
77     newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
78     newprov.pgSubject = &actionid;
79     newprov.pwszDLLFileName = dummydll;
80     newprov.pwszGetFuncName = dummyfunction;
81     newprov.pwszPutFuncName = dummyfunction;
82     newprov.pwszCreateFuncName = dummyfunction;
83     newprov.pwszVerifyFuncName = dummyfunction;
84     newprov.pwszRemoveFuncName = dummyfunction;
85     SetLastError(0xdeadbeef);
86     ret = CryptSIPAddProvider(&newprov);
87     ok ( ret, "CryptSIPAddProvider should have succeeded\n");
88
89     /* Dummy provider will be deleted, but the function still fails because
90      * pwszIsFunctionName and pwszIsFunctionNameFmt2 are not present in the
91      * registry.
92      */
93     SetLastError(0xdeadbeef);
94     ret = CryptSIPRemoveProvider(&actionid);
95     ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
96     ok (GetLastError() == ERROR_FILE_NOT_FOUND,
97         "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
98
99     /* Everything OK */
100     memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
101     newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
102     newprov.pgSubject = &actionid;
103     newprov.pwszDLLFileName = dummydll;
104     newprov.pwszGetFuncName = dummyfunction;
105     newprov.pwszPutFuncName = dummyfunction;
106     newprov.pwszCreateFuncName = dummyfunction;
107     newprov.pwszVerifyFuncName = dummyfunction;
108     newprov.pwszRemoveFuncName = dummyfunction;
109     newprov.pwszIsFunctionNameFmt2 = dummyfunction;
110     newprov.pwszIsFunctionName = dummyfunction;
111     SetLastError(0xdeadbeef);
112     ret = CryptSIPAddProvider(&newprov);
113     ok ( ret, "CryptSIPAddProvider should have succeeded\n");
114
115     /* Dummy provider should be deleted */
116     SetLastError(0xdeadbeef);
117     ret = CryptSIPRemoveProvider(&actionid);
118     ok ( ret, "CryptSIPRemoveProvider should have succeeded\n");
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     ok (GetLastError() == ERROR_INVALID_PARAMETER,
144         "Expected ERROR_INVALID_PARAMETER, got %d.\n", GetLastError());
145
146     /* Test with a nonexistent file (hopefully) */
147     SetLastError(0xdeadbeef);
148     /* Set subject to something other than zeros */
149     memset(&subject, 1, sizeof(GUID));
150     ret = CryptSIPRetrieveSubjectGuid(deadbeef, NULL, &subject);
151     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
152     ok (GetLastError() == ERROR_FILE_NOT_FOUND,
153         "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
154     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
155         "Expected a NULL GUID for c:\\deadbeef.dbf, not %s\n", show_guid(&subject));
156
157     /* Now with an executable that should exist
158      *
159      * Use A-functions where possible as that should be available on all platforms
160      */
161     ret = GetEnvironmentVariableA(windir, regeditPath, MAX_PATH);
162     ok (ret > 0, "expected GEVA(windir) to succeed, last error %d\n", GetLastError());
163     strcat(regeditPath, "\\");
164     strcat(regeditPath, regeditExe);
165     MultiByteToWideChar( CP_ACP, 0, regeditPath,
166                          strlen(regeditPath)+1, regeditPathW,
167                          sizeof(regeditPathW)/sizeof(regeditPathW[0]) );
168
169     SetLastError(0xdeadbeef);
170     memset(&subject, 1, sizeof(GUID));
171     ret = CryptSIPRetrieveSubjectGuid(regeditPathW, NULL, &subject);
172     ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
173     ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
174         "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
175
176     /* The same thing but now with a handle instead of a filename */
177     file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
178     SetLastError(0xdeadbeef);
179     memset(&subject, 1, sizeof(GUID));
180     ret = CryptSIPRetrieveSubjectGuid(NULL, file, &subject);
181     ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
182     ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
183         "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
184     CloseHandle(file);
185
186     /* And both */
187     file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
188     SetLastError(0xdeadbeef);
189     memset(&subject, 1, sizeof(GUID));
190     ret = CryptSIPRetrieveSubjectGuid(regeditPathW, file, &subject);
191     ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
192     ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
193         "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
194     CloseHandle(file);
195
196     /* Now with an empty file */
197     GetTempPathA(sizeof(path), path);
198     GetTempFileNameA(path, "sip", 0 , tempfile);
199     MultiByteToWideChar( CP_ACP, 0, tempfile,
200                          strlen(tempfile)+1, tempfileW,
201                          sizeof(tempfileW)/sizeof(tempfileW[0]) );
202
203     SetLastError(0xdeadbeef);
204     memset(&subject, 1, sizeof(GUID));
205     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
206     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
207     ok ( GetLastError() == ERROR_FILE_INVALID ||
208          GetLastError() == S_OK /* Win98 */,
209         "Expected ERROR_FILE_INVALID or S_OK, got 0x%08x\n", GetLastError());
210     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
211         "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
212
213     /* Use a file with a size of 3 (at least < 4) */
214     file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
215     WriteFile(file, "123", 3, &written, NULL);
216     CloseHandle(file);
217
218     SetLastError(0xdeadbeef);
219     memset(&subject, 1, sizeof(GUID));
220     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
221     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
222     ok ( GetLastError() == ERROR_INVALID_PARAMETER ||
223          GetLastError() == S_OK /* Win98 */,
224         "Expected ERROR_INVALID_PARAMETER or S_OK, got 0x%08x\n", GetLastError());
225     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
226         "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
227
228     /* And now >= 4 */
229     file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
230     WriteFile(file, "1234", 4, &written, NULL);
231     CloseHandle(file);
232
233     SetLastError(0xdeadbeef);
234     memset(&subject, 1, sizeof(GUID));
235     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
236     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
237     ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN ||
238          GetLastError() == S_OK /* Win98 */,
239         "Expected TRUST_E_SUBJECT_FORM_UNKNOWN or S_OK, got 0x%08x\n", GetLastError());
240     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
241         "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
242
243     /* Clean up */
244     DeleteFileA(tempfile);
245 }
246
247 static void test_SIPLoad(void)
248 {
249     BOOL ret;
250     GUID subject;
251     static GUID dummySubject = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
252     static GUID unknown      = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
253     static GUID unknown2     = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
254     /* The next SIP is available on Windows (not on a clean Wine install) */
255     static GUID unknown3     = { 0x000C10F1, 0x0000, 0x0000, { 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }}; /* MSISIP.DLL */
256     SIP_DISPATCH_INFO sdi;
257     HMODULE hCrypt;
258
259     /* All NULL */
260     SetLastError(0xdeadbeef);
261     ret = CryptSIPLoad(NULL, 0, NULL);
262     ok ( !ret, "Expected CryptSIPLoad to fail\n");
263     ok ( GetLastError() == ERROR_INVALID_PARAMETER,
264         "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
265
266     /* Only pSipDispatch NULL */
267     SetLastError(0xdeadbeef);
268     ret = CryptSIPLoad(&subject, 0, NULL);
269     ok ( !ret, "Expected CryptSIPLoad to fail\n");
270     ok ( GetLastError() == ERROR_INVALID_PARAMETER,
271         "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
272
273     /* No NULLs, but nonexistent pgSubject */
274     SetLastError(0xdeadbeef);
275     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
276     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
277     sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
278     ret = CryptSIPLoad(&dummySubject, 0, &sdi);
279     ok ( !ret, "Expected CryptSIPLoad to fail\n");
280     todo_wine
281         ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
282             "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08x\n", GetLastError());
283     ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
284
285     hCrypt = LoadLibraryA("crypt32.dll");
286     if (hCrypt)
287     {
288         funcCryptSIPGetSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPGetSignedDataMsg");
289         funcCryptSIPPutSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPPutSignedDataMsg");
290         funcCryptSIPCreateIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPCreateIndirectData");
291         funcCryptSIPVerifyIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPVerifyIndirectData");
292         funcCryptSIPRemoveSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPRemoveSignedDataMsg");
293     }
294     /* We're not going to use the functions, so we can free already here */
295     FreeLibrary(hCrypt);
296
297     /* All OK */
298     SetLastError(0xdeadbeef);
299     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
300     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
301     sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
302     ret = CryptSIPLoad(&unknown, 0, &sdi);
303     todo_wine
304     {
305         ok ( ret, "Expected CryptSIPLoad to succeed\n");
306         /* On native the last error will always be ERROR_PROC_NOT_FOUND as native searches for the function DllCanUnloadNow
307          * in WINTRUST.DLL (in this case). This function is not available in WINTRUST.DLL.
308          * For now there's no need to implement this is Wine as I doubt any program will rely on
309          * this last error when the call succeeded.
310          */
311         ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
312     }
313
314     /* The function addresses returned by CryptSIPLoad are actually the addresses of
315      * crypt32's own functions. A function calling these addresses will end up first
316      * calling crypt32 functions which in it's turn call the equivalent in the SIP
317      * as dictated by the given GUID.
318      */
319     if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
320         funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
321         todo_wine
322             ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
323                 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
324                 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
325                 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
326                 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
327                 "Expected function addresses to be from crypt32\n");
328     else
329         trace("Couldn't load function pointers\n");
330
331     /* All OK, but different GUID (same SIP though) */
332     SetLastError(0xdeadbeef);
333     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
334     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
335     sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
336     ret = CryptSIPLoad(&unknown2, 0, &sdi);
337     todo_wine
338     {
339         ok ( ret, "Expected CryptSIPLoad to succeed\n");
340         /* This call on it's own would have resulted in a ERROR_PROC_NOT_FOUND, but the previous
341          * call to CryptSIPLoad already loaded wintrust.dll. As this information is cached,
342          * CryptSIPLoad will not try to search for the already mentioned DllCanUnloadNow.
343          */
344     }
345     todo_wine
346         ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
347
348     /* All OK, but other SIP */
349     SetLastError(0xdeadbeef);
350     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
351     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
352     sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
353     ret = CryptSIPLoad(&unknown3, 0, &sdi);
354     if (ret)
355     {
356         /* The SIP is known so we can safely assume that the next tests can be done */
357
358         /* As msisip.dll is not checked yet by any of the previous calls, the
359          * function DllCanUnloadNow will be checked again in msisip.dll (it's not present)
360          */
361         todo_wine
362         {
363             ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
364
365             /* This is another SIP but this test proves the function addresses are the same as
366              * in the previous test.
367              */
368             if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
369                 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
370                 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
371                     sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
372                     sdi.pfCreate == funcCryptSIPCreateIndirectData &&
373                     sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
374                     sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
375                     "Expected function addresses to be from crypt32\n");
376             else
377                 trace("Couldn't load function pointers\n");
378         }
379     }
380
381     /* Reserved parameter not 0 */
382     SetLastError(0xdeadbeef);
383     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
384     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
385     sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
386     ret = CryptSIPLoad(&unknown, 1, &sdi);
387     ok ( !ret, "Expected CryptSIPLoad to fail\n");
388     ok ( GetLastError() == ERROR_INVALID_PARAMETER,
389         "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
390     ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
391 }
392
393 START_TEST(sip)
394 {
395     test_AddRemoveProvider();
396     /* It seems that the caching for loaded dlls is shared between CryptSIPRetrieveSubjectGUID
397      * and CryptSIPLoad. The tests have to be in this order to succeed. This is because in the last
398      * test for CryptSIPRetrieveSubjectGUID, several SIPs will be loaded (on Windows).
399      */
400     test_SIPLoad();
401     test_SIPRetrieveSubjectGUID();
402 }