1 /* Unit test suite for wintrust crypt functions
3 * Copyright 2007 Paul Vriens
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.
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.
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
27 #include "wine/test.h"
29 static char selfname
[MAX_PATH
];
31 static CHAR CURR_DIR
[MAX_PATH
];
33 static BOOL (WINAPI
* pCryptCATAdminAcquireContext
)(HCATADMIN
*, const GUID
*, DWORD
);
34 static BOOL (WINAPI
* pCryptCATAdminReleaseContext
)(HCATADMIN
, DWORD
);
35 static BOOL (WINAPI
* pCryptCATAdminCalcHashFromFileHandle
)(HANDLE hFile
, DWORD
*, BYTE
*, DWORD
);
37 static void InitFunctionPtrs(void)
39 HMODULE hWintrust
= GetModuleHandleA("wintrust.dll");
41 #define WINTRUST_GET_PROC(func) \
42 p ## func = (void*)GetProcAddress(hWintrust, #func); \
44 trace("GetProcAddress(%s) failed\n", #func); \
47 WINTRUST_GET_PROC(CryptCATAdminAcquireContext
)
48 WINTRUST_GET_PROC(CryptCATAdminReleaseContext
)
49 WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle
)
51 #undef WINTRUST_GET_PROC
54 static void test_context(void)
58 static GUID dummy
= { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
59 static GUID unknown
= { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
61 if (!pCryptCATAdminAcquireContext
|| !pCryptCATAdminReleaseContext
)
63 skip("CryptCATAdminAcquireContext and/or CryptCATAdminReleaseContext are not available\n");
68 SetLastError(0xdeadbeef);
69 ret
= pCryptCATAdminAcquireContext(NULL
, NULL
, 0);
72 ok(!ret
, "Expected failure\n");
73 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
74 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
78 SetLastError(0xdeadbeef);
79 ret
= pCryptCATAdminAcquireContext(&hca
, NULL
, 0);
80 ok(ret
, "Expected success\n");
81 ok(GetLastError() == ERROR_SUCCESS
||
82 GetLastError() == 0xdeadbeef /* Vista */,
83 "Expected ERROR_SUCCESS or 0xdeadbeef, got %d\n", GetLastError());
84 ok(hca
!= NULL
, "Expected a context handle, got NULL\n");
87 SetLastError(0xdeadbeef);
88 ret
= pCryptCATAdminReleaseContext(NULL
, 0);
91 ok(!ret
, "Expected failure\n");
92 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
93 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
97 SetLastError(0xdeadbeef);
98 ret
= pCryptCATAdminReleaseContext(hca
, 0);
99 ok(ret
, "Expected success\n");
100 ok(GetLastError() == 0xdeadbeef,
101 "Expected no change in last error, got %d\n", GetLastError());
103 /* Try to release a second time */
104 SetLastError(0xdeadbeef);
105 ret
= pCryptCATAdminReleaseContext(hca
, 0);
108 ok(!ret
, "Expected failure\n");
109 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
110 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
113 /* NULL context handle and dummy GUID */
114 SetLastError(0xdeadbeef);
115 ret
= pCryptCATAdminAcquireContext(NULL
, &dummy
, 0);
118 ok(!ret
, "Expected failure\n");
119 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
120 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
123 /* Correct context handle and dummy GUID */
124 SetLastError(0xdeadbeef);
125 ret
= pCryptCATAdminAcquireContext(&hca
, &dummy
, 0);
126 ok(ret
, "Expected success\n");
127 ok(GetLastError() == ERROR_SUCCESS
||
128 GetLastError() == 0xdeadbeef /* Vista */,
129 "Expected ERROR_SUCCESS or 0xdeadbeef, got %d\n", GetLastError());
130 ok(hca
!= NULL
, "Expected a context handle, got NULL\n");
132 ret
= pCryptCATAdminReleaseContext(hca
, 0);
133 ok(ret
, "Expected success\n");
135 /* Correct context handle and GUID */
136 SetLastError(0xdeadbeef);
137 ret
= pCryptCATAdminAcquireContext(&hca
, &unknown
, 0);
138 ok(ret
, "Expected success\n");
139 ok(GetLastError() == ERROR_SUCCESS
||
140 GetLastError() == 0xdeadbeef /* Vista */,
141 "Expected ERROR_SUCCESS or 0xdeadbeef, got %d\n", GetLastError());
142 ok(hca
!= NULL
, "Expected a context handle, got NULL\n");
144 ret
= pCryptCATAdminReleaseContext(hca
, 0);
145 ok(ret
, "Expected success\n");
147 /* Flags not equal to 0 */
148 SetLastError(0xdeadbeef);
149 ret
= pCryptCATAdminAcquireContext(&hca
, &unknown
, 1);
150 ok(ret
, "Expected success\n");
151 ok(GetLastError() == ERROR_SUCCESS
||
152 GetLastError() == 0xdeadbeef /* Vista */,
153 "Expected ERROR_SUCCESS or 0xdeadbeef, got %d\n", GetLastError());
154 ok(hca
!= NULL
, "Expected a context handle, got NULL\n");
156 ret
= pCryptCATAdminReleaseContext(hca
, 0);
157 ok(ret
, "Expected success\n");
160 /* TODO: Check whether SHA-1 is the algorithm that's always used */
161 static void test_calchash(void)
167 BYTE expectedhash
[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
171 if (!pCryptCATAdminCalcHashFromFileHandle
)
173 skip("CryptCATAdminCalcHashFromFileHandle is not available\n");
178 SetLastError(0xdeadbeef);
179 ret
= pCryptCATAdminCalcHashFromFileHandle(NULL
, NULL
, NULL
, 0);
182 ok(!ret
, "Expected failure\n");
183 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
184 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
187 /* NULL filehandle, rest is legal */
188 SetLastError(0xdeadbeef);
189 ret
= pCryptCATAdminCalcHashFromFileHandle(NULL
, &hashsize
, NULL
, 0);
192 ok(!ret
, "Expected failure\n");
193 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
194 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
197 /* Correct filehandle, rest is NULL */
198 file
= CreateFileA(selfname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
199 SetLastError(0xdeadbeef);
200 ret
= pCryptCATAdminCalcHashFromFileHandle(file
, NULL
, NULL
, 0);
203 ok(!ret
, "Expected failure\n");
204 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
205 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
209 /* All OK, but dwFlags set to 1 */
210 file
= CreateFileA(selfname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
211 SetLastError(0xdeadbeef);
212 ret
= pCryptCATAdminCalcHashFromFileHandle(file
, &hashsize
, NULL
, 1);
215 ok(!ret
, "Expected failure\n");
216 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
217 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
221 /* All OK, requesting the size of the hash */
222 file
= CreateFileA(selfname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
223 SetLastError(0xdeadbeef);
224 ret
= pCryptCATAdminCalcHashFromFileHandle(file
, &hashsize
, NULL
, 0);
225 ok(ret
, "Expected success\n");
228 ok(hashsize
== 20," Expected a hash size of 20, got %d\n", hashsize
);
229 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
230 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
234 /* All OK, retrieve the hash
235 * Double the hash buffer to see what happens to the size parameter
237 file
= CreateFileA(selfname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
239 hash
= HeapAlloc(GetProcessHeap(), 0, hashsize
);
240 SetLastError(0xdeadbeef);
241 ret
= pCryptCATAdminCalcHashFromFileHandle(file
, &hashsize
, hash
, 0);
242 ok(ret
, "Expected success\n");
245 ok(hashsize
== 20," Expected a hash size of 20, got %d\n", hashsize
);
246 ok(GetLastError() == ERROR_SUCCESS
,
247 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
250 HeapFree(GetProcessHeap(), 0, hash
);
252 /* Do the same test with a file created and filled by ourselves (and we thus
253 * have a known hash for).
255 GetTempFileNameA(CURR_DIR
, "hsh", 0, temp
);
256 file
= CreateFileA(temp
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
257 WriteFile(file
, "Text in this file is needed to create a know hash", 49, &written
, NULL
);
260 /* All OK, first request the size and then retrieve the hash */
261 file
= CreateFileA(temp
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
263 pCryptCATAdminCalcHashFromFileHandle(file
, &hashsize
, NULL
, 0);
264 hash
= HeapAlloc(GetProcessHeap(), 0, hashsize
);
265 SetLastError(0xdeadbeef);
266 ret
= pCryptCATAdminCalcHashFromFileHandle(file
, &hashsize
, hash
, 0);
267 ok(ret
, "Expected success\n");
270 ok(GetLastError() == ERROR_SUCCESS
,
271 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
272 ok(!memcmp(hash
, expectedhash
, sizeof(expectedhash
)), "Hashes didn't match\n");
276 HeapFree(GetProcessHeap(), 0, hash
);
287 myARGC
= winetest_get_mainargs(&myARGV
);
288 strcpy(selfname
, myARGV
[0]);
290 GetCurrentDirectoryA(MAX_PATH
, CURR_DIR
);