wintrust: Return error directly from SOFTPUB_GetSIP.
[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         win_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         GetLastError() == ERROR_ACCESS_DENIED,
125         "Expected ERROR_INVALID_PARAMETER or ERROR_ACCESS_DENIED, got %u.\n",
126         GetLastError());
127
128     /* All OK and all functions are correctly defined. The DLL and entrypoints
129      * are not present.
130      */
131     memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
132     ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
133     ActionIDFunctions.sInitProvider = DummyProvider;
134     ActionIDFunctions.sObjectProvider = DummyProvider;
135     ActionIDFunctions.sSignatureProvider = DummyProvider;
136     ActionIDFunctions.sCertificateProvider = DummyProvider;
137     ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
138     ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
139     ActionIDFunctions.sTestPolicyProvider = DummyProvider;
140     ActionIDFunctions.sCleanupProvider = DummyProvider;
141     SetLastError(0xdeadbeef);
142     ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
143     ok (ret, "Expected WintrustAddActionID to succeed.\n");
144     ok (GetLastError() == 0xdeadbeef || GetLastError() == ERROR_ACCESS_DENIED,
145         "Expected 0xdeadbeef or ERROR_ACCESS_DENIED, got %u.\n",
146         GetLastError());
147
148     SetLastError(0xdeadbeef);
149     ret = pWintrustRemoveActionID(&ActionID);
150     ok ( ret, "WintrustRemoveActionID failed : %d\n", GetLastError());
151     ok ( GetLastError() == 0xdeadbeef, "Last error should not have been changed: %u\n", GetLastError());
152
153     /* NULL input */
154     SetLastError(0xdeadbeef);
155     ret = pWintrustRemoveActionID(NULL);
156     ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
157     ok (GetLastError() == ERROR_INVALID_PARAMETER,
158         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
159
160     /* The passed GUID is removed by a previous call, so it's basically a test with a nonexistent Trust provider */ 
161     SetLastError(0xdeadbeef);
162     ret = pWintrustRemoveActionID(&ActionID);
163     ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
164     ok (GetLastError() == 0xdeadbeef,
165         "Expected 0xdeadbeef, got %u.\n", GetLastError());
166 }
167
168 static void test_AddDefaultForUsage(void)
169 {
170     BOOL ret;
171     static GUID ActionID        = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
172     static WCHAR DummyDllW[]    = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
173     static CHAR DummyFunction[] = "dummyfunction";
174     static const CHAR oid[]     = "1.2.3.4.5.6.7.8.9.10";
175     static CRYPT_PROVIDER_REGDEFUSAGE DefUsage;
176
177     if (!pWintrustAddDefaultForUsage)
178     {
179         win_skip("WintrustAddDefaultForUsage is not available\n");
180         return;
181     }
182
183     /* All NULL */
184     SetLastError(0xdeadbeef);
185     ret = pWintrustAddDefaultForUsage(NULL, NULL);
186     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
187     ok (GetLastError() == ERROR_INVALID_PARAMETER,
188         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
189
190     /* NULL defusage */
191     SetLastError(0xdeadbeef);
192     ret = pWintrustAddDefaultForUsage(oid, NULL);
193     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
194     ok (GetLastError() == ERROR_INVALID_PARAMETER,
195         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
196
197     /* NULL oid and proper defusage */
198     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
199     DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
200     DefUsage.pgActionID = &ActionID;
201     DefUsage.pwszDllName = DummyDllW;
202     DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
203     DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
204     SetLastError(0xdeadbeef);
205     ret = pWintrustAddDefaultForUsage(NULL, &DefUsage);
206     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
207     ok (GetLastError() == ERROR_INVALID_PARAMETER,
208         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
209
210     /* cbStruct set to 0 */
211     memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
212     DefUsage.cbStruct = 0;
213     DefUsage.pgActionID = &ActionID;
214     DefUsage.pwszDllName = DummyDllW;
215     DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
216     DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
217     SetLastError(0xdeadbeef);
218     ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
219     ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
220     ok (GetLastError() == ERROR_INVALID_PARAMETER,
221         "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
222 }
223
224 static void test_LoadFunctionPointers(void)
225 {
226     BOOL ret;
227     CRYPT_PROVIDER_FUNCTIONS funcs;
228     GUID action = WINTRUST_ACTION_GENERIC_VERIFY_V2;
229
230     if (!pWintrustLoadFunctionPointers)
231     {
232         win_skip("WintrustLoadFunctionPointers is not available\n");
233         return;
234     }
235     SetLastError(0xdeadbeef);
236     ret = pWintrustLoadFunctionPointers(NULL, NULL);
237     ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
238     SetLastError(0xdeadbeef);
239     ret = pWintrustLoadFunctionPointers(&action, NULL);
240     ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
241
242     SetLastError(0xdeadbeef);
243     ret = pWintrustLoadFunctionPointers(NULL, &funcs);
244     ok(!ret, "WintrustLoadFunctionPointers succeeded\n");
245     ok(GetLastError() == ERROR_INVALID_PARAMETER ||
246         GetLastError() == 0xdeadbeef /* W2K and XP-SP1 */,
247         "Expected ERROR_INVALID_PARAMETER or 0xdeadbeef, got %d\n", GetLastError());
248
249     SetLastError(0xdeadbeef);
250     funcs.cbStruct = 0;
251     ret = pWintrustLoadFunctionPointers(&action, &funcs);
252     ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
253     SetLastError(0xdeadbeef);
254     funcs.cbStruct = sizeof(funcs);
255     ret = pWintrustLoadFunctionPointers(&action, &funcs);
256     ok(ret, "WintrustLoadFunctionPointers failed: %d\n", GetLastError());
257     ok(funcs.pfnAlloc != NULL, "Expected a pointer\n");
258     ok(funcs.pfnFree != NULL, "Expected a pointer\n");
259 }
260
261 static void test_RegPolicyFlags(void)
262 {
263     /* Default state value 0x00023c00, which is
264      *  WTPF_IGNOREREVOCATIONONTS |
265      *  WTPF_OFFLINEOKNBU_COM |
266      *  WTPF_OFFLINEOKNBU_IND |
267      *  WTPF_OFFLINEOK_COM |
268      *  WTPF_OFFLINEOK_IND
269      */
270     static const CHAR Software_Publishing[] =
271      "Software\\Microsoft\\Windows\\CurrentVersion\\Wintrust\\"
272      "Trust Providers\\Software Publishing";
273     static const CHAR State[] = "State";
274     HKEY key;
275     LONG r;
276     DWORD flags1, flags2, flags3, size;
277     BOOL ret;
278
279     if (!pWintrustGetRegPolicyFlags || !pWintrustSetRegPolicyFlags)
280     {
281         win_skip("Policy flags functions not present\n");
282         return;
283     }
284
285     pWintrustGetRegPolicyFlags(&flags2);
286
287     r = RegOpenKeyExA(HKEY_CURRENT_USER, Software_Publishing, 0, KEY_ALL_ACCESS,
288      &key);
289     ok(!r, "RegOpenKeyEx failed: %d\n", r);
290
291     size = sizeof(flags1);
292     r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
293     ok(!r || r == ERROR_FILE_NOT_FOUND, "RegQueryValueEx failed: %d\n", r);
294     if (!r)
295         ok(flags1 == flags2, "Got %08x flags instead of %08x\n", flags1, flags2);
296
297     flags3 = flags2 | 1;
298     ret = pWintrustSetRegPolicyFlags(flags3);
299     ok(ret, "WintrustSetRegPolicyFlags failed: %d\n", GetLastError());
300     size = sizeof(flags1);
301     r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
302     ok(flags1 == flags3, "Got %08x flags instead of %08x\n", flags1, flags3);
303
304     pWintrustSetRegPolicyFlags(flags2);
305
306     RegCloseKey(key);
307 }
308
309 START_TEST(register)
310 {
311     InitFunctionPtrs();
312
313     test_AddRem_ActionID();
314     test_AddDefaultForUsage();
315     test_LoadFunctionPointers();
316     test_RegPolicyFlags();
317 }