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                                       ARRAY_SIZE(username),
62                                       password, ARRAY_SIZE(password),
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                                       ARRAY_SIZE(username),
68                                       password, ARRAY_SIZE(password),
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                                       ARRAY_SIZE(username),
74                                       password, ARRAY_SIZE(password),
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                                           ARRAY_SIZE(username),
90                                           password, ARRAY_SIZE(password),
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, ERROR_ACCESS_DENIED,
101                                           username, ARRAY_SIZE(username),
102                                           password, ARRAY_SIZE(password),
103                                           &save, CREDUI_FLAGS_EXPECT_CONFIRMATION);
104         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
105         if (ret == ERROR_SUCCESS)
106         {
107             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
108             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
109         }
110 
111         credui_info.pszCaptionText = wszCaption2;
112         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL, 0,
113                                           username, ARRAY_SIZE(username),
114                                           password, ARRAY_SIZE(password),
115                                           NULL, CREDUI_FLAGS_INCORRECT_PASSWORD|CREDUI_FLAGS_EXPECT_CONFIRMATION);
116         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
117         if (ret == ERROR_SUCCESS)
118         {
119             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
120             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
121         }
122 
123 
124         save = TRUE;
125         credui_info.pszCaptionText = wszCaption3;
126         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL, 0,
127                                           username, ARRAY_SIZE(username),
128                                           password, ARRAY_SIZE(password),
129                                           &save, CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_EXPECT_CONFIRMATION);
130         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
131         ok(save, "save flag should have been untouched\n");
132 
133         save = FALSE;
134         credui_info.pszCaptionText = wszCaption4;
135         ret = CredUIPromptForCredentialsW(&credui_info, wszServerName, NULL, 0,
136                                           username, ARRAY_SIZE(username),
137                                           password, ARRAY_SIZE(password),
138                                           &save, CREDUI_FLAGS_PERSIST|CREDUI_FLAGS_EXPECT_CONFIRMATION);
139         ok(ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "CredUIPromptForCredentials failed with error %d\n", ret);
140         ok(!save, "save flag should have been untouched\n");
141         if (ret == ERROR_SUCCESS)
142         {
143             ret = CredUIConfirmCredentialsW(wszServerName, FALSE);
144             ok(ret == ERROR_SUCCESS, "CredUIConfirmCredentials failed with error %d\n", ret);
145         }
146 
147     }
148 }
149 
150 static void test_SspiPromptForCredentials(void)
151 {
152     static const WCHAR targetW[] = {'S','s','p','i','T','e','s','t',0};
153     static const WCHAR basicW[] = {'b','a','s','i','c',0};
154     ULONG ret;
155     SECURITY_STATUS status;
156     CREDUI_INFOW info;
157     PSEC_WINNT_AUTH_IDENTITY_OPAQUE id;
158     const WCHAR *username, *domain, *creds;
159     int save;
160 
161     if (!pSspiPromptForCredentialsW || !pSspiFreeAuthIdentity)
162     {
163         win_skip( "SspiPromptForCredentialsW is missing\n" );
164         return;
165     }
166 
167     info.cbSize         = sizeof(info);
168     info.hwndParent     = NULL;
169     info.pszMessageText = targetW;
170     info.pszCaptionText = basicW;
171     info.hbmBanner      = NULL;
172     ret = pSspiPromptForCredentialsW( NULL, &info, 0, basicW, NULL, &id, &save, 0 );
173     ok( ret == ERROR_INVALID_PARAMETER, "got %u\n", ret );
174 
175     ret = pSspiPromptForCredentialsW( targetW, &info, 0, NULL, NULL, &id, &save, 0 );
176     ok( ret == ERROR_NO_SUCH_PACKAGE, "got %u\n", ret );
177 
178     if (winetest_interactive)
179     {
180         id = NULL;
181         save = -1;
182         ret = pSspiPromptForCredentialsW( targetW, &info, 0, basicW, NULL, &id, &save, 0 );
183         ok( ret == ERROR_SUCCESS || ret == ERROR_CANCELLED, "got %u\n", ret );
184         if (ret == ERROR_SUCCESS)
185         {
186             ok( id != NULL, "id not set\n" );
187             ok( save == TRUE || save == FALSE, "got %d\n", save );
188 
189             username = creds = NULL;
190             domain = (const WCHAR *)0xdeadbeef;
191             status = pSspiEncodeAuthIdentityAsStrings( id, &username, &domain, &creds );
192             ok( status == SEC_E_OK, "got %u\n", status );
193             ok( username != NULL, "username not set\n" );
194             ok( domain == NULL, "domain not set\n" );
195             ok( creds != NULL, "creds not set\n" );
196             pSspiFreeAuthIdentity( id );
197         }
198     }
199 }
200 
201 START_TEST(credui)
202 {
203     HMODULE hcredui = GetModuleHandleA( "credui.dll" ), hsecur32 = LoadLibraryA( "secur32.dll" );
204 
205     if (hcredui)
206         pSspiPromptForCredentialsW = (void *)GetProcAddress( hcredui, "SspiPromptForCredentialsW" );
207     if (hsecur32)
208     {
209         pSspiEncodeAuthIdentityAsStrings = (void *)GetProcAddress( hsecur32, "SspiEncodeAuthIdentityAsStrings" );
210         pSspiFreeAuthIdentity = (void *)GetProcAddress( hsecur32, "SspiFreeAuthIdentity" );
211     }
212 
213     test_CredUIPromptForCredentials();
214     test_SspiPromptForCredentials();
215 
216     FreeLibrary( hsecur32 );
217 }
218