wintrust/tests: Use A instead of W-calls.
[wine.git] / dlls / wintrust / tests / register.c
blobdb5726c3af9b4b70d33cacbab43958e722e99f97
1 /* Unit test suite for wintrust API functions
3 * Copyright 2006 Paul Vriens
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windows.h"
25 #include "softpub.h"
26 #include "wintrust.h"
27 #include "winreg.h"
29 #include "wine/test.h"
31 static BOOL (WINAPI * pWintrustAddActionID)(GUID*, DWORD, CRYPT_REGISTER_ACTIONID*);
32 static BOOL (WINAPI * pWintrustAddDefaultForUsage)(const CHAR*,CRYPT_PROVIDER_REGDEFUSAGE*);
33 static BOOL (WINAPI * pWintrustRemoveActionID)(GUID*);
34 static BOOL (WINAPI * pWintrustLoadFunctionPointers)(GUID *, CRYPT_PROVIDER_FUNCTIONS *);
36 static HMODULE hWintrust = 0;
38 #define WINTRUST_GET_PROC(func) \
39 p ## func = (void*)GetProcAddress(hWintrust, #func); \
40 if(!p ## func) { \
41 trace("GetProcAddress(%s) failed\n", #func); \
42 FreeLibrary(hWintrust); \
43 return FALSE; \
46 static BOOL InitFunctionPtrs(void)
48 hWintrust = LoadLibraryA("wintrust.dll");
50 if(!hWintrust)
52 trace("Could not load wintrust.dll\n");
53 return FALSE;
56 WINTRUST_GET_PROC(WintrustAddActionID)
57 WINTRUST_GET_PROC(WintrustAddDefaultForUsage)
58 WINTRUST_GET_PROC(WintrustRemoveActionID)
59 WINTRUST_GET_PROC(WintrustLoadFunctionPointers)
61 return TRUE;
64 static void test_AddRem_ActionID(void)
66 static WCHAR DummyDllW[] = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
67 static WCHAR DummyFunctionW[] = {'d','u','m','m','y','f','u','n','c','t','i','o','n',0 };
68 GUID ActionID = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
69 CRYPT_REGISTER_ACTIONID ActionIDFunctions;
70 CRYPT_TRUST_REG_ENTRY EmptyProvider = { 0, NULL, NULL };
71 CRYPT_TRUST_REG_ENTRY DummyProvider = { sizeof(CRYPT_TRUST_REG_ENTRY), DummyDllW, DummyFunctionW };
72 BOOL ret;
74 /* All NULL */
75 SetLastError(0xdeadbeef);
76 ret = pWintrustAddActionID(NULL, 0, NULL);
77 ok (!ret, "Expected WintrustAddActionID to fail.\n");
78 ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
79 GetLastError() == 0xdeadbeef /* Win98/NT4/W2K */,
80 "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
82 /* NULL functions */
83 SetLastError(0xdeadbeef);
84 ret = pWintrustAddActionID(&ActionID, 0, NULL);
85 ok (!ret, "Expected WintrustAddActionID to fail.\n");
86 ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
87 GetLastError() == 0xdeadbeef /* Win98/NT4/W2K */,
88 "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
90 /* All OK (although no functions defined), except cbStruct is not set in ActionIDFunctions */
91 SetLastError(0xdeadbeef);
92 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
93 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
94 ok (!ret, "Expected WintrustAddActionID to fail.\n");
95 ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
96 GetLastError() == 0xdeadbeef /* Win98/NT4/W2K */,
97 "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
99 /* All OK (although no functions defined) and cbStruct is set now */
100 SetLastError(0xdeadbeef);
101 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
102 ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
103 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
104 ok (ret, "Expected WintrustAddActionID to succeed.\n");
105 ok (GetLastError() == ERROR_INVALID_PARAMETER,
106 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
108 /* All OK and all (but 1) functions are correctly defined. The DLL and entrypoints
109 * are not present.
111 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
112 ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
113 ActionIDFunctions.sInitProvider = DummyProvider;
114 ActionIDFunctions.sObjectProvider = DummyProvider;
115 ActionIDFunctions.sSignatureProvider = EmptyProvider;
116 ActionIDFunctions.sCertificateProvider = DummyProvider;
117 ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
118 ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
119 ActionIDFunctions.sTestPolicyProvider = DummyProvider;
120 ActionIDFunctions.sCleanupProvider = DummyProvider;
121 SetLastError(0xdeadbeef);
122 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
123 ok (ret, "Expected WintrustAddActionID to succeed.\n");
124 ok (GetLastError() == ERROR_INVALID_PARAMETER,
125 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
127 /* All OK and all functions are correctly defined. The DLL and entrypoints
128 * are not present.
130 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
131 ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
132 ActionIDFunctions.sInitProvider = DummyProvider;
133 ActionIDFunctions.sObjectProvider = DummyProvider;
134 ActionIDFunctions.sSignatureProvider = DummyProvider;
135 ActionIDFunctions.sCertificateProvider = DummyProvider;
136 ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
137 ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
138 ActionIDFunctions.sTestPolicyProvider = DummyProvider;
139 ActionIDFunctions.sCleanupProvider = DummyProvider;
140 SetLastError(0xdeadbeef);
141 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
142 ok (ret, "Expected WintrustAddActionID to succeed.\n");
143 ok (GetLastError() == 0xdeadbeef,
144 "Expected 0xdeadbeef, got %u.\n", GetLastError());
146 SetLastError(0xdeadbeef);
147 ret = pWintrustRemoveActionID(&ActionID);
148 ok ( ret, "WintrustRemoveActionID failed : %d\n", GetLastError());
149 ok ( GetLastError() == 0xdeadbeef, "Last error should not have been changed: %u\n", GetLastError());
151 /* NULL input */
152 SetLastError(0xdeadbeef);
153 ret = pWintrustRemoveActionID(NULL);
154 ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
155 ok (GetLastError() == ERROR_INVALID_PARAMETER,
156 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
158 /* The passed GUID is removed by a previous call, so it's basically a test with a nonexistent Trust provider */
159 SetLastError(0xdeadbeef);
160 ret = pWintrustRemoveActionID(&ActionID);
161 ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
162 ok (GetLastError() == 0xdeadbeef,
163 "Expected 0xdeadbeef, got %u.\n", GetLastError());
166 static void test_AddDefaultForUsage(void)
168 BOOL ret;
169 LONG res;
170 static GUID ActionID = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
171 static WCHAR DummyDllW[] = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
172 static CHAR DummyFunction[] = "dummyfunction";
173 static const CHAR oid[] = "1.2.3.4.5.6.7.8.9.10";
174 static const CHAR Usages[] = "SOFTWARE\\Microsoft\\Cryptography\\Providers\\Trust\\Usages\\1.2.3.4.5.6.7.8.9.10";
175 static CRYPT_PROVIDER_REGDEFUSAGE DefUsage;
177 /* All NULL */
178 SetLastError(0xdeadbeef);
179 ret = pWintrustAddDefaultForUsage(NULL, NULL);
180 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
181 ok (GetLastError() == ERROR_INVALID_PARAMETER,
182 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
184 /* NULL defusage */
185 SetLastError(0xdeadbeef);
186 ret = pWintrustAddDefaultForUsage(oid, NULL);
187 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
188 ok (GetLastError() == ERROR_INVALID_PARAMETER,
189 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
191 /* NULL oid and proper defusage */
192 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
193 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
194 DefUsage.pgActionID = &ActionID;
195 DefUsage.pwszDllName = DummyDllW;
196 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
197 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
198 SetLastError(0xdeadbeef);
199 ret = pWintrustAddDefaultForUsage(NULL, &DefUsage);
200 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
201 ok (GetLastError() == ERROR_INVALID_PARAMETER,
202 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
204 /* Just the ActionID */
205 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
206 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
207 DefUsage.pgActionID = &ActionID;
208 SetLastError(0xdeadbeef);
209 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
210 ok ( ret, "Expected WintrustAddDefaultForUsage to succeed\n");
211 ok (GetLastError() == 0xdeadbeef,
212 "Last error should not have been changed: %u\n", GetLastError());
214 /* No ActionID */
215 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
216 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
217 DefUsage.pwszDllName = DummyDllW;
218 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
219 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
220 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
221 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
222 ok (GetLastError() == ERROR_INVALID_PARAMETER,
223 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
225 /* cbStruct set to 0 */
226 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
227 DefUsage.cbStruct = 0;
228 DefUsage.pgActionID = &ActionID;
229 DefUsage.pwszDllName = DummyDllW;
230 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
231 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
232 SetLastError(0xdeadbeef);
233 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
234 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
235 ok (GetLastError() == ERROR_INVALID_PARAMETER,
236 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
238 /* All OK */
239 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
240 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
241 DefUsage.pgActionID = &ActionID;
242 DefUsage.pwszDllName = DummyDllW;
243 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
244 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
245 SetLastError(0xdeadbeef);
246 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
247 ok ( ret, "Expected WintrustAddDefaultForUsage to succeed\n");
248 ok (GetLastError() == 0xdeadbeef,
249 "Last error should not have been changed: %u\n", GetLastError());
251 /* There is no corresponding remove for WintrustAddDefaultForUsage
252 * so we delete the registry key manually.
254 if (ret)
256 res = RegDeleteKeyA(HKEY_LOCAL_MACHINE, Usages);
257 ok (res == ERROR_SUCCESS, "Key delete failed : 0x%08x\n", res);
261 static void test_LoadFunctionPointers(void)
263 BOOL ret;
264 CRYPT_PROVIDER_FUNCTIONS funcs;
265 GUID action = WINTRUST_ACTION_GENERIC_VERIFY_V2;
267 SetLastError(0xdeadbeef);
268 ret = pWintrustLoadFunctionPointers(NULL, NULL);
269 ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
270 SetLastError(0xdeadbeef);
271 ret = pWintrustLoadFunctionPointers(&action, NULL);
272 ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
274 SetLastError(0xdeadbeef);
275 ret = pWintrustLoadFunctionPointers(NULL, &funcs);
276 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
277 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
279 SetLastError(0xdeadbeef);
280 funcs.cbStruct = 0;
281 ret = pWintrustLoadFunctionPointers(&action, &funcs);
282 ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
283 SetLastError(0xdeadbeef);
284 funcs.cbStruct = sizeof(funcs);
285 ret = pWintrustLoadFunctionPointers(&action, &funcs);
286 ok(ret, "WintrustLoadFunctionPointers failed: %d\n", GetLastError());
289 typedef void (WINAPI *WintrustGetRegPolicyFlagsFunc)(DWORD *);
290 typedef BOOL (WINAPI *WintrustSetRegPolicyFlagsFunc)(DWORD);
292 static void test_RegPolicyFlags(void)
294 /* Default state value 0x00023c00, which is
295 * WTPF_IGNOREREVOCATIONONTS |
296 * WTPF_OFFLINEOKNBU_COM |
297 * WTPF_OFFLINEOKNBU_IND |
298 * WTPF_OFFLINEOK_COM |
299 * WTPF_OFFLINEOK_IND
301 static const CHAR Software_Publishing[] =
302 "Software\\Microsoft\\Windows\\CurrentVersion\\Wintrust\\"
303 "Trust Providers\\Software Publishing";
304 static const CHAR State[] = "State";
305 WintrustGetRegPolicyFlagsFunc pGetFlags;
306 WintrustSetRegPolicyFlagsFunc pSetFlags;
307 HKEY key;
308 LONG r;
309 DWORD flags1, flags2, flags3, size;
310 BOOL ret;
312 pGetFlags = (WintrustGetRegPolicyFlagsFunc)GetProcAddress(hWintrust,
313 "WintrustGetRegPolicyFlags");
314 pSetFlags = (WintrustSetRegPolicyFlagsFunc)GetProcAddress(hWintrust,
315 "WintrustSetRegPolicyFlags");
316 if (!pGetFlags || !pSetFlags)
317 skip("Policy flags functions not present\n");
319 r = RegOpenKeyExA(HKEY_CURRENT_USER, Software_Publishing, 0, KEY_ALL_ACCESS,
320 &key);
321 ok(!r, "RegOpenKeyEx failed: %d\n", r);
323 size = sizeof(flags1);
324 r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
325 ok(!r, "RegQueryValueEx failed: %d\n", r);
327 pGetFlags(&flags2);
328 ok(flags1 == flags2, "Didn't get expected flags\n");
330 flags3 = flags2 | 1;
331 ret = pSetFlags(flags3);
332 ok(ret, "pSetFlags failed: %d\n", GetLastError());
333 size = sizeof(flags1);
334 r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
335 ok(flags1 == flags3, "Didn't get expected flags\n");
337 pSetFlags(flags2);
339 RegCloseKey(key);
342 START_TEST(register)
344 if(!InitFunctionPtrs())
345 return;
347 test_AddRem_ActionID();
348 test_AddDefaultForUsage();
349 test_LoadFunctionPointers();
350 test_RegPolicyFlags();
352 FreeLibrary(hWintrust);