- Fixed a problem with dwProvType values greater than 99 in
[wine/wine-kai.git] / dlls / advapi32 / tests / crypt.c
bloba3ac44a615281df6cf108849276eefbf7968d6f8
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"
29 static const char szRsaBaseProv[] = MS_DEF_PROV_A;
30 static const char szNonExistentProv[] = "Wine Non Existent Cryptographic Provider v11.2";
31 static const char szKeySet[] = "wine_test_keyset";
32 static const char szBadKeySet[] = "wine_test_bad_keyset";
33 #define NON_DEF_PROV_TYPE 999
35 static void init_environment(void)
37 HCRYPTPROV hProv;
39 /* Ensure that container "wine_test_keyset" does exist */
40 if (!CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
42 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
44 CryptReleaseContext(hProv, 0);
46 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
47 if (!CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
49 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
51 CryptReleaseContext(hProv, 0);
53 /* Ensure that container "wine_test_bad_keyset" does not exist. */
54 if (CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
56 CryptReleaseContext(hProv, 0);
57 CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
61 static void clean_up_environment(void)
63 HCRYPTPROV hProv;
65 /* Remove container "wine_test_keyset" */
66 if (CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
68 CryptReleaseContext(hProv, 0);
69 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
72 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
73 if (CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
75 CryptReleaseContext(hProv, 0);
76 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
80 static void test_acquire_context(void)
82 BOOL result;
83 HCRYPTPROV hProv;
85 /* Provoke all kinds of error conditions (which are easy to provoke).
86 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
87 * but since this is likely to change between CSP versions, we don't check
88 * this. Please don't change the order of tests. */
89 result = CryptAcquireContext(&hProv, NULL, NULL, 0, 0);
90 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%08x\n", (unsigned int)GetLastError());
92 result = CryptAcquireContext(&hProv, NULL, NULL, 1000, 0);
93 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%08x\n", (unsigned int)GetLastError());
95 result = CryptAcquireContext(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
96 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%08x\n", (unsigned int)GetLastError());
98 todo_wine {
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());
106 result = CryptAcquireContext(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
107 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%08x\n", (unsigned int)GetLastError());
109 /* Last not least, try to really acquire a context. */
110 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
111 ok(result, "%08x\n", (unsigned int)GetLastError());
113 if (GetLastError() == ERROR_SUCCESS)
114 CryptReleaseContext(hProv, 0);
116 todo_wine {
117 /* Try again, witch an empty ("\0") szProvider parameter */
118 result = CryptAcquireContext(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
119 ok(result, "%08x\n", (unsigned int)GetLastError());
121 if (GetLastError() == ERROR_SUCCESS)
122 CryptReleaseContext(hProv, 0);
126 START_TEST(crypt)
128 init_environment();
129 test_acquire_context();
130 clean_up_environment();