urlmon: Don't create stgmed_obj for binding to object.
[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 *pCredReadA)(LPCSTR,DWORD,DWORD,PCREDENTIALA *);
34 static BOOL (WINAPI *pCredRenameA)(LPCSTR,LPCSTR,DWORD,DWORD);
35 static BOOL (WINAPI *pCredWriteA)(PCREDENTIALA,DWORD);
36
37 #define TEST_TARGET_NAME  "credtest.winehq.org"
38 #define TEST_TARGET_NAME2 "credtest2.winehq.org"
39
40 static void test_CredReadA(void)
41 {
42     BOOL ret;
43     PCREDENTIALA cred;
44
45     SetLastError(0xdeadbeef);
46     ret = pCredReadA(TEST_TARGET_NAME, -1, 0, &cred);
47     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
48         "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
49         GetLastError());
50
51     SetLastError(0xdeadbeef);
52     ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef, &cred);
53     ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER ),
54         "CredReadA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
55         GetLastError());
56
57     SetLastError(0xdeadbeef);
58     ret = pCredReadA(NULL, CRED_TYPE_GENERIC, 0, &cred);
59     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
60         "CredReadA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
61         GetLastError());
62 }
63
64 static void test_CredWriteA(void)
65 {
66     CREDENTIALA new_cred;
67     BOOL ret;
68
69     SetLastError(0xdeadbeef);
70     ret = pCredWriteA(NULL, 0);
71     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
72         "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
73         GetLastError());
74
75     new_cred.Flags = 0;
76     new_cred.Type = CRED_TYPE_GENERIC;
77     new_cred.TargetName = NULL;
78     new_cred.Comment = (char *)"Comment";
79     new_cred.CredentialBlobSize = 0;
80     new_cred.CredentialBlob = NULL;
81     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
82     new_cred.AttributeCount = 0;
83     new_cred.Attributes = NULL;
84     new_cred.TargetAlias = NULL;
85     new_cred.UserName = (char *)"winetest";
86
87     SetLastError(0xdeadbeef);
88     ret = pCredWriteA(&new_cred, 0);
89     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
90         "CredWriteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
91         GetLastError());
92
93     new_cred.TargetName = (char *)TEST_TARGET_NAME;
94     new_cred.Type = CRED_TYPE_DOMAIN_PASSWORD;
95
96     SetLastError(0xdeadbeef);
97     ret = pCredWriteA(&new_cred, 0);
98     ok(!ret && ( GetLastError() == ERROR_BAD_USERNAME || GetLastError() == ERROR_NO_SUCH_LOGON_SESSION /* Vista */ ),
99         "CredWrite with username without domain should return ERROR_BAD_USERNAME or ERROR_NO_SUCH_LOGON_SESSION not %d\n", GetLastError());
100
101     new_cred.UserName = NULL;
102     SetLastError(0xdeadbeef);
103     ret = pCredWriteA(&new_cred, 0);
104     ok(!ret && GetLastError() == ERROR_BAD_USERNAME,
105         "CredWriteA with NULL username should have failed with ERROR_BAD_USERNAME instead of %d\n",
106         GetLastError());
107 }
108
109 static void test_CredDeleteA(void)
110 {
111     BOOL ret;
112
113     SetLastError(0xdeadbeef);
114     ret = pCredDeleteA(TEST_TARGET_NAME, -1, 0);
115     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
116         "CredDeleteA should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
117         GetLastError());
118
119     SetLastError(0xdeadbeef);
120     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0xdeadbeef);
121     ok(!ret && ( GetLastError() == ERROR_INVALID_FLAGS || GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ),
122         "CredDeleteA should have failed with ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER instead of %d\n",
123         GetLastError());
124 }
125
126 static void test_generic(void)
127 {
128     BOOL ret;
129     DWORD count, i;
130     PCREDENTIALA *creds;
131     CREDENTIALA new_cred;
132     PCREDENTIALA cred;
133     static const WCHAR password[] = {'p','4','$','$','w','0','r','d','!',0};
134     BOOL found = FALSE;
135
136     new_cred.Flags = 0;
137     new_cred.Type = CRED_TYPE_GENERIC;
138     new_cred.TargetName = (char *)TEST_TARGET_NAME;
139     new_cred.Comment = (char *)"Comment";
140     new_cred.CredentialBlobSize = sizeof(password);
141     new_cred.CredentialBlob = (LPBYTE)password;
142     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
143     new_cred.AttributeCount = 0;
144     new_cred.Attributes = NULL;
145     new_cred.TargetAlias = NULL;
146     new_cred.UserName = (char *)"winetest";
147
148     ret = pCredWriteA(&new_cred, 0);
149     ok(ret, "CredWriteA failed with error %d\n", GetLastError());
150
151     ret = pCredEnumerateA(NULL, 0, &count, &creds);
152     ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
153
154     for (i = 0; i < count; i++)
155     {
156         if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
157         {
158             ok(creds[i]->Type == CRED_TYPE_GENERIC, "expected creds[%d]->Type CRED_TYPE_GENERIC but got %d\n", i, creds[i]->Type);
159             ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
160             ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
161             ok(creds[i]->CredentialBlobSize == sizeof(password), "wrong CredentialBlobSize %d\n", creds[i]->CredentialBlobSize);
162             ok(creds[i]->CredentialBlob != NULL, "CredentialBlob should be present\n");
163             if (creds[i]->CredentialBlob)
164                 ok(!memcmp(creds[i]->CredentialBlob, password, sizeof(password)), "credentials don't match\n");
165             ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
166             ok(!strcmp(creds[i]->UserName, "winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
167             found = TRUE;
168         }
169     }
170     pCredFree(creds);
171     ok(found, "credentials not found\n");
172
173     ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0, &cred);
174     ok(ret, "CredReadA failed with error %d\n", GetLastError());
175     pCredFree(cred);
176
177     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_GENERIC, 0);
178     ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
179 }
180
181 static void test_domain_password(void)
182 {
183     BOOL ret;
184     DWORD count, i;
185     PCREDENTIALA *creds;
186     CREDENTIALA new_cred;
187     PCREDENTIALA cred;
188     static const WCHAR password[] = {'p','4','$','$','w','0','r','d','!',0};
189     BOOL found = FALSE;
190
191     new_cred.Flags = 0;
192     new_cred.Type = CRED_TYPE_DOMAIN_PASSWORD;
193     new_cred.TargetName = (char *)TEST_TARGET_NAME;
194     new_cred.Comment = (char *)"Comment";
195     new_cred.CredentialBlobSize = sizeof(password);
196     new_cred.CredentialBlob = (LPBYTE)password;
197     new_cred.Persist = CRED_PERSIST_ENTERPRISE;
198     new_cred.AttributeCount = 0;
199     new_cred.Attributes = NULL;
200     new_cred.TargetAlias = NULL;
201     new_cred.UserName = (char *)"test\\winetest";
202     ret = pCredWriteA(&new_cred, 0);
203     ok(ret, "CredWriteA failed with error %d\n", GetLastError());
204
205     ret = pCredEnumerateA(NULL, 0, &count, &creds);
206     ok(ret, "CredEnumerateA failed with error %d\n", GetLastError());
207
208     for (i = 0; i < count; i++)
209     {
210         if (!strcmp(creds[i]->TargetName, TEST_TARGET_NAME))
211         {
212             ok(creds[i]->Type == CRED_TYPE_DOMAIN_PASSWORD, "expected creds[%d]->Type CRED_TYPE_DOMAIN_PASSWORD but got %d\n", i, creds[i]->Type);
213             ok(!creds[i]->Flags, "expected creds[%d]->Flags 0 but got 0x%x\n", i, creds[i]->Flags);
214             ok(!strcmp(creds[i]->Comment, "Comment"), "expected creds[%d]->Comment \"Comment\" but got \"%s\"\n", i, creds[i]->Comment);
215             todo_wine
216             ok(creds[i]->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", creds[i]->CredentialBlobSize);
217             todo_wine
218             ok(!creds[i]->CredentialBlob, "expected NULL credentials but got %p\n", creds[i]->CredentialBlob);
219             ok(creds[i]->Persist, "expected creds[%d]->Persist CRED_PERSIST_ENTERPRISE but got %d\n", i, creds[i]->Persist);
220             ok(!strcmp(creds[i]->UserName, "test\\winetest"), "expected creds[%d]->UserName \"winetest\" but got \"%s\"\n", i, creds[i]->UserName);
221             found = TRUE;
222         }
223     }
224     pCredFree(creds);
225     ok(found, "credentials not found\n");
226
227     ret = pCredReadA(TEST_TARGET_NAME, CRED_TYPE_DOMAIN_PASSWORD, 0, &cred);
228     ok(ret, "CredReadA failed with error %d\n", GetLastError());
229     if (ret)  /* don't check the values of cred, if CredReadA failed. */
230     {
231         todo_wine
232         ok(cred->CredentialBlobSize == 0, "expected CredentialBlobSize of 0 but got %d\n", cred->CredentialBlobSize);
233         todo_wine
234         ok(!cred->CredentialBlob, "expected NULL credentials but got %p\n", cred->CredentialBlob);
235         pCredFree(cred);
236     }
237
238     ret = pCredDeleteA(TEST_TARGET_NAME, CRED_TYPE_DOMAIN_PASSWORD, 0);
239     ok(ret, "CredDeleteA failed with error %d\n", GetLastError());
240 }
241
242 START_TEST(cred)
243 {
244     pCredEnumerateA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredEnumerateA");
245     pCredFree = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredFree");
246     pCredWriteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredWriteA");
247     pCredDeleteA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredDeleteA");
248     pCredReadA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredReadA");
249     pCredRenameA = (void *)GetProcAddress(GetModuleHandle("advapi32.dll"), "CredRenameA");
250
251     if (!pCredEnumerateA || !pCredFree || !pCredWriteA || !pCredDeleteA ||
252         !pCredReadA)
253     {
254         skip("credentials functions not present in advapi32.dll\n");
255         return;
256     }
257
258     test_CredReadA();
259     test_CredWriteA();
260     test_CredDeleteA();
261
262     trace("generic:\n");
263     test_generic();
264     trace("domain password:\n");
265     test_domain_password();
266 }