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
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)
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",
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",
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",
68 static void test_CredWriteA(void)
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",
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",
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);
104 ok(GetLastError() == ERROR_SUCCESS ||
105 GetLastError() == ERROR_IO_PENDING, /* Vista */
106 "Expected ERROR_IO_PENDING, got %d\n", GetLastError());
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",
124 static void test_CredDeleteA(void)
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",
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",
141 static void test_CredReadDomainCredentialsA(void)
144 char target_name[] = "no_such_target";
145 CREDENTIAL_TARGET_INFORMATIONA info = {target_name, NULL, target_name, NULL, NULL, NULL, NULL, 0, 0, NULL};
149 if (!pCredReadDomainCredentialsA)
151 win_skip("CredReadDomainCredentialsA() is not implemented\n");
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 */
159 ok(!pCredReadDomainCredentialsA(&info, 0, NULL, &creds) &&
160 GetLastError() == ERROR_INVALID_PARAMETER, "!\n");
161 ok(!pCredReadDomainCredentialsA(&info, 0, &count, NULL) &&
162 GetLastError() == ERROR_INVALID_PARAMETER, "!\n");
165 SetLastError(0xdeadbeef);
166 ret = pCredReadDomainCredentialsA(NULL, 0, &count, &creds);
167 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
168 "CredReadDomainCredentialsA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
171 SetLastError(0xdeadbeef);
172 creds = (void*)0x12345;
174 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
175 ok(!ret && GetLastError() == ERROR_NOT_FOUND,
176 "CredReadDomainCredentialsA should have failed with ERROR_NOT_FOUND instead of %d\n",
178 ok(count ==0 && creds == NULL, "CredReadDomainCredentialsA must not return any result\n");
180 info.TargetName = NULL;
182 SetLastError(0xdeadbeef);
183 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
184 ok(!ret, "CredReadDomainCredentialsA should have failed\n");
185 ok(GetLastError() == ERROR_NOT_FOUND ||
186 GetLastError() == ERROR_INVALID_PARAMETER, /* Vista, W2K8 */
187 "Expected ERROR_NOT_FOUND or ERROR_INVALID_PARAMETER instead of %d\n",
190 info.DnsServerName = NULL;
192 SetLastError(0xdeadbeef);
193 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
194 ok(!ret, "CredReadDomainCredentialsA should have failed\n");
195 ok(GetLastError() == ERROR_NOT_FOUND ||
196 GetLastError() == ERROR_INVALID_PARAMETER, /* Vista, W2K8 */
197 "Expected ERROR_NOT_FOUND or ERROR_INVALID_PARAMETER instead of %d\n",
201 static void check_blob(int line, DWORD cred_type, PCREDENTIALA cred)
203 if (cred_type == CRED_TYPE_DOMAIN_PASSWORD)
205 ok_(__FILE__, line)(cred->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", cred->CredentialBlobSize);
206 ok_(__FILE__, line)(!cred->CredentialBlob, "expected NULL credentials but got %p\n", cred->CredentialBlob);
210 DWORD size=sizeof(TEST_PASSWORD);
211 ok_(__FILE__, line)(cred->CredentialBlobSize == size, "expected CredentialBlobSize of %u but got %u\n", size, cred->CredentialBlobSize);
212 ok_(__FILE__, line)(cred->CredentialBlob != NULL, "CredentialBlob should be present\n");
213 if (cred->CredentialBlob)
214 ok_(__FILE__, line)(!memcmp(cred->CredentialBlob, TEST_PASSWORD, size), "wrong CredentialBlob\n");
218 static void test_generic(void)
223 CREDENTIALA new_cred;
228 new_cred.Type = CRED_TYPE_GENERIC;
229 new_cred.TargetName = (char *)TEST_TARGET_NAME;
230 new_cred.Comment = (char *)"Comment";
231 new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
232 new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
233 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
234 new_cred.AttributeCount = 0;
235 new_cred.Attributes = NULL;
236 new_cred.TargetAlias = NULL;
237 new_cred.UserName = (char *)"winetest";
239 ret = pCredWriteA(&new_cred, 0);
240 ok(ret || broken(GetLastError() == ERROR_NO_SUCH_LOGON_SESSION),
241 "CredWriteA failed with error %d\n", GetLastError());
244 skip("couldn't write generic credentials, skipping tests\n");
248 ret = pCredEnumerateA(NULL, 0, &count, &creds);
249 ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
251 for (i = 0; i < count; i++)
253 if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
255 ok(creds[i]->Type == CRED_TYPE_GENERIC ||
256 creds[i]->Type == CRED_TYPE_DOMAIN_PASSWORD, /* Vista */
257 "expected creds[%d]->Type CRED_TYPE_GENERIC or CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
258 ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
259 ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
260 check_blob(__LINE__, creds[i]->Type, creds[i]);
261 ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
262 ok(!strcmp(creds[i]->UserName, "winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
267 ok(found, "credentials not found\n");
269 ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0, &cred);
270 ok(ret, "CredReadA failed with error %d\n", GetLastError());
273 ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0);
274 ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
277 static void test_domain_password(DWORD cred_type)
282 CREDENTIALA new_cred;
287 new_cred.Type = cred_type;
288 new_cred.TargetName = (char *)TEST_TARGET_NAME;
289 new_cred.Comment = (char *)"Comment";
290 new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
291 new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
292 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
293 new_cred.AttributeCount = 0;
294 new_cred.Attributes = NULL;
295 new_cred.TargetAlias = NULL;
296 new_cred.UserName = (char *)"test\\winetest";
297 ret = pCredWriteA(&new_cred, 0);
298 if (!ret && GetLastError() == ERROR_NO_SUCH_LOGON_SESSION)
300 skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported "
301 "or are disabled. Skipping\n");
304 ok(ret, "CredWriteA failed with error %d\n", GetLastError());
306 ret = pCredEnumerateA(NULL, 0, &count, &creds);
307 ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
309 for (i = 0; i < count; i++)
311 if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
313 ok(creds[i]->Type == cred_type, "expected creds[%d]->Type CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
314 ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
315 ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
316 check_blob(__LINE__, cred_type, creds[i]);
317 ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
318 ok(!strcmp(creds[i]->UserName, "test\\winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
323 ok(found, "credentials not found\n");
325 ret = pCredReadA(TEST_TARGET_NAME, cred_type, 0, &cred);
326 ok(ret, "CredReadA failed with error %d\n", GetLastError());
327 if (ret) /* don't check the values of cred, if CredReadA failed. */
329 check_blob(__LINE__, cred_type, cred);
333 ret = pCredDeleteA(TEST_TARGET_NAME, cred_type, 0);
334 ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
339 DWORD persists[CRED_TYPE_MAXIMUM];
341 pCredEnumerateA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredEnumerateA");
342 pCredFree = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredFree");
343 pCredGetSessionTypes = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredGetSessionTypes");
344 pCredWriteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredWriteA");
345 pCredDeleteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredDeleteA");
346 pCredReadA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadA");
347 pCredRenameA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredRenameA");
348 pCredReadDomainCredentialsA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadDomainCredentialsA");
350 if (!pCredEnumerateA || !pCredFree || !pCredWriteA || !pCredDeleteA ||
353 win_skip("credentials functions not present in advapi32.dll\n");
357 if (pCredGetSessionTypes)
361 ret = pCredGetSessionTypes(CRED_TYPE_MAXIMUM, persists);
362 ok(ret, "CredGetSessionTypes failed with error %d\n", GetLastError());
363 ok(persists[0] == CRED_PERSIST_NONE, "persists[0] = %u instead of CRED_PERSIST_NONE\n", persists[0]);
364 for (i=0; i < CRED_TYPE_MAXIMUM; i++)
365 ok(persists[i] <= CRED_PERSIST_ENTERPRISE, "bad value for persists[%u]: %u\n", i, persists[i]);
368 memset(persists, CRED_PERSIST_ENTERPRISE, sizeof(persists));
374 test_CredReadDomainCredentialsA();
377 if (persists[CRED_TYPE_GENERIC] == CRED_PERSIST_NONE)
378 skip("CRED_TYPE_GENERIC credentials are not supported or are disabled. Skipping\n");
382 trace("domain password:\n");
383 if (persists[CRED_TYPE_DOMAIN_PASSWORD] == CRED_PERSIST_NONE)
384 skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported or are disabled. Skipping\n");
386 test_domain_password(CRED_TYPE_DOMAIN_PASSWORD);
388 trace("domain visible password:\n");
389 if (persists[CRED_TYPE_DOMAIN_VISIBLE_PASSWORD] == CRED_PERSIST_NONE)
390 skip("CRED_TYPE_DOMAIN_VISIBLE_PASSWORD credentials are not supported or are disabled. Skipping\n");
392 test_domain_password(CRED_TYPE_DOMAIN_VISIBLE_PASSWORD);