| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/password_manager/chrome_password_manager_client.h" | 5 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/sync/profile_sync_service_factory.h" | 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 14 #include "chrome/browser/sync/profile_sync_service_mock.h" | 14 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 15 #include "chrome/common/channel_info.h" | 15 #include "chrome/common/channel_info.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/testing_pref_service_syncable.h" | 17 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 19 #include "components/autofill/content/common/autofill_messages.h" | 19 #include "components/autofill/content/common/autofill_messages.h" |
| 20 #include "components/password_manager/content/browser/password_manager_internals
_service_factory.h" | 20 #include "components/password_manager/content/browser/password_manager_internals
_service_factory.h" |
| 21 #include "components/password_manager/content/common/credential_manager_messages
.h" | 21 #include "components/password_manager/content/common/credential_manager_messages
.h" |
| 22 #include "components/password_manager/core/browser/log_receiver.h" | 22 #include "components/password_manager/core/browser/log_receiver.h" |
| 23 #include "components/password_manager/core/browser/password_manager_internals_se
rvice.h" | 23 #include "components/password_manager/core/browser/password_manager_internals_se
rvice.h" |
| 24 #include "components/password_manager/core/browser/store_result_filter.h" |
| 24 #include "components/password_manager/core/common/credential_manager_types.h" | 25 #include "components/password_manager/core/common/credential_manager_types.h" |
| 25 #include "components/password_manager/core/common/password_manager_pref_names.h" | 26 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 26 #include "components/password_manager/core/common/password_manager_switches.h" | 27 #include "components/password_manager/core/common/password_manager_switches.h" |
| 27 #include "components/version_info/version_info.h" | 28 #include "components/version_info/version_info.h" |
| 28 #include "content/public/browser/browser_context.h" | 29 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/test/mock_render_process_host.h" | 31 #include "content/public/test/mock_render_process_host.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 34 |
| 34 using content::BrowserContext; | 35 using content::BrowserContext; |
| 35 using content::WebContents; | 36 using content::WebContents; |
| 36 using testing::Return; | 37 using testing::Return; |
| 37 using testing::_; | 38 using testing::_; |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 const char kTestText[] = "abcd1234"; | 42 const char kTestText[] = "abcd1234"; |
| 42 | 43 |
| 43 class MockLogReceiver : public password_manager::LogReceiver { | 44 class MockLogReceiver : public password_manager::LogReceiver { |
| 44 public: | 45 public: |
| 45 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); | 46 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 // TODO(vabr): Get rid of the mocked client in the client's own test, see | 49 // TODO(vabr): Get rid of the mocked client in the client's own test, see |
| 49 // http://crbug.com/474577. | 50 // http://crbug.com/474577. |
| 50 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { | 51 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { |
| 51 public: | 52 public: |
| 52 MOCK_CONST_METHOD0(IsPasswordManagementEnabledForCurrentPage, bool()); | |
| 53 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); | 53 MOCK_CONST_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); |
| 54 MOCK_CONST_METHOD2(IsSyncAccountCredential, | 54 MOCK_CONST_METHOD2(IsSyncAccountCredential, |
| 55 bool(const std::string& username, | 55 bool(const std::string& username, |
| 56 const std::string& origin)); | 56 const std::string& origin)); |
| 57 | 57 |
| 58 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) | 58 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) |
| 59 : ChromePasswordManagerClient(web_contents, nullptr) { | 59 : ChromePasswordManagerClient(web_contents, nullptr) { |
| 60 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors()) | 60 ON_CALL(*this, DidLastPageLoadEncounterSSLErrors()) |
| 61 .WillByDefault(testing::Return(false)); | 61 .WillByDefault(testing::Return(false)); |
| 62 ON_CALL(*this, IsPasswordManagementEnabledForCurrentPage()) | |
| 63 .WillByDefault(testing::Return(true)); | |
| 64 } | 62 } |
| 65 ~MockChromePasswordManagerClient() override {} | 63 ~MockChromePasswordManagerClient() override {} |
| 66 | 64 |
| 67 private: | 65 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); | 66 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); |
| 69 }; | 67 }; |
| 70 | 68 |
| 71 } // namespace | 69 } // namespace |
| 72 | 70 |
| 73 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 71 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 service_->RegisterReceiver(&receiver_); | 229 service_->RegisterReceiver(&receiver_); |
| 232 EXPECT_TRUE(client->IsLoggingActive()); | 230 EXPECT_TRUE(client->IsLoggingActive()); |
| 233 | 231 |
| 234 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); | 232 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); |
| 235 client->LogSavePasswordProgress(kTestText); | 233 client->LogSavePasswordProgress(kTestText); |
| 236 | 234 |
| 237 service_->UnregisterReceiver(&receiver_); | 235 service_->UnregisterReceiver(&receiver_); |
| 238 EXPECT_FALSE(client->IsLoggingActive()); | 236 EXPECT_FALSE(client->IsLoggingActive()); |
| 239 } | 237 } |
| 240 | 238 |
| 241 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult_Reauth) { | |
| 242 // Make client disallow only reauth requests. | |
| 243 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 244 command_line->AppendSwitch( | |
| 245 password_manager::switches::kDisallowAutofillSyncCredentialForReauth); | |
| 246 scoped_ptr<MockChromePasswordManagerClient> client( | |
| 247 new MockChromePasswordManagerClient(web_contents())); | |
| 248 autofill::PasswordForm form; | |
| 249 | |
| 250 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) | |
| 251 .WillRepeatedly(Return(false)); | |
| 252 NavigateAndCommit( | |
| 253 GURL("https://accounts.google.com/login?rart=123&continue=blah")); | |
| 254 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | |
| 255 | |
| 256 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) | |
| 257 .WillRepeatedly(Return(true)); | |
| 258 NavigateAndCommit( | |
| 259 GURL("https://accounts.google.com/login?rart=123&continue=blah")); | |
| 260 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); | |
| 261 | |
| 262 // This counts as a reauth url, though a valid URL should have a value for | |
| 263 // "rart" | |
| 264 NavigateAndCommit(GURL("https://accounts.google.com/addlogin?rart")); | |
| 265 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); | |
| 266 | |
| 267 NavigateAndCommit(GURL("https://accounts.google.com/login?param=123")); | |
| 268 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | |
| 269 | |
| 270 NavigateAndCommit(GURL("https://site.com/login?rart=678")); | |
| 271 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | |
| 272 } | |
| 273 | |
| 274 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult) { | |
| 275 // Normally the client should allow any credentials through, even if they | |
| 276 // are the sync credential. | |
| 277 scoped_ptr<MockChromePasswordManagerClient> client( | |
| 278 new MockChromePasswordManagerClient(web_contents())); | |
| 279 autofill::PasswordForm form; | |
| 280 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) | |
| 281 .WillRepeatedly(Return(true)); | |
| 282 NavigateAndCommit(GURL("https://accounts.google.com/Login")); | |
| 283 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | |
| 284 | |
| 285 // Adding disallow switch should cause sync credential to be filtered. | |
| 286 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 287 command_line->AppendSwitch( | |
| 288 password_manager::switches::kDisallowAutofillSyncCredential); | |
| 289 client.reset(new MockChromePasswordManagerClient(web_contents())); | |
| 290 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) | |
| 291 .WillRepeatedly(Return(true)); | |
| 292 NavigateAndCommit(GURL("https://accounts.google.com/Login")); | |
| 293 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); | |
| 294 } | |
| 295 | |
| 296 TEST_F(ChromePasswordManagerClientTest, | 239 TEST_F(ChromePasswordManagerClientTest, |
| 297 IsPasswordManagementEnabledForCurrentPage) { | 240 IsPasswordManagementEnabledForCurrentPage) { |
| 298 ChromePasswordManagerClient* client = GetClient(); | 241 ChromePasswordManagerClient* client = GetClient(); |
| 299 NavigateAndCommit( | 242 NavigateAndCommit( |
| 300 GURL("https://accounts.google.com/ServiceLogin?continue=" | 243 GURL("https://accounts.google.com/ServiceLogin?continue=" |
| 301 "https://passwords.google.com/settings&rart=123")); | 244 "https://passwords.google.com/settings&rart=123")); |
| 302 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage()); | 245 EXPECT_FALSE(client->IsPasswordManagementEnabledForCurrentPage()); |
| 303 | 246 |
| 304 // Password site is inaccesible via HTTP, but because of HSTS the following | 247 // Password site is inaccesible via HTTP, but because of HSTS the following |
| 305 // link should still continue to https://passwords.google.com. | 248 // link should still continue to https://passwords.google.com. |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 profile()->ForceIncognito(true); | 404 profile()->ForceIncognito(true); |
| 462 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage()); | 405 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage()); |
| 463 | 406 |
| 464 // Functionality disabled in Incognito mode also when manager itself is | 407 // Functionality disabled in Incognito mode also when manager itself is |
| 465 // enabled. | 408 // enabled. |
| 466 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled, | 409 prefs()->SetUserPref(password_manager::prefs::kPasswordManagerSavingEnabled, |
| 467 new base::FundamentalValue(true)); | 410 new base::FundamentalValue(true)); |
| 468 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage()); | 411 EXPECT_FALSE(client->IsSavingEnabledForCurrentPage()); |
| 469 profile()->ForceIncognito(false); | 412 profile()->ForceIncognito(false); |
| 470 } | 413 } |
| 414 |
| 415 TEST_F(ChromePasswordManagerClientTest, GetLastCommittedEntryURL_Empty) { |
| 416 EXPECT_EQ(GURL::EmptyGURL(), GetClient()->GetLastCommittedEntryURL()); |
| 417 } |
| 418 |
| 419 TEST_F(ChromePasswordManagerClientTest, GetLastCommittedEntryURL) { |
| 420 GURL kUrl( |
| 421 "https://accounts.google.com/ServiceLogin?continue=" |
| 422 "https://passwords.google.com/settings&rart=123"); |
| 423 NavigateAndCommit(kUrl); |
| 424 EXPECT_EQ(kUrl, GetClient()->GetLastCommittedEntryURL()); |
| 425 } |
| 426 |
| 427 TEST_F(ChromePasswordManagerClientTest, CreateStoreResulFilter) { |
| 428 scoped_ptr<password_manager::StoreResultFilter> filter1 = |
| 429 GetClient()->CreateStoreResultFilter(); |
| 430 scoped_ptr<password_manager::StoreResultFilter> filter2 = |
| 431 GetClient()->CreateStoreResultFilter(); |
| 432 EXPECT_TRUE(filter1); |
| 433 EXPECT_TRUE(filter2); |
| 434 EXPECT_NE(filter1.get(), filter2.get()); |
| 435 } |
| OLD | NEW |