1 /*
2  * Credentials User Interface Tests
3  *
4  * Copyright 2007 Robert Shearman for CodeWeavers
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 
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wincred.h"
26 #include "sspi.h"
27 
28 #include "wine/test.h"
29 
30 static SECURITY_STATUS (SEC_ENTRY *pSspiEncodeAuthIdentityAsStrings)
31     (PSEC_WINNT_AUTH_IDENTITY_OPAQUE,PCWSTR*,PCWSTR*,PCWSTR*);
32 static void (SEC_ENTRY *pSspiFreeAuthIdentity)
33     (PSEC_WINNT_AUTH_IDENTITY_OPAQUE);
34 static ULONG (SEC_ENTRY *pSspiPromptForCredentialsW)
35     (PCWSTR,void*,ULONG,PCWSTR,PSEC_WINNT_AUTH_IDENTITY_OPAQUE,PSEC_WINNT_AUTH_IDENTITY_OPAQUE*,int*,ULONG);
36 
37 static void test_CredUIPromptForCredentials(void)
38 {
39     static const WCHAR wszServerName[] = {'W','i','n','e','T','e','s','t',0};
40     DWORD ret;
41     WCHAR username[256];
42     WCHAR password[256];
43     CREDUI_INFOW credui_info;
44     BOOL save = FALSE;
45 
46     credui_info.cbSize = sizeof(credui_info);
47     credui_info.hwndParent = NULL;
48     credui_info.pszMessageText = NULL;
49     credui_info.hbmBanner = NULL;
50 
51     ret = CredUIConfirmCredentialsW(NULL, TRUE);
52     ok(ret == ERROR_INVALID_PARAMETER /* 2003 + */ || ret == ERROR_NOT_FOUND /* XP */,
53         "CredUIConfirmCredentials should have returned ERROR_INVALID_PARAMETER or ERROR_NOT_FOUND instead of %d\n", ret);
54 
55     ret = CredUIConfirmCredentialsW(wszServerName, TRUE);
56     ok(ret == ERROR_NOT_FOUND, "CredUIConfirmCredentials should have returned ERROR_NOT_FOUND instead of %d\n", ret);
57 
58     username[0] = '\0';
59     password[0] = '\0';
60     ret = CredUIPromptForCredentialsW(NULL, NULL, NULL, 0, username,
61                                       sizeof(username)/sizeof(username[0]),
62                                       password, sizeof(password)/sizeof(password[0]),
63                                       NULL, CREDUI_FLAGS_ALWAYS_SHOW_UI);
64     ok(ret == ERROR_INVALID_FLAGS, "CredUIPromptForCredentials should have returned ERROR_INVALID_FLAGS instead of %d\n", ret);
65 
66     ret = CredUIPromptForCredentialsW(NULL, NULL, NULL, 0, username,
67                                       sizeof(username)/sizeof(username[0]),
68                                       password, sizeof(password)/sizeof(password[0]),
69                                       NULL, CREDUI_FLAGS_ALWAYS_SHOW_UI | CREDUI_FLAGS_GENERIC_CREDENTIALS);
70     ok(ret == ERROR_INVALID_PARAMETER, "CredUIPromptForCredentials should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
71 
72     ret = CredUIPromptForCredentialsW(NULL, wszServerName, NULL, 0, username,
73                                       sizeof(username)/sizeof(username[0]),
74                                       password, sizeof(password)/sizeof(password[0]),
75                                       NULL, CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX);
76     ok(ret == ERROR_INVALID_PARAMETER, "CredUIPromptForCredentials should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
77 
78     if (winetest_interactive)
79     {
80         static const WCHAR wszCaption1[] = {'C','R','E','D','U','I','_','F','L','A','G','S','_','E','X','P','E','C','T','_','C','O','N','F','I','R','M','A','T','I','O','N',0};
81         static const WCHAR wszCaption2[] = {'C','R','E','D','U','I','_','F','L','A','G','S','_','I','N','C','O','R','R','E','C','T','_','P','A','S','S','W','O','R','D','|',
82             'C','R','E','D','U','I','_','F','L','A','G','S','_','E','X','P','E','C','T','_','C','O','N','F','I','R','M','A','T','I','O','N',0};
83         static const WCHAR wszCaption3[] = {'C','R','E','D','U','I','_','F','L','A','G','S','_','D','O','_','N','O','T','_','P','E','R','S','I','S','T','|',
84             'C','R','E','D','U','I','_','F','L','A','G','S','_','E','X','P','E','C','T','_','C','O','N','F','I','R','M','A','T','I','O','N',0};
85         static const WCHAR wszCaption4[] = {'C','R','E','D','U','I','_','F','L','A','G','S','_','P','E','R','S','I','S','T','|',
86             'C','R','E','D','U','I','_','F','L','A','G','S','_','E','X','P','E','C','T','_','C','O','N','F','I','R','M','A','T','I','O','N',0};
87 
88         ret = CredUIPromptForCredentialsW(NULL, wszServerName, NULL, 0, username,
89                                           sizeof(username)/sizeof(username[0]),
90                                           password, sizeof(password)/sizeof(password[0]),
91                                           &save, CREDUI_FLAGS_EXPECT_CONFIRMATION);
92         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
93         if (ret == ERROR_SUCCESS)
94         {
95             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
96             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
97         }
98 
99         credui_info.pszCaptionText = wszCaption1;
100         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL,
101                                           ERROR_ACCESS_DENIED,
102                                           username, sizeof(username)/sizeof(username[0]),
103                                           password, sizeof(password)/sizeof(password[0]),
104                                           &save, CREDUI_FLAGS_EXPECT_CONFIRMATION);
105         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
106         if (ret == ERROR_SUCCESS)
107         {
108             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
109             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
110         }
111 
112         credui_info.pszCaptionText = wszCaption2;
113         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL, 0,
114                                           username, sizeof(username)/sizeof(username[0]),
115                                           password, sizeof(password)/sizeof(password[0]),
116                                           NULL, CREDUI_FLAGS_INCORRECT_PASSWORD|CREDUI_FLAGS_EXPECT_CONFIRMATION);
117         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
118         if (ret == ERROR_SUCCESS)
119         {
120             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
121             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
122         }
123 
124 
125         save = TRUE;
126         credui_info.pszCaptionText = wszCaption3;
127         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL, 0,
128                                           username, sizeof(username)/sizeof(username[0]),
129                                           password, sizeof(password)/sizeof(password[0]),
130                                           &save, CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_EXPECT_CONFIRMATION);
131         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
132         ok(save, "save flag should have been untouched\n");
133 
134         save = FALSE;
135         credui_info.pszCaptionText = wszCaption4;
136         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL, 0,
137                                           username, sizeof(username)/sizeof(username[0]),
138                                           password, sizeof(password)/sizeof(password[0]),
139                                           &save, CREDUI_FLAGS_PERSIST|CREDUI_FLAGS_EXPECT_CONFIRMATION);
140         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
141         ok(!save, "save flag should have been untouched\n");
142         if (ret == ERROR_SUCCESS)
143         {
144             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
145             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
146         }
147 
148     }
149 }
150 
151 static void test_SspiPromptForCredentials(void)
152 {
153     static const WCHAR targetW[] = {'S','s','p','i','T','e','s','t',0};
154     static const WCHAR basicW[] = {'b','a','s','i','c',0};
155     ULONG ret;
156     SECURITY_STATUS status;
157     CREDUI_INFOW info;
158     PSEC_WINNT_AUTH_IDENTITY_OPAQUE id;
159     const WCHAR *username, *domain, *creds;
160     int save;
161 
162     if (!pSspiPromptForCredentialsW || !pSspiFreeAuthIdentity)
163     {
164         win_skip( "SspiPromptForCredentialsW is missing\n" );
165         return;
166     }
167 
168     info.cbSize         = sizeof(info);
169     info.hwndParent     = NULL;
170     info.pszMessageText = targetW;
171     info.pszCaptionText = basicW;
172     info.hbmBanner      = NULL;
173     ret = pSspiPromptForCredentialsW( NULL, &info, 0, basicW, NULL, &id, &save, 0 );
174     ok( ret == ERROR_INVALID_PARAMETER, "got %u\n", ret );
175 
176     ret = pSspiPromptForCredentialsW( targetW, &info, 0, NULL, NULL, &id, &save, 0 );
177     ok( ret == ERROR_NO_SUCH_PACKAGE, "got %u\n", ret );
178 
179     if (winetest_interactive)
180     {
181         id = NULL;
182         save = -1;
183         ret = pSspiPromptForCredentialsW( targetW, &info, 0, basicW, NULL, &id, &save, 0 );
184         ok( ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "got %u\n", ret );
185         if (ret == ERROR_SUCCESS)
186         {
187             ok( id != NULL, "id not set\n" );
188             ok( save == TRUE || save == FALSE, "got %d\n", save );
189 
190             username = creds = NULL;
191             domain = (const WCHAR *)0xdeadbeef;
192             status = pSspiEncodeAuthIdentityAsStrings( id, &username, &domain, &creds );
193             ok( status == SEC_E_OK, "got %u\n", status );
194             ok( username != NULL, "username not set\n" );
195             ok( domain == NULL, "domain not set\n" );
196             ok( creds != NULL, "creds not set\n" );
197             pSspiFreeAuthIdentity( id );
198         }
199     }
200 }
201 
202 START_TEST(credui)
203 {
204     HMODULE hcredui = GetModuleHandleA( "credui.dll" ), hsecur32 = LoadLibraryA( "secur32.dll" );
205 
206     if (hcredui)
207         pSspiPromptForCredentialsW = (void *)GetProcAddress( hcredui, "SspiPromptForCredentialsW" );
208     if (hsecur32)
209     {
210         pSspiEncodeAuthIdentityAsStrings = (void *)GetProcAddress( hsecur32, "SspiEncodeAuthIdentityAsStrings" );
211         pSspiFreeAuthIdentity = (void *)GetProcAddress( hsecur32, "SspiFreeAuthIdentity" );
212     }
213 
214     test_CredUIPromptForCredentials();
215     test_SspiPromptForCredentials();
216 
217     FreeLibrary( hsecur32 );
218 }
219