winex11: add owned windows to taskbar if owner is not mapped
[wine] / dlls / advapi32 / tests / cred.c
1 /*
2  * Credential Function Tests
3  *
4  * Copyright 2007 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincred.h"
27
28 #include "wine/test.h"
29
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
38 #define TEST_TARGET_NAME  "credtest.winehq.org"
39 #define TEST_TARGET_NAME2 "credtest2.winehq.org"
40 static const WCHAR TEST_PASSWORD[] = {'p','4','$','$','w','0','r','d','!',0};
41
42 static void test_CredReadA(void)
43 {
44     BOOL ret;
45     PCREDENTIALA cred;
46
47     SetLastError(0xdeadbeef);
48     ret = pCredReadA(TEST_TARGET_NAME, -1, 0, &cred);
49     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
50         "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
51         GetLastError());
52
53     SetLastError(0xdeadbeef);
54     ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef, &cred);
55     ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER ),
56         "CredReadA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
57         GetLastError());
58
59     SetLastError(0xdeadbeef);
60     ret = pCredReadA(NULL, CRED_TYPE_GENERIC, 0, &cred);
61     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
62         "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
63         GetLastError());
64 }
65
66 static void test_CredWriteA(void)
67 {
68     CREDENTIALA new_cred;
69     BOOL ret;
70
71     SetLastError(0xdeadbeef);
72     ret = pCredWriteA(NULL, 0);
73     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
74         "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
75         GetLastError());
76
77     new_cred.Flags = 0;
78     new_cred.Type = CRED_TYPE_GENERIC;
79     new_cred.TargetName = NULL;
80     new_cred.Comment = (char *)"Comment";
81     new_cred.CredentialBlobSize = 0;
82     new_cred.CredentialBlob = NULL;
83     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
84     new_cred.AttributeCount = 0;
85     new_cred.Attributes = NULL;
86     new_cred.TargetAlias = NULL;
87     new_cred.UserName = (char *)"winetest";
88
89     SetLastError(0xdeadbeef);
90     ret = pCredWriteA(&new_cred, 0);
91     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
92         "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
93         GetLastError());
94
95     new_cred.TargetName = (char *)TEST_TARGET_NAME;
96     new_cred.Type = CRED_TYPE_DOMAIN_PASSWORD;
97
98     SetLastError(0xdeadbeef);
99     ret = pCredWriteA(&new_cred, 0);
100     ok(!ret && ( GetLastError() == ERROR_BAD_USERNAME || GetLastError() == ERROR_NO_SUCH_LOGON_SESSION /* Vista */ ),
101         "CredWrite with username without domain should return ERROR_BAD_USERNAME or ERROR_NO_SUCH_LOGON_SESSION not %d\n", GetLastError());
102
103     new_cred.UserName = NULL;
104     SetLastError(0xdeadbeef);
105     ret = pCredWriteA(&new_cred, 0);
106     ok(!ret && GetLastError() == ERROR_BAD_USERNAME,
107         "CredWriteA with NULL username should have failed with ERROR_BAD_USERNAME instead of %d\n",
108         GetLastError());
109 }
110
111 static void test_CredDeleteA(void)
112 {
113     BOOL ret;
114
115     SetLastError(0xdeadbeef);
116     ret = pCredDeleteA(TEST_TARGET_NAME, -1, 0);
117     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
118         "CredDeleteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
119         GetLastError());
120
121     SetLastError(0xdeadbeef);
122     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef);
123     ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ),
124         "CredDeleteA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
125         GetLastError());
126 }
127
128 static void check_blob(int line, DWORD cred_type, PCREDENTIALA cred)
129 {
130     if (cred_type == CRED_TYPE_DOMAIN_PASSWORD)
131     {
132         todo_wine
133         ok_(__FILE__, line)(cred->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", cred->CredentialBlobSize);
134         todo_wine
135         ok_(__FILE__, line)(!cred->CredentialBlob, "expected NULL credentials but got %p\n", cred->CredentialBlob);
136     }
137     else
138     {
139         DWORD size=sizeof(TEST_PASSWORD);
140         ok_(__FILE__, line)(cred->CredentialBlobSize == size, "expected CredentialBlobSize of %u but got %u\n", size, cred->CredentialBlobSize);
141         ok_(__FILE__, line)(cred->CredentialBlob != NULL, "CredentialBlob should be present\n");
142         if (cred->CredentialBlob)
143             ok_(__FILE__, line)(!memcmp(cred->CredentialBlob, TEST_PASSWORD, size), "wrong CredentialBlob\n");
144     }
145 }
146
147 static void test_generic(void)
148 {
149     BOOL ret;
150     DWORD count, i;
151     PCREDENTIALA *creds;
152     CREDENTIALA new_cred;
153     PCREDENTIALA cred;
154     BOOL found = FALSE;
155
156     new_cred.Flags = 0;
157     new_cred.Type = CRED_TYPE_GENERIC;
158     new_cred.TargetName = (char *)TEST_TARGET_NAME;
159     new_cred.Comment = (char *)"Comment";
160     new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
161     new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
162     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
163     new_cred.AttributeCount = 0;
164     new_cred.Attributes = NULL;
165     new_cred.TargetAlias = NULL;
166     new_cred.UserName = (char *)"winetest";
167
168     ret = pCredWriteA(&new_cred, 0);
169     ok(ret, "CredWriteA failed with error %d\n", GetLastError());
170
171     ret = pCredEnumerateA(NULL, 0, &count, &creds);
172     ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
173
174     for (i = 0; i < count; i++)
175     {
176         if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
177         {
178             ok(creds[i]->Type == CRED_TYPE_GENERIC, "expected creds[%d]->Type CRED_TYPE_GENERIC but got %d\n", i, creds[i]->Type);
179             ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
180             ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
181             check_blob(__LINE__, CRED_TYPE_GENERIC, creds[i]);
182             ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
183             ok(!strcmp(creds[i]->UserName, "winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
184             found = TRUE;
185         }
186     }
187     pCredFree(creds);
188     ok(found, "credentials not found\n");
189
190     ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0, &cred);
191     ok(ret, "CredReadA failed with error %d\n", GetLastError());
192     pCredFree(cred);
193
194     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0);
195     ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
196 }
197
198 static void test_domain_password(DWORD cred_type)
199 {
200     BOOL ret;
201     DWORD count, i;
202     PCREDENTIALA *creds;
203     CREDENTIALA new_cred;
204     PCREDENTIALA cred;
205     BOOL found = FALSE;
206
207     new_cred.Flags = 0;
208     new_cred.Type = cred_type;
209     new_cred.TargetName = (char *)TEST_TARGET_NAME;
210     new_cred.Comment = (char *)"Comment";
211     new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
212     new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
213     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
214     new_cred.AttributeCount = 0;
215     new_cred.Attributes = NULL;
216     new_cred.TargetAlias = NULL;
217     new_cred.UserName = (char *)"test\\winetest";
218     ret = pCredWriteA(&new_cred, 0);
219     ok(ret, "CredWriteA failed with error %d\n", GetLastError());
220
221     ret = pCredEnumerateA(NULL, 0, &count, &creds);
222     ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
223
224     for (i = 0; i < count; i++)
225     {
226         if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
227         {
228             ok(creds[i]->Type == cred_type, "expected creds[%d]->Type CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
229             ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
230             ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
231             check_blob(__LINE__, cred_type, creds[i]);
232             ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
233             ok(!strcmp(creds[i]->UserName, "test\\winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
234             found = TRUE;
235         }
236     }
237     pCredFree(creds);
238     ok(found, "credentials not found\n");
239
240     ret = pCredReadA(TEST_TARGET_NAME, cred_type, 0, &cred);
241     ok(ret, "CredReadA failed with error %d\n", GetLastError());
242     if (ret)  /* don't check the values of cred, if CredReadA failed. */
243     {
244         check_blob(__LINE__, cred_type, cred);
245         pCredFree(cred);
246     }
247
248     ret = pCredDeleteA(TEST_TARGET_NAME, cred_type, 0);
249     ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
250 }
251
252 START_TEST(cred)
253 {
254     DWORD persists[CRED_TYPE_MAXIMUM];
255
256     pCredEnumerateA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredEnumerateA");
257     pCredFree = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredFree");
258     pCredGetSessionTypes = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredGetSessionTypes");
259     pCredWriteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredWriteA");
260     pCredDeleteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredDeleteA");
261     pCredReadA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadA");
262     pCredRenameA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredRenameA");
263
264     if (!pCredEnumerateA || !pCredFree || !pCredWriteA || !pCredDeleteA ||
265         !pCredReadA)
266     {
267         skip("credentials functions not present in advapi32.dll\n");
268         return;
269     }
270
271     if (pCredGetSessionTypes)
272     {
273         BOOL ret;
274         DWORD i;
275         ret = pCredGetSessionTypes(CRED_TYPE_MAXIMUM, persists);
276         ok(ret, "CredGetSessionTypes failed with error %d\n", GetLastError());
277         ok(persists[0] == CRED_PERSIST_NONE, "persists[0] = %u instead of CRED_PERSIST_NONE\n", persists[0]);
278         for (i=0; i < CRED_TYPE_MAXIMUM; i++)
279             ok(persists[i] <= CRED_PERSIST_ENTERPRISE, "bad value for persists[%u]: %u\n", i, persists[i]);
280     }
281     else
282         memset(persists, CRED_PERSIST_ENTERPRISE, sizeof(persists));
283
284     test_CredReadA();
285     test_CredWriteA();
286     test_CredDeleteA();
287
288     trace("generic:\n");
289     if (persists[CRED_TYPE_GENERIC] == CRED_PERSIST_NONE)
290         skip("CRED_TYPE_GENERIC credentials are not supported or are disabled. Skipping\n");
291     else
292         test_generic();
293
294         trace("domain password:\n");
295     if (persists[CRED_TYPE_DOMAIN_PASSWORD] == CRED_PERSIST_NONE)
296         skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported or are disabled. Skipping\n");
297     else
298         test_domain_password(CRED_TYPE_DOMAIN_PASSWORD);
299
300     trace("domain visible password:\n");
301     if (persists[CRED_TYPE_DOMAIN_VISIBLE_PASSWORD] == CRED_PERSIST_NONE)
302         skip("CRED_TYPE_DOMAIN_VISIBLE_PASSWORD credentials are not supported or are disabled. Skipping\n");
303     else
304         test_domain_password(CRED_TYPE_DOMAIN_VISIBLE_PASSWORD);
305 }