wined3d: Inherit from the proper state table in atifs.
[wine] / dlls / wintrust / tests / register.c
1 /* Unit test suite for wintrust API functions
2  *
3  * Copyright 2006 Paul Vriens
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  *
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windows.h"
25 #include "softpub.h"
26 #include "wintrust.h"
27 #include "winreg.h"
28
29 #include "wine/test.h"
30
31 static BOOL (WINAPI * pWintrustAddActionID)(GUID*, DWORD, CRYPT_REGISTER_ACTIONID*);
32 static BOOL (WINAPI * pWintrustAddDefaultForUsage)(const CHAR*,CRYPT_PROVIDER_REGDEFUSAGE*);
33 static void (WINAPI * pWintrustGetRegPolicyFlags)(DWORD *);
34 static BOOL (WINAPI * pWintrustLoadFunctionPointers)(GUID *, CRYPT_PROVIDER_FUNCTIONS *);
35 static BOOL (WINAPI * pWintrustRemoveActionID)(GUID*);
36 static BOOL (WINAPI * pWintrustSetRegPolicyFlags)(DWORD);
37
38 static void InitFunctionPtrs(void)
39 {
40     HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
41
42 #define WINTRUST_GET_PROC(func) \
43     p ## func = (void*)GetProcAddress(hWintrust, #func); \
44     if(!p ## func) \
45       trace("GetProcAddress(%s) failed\n", #func);
46
47     WINTRUST_GET_PROC(WintrustAddActionID)
48     WINTRUST_GET_PROC(WintrustAddDefaultForUsage)
49     WINTRUST_GET_PROC(WintrustGetRegPolicyFlags)
50     WINTRUST_GET_PROC(WintrustLoadFunctionPointers)
51     WINTRUST_GET_PROC(WintrustRemoveActionID)
52     WINTRUST_GET_PROC(WintrustSetRegPolicyFlags)
53
54 #undef WINTRUST_GET_PROC
55 }
56
57 static void test_AddRem_ActionID(void)
58 {
59     static WCHAR DummyDllW[]      = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
60     static WCHAR DummyFunctionW[] = {'d','u','m','m','y','f','u','n','c','t','i','o','n',0 };
61     GUID ActionID = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
62     CRYPT_REGISTER_ACTIONID ActionIDFunctions;
63     CRYPT_TRUST_REG_ENTRY EmptyProvider = { 0, NULL, NULL };
64     CRYPT_TRUST_REG_ENTRY DummyProvider = { sizeof(CRYPT_TRUST_REG_ENTRY), DummyDllW, DummyFunctionW };
65     BOOL ret;
66
67     if (!pWintrustAddActionID || !pWintrustRemoveActionID)
68     {
69         skip("WintrustAddActionID and/or WintrustRemoveActionID are not available\n");
70         return;
71     }
72
73     /* All NULL */
74     SetLastError(0xdeadbeef);
75     ret = pWintrustAddActionID(NULL, 0, NULL);
76     ok (!ret, "Expected WintrustAddActionID to fail.\n");
77     ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
78         GetLastError() == 0xdeadbeef              /* Win98/NT4/W2K */,
79         "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
80
81     /* NULL functions */
82     SetLastError(0xdeadbeef);
83     ret = pWintrustAddActionID(&ActionID, 0, NULL);
84     ok (!ret, "Expected WintrustAddActionID to fail.\n");
85     ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
86         GetLastError() == 0xdeadbeef              /* Win98/NT4/W2K */,
87         "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
88
89     /* All OK (although no functions defined), except cbStruct is not set in ActionIDFunctions */
90     SetLastError(0xdeadbeef);
91     memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
92     ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
93     ok (!ret, "Expected WintrustAddActionID to fail.\n");
94     ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
95         GetLastError() == 0xdeadbeef              /* Win98/NT4/W2K */,
96         "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
97
98     /* All OK (although no functions defined) and cbStruct is set now */
99     SetLastError(0xdeadbeef);
100     memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
101     ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
102     ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
103     ok (ret, "Expected WintrustAddActionID to succeed.\n");
104     ok (GetLastError() == ERROR_INVALID_PARAMETER,
105         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
106
107     /* All OK and all (but 1) functions are correctly defined. The DLL and entrypoints
108      * are not present.
109      */
110     memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
111     ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
112     ActionIDFunctions.sInitProvider = DummyProvider;
113     ActionIDFunctions.sObjectProvider = DummyProvider;
114     ActionIDFunctions.sSignatureProvider = EmptyProvider;
115     ActionIDFunctions.sCertificateProvider = DummyProvider;
116     ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
117     ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
118     ActionIDFunctions.sTestPolicyProvider = DummyProvider;
119     ActionIDFunctions.sCleanupProvider = DummyProvider;
120     SetLastError(0xdeadbeef);
121     ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
122     ok (ret, "Expected WintrustAddActionID to succeed.\n");
123     ok (GetLastError() == ERROR_INVALID_PARAMETER,
124         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
125
126     /* All OK and all functions are correctly defined. The DLL and entrypoints
127      * are not present.
128      */
129     memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
130     ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
131     ActionIDFunctions.sInitProvider = DummyProvider;
132     ActionIDFunctions.sObjectProvider = DummyProvider;
133     ActionIDFunctions.sSignatureProvider = DummyProvider;
134     ActionIDFunctions.sCertificateProvider = DummyProvider;
135     ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
136     ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
137     ActionIDFunctions.sTestPolicyProvider = DummyProvider;
138     ActionIDFunctions.sCleanupProvider = DummyProvider;
139     SetLastError(0xdeadbeef);
140     ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
141     ok (ret, "Expected WintrustAddActionID to succeed.\n");
142     ok (GetLastError() == 0xdeadbeef,
143         "Expected 0xdeadbeef, got %u.\n", GetLastError());
144
145     SetLastError(0xdeadbeef);
146     ret = pWintrustRemoveActionID(&ActionID);
147     ok ( ret, "WintrustRemoveActionID failed : %d\n", GetLastError());
148     ok ( GetLastError() == 0xdeadbeef, "Last error should not have been changed: %u\n", GetLastError());
149
150     /* NULL input */
151     SetLastError(0xdeadbeef);
152     ret = pWintrustRemoveActionID(NULL);
153     ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
154     ok (GetLastError() == ERROR_INVALID_PARAMETER,
155         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
156
157     /* The passed GUID is removed by a previous call, so it's basically a test with a nonexistent Trust provider */ 
158     SetLastError(0xdeadbeef);
159     ret = pWintrustRemoveActionID(&ActionID);
160     ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
161     ok (GetLastError() == 0xdeadbeef,
162         "Expected 0xdeadbeef, got %u.\n", GetLastError());
163 }
164
165 static void test_AddDefaultForUsage(void)
166 {
167     BOOL ret;
168     LONG res;
169     static GUID ActionID        = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
170     static WCHAR DummyDllW[]    = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
171     static CHAR DummyFunction[] = "dummyfunction";
172     static const CHAR oid[]     = "1.2.3.4.5.6.7.8.9.10";
173     static const CHAR Usages[]  = "SOFTWARE\\Microsoft\\Cryptography\\Providers\\Trust\\Usages\\1.2.3.4.5.6.7.8.9.10";
174     static CRYPT_PROVIDER_REGDEFUSAGE DefUsage;
175
176     if (!pWintrustAddDefaultForUsage)
177     {
178         skip("WintrustAddDefaultForUsage is not available\n");
179         return;
180     }
181
182     /* All NULL */
183     SetLastError(0xdeadbeef);
184     ret = pWintrustAddDefaultForUsage(NULL, NULL);
185     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
186     ok (GetLastError() == ERROR_INVALID_PARAMETER,
187         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
188
189     /* NULL defusage */
190     SetLastError(0xdeadbeef);
191     ret = pWintrustAddDefaultForUsage(oid, NULL);
192     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
193     ok (GetLastError() == ERROR_INVALID_PARAMETER,
194         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
195
196     /* NULL oid and proper defusage */
197     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
198     DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
199     DefUsage.pgActionID = &ActionID;
200     DefUsage.pwszDllName = DummyDllW;
201     DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
202     DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
203     SetLastError(0xdeadbeef);
204     ret = pWintrustAddDefaultForUsage(NULL, &DefUsage);
205     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
206     ok (GetLastError() == ERROR_INVALID_PARAMETER,
207         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
208
209     /* Just the ActionID */
210     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
211     DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
212     DefUsage.pgActionID = &ActionID;
213     SetLastError(0xdeadbeef);
214     ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
215     ok ( ret, "Expected WintrustAddDefaultForUsage to succeed\n");
216     ok (GetLastError() == 0xdeadbeef,
217         "Last error should not have been changed: %u\n", GetLastError());
218    
219     /* No ActionID */
220     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
221     DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
222     DefUsage.pwszDllName = DummyDllW;
223     DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
224     DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
225     ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
226     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
227     ok (GetLastError() == ERROR_INVALID_PARAMETER,
228         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
229
230     /* cbStruct set to 0 */
231     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
232     DefUsage.cbStruct = 0;
233     DefUsage.pgActionID = &ActionID;
234     DefUsage.pwszDllName = DummyDllW;
235     DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
236     DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
237     SetLastError(0xdeadbeef);
238     ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
239     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
240     ok (GetLastError() == ERROR_INVALID_PARAMETER,
241         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
242
243     /* All OK */
244     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
245     DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
246     DefUsage.pgActionID = &ActionID;
247     DefUsage.pwszDllName = DummyDllW;
248     DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
249     DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
250     SetLastError(0xdeadbeef);
251     ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
252     ok ( ret, "Expected WintrustAddDefaultForUsage to succeed\n");
253     ok (GetLastError() == 0xdeadbeef,
254         "Last error should not have been changed: %u\n", GetLastError());
255
256     /* There is no corresponding remove for WintrustAddDefaultForUsage
257      * so we delete the registry key manually.
258      */
259     if (ret)
260     {
261         res = RegDeleteKeyA(HKEY_LOCAL_MACHINE, Usages);
262         ok (res == ERROR_SUCCESS, "Key delete failed : 0x%08x\n", res);
263     }
264 }
265
266 static void test_LoadFunctionPointers(void)
267 {
268     BOOL ret;
269     CRYPT_PROVIDER_FUNCTIONS funcs;
270     GUID action = WINTRUST_ACTION_GENERIC_VERIFY_V2;
271
272     if (!pWintrustLoadFunctionPointers)
273     {
274         skip("WintrustLoadFunctionPointers is not available\n");
275         return;
276     }
277     SetLastError(0xdeadbeef);
278     ret = pWintrustLoadFunctionPointers(NULL, NULL);
279     ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
280     SetLastError(0xdeadbeef);
281     ret = pWintrustLoadFunctionPointers(&action, NULL);
282     ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
283
284     SetLastError(0xdeadbeef);
285     ret = pWintrustLoadFunctionPointers(NULL, &funcs);
286     ok(!ret, "WintrustLoadFunctionPointers succeeded\n");
287     ok(GetLastError() == ERROR_INVALID_PARAMETER ||
288         GetLastError() == 0xdeadbeef /* W2K and XP-SP1 */,
289         "Expected ERROR_INVALID_PARAMETER or 0xdeadbeef, got %d\n", GetLastError());
290
291     SetLastError(0xdeadbeef);
292     funcs.cbStruct = 0;
293     ret = pWintrustLoadFunctionPointers(&action, &funcs);
294     ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
295     SetLastError(0xdeadbeef);
296     funcs.cbStruct = sizeof(funcs);
297     ret = pWintrustLoadFunctionPointers(&action, &funcs);
298     ok(ret, "WintrustLoadFunctionPointers failed: %d\n", GetLastError());
299     ok(funcs.pfnAlloc != NULL, "Expected a pointer\n");
300     ok(funcs.pfnFree != NULL, "Expected a pointer\n");
301 }
302
303 static void test_RegPolicyFlags(void)
304 {
305     /* Default state value 0x00023c00, which is
306      *  WTPF_IGNOREREVOCATIONONTS |
307      *  WTPF_OFFLINEOKNBU_COM |
308      *  WTPF_OFFLINEOKNBU_IND |
309      *  WTPF_OFFLINEOK_COM |
310      *  WTPF_OFFLINEOK_IND
311      */
312     static const CHAR Software_Publishing[] =
313      "Software\\Microsoft\\Windows\\CurrentVersion\\Wintrust\\"
314      "Trust Providers\\Software Publishing";
315     static const CHAR State[] = "State";
316     HKEY key;
317     LONG r;
318     DWORD flags1, flags2, flags3, size;
319     BOOL ret;
320
321     if (!pWintrustGetRegPolicyFlags || !pWintrustSetRegPolicyFlags)
322     {
323         skip("Policy flags functions not present\n");
324         return;
325     }
326
327     pWintrustGetRegPolicyFlags(&flags2);
328
329     r = RegOpenKeyExA(HKEY_CURRENT_USER, Software_Publishing, 0, KEY_ALL_ACCESS,
330      &key);
331     ok(!r, "RegOpenKeyEx failed: %d\n", r);
332
333     size = sizeof(flags1);
334     r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
335     ok(!r, "RegQueryValueEx failed: %d\n", r);
336
337     ok(flags1 == flags2, "Got %08x flags instead of %08x\n", flags1, flags2);
338
339     flags3 = flags2 | 1;
340     ret = pWintrustSetRegPolicyFlags(flags3);
341     ok(ret, "WintrustSetRegPolicyFlags failed: %d\n", GetLastError());
342     size = sizeof(flags1);
343     r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
344     ok(flags1 == flags3, "Got %08x flags instead of %08x\n", flags1, flags3);
345
346     pWintrustSetRegPolicyFlags(flags2);
347
348     RegCloseKey(key);
349 }
350
351 START_TEST(register)
352 {
353     InitFunctionPtrs();
354
355     test_AddRem_ActionID();
356     test_AddDefaultForUsage();
357     test_LoadFunctionPointers();
358     test_RegPolicyFlags();
359 }