If the pszProvName param of CryptEnumProviders is too small to hold
[wine/wine-gecko.git] / dlls / advapi32 / tests / crypt.c
blobc4b18d4c17d07bc5dc61421f965a9028d86938c8
1 /*
2 * Unit tests for crypt functions
4 * Copyright (c) 2004 Michael Jung
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.
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.
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
21 #include <stdio.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincrypt.h"
27 #include "winerror.h"
28 #include "winreg.h"
30 static const char szRsaBaseProv[] = MS_DEF_PROV_A;
31 static const char szNonExistentProv[] = "Wine Non Existent Cryptographic Provider v11.2";
32 static const char szKeySet[] = "wine_test_keyset";
33 static const char szBadKeySet[] = "wine_test_bad_keyset";
34 #define NON_DEF_PROV_TYPE 999
36 static void init_environment(void)
38 HCRYPTPROV hProv;
40 /* Ensure that container "wine_test_keyset" does exist */
41 if (!CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
43 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
45 CryptReleaseContext(hProv, 0);
47 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
48 if (!CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
50 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
52 CryptReleaseContext(hProv, 0);
54 /* Ensure that container "wine_test_bad_keyset" does not exist. */
55 if (CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
57 CryptReleaseContext(hProv, 0);
58 CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
62 static void clean_up_environment(void)
64 HCRYPTPROV hProv;
66 /* Remove container "wine_test_keyset" */
67 if (CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
69 CryptReleaseContext(hProv, 0);
70 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
73 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
74 if (CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
76 CryptReleaseContext(hProv, 0);
77 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
81 static void test_acquire_context(void)
83 BOOL result;
84 HCRYPTPROV hProv;
86 /* Provoke all kinds of error conditions (which are easy to provoke).
87 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
88 * but since this is likely to change between CSP versions, we don't check
89 * this. Please don't change the order of tests. */
90 result = CryptAcquireContext(&hProv, NULL, NULL, 0, 0);
91 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%08x\n", (unsigned int)GetLastError());
93 result = CryptAcquireContext(&hProv, NULL, NULL, 1000, 0);
94 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%08x\n", (unsigned int)GetLastError());
96 result = CryptAcquireContext(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
97 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%08x\n", (unsigned int)GetLastError());
99 result = CryptAcquireContext(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
100 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%08x\n", (unsigned int)GetLastError());
102 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
103 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%08x\n", (unsigned int)GetLastError());
105 result = CryptAcquireContext(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
106 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%08x\n", (unsigned int)GetLastError());
108 /* Last not least, try to really acquire a context. */
109 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
110 ok(result, "%08x\n", (unsigned int)GetLastError());
112 if (GetLastError() == ERROR_SUCCESS)
113 CryptReleaseContext(hProv, 0);
115 /* Try again, witch an empty ("\0") szProvider parameter */
116 result = CryptAcquireContext(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
117 ok(result, "%08x\n", (unsigned int)GetLastError());
119 if (GetLastError() == ERROR_SUCCESS)
120 CryptReleaseContext(hProv, 0);
123 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
124 DWORD *pcbProvName, DWORD *pdwProvCount)
126 HKEY hKey;
127 HKEY subkey;
128 DWORD size = sizeof(DWORD);
130 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
131 return FALSE;
133 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
134 NULL, NULL, NULL, NULL, NULL, NULL);
136 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
137 return FALSE;
139 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
140 (*pcbProvName)++;
142 RegOpenKey(hKey, *pszProvName, &subkey);
143 RegQueryValueEx(subkey, "Type", NULL, NULL, (BYTE*)pdwProvType, &size);
145 RegCloseKey(subkey);
146 RegCloseKey(hKey);
148 return TRUE;
151 static void test_enum_providers(void)
153 /* expected results */
154 CHAR *pszProvName = NULL;
155 DWORD cbName;
156 DWORD dwType;
157 DWORD provCount;
158 DWORD dwIndex = 0;
160 /* actual results */
161 CHAR *provider = NULL;
162 DWORD providerLen;
163 DWORD type;
164 DWORD count;
165 BOOL result;
166 DWORD notNull = 5;
167 DWORD notZeroFlags = 5;
169 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
170 return;
172 /* check pdwReserved flag for NULL */
173 result = CryptEnumProviders(dwIndex, &notNull, 0, &type, NULL, &providerLen);
174 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%08x\n", (unsigned int)GetLastError());
176 /* check dwFlags == 0 */
177 result = CryptEnumProviders(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
178 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%08x\n", (unsigned int)GetLastError());
180 /* alloc provider to half the size required
181 * cbName holds the size required */
182 providerLen = cbName / 2;
183 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
184 return;
186 result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
187 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %08x, got %08x\n",
188 ERROR_MORE_DATA, (unsigned int)GetLastError());
190 LocalFree(provider);
192 /* loop through the providers to get the number of providers
193 * after loop ends, count should be provCount + 1 so subtract 1
194 * to get actual number of providers */
195 count = 0;
196 while(CryptEnumProviders(count++, NULL, 0, &dwType, NULL, &providerLen))
198 count--;
199 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
201 /* loop past the actual number of providers to get the error
202 * ERROR_NO_MORE_ITEMS */
203 for (count = 0; count < provCount + 1; count++)
204 result = CryptEnumProviders(count, NULL, 0, &dwType, NULL, &providerLen);
205 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %08x, got %08x\n",
206 ERROR_NO_MORE_ITEMS, (unsigned int)GetLastError());
208 /* check expected versus actual values returned */
209 result = CryptEnumProviders(dwIndex, NULL, 0, &type, NULL, &providerLen);
210 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
211 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
212 return;
214 result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
215 ok(result && type==dwType, "expected %i, got %i\n",
216 (unsigned int)dwType, (unsigned int)type);
217 ok(result && !strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
218 ok(result && cbName==providerLen, "expected %i, got %i\n",
219 (unsigned int)cbName, (unsigned int)providerLen);
222 START_TEST(crypt)
224 init_environment();
225 test_acquire_context();
226 clean_up_environment();
228 test_enum_providers();