push 212f1dad91f15aefd8e676124180e0b86d7c9ee6
[wine/hacks.git] / dlls / advapi32 / tests / cred.c
blobe23f8721c09c54ccde445efb3e8a169c9a511c48
1 /*
2 * Credential Function Tests
4 * Copyright 2007 Robert Shearman
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincred.h"
28 #include "wine/test.h"
30 static BOOL (WINAPI *pCredDeleteA)(LPCSTR,DWORD,DWORD);
31 static BOOL (WINAPI *pCredEnumerateA)(LPCSTR,DWORD,DWORD *,PCREDENTIALA **);
32 static VOID (WINAPI *pCredFree)(PVOID);
33 static BOOL (WINAPI *pCredGetSessionTypes)(DWORD,LPDWORD);
34 static BOOL (WINAPI *pCredReadA)(LPCSTR,DWORD,DWORD,PCREDENTIALA *);
35 static BOOL (WINAPI *pCredRenameA)(LPCSTR,LPCSTR,DWORD,DWORD);
36 static BOOL (WINAPI *pCredWriteA)(PCREDENTIALA,DWORD);
37 static BOOL (WINAPI *pCredReadDomainCredentialsA)(PCREDENTIAL_TARGET_INFORMATIONA,DWORD,DWORD*,PCREDENTIALA**);
40 #define TEST_TARGET_NAME "credtest.winehq.org"
41 #define TEST_TARGET_NAME2 "credtest2.winehq.org"
42 static const WCHAR TEST_PASSWORD[] = {'p','4','$','$','w','0','r','d','!',0};
44 static void test_CredReadA(void)
46 BOOL ret;
47 PCREDENTIALA cred;
49 SetLastError(0xdeadbeef);
50 ret = pCredReadA(TEST_TARGET_NAME, -1, 0, &cred);
51 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
52 "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
53 GetLastError());
55 SetLastError(0xdeadbeef);
56 ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef, &cred);
57 ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER ),
58 "CredReadA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
59 GetLastError());
61 SetLastError(0xdeadbeef);
62 ret = pCredReadA(NULL, CRED_TYPE_GENERIC, 0, &cred);
63 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
64 "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
65 GetLastError());
68 static void test_CredWriteA(void)
70 CREDENTIALA new_cred;
71 BOOL ret;
73 SetLastError(0xdeadbeef);
74 ret = pCredWriteA(NULL, 0);
75 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
76 "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
77 GetLastError());
79 new_cred.Flags = 0;
80 new_cred.Type = CRED_TYPE_GENERIC;
81 new_cred.TargetName = NULL;
82 new_cred.Comment = (char *)"Comment";
83 new_cred.CredentialBlobSize = 0;
84 new_cred.CredentialBlob = NULL;
85 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
86 new_cred.AttributeCount = 0;
87 new_cred.Attributes = NULL;
88 new_cred.TargetAlias = NULL;
89 new_cred.UserName = (char *)"winetest";
91 SetLastError(0xdeadbeef);
92 ret = pCredWriteA(&new_cred, 0);
93 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
94 "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
95 GetLastError());
97 new_cred.TargetName = (char *)TEST_TARGET_NAME;
98 new_cred.Type = CRED_TYPE_DOMAIN_PASSWORD;
100 SetLastError(0xdeadbeef);
101 ret = pCredWriteA(&new_cred, 0);
102 if (ret)
104 /* Vista */
105 ok(GetLastError() == ERROR_IO_PENDING,
106 "Expected ERROR_IO_PENDING, got %d\n", GetLastError());
108 else
110 ok(GetLastError() == ERROR_BAD_USERNAME ||
111 GetLastError() == ERROR_NO_SUCH_LOGON_SESSION, /* Vista */
112 "CredWrite with username without domain should return ERROR_BAD_USERNAME"
113 "or ERROR_NO_SUCH_LOGON_SESSION not %d\n", GetLastError());
116 new_cred.UserName = NULL;
117 SetLastError(0xdeadbeef);
118 ret = pCredWriteA(&new_cred, 0);
119 ok(!ret && GetLastError() == ERROR_BAD_USERNAME,
120 "CredWriteA with NULL username should have failed with ERROR_BAD_USERNAME instead of %d\n",
121 GetLastError());
124 static void test_CredDeleteA(void)
126 BOOL ret;
128 SetLastError(0xdeadbeef);
129 ret = pCredDeleteA(TEST_TARGET_NAME, -1, 0);
130 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
131 "CredDeleteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
132 GetLastError());
134 SetLastError(0xdeadbeef);
135 ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef);
136 ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ),
137 "CredDeleteA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
138 GetLastError());
141 static void test_CredReadDomainCredentialsA(void)
143 BOOL ret;
144 char target_name[] = "no_such_target";
145 CREDENTIAL_TARGET_INFORMATIONA info = {target_name, NULL, target_name, NULL, NULL, NULL, NULL, 0, 0, NULL};
146 DWORD count;
147 PCREDENTIAL* creds;
149 if (!pCredReadDomainCredentialsA)
151 win_skip("CredReadDomainCredentialsA() is not implemented\n");
152 return;
155 /* these two tests would crash on both native and Wine. Implementations
156 * does not check for NULL output pointers and try to zero them out early */
157 #if 0
158 ok(!pCredReadDomainCredentialsA(&info, 0, NULL, &creds) &&
159 GetLastError() == ERROR_INVALID_PARAMETER, "!\n");
160 ok(!pCredReadDomainCredentialsA(&info, 0, &count, NULL) &&
161 GetLastError() == ERROR_INVALID_PARAMETER, "!\n");
162 #endif
164 SetLastError(0xdeadbeef);
165 ret = pCredReadDomainCredentialsA(NULL, 0, &count, &creds);
166 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
167 "CredReadDomainCredentialsA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
168 GetLastError());
170 SetLastError(0xdeadbeef);
171 creds = (void*)0x12345;
172 count = 2;
173 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
174 ok(!ret && GetLastError() == ERROR_NOT_FOUND,
175 "CredReadDomainCredentialsA should have failed with ERROR_NOT_FOUND instead of %d\n",
176 GetLastError());
177 ok(count ==0 && creds == NULL, "CredReadDomainCredentialsA must not return any result\n");
179 info.TargetName = NULL;
181 SetLastError(0xdeadbeef);
182 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
183 ok(!ret && GetLastError() == ERROR_NOT_FOUND,
184 "CredReadDomainCredentialsA should have failed with ERROR_NOT_FOUND instead of %d\n",
185 GetLastError());
187 info.DnsServerName = NULL;
189 SetLastError(0xdeadbeef);
190 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
191 ok(!ret && GetLastError() == ERROR_NOT_FOUND,
192 "CredReadDomainCredentialsA should have failed with ERROR_NOT_FOUND instead of %d\n",
193 GetLastError());
196 static void check_blob(int line, DWORD cred_type, PCREDENTIALA cred)
198 if (cred_type == CRED_TYPE_DOMAIN_PASSWORD)
200 todo_wine
201 ok_(__FILE__, line)(cred->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", cred->CredentialBlobSize);
202 todo_wine
203 ok_(__FILE__, line)(!cred->CredentialBlob, "expected NULL credentials but got %p\n", cred->CredentialBlob);
205 else
207 DWORD size=sizeof(TEST_PASSWORD);
208 ok_(__FILE__, line)(cred->CredentialBlobSize == size, "expected CredentialBlobSize of %u but got %u\n", size, cred->CredentialBlobSize);
209 ok_(__FILE__, line)(cred->CredentialBlob != NULL, "CredentialBlob should be present\n");
210 if (cred->CredentialBlob)
211 ok_(__FILE__, line)(!memcmp(cred->CredentialBlob, TEST_PASSWORD, size), "wrong CredentialBlob\n");
215 static void test_generic(void)
217 BOOL ret;
218 DWORD count, i;
219 PCREDENTIALA *creds;
220 CREDENTIALA new_cred;
221 PCREDENTIALA cred;
222 BOOL found = FALSE;
224 new_cred.Flags = 0;
225 new_cred.Type = CRED_TYPE_GENERIC;
226 new_cred.TargetName = (char *)TEST_TARGET_NAME;
227 new_cred.Comment = (char *)"Comment";
228 new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
229 new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
230 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
231 new_cred.AttributeCount = 0;
232 new_cred.Attributes = NULL;
233 new_cred.TargetAlias = NULL;
234 new_cred.UserName = (char *)"winetest";
236 ret = pCredWriteA(&new_cred, 0);
237 ok(ret, "CredWriteA failed with error %d\n", GetLastError());
239 ret = pCredEnumerateA(NULL, 0, &count, &creds);
240 ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
242 for (i = 0; i < count; i++)
244 if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
246 ok(creds[i]->Type == CRED_TYPE_GENERIC ||
247 creds[i]->Type == CRED_TYPE_DOMAIN_PASSWORD, /* Vista */
248 "expected creds[%d]->Type CRED_TYPE_GENERIC or CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
249 ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
250 ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
251 check_blob(__LINE__, creds[i]->Type, creds[i]);
252 ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
253 ok(!strcmp(creds[i]->UserName, "winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
254 found = TRUE;
257 pCredFree(creds);
258 ok(found, "credentials not found\n");
260 ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0, &cred);
261 ok(ret, "CredReadA failed with error %d\n", GetLastError());
262 pCredFree(cred);
264 ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0);
265 ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
268 static void test_domain_password(DWORD cred_type)
270 BOOL ret;
271 DWORD count, i;
272 PCREDENTIALA *creds;
273 CREDENTIALA new_cred;
274 PCREDENTIALA cred;
275 BOOL found = FALSE;
277 new_cred.Flags = 0;
278 new_cred.Type = cred_type;
279 new_cred.TargetName = (char *)TEST_TARGET_NAME;
280 new_cred.Comment = (char *)"Comment";
281 new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
282 new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
283 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
284 new_cred.AttributeCount = 0;
285 new_cred.Attributes = NULL;
286 new_cred.TargetAlias = NULL;
287 new_cred.UserName = (char *)"test\\winetest";
288 ret = pCredWriteA(&new_cred, 0);
289 if (!ret && GetLastError() == ERROR_NO_SUCH_LOGON_SESSION)
291 skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported "
292 "or are disabled. Skipping\n");
293 return;
295 ok(ret, "CredWriteA failed with error %d\n", GetLastError());
297 ret = pCredEnumerateA(NULL, 0, &count, &creds);
298 ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
300 for (i = 0; i < count; i++)
302 if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
304 ok(creds[i]->Type == cred_type, "expected creds[%d]->Type CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
305 ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
306 ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
307 check_blob(__LINE__, cred_type, creds[i]);
308 ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
309 ok(!strcmp(creds[i]->UserName, "test\\winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
310 found = TRUE;
313 pCredFree(creds);
314 ok(found, "credentials not found\n");
316 ret = pCredReadA(TEST_TARGET_NAME, cred_type, 0, &cred);
317 ok(ret, "CredReadA failed with error %d\n", GetLastError());
318 if (ret) /* don't check the values of cred, if CredReadA failed. */
320 check_blob(__LINE__, cred_type, cred);
321 pCredFree(cred);
324 ret = pCredDeleteA(TEST_TARGET_NAME, cred_type, 0);
325 ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
328 START_TEST(cred)
330 DWORD persists[CRED_TYPE_MAXIMUM];
332 pCredEnumerateA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredEnumerateA");
333 pCredFree = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredFree");
334 pCredGetSessionTypes = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredGetSessionTypes");
335 pCredWriteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredWriteA");
336 pCredDeleteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredDeleteA");
337 pCredReadA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadA");
338 pCredRenameA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredRenameA");
339 pCredReadDomainCredentialsA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadDomainCredentialsA");
341 if (!pCredEnumerateA || !pCredFree || !pCredWriteA || !pCredDeleteA ||
342 !pCredReadA)
344 skip("credentials functions not present in advapi32.dll\n");
345 return;
348 if (pCredGetSessionTypes)
350 BOOL ret;
351 DWORD i;
352 ret = pCredGetSessionTypes(CRED_TYPE_MAXIMUM, persists);
353 ok(ret, "CredGetSessionTypes failed with error %d\n", GetLastError());
354 ok(persists[0] == CRED_PERSIST_NONE, "persists[0] = %u instead of CRED_PERSIST_NONE\n", persists[0]);
355 for (i=0; i < CRED_TYPE_MAXIMUM; i++)
356 ok(persists[i] <= CRED_PERSIST_ENTERPRISE, "bad value for persists[%u]: %u\n", i, persists[i]);
358 else
359 memset(persists, CRED_PERSIST_ENTERPRISE, sizeof(persists));
361 test_CredReadA();
362 test_CredWriteA();
363 test_CredDeleteA();
365 test_CredReadDomainCredentialsA();
367 trace("generic:\n");
368 if (persists[CRED_TYPE_GENERIC] == CRED_PERSIST_NONE)
369 skip("CRED_TYPE_GENERIC credentials are not supported or are disabled. Skipping\n");
370 else
371 test_generic();
373 trace("domain password:\n");
374 if (persists[CRED_TYPE_DOMAIN_PASSWORD] == CRED_PERSIST_NONE)
375 skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported or are disabled. Skipping\n");
376 else
377 test_domain_password(CRED_TYPE_DOMAIN_PASSWORD);
379 trace("domain visible password:\n");
380 if (persists[CRED_TYPE_DOMAIN_VISIBLE_PASSWORD] == CRED_PERSIST_NONE)
381 skip("CRED_TYPE_DOMAIN_VISIBLE_PASSWORD credentials are not supported or are disabled. Skipping\n");
382 else
383 test_domain_password(CRED_TYPE_DOMAIN_VISIBLE_PASSWORD);