1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <string> 6 7 #include "chrome/browser/net/url_fixer_upper.h" 8 #include "chrome/common/url_constants.h" 9 #include "chrome/test/automation/tab_proxy.h" 10 #include "chrome/test/automation/browser_proxy.h" 11 #include "chrome/test/ui/ui_test.h" 12 #include "net/test/test_server.h" 13 14 using std::wstring; 15 16 namespace { 17 18 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); 19 20 } // namespace 21 22 class LoginPromptTest : public UITest { 23 protected: 24 LoginPromptTest() 25 : username_basic_(L"basicuser"), 26 username_digest_(L"digestuser"), 27 password_(L"secret"), 28 password_bad_(L"denyme"), 29 test_server_(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)) { 30 } 31 32 void AppendTab(const GURL& url) { 33 scoped_refptr<BrowserProxy> window_proxy(automation()->GetBrowserWindow(0)); 34 ASSERT_TRUE(window_proxy.get()); 35 ASSERT_TRUE(window_proxy->AppendTab(url)); 36 } 37 38 protected: 39 wstring username_basic_; 40 wstring username_digest_; 41 wstring password_; 42 wstring password_bad_; 43 44 net::TestServer test_server_; 45 }; 46 47 wstring ExpectedTitleFromAuth(const wstring& username, 48 const wstring& password) { 49 // The TestServer sets the title to username/password on successful login. 50 return username + L"/" + password; 51 } 52 53 #if defined(OS_WIN) 54 // Probably related to test server flakiness in http://crbug.com/60937 55 #define MAYBE_TestBasicAuth FLAKY_TestBasicAuth 56 #else 57 #define MAYBE_TestBasicAuth TestBasicAuth 58 #endif 59 60 // Test that "Basic" HTTP authentication works. 61 TEST_F(LoginPromptTest, MAYBE_TestBasicAuth) { 62 ASSERT_TRUE(test_server_.Start()); 63 64 scoped_refptr<TabProxy> tab(GetActiveTab()); 65 ASSERT_TRUE(tab.get()); 66 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 67 tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 68 69 EXPECT_TRUE(tab->NeedsAuth()); 70 EXPECT_FALSE(tab->SetAuth(username_basic_, password_bad_)); 71 EXPECT_TRUE(tab->NeedsAuth()); 72 EXPECT_TRUE(tab->CancelAuth()); 73 EXPECT_EQ(L"Denied: wrong password", GetActiveTabTitle()); 74 75 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 76 tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 77 78 EXPECT_TRUE(tab->NeedsAuth()); 79 EXPECT_TRUE(tab->SetAuth(username_basic_, password_)); 80 EXPECT_EQ(ExpectedTitleFromAuth(username_basic_, password_), 81 GetActiveTabTitle()); 82 } 83 84 #if defined(OS_WIN) 85 // Probably related to test server flakiness in http://crbug.com/60937 86 #define MAYBE_TestDigestAuth FLAKY_TestDigestAuth 87 #else 88 #define MAYBE_TestDigestAuth TestDigestAuth 89 #endif 90 91 // Test that "Digest" HTTP authentication works. 92 TEST_F(LoginPromptTest, MAYBE_TestDigestAuth) { 93 ASSERT_TRUE(test_server_.Start()); 94 95 scoped_refptr<TabProxy> tab(GetActiveTab()); 96 ASSERT_TRUE(tab.get()); 97 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 98 tab->NavigateToURL(test_server_.GetURL("auth-digest"))); 99 100 EXPECT_TRUE(tab->NeedsAuth()); 101 EXPECT_FALSE(tab->SetAuth(username_digest_, password_bad_)); 102 EXPECT_TRUE(tab->CancelAuth()); 103 EXPECT_EQ(L"Denied: wrong password", GetActiveTabTitle()); 104 105 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 106 tab->NavigateToURL(test_server_.GetURL("auth-digest"))); 107 108 EXPECT_TRUE(tab->NeedsAuth()); 109 EXPECT_TRUE(tab->SetAuth(username_digest_, password_)); 110 EXPECT_EQ(ExpectedTitleFromAuth(username_digest_, password_), 111 GetActiveTabTitle()); 112 } 113 114 #if defined(OS_WIN) 115 // Probably related to test server flakiness in http://crbug.com/60937 116 #define MAYBE_TestTwoAuths FLAKY_TestTwoAuths 117 #else 118 #define MAYBE_TestTwoAuths TestTwoAuths 119 #endif 120 121 // Test that logging in on 2 tabs at once works. 122 TEST_F(LoginPromptTest, MAYBE_TestTwoAuths) { 123 ASSERT_TRUE(test_server_.Start()); 124 125 scoped_refptr<TabProxy> basic_tab(GetActiveTab()); 126 ASSERT_TRUE(basic_tab.get()); 127 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 128 basic_tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 129 130 AppendTab(GURL(chrome::kAboutBlankURL)); 131 scoped_refptr<TabProxy> digest_tab(GetActiveTab()); 132 ASSERT_TRUE(digest_tab.get()); 133 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 134 digest_tab->NavigateToURL(test_server_.GetURL("auth-digest"))); 135 136 EXPECT_TRUE(basic_tab->NeedsAuth()); 137 EXPECT_TRUE(basic_tab->SetAuth(username_basic_, password_)); 138 EXPECT_TRUE(digest_tab->NeedsAuth()); 139 EXPECT_TRUE(digest_tab->SetAuth(username_digest_, password_)); 140 141 wstring title; 142 EXPECT_TRUE(basic_tab->GetTabTitle(&title)); 143 EXPECT_EQ(ExpectedTitleFromAuth(username_basic_, password_), title); 144 145 EXPECT_TRUE(digest_tab->GetTabTitle(&title)); 146 EXPECT_EQ(ExpectedTitleFromAuth(username_digest_, password_), title); 147 } 148 149 #if defined(OS_WIN) 150 // Probably related to test server flakiness in http://crbug.com/60937 151 #define MAYBE_TestCancelAuth FLAKY_TestCancelAuth 152 #else 153 #define MAYBE_TestCancelAuth TestCancelAuth 154 #endif 155 156 // Test that cancelling authentication works. 157 TEST_F(LoginPromptTest, MAYBE_TestCancelAuth) { 158 ASSERT_TRUE(test_server_.Start()); 159 160 scoped_refptr<TabProxy> tab(GetActiveTab()); 161 ASSERT_TRUE(tab.get()); 162 163 // First navigate to a test server page so we have something to go back to. 164 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, 165 tab->NavigateToURL(test_server_.GetURL("a"))); 166 167 // Navigating while auth is requested is the same as cancelling. 168 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 169 tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 170 EXPECT_TRUE(tab->NeedsAuth()); 171 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, 172 tab->NavigateToURL(test_server_.GetURL("b"))); 173 EXPECT_FALSE(tab->NeedsAuth()); 174 175 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 176 tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 177 EXPECT_TRUE(tab->NeedsAuth()); 178 EXPECT_TRUE(tab->GoBack()); // should bring us back to 'a' 179 EXPECT_FALSE(tab->NeedsAuth()); 180 181 // Now add a page and go back, so we have something to go forward to. 182 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, 183 tab->NavigateToURL(test_server_.GetURL("c"))); 184 EXPECT_TRUE(tab->GoBack()); // should bring us back to 'a' 185 186 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 187 tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 188 EXPECT_TRUE(tab->NeedsAuth()); 189 EXPECT_TRUE(tab->GoForward()); // should bring us to 'c' 190 EXPECT_FALSE(tab->NeedsAuth()); 191 192 // Now test that cancelling works as expected. 193 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 194 tab->NavigateToURL(test_server_.GetURL("auth-basic"))); 195 EXPECT_TRUE(tab->NeedsAuth()); 196 EXPECT_TRUE(tab->CancelAuth()); 197 EXPECT_FALSE(tab->NeedsAuth()); 198 EXPECT_EQ(L"Denied: no auth", GetActiveTabTitle()); 199 } 200 201 #if defined(OS_WIN) 202 // Probably related to test server flakiness in http://crbug.com/60937 203 #define MAYBE_SupplyRedundantAuths FLAKY_SupplyRedundantAuths 204 #else 205 #define MAYBE_SupplyRedundantAuths SupplyRedundantAuths 206 #endif 207 208 // If multiple tabs are looking for the same auth, the user should only have to 209 // enter it once. 210 TEST_F(LoginPromptTest, MAYBE_SupplyRedundantAuths) { 211 ASSERT_TRUE(test_server_.Start()); 212 213 scoped_refptr<TabProxy> basic_tab1(GetActiveTab()); 214 ASSERT_TRUE(basic_tab1.get()); 215 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 216 basic_tab1->NavigateToURL(test_server_.GetURL("auth-basic/1"))); 217 EXPECT_TRUE(basic_tab1->NeedsAuth()); 218 219 AppendTab(GURL(chrome::kAboutBlankURL)); 220 scoped_refptr<TabProxy> basic_tab2(GetActiveTab()); 221 ASSERT_TRUE(basic_tab2.get()); 222 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 223 basic_tab2->NavigateToURL(test_server_.GetURL("auth-basic/2"))); 224 EXPECT_TRUE(basic_tab2->NeedsAuth()); 225 226 // Set the auth in only one of the tabs (but wait for the other to load). 227 int64 last_navigation_time; 228 ASSERT_TRUE(basic_tab2->GetLastNavigationTime(&last_navigation_time)); 229 EXPECT_TRUE(basic_tab1->SetAuth(username_basic_, password_)); 230 EXPECT_TRUE(basic_tab2->WaitForNavigation(last_navigation_time)); 231 232 // Now both tabs have loaded. 233 wstring title1; 234 EXPECT_TRUE(basic_tab1->GetTabTitle(&title1)); 235 EXPECT_EQ(ExpectedTitleFromAuth(username_basic_, password_), title1); 236 wstring title2; 237 EXPECT_TRUE(basic_tab2->GetTabTitle(&title2)); 238 EXPECT_EQ(ExpectedTitleFromAuth(username_basic_, password_), title2); 239 } 240 241 #if defined(OS_WIN) 242 // Probably related to test server flakiness in http://crbug.com/60937 243 #define MAYBE_CancelRedundantAuths FLAKY_CancelRedundantAuths 244 #else 245 #define MAYBE_CancelRedundantAuths CancelRedundantAuths 246 #endif 247 248 // If multiple tabs are looking for the same auth, and one is cancelled, the 249 // other should be cancelled as well. 250 TEST_F(LoginPromptTest, MAYBE_CancelRedundantAuths) { 251 ASSERT_TRUE(test_server_.Start()); 252 253 scoped_refptr<TabProxy> basic_tab1(GetActiveTab()); 254 ASSERT_TRUE(basic_tab1.get()); 255 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 256 basic_tab1->NavigateToURL(test_server_.GetURL("auth-basic/1"))); 257 EXPECT_TRUE(basic_tab1->NeedsAuth()); 258 259 AppendTab(GURL(chrome::kAboutBlankURL)); 260 scoped_refptr<TabProxy> basic_tab2(GetActiveTab()); 261 ASSERT_TRUE(basic_tab2.get()); 262 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, 263 basic_tab2->NavigateToURL(test_server_.GetURL("auth-basic/2"))); 264 EXPECT_TRUE(basic_tab2->NeedsAuth()); 265 266 // Cancel the auth in only one of the tabs (but wait for the other to load). 267 int64 last_navigation_time; 268 ASSERT_TRUE(basic_tab2->GetLastNavigationTime(&last_navigation_time)); 269 EXPECT_TRUE(basic_tab1->CancelAuth()); 270 EXPECT_TRUE(basic_tab2->WaitForNavigation(last_navigation_time)); 271 272 // Now both tabs have been denied. 273 wstring title1; 274 EXPECT_TRUE(basic_tab1->GetTabTitle(&title1)); 275 EXPECT_EQ(L"Denied: no auth", title1); 276 wstring title2; 277 EXPECT_TRUE(basic_tab2->GetTabTitle(&title2)); 278 EXPECT_EQ(L"Denied: no auth", title2); 279 } 280