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