Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / advapi32 / tests / cred.c
blob5762f1be50775863dc0870a1b24e06782f4d2b97
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**);
38 static BOOL (WINAPI *pCredMarshalCredentialA)(CRED_MARSHAL_TYPE,PVOID,LPSTR *);
39 static BOOL (WINAPI *pCredUnmarshalCredentialA)(LPCSTR,PCRED_MARSHAL_TYPE,PVOID);
40 static BOOL (WINAPI *pCredIsMarshaledCredentialA)(LPCSTR);
42 #define TEST_TARGET_NAME "credtest.winehq.org"
43 #define TEST_TARGET_NAME2 "credtest2.winehq.org"
44 static const WCHAR TEST_PASSWORD[] = {'p','4','$','$','w','0','r','d','!',0};
46 static void test_CredReadA(void)
48 BOOL ret;
49 PCREDENTIALA cred;
51 SetLastError(0xdeadbeef);
52 ret = pCredReadA(TEST_TARGET_NAME, -1, 0, &cred);
53 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
54 "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
55 GetLastError());
57 SetLastError(0xdeadbeef);
58 ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef, &cred);
59 ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER ),
60 "CredReadA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
61 GetLastError());
63 SetLastError(0xdeadbeef);
64 ret = pCredReadA(NULL, CRED_TYPE_GENERIC, 0, &cred);
65 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
66 "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
67 GetLastError());
70 static void test_CredWriteA(void)
72 CREDENTIALA new_cred;
73 BOOL ret;
75 SetLastError(0xdeadbeef);
76 ret = pCredWriteA(NULL, 0);
77 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
78 "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
79 GetLastError());
81 new_cred.Flags = 0;
82 new_cred.Type = CRED_TYPE_GENERIC;
83 new_cred.TargetName = NULL;
84 new_cred.Comment = (char *)"Comment";
85 new_cred.CredentialBlobSize = 0;
86 new_cred.CredentialBlob = NULL;
87 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
88 new_cred.AttributeCount = 0;
89 new_cred.Attributes = NULL;
90 new_cred.TargetAlias = NULL;
91 new_cred.UserName = (char *)"winetest";
93 SetLastError(0xdeadbeef);
94 ret = pCredWriteA(&new_cred, 0);
95 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
96 "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
97 GetLastError());
99 new_cred.TargetName = (char *)TEST_TARGET_NAME;
100 new_cred.Type = CRED_TYPE_DOMAIN_PASSWORD;
102 SetLastError(0xdeadbeef);
103 ret = pCredWriteA(&new_cred, 0);
104 if (ret)
106 ok(GetLastError() == ERROR_SUCCESS ||
107 GetLastError() == ERROR_IO_PENDING, /* Vista */
108 "Expected ERROR_IO_PENDING, got %d\n", GetLastError());
110 else
112 ok(GetLastError() == ERROR_BAD_USERNAME ||
113 GetLastError() == ERROR_NO_SUCH_LOGON_SESSION, /* Vista */
114 "CredWrite with username without domain should return ERROR_BAD_USERNAME"
115 "or ERROR_NO_SUCH_LOGON_SESSION not %d\n", GetLastError());
118 new_cred.UserName = NULL;
119 SetLastError(0xdeadbeef);
120 ret = pCredWriteA(&new_cred, 0);
121 ok(!ret && GetLastError() == ERROR_BAD_USERNAME,
122 "CredWriteA with NULL username should have failed with ERROR_BAD_USERNAME instead of %d\n",
123 GetLastError());
126 static void test_CredDeleteA(void)
128 BOOL ret;
130 SetLastError(0xdeadbeef);
131 ret = pCredDeleteA(TEST_TARGET_NAME, -1, 0);
132 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
133 "CredDeleteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
134 GetLastError());
136 SetLastError(0xdeadbeef);
137 ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef);
138 ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ),
139 "CredDeleteA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
140 GetLastError());
143 static void test_CredReadDomainCredentialsA(void)
145 BOOL ret;
146 char target_name[] = "no_such_target";
147 CREDENTIAL_TARGET_INFORMATIONA info = {target_name, NULL, target_name, NULL, NULL, NULL, NULL, 0, 0, NULL};
148 DWORD count;
149 PCREDENTIAL* creds;
151 if (!pCredReadDomainCredentialsA)
153 win_skip("CredReadDomainCredentialsA() is not implemented\n");
154 return;
157 /* these two tests would crash on both native and Wine. Implementations
158 * does not check for NULL output pointers and try to zero them out early */
159 if(0)
161 ret = pCredReadDomainCredentialsA(&info, 0, NULL, &creds);
162 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "!\n");
163 ret = pCredReadDomainCredentialsA(&info, 0, &count, NULL);
164 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "!\n");
167 SetLastError(0xdeadbeef);
168 ret = pCredReadDomainCredentialsA(NULL, 0, &count, &creds);
169 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
170 "CredReadDomainCredentialsA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
171 GetLastError());
173 SetLastError(0xdeadbeef);
174 creds = (void*)0x12345;
175 count = 2;
176 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
177 ok(!ret && GetLastError() == ERROR_NOT_FOUND,
178 "CredReadDomainCredentialsA should have failed with ERROR_NOT_FOUND instead of %d\n",
179 GetLastError());
180 ok(count ==0 && creds == NULL, "CredReadDomainCredentialsA must not return any result\n");
182 info.TargetName = NULL;
184 SetLastError(0xdeadbeef);
185 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
186 ok(!ret, "CredReadDomainCredentialsA should have failed\n");
187 ok(GetLastError() == ERROR_NOT_FOUND ||
188 GetLastError() == ERROR_INVALID_PARAMETER, /* Vista, W2K8 */
189 "Expected ERROR_NOT_FOUND or ERROR_INVALID_PARAMETER instead of %d\n",
190 GetLastError());
192 info.DnsServerName = NULL;
194 SetLastError(0xdeadbeef);
195 ret = pCredReadDomainCredentialsA(&info, 0, &count, &creds);
196 ok(!ret, "CredReadDomainCredentialsA should have failed\n");
197 ok(GetLastError() == ERROR_NOT_FOUND ||
198 GetLastError() == ERROR_INVALID_PARAMETER, /* Vista, W2K8 */
199 "Expected ERROR_NOT_FOUND or ERROR_INVALID_PARAMETER instead of %d\n",
200 GetLastError());
203 static void check_blob(int line, DWORD cred_type, PCREDENTIALA cred)
205 if (cred_type == CRED_TYPE_DOMAIN_PASSWORD)
207 todo_wine
208 ok_(__FILE__, line)(cred->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", cred->CredentialBlobSize);
209 todo_wine
210 ok_(__FILE__, line)(!cred->CredentialBlob, "expected NULL credentials but got %p\n", cred->CredentialBlob);
212 else
214 DWORD size=sizeof(TEST_PASSWORD);
215 ok_(__FILE__, line)(cred->CredentialBlobSize == size, "expected CredentialBlobSize of %u but got %u\n", size, cred->CredentialBlobSize);
216 ok_(__FILE__, line)(cred->CredentialBlob != NULL, "CredentialBlob should be present\n");
217 if (cred->CredentialBlob)
218 ok_(__FILE__, line)(!memcmp(cred->CredentialBlob, TEST_PASSWORD, size), "wrong CredentialBlob\n");
222 static void test_generic(void)
224 BOOL ret;
225 DWORD count, i;
226 PCREDENTIALA *creds;
227 CREDENTIALA new_cred;
228 PCREDENTIALA cred;
229 BOOL found = FALSE;
231 new_cred.Flags = 0;
232 new_cred.Type = CRED_TYPE_GENERIC;
233 new_cred.TargetName = (char *)TEST_TARGET_NAME;
234 new_cred.Comment = (char *)"Comment";
235 new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
236 new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
237 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
238 new_cred.AttributeCount = 0;
239 new_cred.Attributes = NULL;
240 new_cred.TargetAlias = NULL;
241 new_cred.UserName = (char *)"winetest";
243 ret = pCredWriteA(&new_cred, 0);
244 ok(ret || broken(GetLastError() == ERROR_NO_SUCH_LOGON_SESSION),
245 "CredWriteA failed with error %d\n", GetLastError());
246 if (!ret)
248 skip("couldn't write generic credentials, skipping tests\n");
249 return;
252 ret = pCredEnumerateA(NULL, 0, &count, &creds);
253 ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
255 for (i = 0; i < count; i++)
257 if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
259 ok(creds[i]->Type == CRED_TYPE_GENERIC ||
260 creds[i]->Type == CRED_TYPE_DOMAIN_PASSWORD, /* Vista */
261 "expected creds[%d]->Type CRED_TYPE_GENERIC or CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
262 ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
263 ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
264 check_blob(__LINE__, creds[i]->Type, creds[i]);
265 ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
266 ok(!strcmp(creds[i]->UserName, "winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
267 found = TRUE;
270 pCredFree(creds);
271 ok(found, "credentials not found\n");
273 ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0, &cred);
274 ok(ret, "CredReadA failed with error %d\n", GetLastError());
275 pCredFree(cred);
277 ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0);
278 ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
281 static void test_domain_password(DWORD cred_type)
283 BOOL ret;
284 DWORD count, i;
285 PCREDENTIALA *creds;
286 CREDENTIALA new_cred;
287 PCREDENTIALA cred;
288 BOOL found = FALSE;
290 new_cred.Flags = 0;
291 new_cred.Type = cred_type;
292 new_cred.TargetName = (char *)TEST_TARGET_NAME;
293 new_cred.Comment = (char *)"Comment";
294 new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
295 new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
296 new_cred.Persist = CRED_PERSIST_ENTERPRISE;
297 new_cred.AttributeCount = 0;
298 new_cred.Attributes = NULL;
299 new_cred.TargetAlias = NULL;
300 new_cred.UserName = (char *)"test\\winetest";
301 ret = pCredWriteA(&new_cred, 0);
302 if (!ret && GetLastError() == ERROR_NO_SUCH_LOGON_SESSION)
304 skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported "
305 "or are disabled. Skipping\n");
306 return;
308 ok(ret, "CredWriteA failed with error %d\n", GetLastError());
310 ret = pCredEnumerateA(NULL, 0, &count, &creds);
311 ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
313 for (i = 0; i < count; i++)
315 if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
317 ok(creds[i]->Type == cred_type, "expected creds[%d]->Type CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
318 ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
319 ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
320 check_blob(__LINE__, cred_type, creds[i]);
321 ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
322 ok(!strcmp(creds[i]->UserName, "test\\winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
323 found = TRUE;
326 pCredFree(creds);
327 ok(found, "credentials not found\n");
329 ret = pCredReadA(TEST_TARGET_NAME, cred_type, 0, &cred);
330 ok(ret, "CredReadA failed with error %d\n", GetLastError());
331 if (ret) /* don't check the values of cred, if CredReadA failed. */
333 check_blob(__LINE__, cred_type, cred);
334 pCredFree(cred);
337 ret = pCredDeleteA(TEST_TARGET_NAME, cred_type, 0);
338 ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
341 static void test_CredMarshalCredentialA(void)
343 static WCHAR emptyW[] = {0};
344 static WCHAR tW[] = {'t',0};
345 static WCHAR teW[] = {'t','e',0};
346 static WCHAR tesW[] = {'t','e','s',0};
347 static WCHAR testW[] = {'t','e','s','t',0};
348 static WCHAR test1W[] = {'t','e','s','t','1',0};
349 CERT_CREDENTIAL_INFO cert;
350 USERNAME_TARGET_CREDENTIAL_INFO username;
351 DWORD error;
352 char *str;
353 BOOL ret;
355 SetLastError( 0xdeadbeef );
356 ret = pCredMarshalCredentialA( 0, NULL, NULL );
357 error = GetLastError();
358 ok( !ret, "unexpected success\n" );
359 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
361 memset( cert.rgbHashOfCert, 0, sizeof(cert.rgbHashOfCert) );
362 cert.cbSize = sizeof(cert);
363 SetLastError( 0xdeadbeef );
364 ret = pCredMarshalCredentialA( 0, &cert, NULL );
365 error = GetLastError();
366 ok( !ret, "unexpected success\n" );
367 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
369 str = (char *)0xdeadbeef;
370 SetLastError( 0xdeadbeef );
371 ret = pCredMarshalCredentialA( 0, &cert, &str );
372 error = GetLastError();
373 ok( !ret, "unexpected success\n" );
374 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
375 ok( str == (char *)0xdeadbeef, "got %p\n", str );
377 SetLastError( 0xdeadbeef );
378 ret = pCredMarshalCredentialA( CertCredential, NULL, NULL );
379 error = GetLastError();
380 ok( !ret, "unexpected success\n" );
381 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
383 if (0) { /* crash */
384 SetLastError( 0xdeadbeef );
385 ret = pCredMarshalCredentialA( CertCredential, &cert, NULL );
386 error = GetLastError();
387 ok( !ret, "unexpected success\n" );
388 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
391 cert.cbSize = 0;
392 str = (char *)0xdeadbeef;
393 SetLastError( 0xdeadbeef );
394 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
395 error = GetLastError();
396 ok( !ret, "unexpected success\n" );
397 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
398 ok( str == (char *)0xdeadbeef, "got %p\n", str );
400 cert.cbSize = sizeof(cert) + 4;
401 str = NULL;
402 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
403 ok( ret, "unexpected failure %u\n", GetLastError() );
404 ok( str != NULL, "str not set\n" );
405 ok( !lstrcmpA( str, "@@BAAAAAAAAAAAAAAAAAAAAAAAAAAA" ), "got %s\n", str );
406 pCredFree( str );
408 cert.cbSize = sizeof(cert);
409 cert.rgbHashOfCert[0] = 2;
410 str = NULL;
411 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
412 ok( ret, "unexpected failure %u\n", GetLastError() );
413 ok( str != NULL, "str not set\n" );
414 ok( !lstrcmpA( str, "@@BCAAAAAAAAAAAAAAAAAAAAAAAAAA" ), "got %s\n", str );
415 pCredFree( str );
417 cert.rgbHashOfCert[0] = 255;
418 str = NULL;
419 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
420 ok( ret, "unexpected failure %u\n", GetLastError() );
421 ok( str != NULL, "str not set\n" );
422 ok( !lstrcmpA( str, "@@B-DAAAAAAAAAAAAAAAAAAAAAAAAA" ), "got %s\n", str );
423 pCredFree( str );
425 cert.rgbHashOfCert[0] = 1;
426 cert.rgbHashOfCert[1] = 1;
427 str = NULL;
428 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
429 ok( ret, "unexpected failure %u\n", GetLastError() );
430 ok( str != NULL, "str not set\n" );
431 ok( !lstrcmpA( str, "@@BBEAAAAAAAAAAAAAAAAAAAAAAAAA" ), "got %s\n", str );
432 pCredFree( str );
434 cert.rgbHashOfCert[0] = 1;
435 cert.rgbHashOfCert[1] = 1;
436 cert.rgbHashOfCert[2] = 1;
437 str = NULL;
438 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
439 ok( ret, "unexpected failure %u\n", GetLastError() );
440 ok( str != NULL, "str not set\n" );
441 ok( !lstrcmpA( str, "@@BBEQAAAAAAAAAAAAAAAAAAAAAAAA" ), "got %s\n", str );
442 pCredFree( str );
444 memset( cert.rgbHashOfCert, 0, sizeof(cert.rgbHashOfCert) );
445 cert.rgbHashOfCert[0] = 'W';
446 cert.rgbHashOfCert[1] = 'i';
447 cert.rgbHashOfCert[2] = 'n';
448 cert.rgbHashOfCert[3] = 'e';
449 str = NULL;
450 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
451 ok( ret, "unexpected failure %u\n", GetLastError() );
452 ok( str != NULL, "str not set\n" );
453 ok( !lstrcmpA( str, "@@BXlmblBAAAAAAAAAAAAAAAAAAAAA" ), "got %s\n", str );
454 pCredFree( str );
456 memset( cert.rgbHashOfCert, 0xff, sizeof(cert.rgbHashOfCert) );
457 str = NULL;
458 ret = pCredMarshalCredentialA( CertCredential, &cert, &str );
459 ok( ret, "unexpected failure %u\n", GetLastError() );
460 ok( str != NULL, "str not set\n" );
461 ok( !lstrcmpA( str, "@@B--------------------------P" ), "got %s\n", str );
462 pCredFree( str );
464 username.UserName = NULL;
465 str = (char *)0xdeadbeef;
466 SetLastError( 0xdeadbeef );
467 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
468 error = GetLastError();
469 ok( !ret, "unexpected success\n" );
470 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
471 ok( str == (char *)0xdeadbeef, "got %p\n", str );
473 username.UserName = emptyW;
474 str = (char *)0xdeadbeef;
475 SetLastError( 0xdeadbeef );
476 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
477 error = GetLastError();
478 ok( !ret, "unexpected success\n" );
479 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
480 ok( str == (char *)0xdeadbeef, "got %p\n", str );
482 username.UserName = tW;
483 str = NULL;
484 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
485 ok( ret, "unexpected failure %u\n", GetLastError() );
486 ok( str != NULL, "str not set\n" );
487 ok( !lstrcmpA( str, "@@CCAAAAA0BA" ), "got %s\n", str );
488 pCredFree( str );
490 username.UserName = teW;
491 str = NULL;
492 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
493 ok( ret, "unexpected failure %u\n", GetLastError() );
494 ok( str != NULL, "str not set\n" );
495 ok( !lstrcmpA( str, "@@CEAAAAA0BQZAA" ), "got %s\n", str );
496 pCredFree( str );
498 username.UserName = tesW;
499 str = NULL;
500 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
501 ok( ret, "unexpected failure %u\n", GetLastError() );
502 ok( str != NULL, "str not set\n" );
503 ok( !lstrcmpA( str, "@@CGAAAAA0BQZAMHA" ), "got %s\n", str );
504 pCredFree( str );
506 username.UserName = testW;
507 str = NULL;
508 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
509 ok( ret, "unexpected failure %u\n", GetLastError() );
510 ok( str != NULL, "str not set\n" );
511 ok( !lstrcmpA( str, "@@CIAAAAA0BQZAMHA0BA" ), "got %s\n", str );
512 pCredFree( str );
514 username.UserName = test1W;
515 str = NULL;
516 ret = pCredMarshalCredentialA( UsernameTargetCredential, &username, &str );
517 ok( ret, "unexpected failure %u\n", GetLastError() );
518 ok( str != NULL, "str not set\n" );
519 ok( !lstrcmpA( str, "@@CKAAAAA0BQZAMHA0BQMAA" ), "got %s\n", str );
520 pCredFree( str );
523 static void test_CredUnmarshalCredentialA(void)
525 static WCHAR tW[] = {'t',0};
526 static WCHAR testW[] = {'t','e','s','t',0};
527 CERT_CREDENTIAL_INFO *cert;
528 USERNAME_TARGET_CREDENTIAL_INFO *username;
529 CRED_MARSHAL_TYPE type;
530 unsigned int i;
531 DWORD error;
532 BOOL ret;
534 SetLastError( 0xdeadbeef );
535 ret = pCredUnmarshalCredentialA( NULL, NULL, NULL );
536 error = GetLastError();
537 ok( !ret, "unexpected success\n" );
538 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
540 cert = NULL;
541 SetLastError( 0xdeadbeef );
542 ret = pCredUnmarshalCredentialA( NULL, NULL, (void **)&cert );
543 error = GetLastError();
544 ok( !ret, "unexpected success\n" );
545 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
547 type = 0;
548 cert = NULL;
549 SetLastError( 0xdeadbeef );
550 ret = pCredUnmarshalCredentialA( NULL, &type, (void **)&cert );
551 error = GetLastError();
552 ok( !ret, "unexpected success\n" );
553 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
555 type = 0;
556 cert = NULL;
557 SetLastError( 0xdeadbeef );
558 ret = pCredUnmarshalCredentialA( "", &type, (void **)&cert );
559 error = GetLastError();
560 ok( !ret, "unexpected success\n" );
561 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
563 if (0) { /* crash */
564 SetLastError( 0xdeadbeef );
565 ret = pCredUnmarshalCredentialA( "@@BAAAAAAAAAAAAAAAAAAAAAAAAAAA", &type, NULL );
566 error = GetLastError();
567 ok( !ret, "unexpected success\n" );
568 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
570 SetLastError( 0xdeadbeef );
571 ret = pCredUnmarshalCredentialA( "@@BAAAAAAAAAAAAAAAAAAAAAAAAAAA", NULL, (void **)&cert );
572 error = GetLastError();
573 ok( !ret, "unexpected success\n" );
574 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
577 type = 0;
578 cert = NULL;
579 ret = pCredUnmarshalCredentialA( "@@BAAAAAAAAAAAAAAAAAAAAAAAAAAA", &type, (void **)&cert );
580 ok( ret, "unexpected failure %u\n", GetLastError() );
581 ok( type == CertCredential, "got %u\n", type );
582 ok( cert != NULL, "cert is NULL\n" );
583 ok( cert->cbSize == sizeof(*cert), "wrong size %u\n", cert->cbSize );
584 for (i = 0; i < sizeof(cert->rgbHashOfCert); i++) ok( !cert->rgbHashOfCert[i], "wrong data\n" );
585 pCredFree( cert );
587 type = 0;
588 cert = NULL;
589 ret = pCredUnmarshalCredentialA( "@@BXlmblBAAAAAAAAAAAAAAAAAAAAA", &type, (void **)&cert );
590 ok( ret, "unexpected failure %u\n", GetLastError() );
591 ok( type == CertCredential, "got %u\n", type );
592 ok( cert != NULL, "cert is NULL\n" );
593 ok( cert->cbSize == sizeof(*cert), "wrong size %u\n", cert->cbSize );
594 ok( cert->rgbHashOfCert[0] == 'W', "wrong data)\n" );
595 ok( cert->rgbHashOfCert[1] == 'i', "wrong data\n" );
596 ok( cert->rgbHashOfCert[2] == 'n', "wrong data\n" );
597 ok( cert->rgbHashOfCert[3] == 'e', "wrong data\n" );
598 for (i = 4; i < sizeof(cert->rgbHashOfCert); i++) ok( !cert->rgbHashOfCert[i], "wrong data\n" );
599 pCredFree( cert );
601 SetLastError( 0xdeadbeef );
602 ret = pCredUnmarshalCredentialA( "@@CAAAAAA", &type, (void **)&username );
603 error = GetLastError();
604 ok( !ret, "unexpected success\n" );
605 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
607 SetLastError( 0xdeadbeef );
608 ret = pCredUnmarshalCredentialA( "@@CAAAAAA0BA", &type, (void **)&username );
609 error = GetLastError();
610 ok( !ret, "unexpected success\n" );
611 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
613 type = 0;
614 username = NULL;
615 ret = pCredUnmarshalCredentialA( "@@CCAAAAA0BA", &type, (void **)&username );
616 ok( ret, "unexpected failure %u\n", GetLastError() );
617 ok( type == UsernameTargetCredential, "got %u\n", type );
618 ok( username != NULL, "username is NULL\n" );
619 ok( username->UserName != NULL, "UserName is NULL\n" );
620 ok( !lstrcmpW( username->UserName, tW ), "got %s\n", wine_dbgstr_w(username->UserName) );
621 pCredFree( username );
623 type = 0;
624 username = NULL;
625 ret = pCredUnmarshalCredentialA( "@@CIAAAAA0BQZAMHA0BA", &type, (void **)&username );
626 ok( ret, "unexpected failure %u\n", GetLastError() );
627 ok( type == UsernameTargetCredential, "got %u\n", type );
628 ok( username != NULL, "username is NULL\n" );
629 ok( username->UserName != NULL, "UserName is NULL\n" );
630 ok( !lstrcmpW( username->UserName, testW ), "got %s\n", wine_dbgstr_w(username->UserName) );
631 pCredFree( username );
634 static void test_CredIsMarshaledCredentialA(void)
636 int i;
637 BOOL res;
638 BOOL expected = TRUE;
640 const char * ptr[] = {
641 /* CertCredential */
642 "@@BXlmblBAAAAAAAAAAAAAAAAAAAAA", /* hash for 'W','i','n','e' */
643 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAAA", /* hash for all 0 */
645 /* UsernameTargetCredential */
646 "@@CCAAAAA0BA", /* "t" */
647 "@@CIAAAAA0BQZAMHA0BA", /* "test" */
649 /* todo: BinaryBlobCredential */
651 /* not marshaled names return always FALSE */
652 "winetest",
654 "@@",
655 "@@A",
656 "@@AA",
657 "@@AAA",
658 "@@B",
659 "@@BB",
660 "@@BBB",
662 /* CertCredential */
663 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAAAA", /* to long */
664 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAA", /* to short */
665 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAA+", /* bad char */
666 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAA:",
667 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAA>",
668 "@@BAAAAAAAAAAAAAAAAAAAAAAAAAA<",
670 "@@C",
671 "@@CC",
672 "@@CCC",
673 "@@D",
674 "@@DD",
675 "@@DDD",
676 NULL};
678 for (i = 0; ptr[i]; i++)
680 if (*ptr[i] != '@')
681 expected = FALSE;
683 SetLastError(0xdeadbeef);
684 res = pCredIsMarshaledCredentialA(ptr[i]);
685 if (expected)
686 ok(res != FALSE, "%d: got %d and %u for %s (expected TRUE)\n", i, res, GetLastError(), ptr[i]);
687 else
689 /* Windows returns ERROR_INVALID_PARAMETER here, but that's not documented */
690 ok(!res, "%d: got %d and %u for %s (expected FALSE)\n", i, res, GetLastError(), ptr[i]);
695 START_TEST(cred)
697 DWORD persists[CRED_TYPE_MAXIMUM];
699 pCredEnumerateA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredEnumerateA");
700 pCredFree = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredFree");
701 pCredGetSessionTypes = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredGetSessionTypes");
702 pCredWriteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredWriteA");
703 pCredDeleteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredDeleteA");
704 pCredReadA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadA");
705 pCredRenameA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredRenameA");
706 pCredReadDomainCredentialsA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadDomainCredentialsA");
707 pCredMarshalCredentialA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredMarshalCredentialA");
708 pCredUnmarshalCredentialA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredUnmarshalCredentialA");
709 pCredIsMarshaledCredentialA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredIsMarshaledCredentialA");
711 if (!pCredEnumerateA || !pCredFree || !pCredWriteA || !pCredDeleteA || !pCredReadA)
713 win_skip("credentials functions not present in advapi32.dll\n");
714 return;
717 if (pCredGetSessionTypes)
719 BOOL ret;
720 DWORD i;
721 ret = pCredGetSessionTypes(CRED_TYPE_MAXIMUM, persists);
722 ok(ret, "CredGetSessionTypes failed with error %d\n", GetLastError());
723 ok(persists[0] == CRED_PERSIST_NONE, "persists[0] = %u instead of CRED_PERSIST_NONE\n", persists[0]);
724 for (i=0; i < CRED_TYPE_MAXIMUM; i++)
725 ok(persists[i] <= CRED_PERSIST_ENTERPRISE, "bad value for persists[%u]: %u\n", i, persists[i]);
727 else
728 memset(persists, CRED_PERSIST_ENTERPRISE, sizeof(persists));
730 test_CredReadA();
731 test_CredWriteA();
732 test_CredDeleteA();
734 test_CredReadDomainCredentialsA();
736 trace("generic:\n");
737 if (persists[CRED_TYPE_GENERIC] == CRED_PERSIST_NONE)
738 skip("CRED_TYPE_GENERIC credentials are not supported or are disabled. Skipping\n");
739 else
740 test_generic();
742 trace("domain password:\n");
743 if (persists[CRED_TYPE_DOMAIN_PASSWORD] == CRED_PERSIST_NONE)
744 skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported or are disabled. Skipping\n");
745 else
746 test_domain_password(CRED_TYPE_DOMAIN_PASSWORD);
748 trace("domain visible password:\n");
749 if (persists[CRED_TYPE_DOMAIN_VISIBLE_PASSWORD] == CRED_PERSIST_NONE)
750 skip("CRED_TYPE_DOMAIN_VISIBLE_PASSWORD credentials are not supported or are disabled. Skipping\n");
751 else
752 test_domain_password(CRED_TYPE_DOMAIN_VISIBLE_PASSWORD);
754 test_CredMarshalCredentialA();
755 test_CredUnmarshalCredentialA();
756 test_CredIsMarshaledCredentialA();