kernel32: FindFirstChangeNotification needs a static IO_STATUS_BLOCK.
[wine] / dlls / crypt32 / tests / oid.c
1 /*
2  * Unit test suite for crypt32.dll's OID support functions.
3  *
4  * Copyright 2005 Juan Lang
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winerror.h>
25 #include <wincrypt.h>
26
27 #include "wine/test.h"
28
29 static void test_oidFunctionSet(void)
30 {
31     HCRYPTOIDFUNCSET set1, set2;
32     BOOL ret;
33     LPWSTR buf = NULL;
34     DWORD size;
35
36     /* This crashes
37     set = CryptInitOIDFunctionSet(NULL, 0);
38      */
39
40     /* The name doesn't mean much */
41     set1 = CryptInitOIDFunctionSet("funky", 0);
42     ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08lx\n", GetLastError());
43     if (set1)
44     {
45         /* These crash
46         ret = CryptGetDefaultOIDDllList(NULL, 0, NULL, NULL);
47         ret = CryptGetDefaultOIDDllList(NULL, 0, NULL, &size);
48          */
49         size = 0;
50         ret = CryptGetDefaultOIDDllList(set1, 0, NULL, &size);
51         ok(ret, "CryptGetDefaultOIDDllList failed: %08lx\n", GetLastError());
52         if (ret)
53         {
54             buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
55             if (buf)
56             {
57                 ret = CryptGetDefaultOIDDllList(set1, 0, buf, &size);
58                 ok(ret, "CryptGetDefaultOIDDllList failed: %08lx\n",
59                  GetLastError());
60                 ok(!*buf, "Expected empty DLL list\n");
61                 HeapFree(GetProcessHeap(), 0, buf);
62             }
63         }
64     }
65
66     /* MSDN says flags must be 0, but it's not checked */
67     set1 = CryptInitOIDFunctionSet("", 1);
68     ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08lx\n", GetLastError());
69     set2 = CryptInitOIDFunctionSet("", 0);
70     ok(set2 != 0, "CryptInitOIDFunctionSet failed: %08lx\n", GetLastError());
71     /* There isn't a free function, so there must be only one set per name to
72      * limit leaks.  (I guess the sets are freed when crypt32 is unloaded.)
73      */
74     ok(set1 == set2, "Expected identical sets\n");
75     if (set1)
76     {
77         /* The empty name function set used here seems to correspond to
78          * DEFAULT.
79          */
80     }
81
82     /* There's no installed function for a built-in encoding. */
83     set1 = CryptInitOIDFunctionSet("CryptDllEncodeObject", 0);
84     ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08lx\n", GetLastError());
85     if (set1)
86     {
87         void *funcAddr;
88         HCRYPTOIDFUNCADDR hFuncAddr;
89
90         ret = CryptGetOIDFunctionAddress(set1, X509_ASN_ENCODING, X509_CERT, 0,
91          &funcAddr, &hFuncAddr);
92         ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
93          "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
94     }
95 }
96
97 typedef int (*funcY)(int);
98
99 static int funky(int x)
100 {
101     return x;
102 }
103
104 static void test_installOIDFunctionAddress(void)
105 {
106     BOOL ret;
107     CRYPT_OID_FUNC_ENTRY entry = { CRYPT_DEFAULT_OID, funky };
108     HCRYPTOIDFUNCSET set;
109
110     /* This crashes
111     ret = CryptInstallOIDFunctionAddress(NULL, 0, NULL, 0, NULL, 0);
112      */
113
114     /* Installing zero functions should work */
115     SetLastError(0xdeadbeef);
116     ret = CryptInstallOIDFunctionAddress(NULL, 0, "CryptDllEncodeObject", 0,
117      NULL, 0);
118     ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08lx\n",
119      GetLastError());
120
121     /* The function name doesn't much matter */
122     SetLastError(0xdeadbeef);
123     ret = CryptInstallOIDFunctionAddress(NULL, 0, "OhSoFunky", 0, NULL, 0);
124     ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08lx\n",
125      GetLastError());
126     SetLastError(0xdeadbeef);
127     entry.pszOID = X509_CERT;
128     ret = CryptInstallOIDFunctionAddress(NULL, 0, "OhSoFunky", 1, &entry, 0);
129     ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08lx\n",
130      GetLastError());
131     set = CryptInitOIDFunctionSet("OhSoFunky", 0);
132     ok(set != 0, "CryptInitOIDFunctionSet failed: %08lx\n", GetLastError());
133     if (set)
134     {
135         funcY funcAddr = NULL;
136         HCRYPTOIDFUNCADDR hFuncAddr = NULL;
137
138         /* This crashes
139         ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, 0, 0, NULL,
140          NULL);
141          */
142         ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, 0, 0,
143          (void **)&funcAddr, &hFuncAddr);
144         ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
145          "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
146         ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, X509_CERT, 0,
147          (void **)&funcAddr, &hFuncAddr);
148         ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
149          "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
150         ret = CryptGetOIDFunctionAddress(set, 0, X509_CERT, 0,
151          (void **)&funcAddr, &hFuncAddr);
152         ok(ret, "CryptGetOIDFunctionAddress failed: %ld\n", GetLastError());
153         if (funcAddr)
154         {
155             int y = funcAddr(0xabadc0da);
156
157             ok(y == 0xabadc0da, "Unexpected return (%d) from function\n", y);
158             CryptFreeOIDFunctionAddress(hFuncAddr, 0);
159         }
160     }
161 }
162
163 static void test_registerOIDFunction(void)
164 {
165     static const WCHAR bogusDll[] = { 'b','o','g','u','s','.','d','l','l',0 };
166     BOOL ret;
167
168     /* oddly, this succeeds under WinXP; the function name key is merely
169      * omitted.  This may be a side effect of the registry code, I don't know.
170      * I don't check it because I doubt anyone would depend on it.
171     ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, NULL,
172      "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
173      */
174     /* On windows XP, GetLastError is incorrectly being set with an HRESULT,
175      * HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER)
176      */
177     ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo", NULL, bogusDll,
178      NULL);
179     ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
180      "Expected ERROR_INVALID_PARAMETER: %ld\n", GetLastError());
181     /* This has no effect, but "succeeds" on XP */
182     ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo",
183      "1.2.3.4.5.6.7.8.9.10", NULL, NULL);
184     ok(ret, "Expected pseudo-success, got %ld\n", GetLastError());
185     ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
186      "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
187     ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
188     ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
189      "1.2.3.4.5.6.7.8.9.10");
190     ok(ret, "CryptUnregisterOIDFunction failed: %ld\n", GetLastError());
191     ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "bogus",
192      "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
193     ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
194     ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "bogus",
195      "1.2.3.4.5.6.7.8.9.10");
196     ok(ret, "CryptUnregisterOIDFunction failed: %ld\n", GetLastError());
197     /* This has no effect */
198     ret = CryptRegisterOIDFunction(PKCS_7_ASN_ENCODING, "CryptDllEncodeObject",
199      "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
200     ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
201     /* Check with bogus encoding type: */
202     ret = CryptRegisterOIDFunction(0, "CryptDllEncodeObject",
203      "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
204     ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
205     /* This is written with value 3 verbatim.  Thus, the encoding type isn't
206      * (for now) treated as a mask.
207      */
208     ret = CryptRegisterOIDFunction(3, "CryptDllEncodeObject",
209      "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
210     ok(ret, "CryptRegisterOIDFunction failed: %ld\n", GetLastError());
211     ret = CryptUnregisterOIDFunction(3, "CryptDllEncodeObject",
212      "1.2.3.4.5.6.7.8.9.10");
213     ok(ret, "CryptUnregisterOIDFunction failed: %ld\n", GetLastError());
214 }
215
216 START_TEST(oid)
217 {
218     test_oidFunctionSet();
219     test_installOIDFunctionAddress();
220     test_registerOIDFunction();
221 }