wintrust: Implement WintrustLoadFunctionPointers.
[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 BOOL (WINAPI * pWintrustRemoveActionID)(GUID*);
34 static BOOL (WINAPI * pWintrustLoadFunctionPointers)(GUID *, CRYPT_PROVIDER_FUNCTIONS *);
35
36 static HMODULE hWintrust = 0;
37
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; \
44     }
45
46 static BOOL InitFunctionPtrs(void)
47 {
48     hWintrust = LoadLibraryA("wintrust.dll");
49
50     if(!hWintrust)
51     {
52         trace("Could not load wintrust.dll\n");
53         return FALSE;
54     }
55
56     WINTRUST_GET_PROC(WintrustAddActionID)
57     WINTRUST_GET_PROC(WintrustAddDefaultForUsage)
58     WINTRUST_GET_PROC(WintrustRemoveActionID)
59     WINTRUST_GET_PROC(WintrustLoadFunctionPointers)
60
61     return TRUE;
62 }
63
64 static void test_AddRem_ActionID(void)
65 {
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;
73
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());
81
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());
89
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());
98
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());
107
108     /* All OK and all (but 1) functions are correctly defined. The DLL and entrypoints
109      * are not present.
110      */
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());
126
127     /* All OK and all functions are correctly defined. The DLL and entrypoints
128      * are not present.
129      */
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());
145
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());
150
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());
157
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());
164 }
165
166 static void test_AddDefaultForUsage(void)
167 {
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;
176
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());
183
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());
190
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());
203
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());
213    
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());
224
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());
237
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());
250
251     /* There is no corresponding remove for WintrustAddDefaultForUsage
252      * so we delete the registry key manually.
253      */
254     if (ret)
255     {
256         res = RegDeleteKeyA(HKEY_LOCAL_MACHINE, Usages);
257         ok (res == ERROR_SUCCESS, "Key delete failed : 0x%08x\n", res);
258     }
259 }
260
261 static void test_LoadFunctionPointers(void)
262 {
263     BOOL ret;
264     CRYPT_PROVIDER_FUNCTIONS funcs;
265     GUID action = WINTRUST_ACTION_GENERIC_VERIFY_V2;
266
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");
273
274     SetLastError(0xdeadbeef);
275     ret = pWintrustLoadFunctionPointers(NULL, &funcs);
276     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
277         "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
278
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());
287 }
288
289 START_TEST(register)
290 {
291     if(!InitFunctionPtrs())
292         return;
293
294     test_AddRem_ActionID();
295     test_AddDefaultForUsage();
296     test_LoadFunctionPointers();
297
298     FreeLibrary(hWintrust);
299 }