Remove unnecessary casts in crypt tests.
[wine] / dlls / advapi32 / tests / crypt.c
1 /*
2  * Unit tests for crypt functions
3  *
4  * Copyright (c) 2004 Michael Jung
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
21 #include <stdio.h>
22
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"
29
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
35
36 static void init_environment(void)
37 {
38         HCRYPTPROV hProv;
39         
40         /* Ensure that container "wine_test_keyset" does exist */
41         if (!CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
42         {
43                 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
44         }
45         CryptReleaseContext(hProv, 0);
46
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))
49         {
50                 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
51         }
52         CryptReleaseContext(hProv, 0);
53
54         /* Ensure that container "wine_test_bad_keyset" does not exist. */
55         if (CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
56         {
57                 CryptReleaseContext(hProv, 0);
58                 CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
59         }
60 }
61
62 static void clean_up_environment(void)
63 {
64         HCRYPTPROV hProv;
65
66         /* Remove container "wine_test_keyset" */
67         if (CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
68         {
69                 CryptReleaseContext(hProv, 0);
70                 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
71         }
72
73         /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
74         if (CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
75         {
76                 CryptReleaseContext(hProv, 0);
77                 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
78         }
79 }
80
81 static void test_acquire_context(void)
82 {
83         BOOL result;
84         HCRYPTPROV hProv;
85
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, "%ld\n", GetLastError());
92         
93         result = CryptAcquireContext(&hProv, NULL, NULL, 1000, 0);
94         ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
95
96         result = CryptAcquireContext(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
97         ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%ld\n", GetLastError());
98         
99         result = CryptAcquireContext(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
100         ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%ld\n", GetLastError());
101
102         result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
103         ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%ld\n", GetLastError());
104         
105         result = CryptAcquireContext(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
106         ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
107         
108         /* Last not least, try to really acquire a context. */
109         result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
110         ok(result, "%ld\n", GetLastError());
111
112         if (GetLastError() == ERROR_SUCCESS) 
113                 CryptReleaseContext(hProv, 0);
114
115         /* Try again, witch an empty ("\0") szProvider parameter */
116         result = CryptAcquireContext(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
117         ok(result, "%ld\n", GetLastError());
118
119         if (GetLastError() == ERROR_SUCCESS)
120                 CryptReleaseContext(hProv, 0);
121 }
122
123 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName, 
124                             DWORD *pcbProvName, DWORD *pdwProvCount)
125 {
126         HKEY hKey;
127         HKEY subkey;
128         DWORD size = sizeof(DWORD);
129         
130         if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
131                 return FALSE;
132         
133         RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName, 
134                                  NULL, NULL, NULL, NULL, NULL, NULL);
135         (*pcbProvName)++;
136         
137         if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
138                 return FALSE;
139         
140         RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
141         (*pcbProvName)++;
142
143         RegOpenKey(hKey, *pszProvName, &subkey);
144         RegQueryValueEx(subkey, "Type", NULL, NULL, (BYTE*)pdwProvType, &size);
145         
146         RegCloseKey(subkey);
147         RegCloseKey(hKey);
148         
149         return TRUE;
150 }
151
152 static void test_enum_providers(void)
153 {
154         /* expected results */
155         CHAR *pszProvName = NULL;
156         DWORD cbName;
157         DWORD dwType;
158         DWORD provCount;
159         DWORD dwIndex = 0;
160         
161         /* actual results */
162         CHAR *provider = NULL;
163         DWORD providerLen;
164         DWORD type;
165         DWORD count;
166         BOOL result;
167         DWORD notNull = 5;
168         DWORD notZeroFlags = 5;
169         
170         if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
171                 return;
172         
173         /* check pdwReserved flag for NULL */
174         result = CryptEnumProviders(dwIndex, &notNull, 0, &type, NULL, &providerLen);
175         ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%08x\n", (unsigned int)GetLastError());
176         
177         /* check dwFlags == 0 */
178         result = CryptEnumProviders(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
179         ok(!result && GetLastError()==NTE_BAD_FLAGS, "%08x\n", (unsigned int)GetLastError());
180         
181         /* alloc provider to half the size required
182          * cbName holds the size required */
183         providerLen = cbName / 2;
184         if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
185                 return;
186
187         result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
188         ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %08x, got %08x\n",
189                 ERROR_MORE_DATA, (unsigned int)GetLastError());
190
191         LocalFree(provider);
192
193         /* loop through the providers to get the number of providers 
194          * after loop ends, count should be provCount + 1 so subtract 1
195          * to get actual number of providers */
196         count = 0;
197         while(CryptEnumProviders(count++, NULL, 0, &type, NULL, &providerLen))
198                 ;
199         count--;
200         ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
201         
202         /* loop past the actual number of providers to get the error
203          * ERROR_NO_MORE_ITEMS */
204         for (count = 0; count < provCount + 1; count++)
205                 result = CryptEnumProviders(count, NULL, 0, &type, NULL, &providerLen);
206         ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %08x, got %08x\n", 
207                         ERROR_NO_MORE_ITEMS, (unsigned int)GetLastError());
208         
209         /* check expected versus actual values returned */
210         result = CryptEnumProviders(dwIndex, NULL, 0, &type, NULL, &providerLen);
211         ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
212         if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
213                 return;
214                 
215         result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
216         ok(result && type==dwType, "expected %i, got %i\n", 
217                 (unsigned int)dwType, (unsigned int)type);
218         ok(result && !strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
219         ok(result && cbName==providerLen, "expected %i, got %i\n", 
220                 (unsigned int)cbName, (unsigned int)providerLen);
221 }
222
223 START_TEST(crypt)
224 {
225         init_environment();
226         test_acquire_context();
227         clean_up_environment();
228         
229         test_enum_providers();
230 }