comctl32: Add implementation of LVS_EX_ONECLICKACTIVATE.
[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     if (ret)
101     {
102         /* Vista */
103         ok(GetLastError() == ERROR_IO_PENDING,
104            "Expected ERROR_IO_PENDING, got %d\n", GetLastError());
105     }
106     else
107     {
108         ok(GetLastError() == ERROR_BAD_USERNAME ||
109            GetLastError() == ERROR_NO_SUCH_LOGON_SESSION, /* Vista */
110            "CredWrite with username without domain should return ERROR_BAD_USERNAME"
111            "or ERROR_NO_SUCH_LOGON_SESSION not %d\n", GetLastError());
112     }
113
114     new_cred.UserName = NULL;
115     SetLastError(0xdeadbeef);
116     ret = pCredWriteA(&new_cred, 0);
117     ok(!ret && GetLastError() == ERROR_BAD_USERNAME,
118         "CredWriteA with NULL username should have failed with ERROR_BAD_USERNAME instead of %d\n",
119         GetLastError());
120 }
121
122 static void test_CredDeleteA(void)
123 {
124     BOOL ret;
125
126     SetLastError(0xdeadbeef);
127     ret = pCredDeleteA(TEST_TARGET_NAME, -1, 0);
128     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
129         "CredDeleteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
130         GetLastError());
131
132     SetLastError(0xdeadbeef);
133     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef);
134     ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ),
135         "CredDeleteA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
136         GetLastError());
137 }
138
139 static void check_blob(int line, DWORD cred_type, PCREDENTIALA cred)
140 {
141     if (cred_type == CRED_TYPE_DOMAIN_PASSWORD)
142     {
143         todo_wine
144         ok_(__FILE__, line)(cred->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", cred->CredentialBlobSize);
145         todo_wine
146         ok_(__FILE__, line)(!cred->CredentialBlob, "expected NULL credentials but got %p\n", cred->CredentialBlob);
147     }
148     else
149     {
150         DWORD size=sizeof(TEST_PASSWORD);
151         ok_(__FILE__, line)(cred->CredentialBlobSize == size, "expected CredentialBlobSize of %u but got %u\n", size, cred->CredentialBlobSize);
152         ok_(__FILE__, line)(cred->CredentialBlob != NULL, "CredentialBlob should be present\n");
153         if (cred->CredentialBlob)
154             ok_(__FILE__, line)(!memcmp(cred->CredentialBlob, TEST_PASSWORD, size), "wrong CredentialBlob\n");
155     }
156 }
157
158 static void test_generic(void)
159 {
160     BOOL ret;
161     DWORD count, i;
162     PCREDENTIALA *creds;
163     CREDENTIALA new_cred;
164     PCREDENTIALA cred;
165     BOOL found = FALSE;
166
167     new_cred.Flags = 0;
168     new_cred.Type = CRED_TYPE_GENERIC;
169     new_cred.TargetName = (char *)TEST_TARGET_NAME;
170     new_cred.Comment = (char *)"Comment";
171     new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
172     new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
173     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
174     new_cred.AttributeCount = 0;
175     new_cred.Attributes = NULL;
176     new_cred.TargetAlias = NULL;
177     new_cred.UserName = (char *)"winetest";
178
179     ret = pCredWriteA(&new_cred, 0);
180     ok(ret, "CredWriteA failed with error %d\n", GetLastError());
181
182     ret = pCredEnumerateA(NULL, 0, &count, &creds);
183     ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
184
185     for (i = 0; i < count; i++)
186     {
187         if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
188         {
189             ok(creds[i]->Type == CRED_TYPE_GENERIC ||
190                creds[i]->Type == CRED_TYPE_DOMAIN_PASSWORD, /* Vista */
191                "expected creds[%d]->Type CRED_TYPE_GENERIC or CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
192             ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
193             ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
194             check_blob(__LINE__, creds[i]->Type, creds[i]);
195             ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
196             ok(!strcmp(creds[i]->UserName, "winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
197             found = TRUE;
198         }
199     }
200     pCredFree(creds);
201     ok(found, "credentials not found\n");
202
203     ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0, &cred);
204     ok(ret, "CredReadA failed with error %d\n", GetLastError());
205     pCredFree(cred);
206
207     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0);
208     ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
209 }
210
211 static void test_domain_password(DWORD cred_type)
212 {
213     BOOL ret;
214     DWORD count, i;
215     PCREDENTIALA *creds;
216     CREDENTIALA new_cred;
217     PCREDENTIALA cred;
218     BOOL found = FALSE;
219
220     new_cred.Flags = 0;
221     new_cred.Type = cred_type;
222     new_cred.TargetName = (char *)TEST_TARGET_NAME;
223     new_cred.Comment = (char *)"Comment";
224     new_cred.CredentialBlobSize = sizeof(TEST_PASSWORD);
225     new_cred.CredentialBlob = (LPBYTE)TEST_PASSWORD;
226     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
227     new_cred.AttributeCount = 0;
228     new_cred.Attributes = NULL;
229     new_cred.TargetAlias = NULL;
230     new_cred.UserName = (char *)"test\\winetest";
231     ret = pCredWriteA(&new_cred, 0);
232     if (!ret && GetLastError() == ERROR_NO_SUCH_LOGON_SESSION)
233     {
234         skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported "
235              "or are disabled. Skipping\n");
236         return;
237     }
238     ok(ret, "CredWriteA failed with error %d\n", GetLastError());
239
240     ret = pCredEnumerateA(NULL, 0, &count, &creds);
241     ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
242
243     for (i = 0; i < count; i++)
244     {
245         if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
246         {
247             ok(creds[i]->Type == cred_type, "expected creds[%d]->Type CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
248             ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
249             ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
250             check_blob(__LINE__, cred_type, creds[i]);
251             ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
252             ok(!strcmp(creds[i]->UserName, "test\\winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
253             found = TRUE;
254         }
255     }
256     pCredFree(creds);
257     ok(found, "credentials not found\n");
258
259     ret = pCredReadA(TEST_TARGET_NAME, cred_type, 0, &cred);
260     ok(ret, "CredReadA failed with error %d\n", GetLastError());
261     if (ret)  /* don't check the values of cred, if CredReadA failed. */
262     {
263         check_blob(__LINE__, cred_type, cred);
264         pCredFree(cred);
265     }
266
267     ret = pCredDeleteA(TEST_TARGET_NAME, cred_type, 0);
268     ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
269 }
270
271 START_TEST(cred)
272 {
273     DWORD persists[CRED_TYPE_MAXIMUM];
274
275     pCredEnumerateA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredEnumerateA");
276     pCredFree = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredFree");
277     pCredGetSessionTypes = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredGetSessionTypes");
278     pCredWriteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredWriteA");
279     pCredDeleteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredDeleteA");
280     pCredReadA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadA");
281     pCredRenameA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredRenameA");
282
283     if (!pCredEnumerateA || !pCredFree || !pCredWriteA || !pCredDeleteA ||
284         !pCredReadA)
285     {
286         skip("credentials functions not present in advapi32.dll\n");
287         return;
288     }
289
290     if (pCredGetSessionTypes)
291     {
292         BOOL ret;
293         DWORD i;
294         ret = pCredGetSessionTypes(CRED_TYPE_MAXIMUM, persists);
295         ok(ret, "CredGetSessionTypes failed with error %d\n", GetLastError());
296         ok(persists[0] == CRED_PERSIST_NONE, "persists[0] = %u instead of CRED_PERSIST_NONE\n", persists[0]);
297         for (i=0; i < CRED_TYPE_MAXIMUM; i++)
298             ok(persists[i] <= CRED_PERSIST_ENTERPRISE, "bad value for persists[%u]: %u\n", i, persists[i]);
299     }
300     else
301         memset(persists, CRED_PERSIST_ENTERPRISE, sizeof(persists));
302
303     test_CredReadA();
304     test_CredWriteA();
305     test_CredDeleteA();
306
307     trace("generic:\n");
308     if (persists[CRED_TYPE_GENERIC] == CRED_PERSIST_NONE)
309         skip("CRED_TYPE_GENERIC credentials are not supported or are disabled. Skipping\n");
310     else
311         test_generic();
312
313         trace("domain password:\n");
314     if (persists[CRED_TYPE_DOMAIN_PASSWORD] == CRED_PERSIST_NONE)
315         skip("CRED_TYPE_DOMAIN_PASSWORD credentials are not supported or are disabled. Skipping\n");
316     else
317         test_domain_password(CRED_TYPE_DOMAIN_PASSWORD);
318
319     trace("domain visible password:\n");
320     if (persists[CRED_TYPE_DOMAIN_VISIBLE_PASSWORD] == CRED_PERSIST_NONE)
321         skip("CRED_TYPE_DOMAIN_VISIBLE_PASSWORD credentials are not supported or are disabled. Skipping\n");
322     else
323         test_domain_password(CRED_TYPE_DOMAIN_VISIBLE_PASSWORD);
324 }