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/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/sync/profile_sync_service_factory.h" | 10 #include "chrome/browser/sync/profile_sync_service_factory.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "components/password_manager/core/common/password_manager_switches.h" | 21 #include "components/password_manager/core/common/password_manager_switches.h" |
22 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "content/public/test/mock_render_process_host.h" | 24 #include "content/public/test/mock_render_process_host.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 using content::BrowserContext; | 28 using content::BrowserContext; |
29 using content::WebContents; | 29 using content::WebContents; |
30 using testing::Return; | 30 using testing::Return; |
| 31 using testing::_; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 const char kTestText[] = "abcd1234"; | 35 const char kTestText[] = "abcd1234"; |
35 | 36 |
36 class MockLogReceiver : public password_manager::LogReceiver { | 37 class MockLogReceiver : public password_manager::LogReceiver { |
37 public: | 38 public: |
38 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); | 39 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); |
39 }; | 40 }; |
40 | 41 |
41 class TestChromePasswordManagerClient : public ChromePasswordManagerClient { | 42 class MockChromePasswordManagerClient : public ChromePasswordManagerClient { |
42 public: | 43 public: |
43 explicit TestChromePasswordManagerClient(content::WebContents* web_contents) | 44 MOCK_CONST_METHOD2(IsSyncAccountCredential, |
44 : ChromePasswordManagerClient(web_contents, nullptr), | 45 bool(const std::string& username, |
45 is_sync_account_credential_(false) {} | 46 const std::string& origin)); |
46 ~TestChromePasswordManagerClient() override {} | |
47 | 47 |
48 bool IsSyncAccountCredential(const std::string& username, | 48 explicit MockChromePasswordManagerClient(content::WebContents* web_contents) |
49 const std::string& origin) const override { | 49 : ChromePasswordManagerClient(web_contents, nullptr) {} |
50 return is_sync_account_credential_; | 50 ~MockChromePasswordManagerClient() override {} |
51 } | |
52 | |
53 void set_is_sync_account_credential(bool is_sync_account_credential) { | |
54 is_sync_account_credential_ = is_sync_account_credential; | |
55 } | |
56 | 51 |
57 private: | 52 private: |
58 bool is_sync_account_credential_; | 53 DISALLOW_COPY_AND_ASSIGN(MockChromePasswordManagerClient); |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(TestChromePasswordManagerClient); | |
61 }; | 54 }; |
62 | 55 |
63 } // namespace | 56 } // namespace |
64 | 57 |
65 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 58 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
66 public: | 59 public: |
67 ChromePasswordManagerClientTest(); | 60 ChromePasswordManagerClientTest(); |
68 | 61 |
69 virtual void SetUp() override; | 62 virtual void SetUp() override; |
70 | 63 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 214 |
222 service_->UnregisterReceiver(&receiver_); | 215 service_->UnregisterReceiver(&receiver_); |
223 EXPECT_FALSE(client->IsLoggingActive()); | 216 EXPECT_FALSE(client->IsLoggingActive()); |
224 } | 217 } |
225 | 218 |
226 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult_Reauth) { | 219 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult_Reauth) { |
227 // Make client disallow only reauth requests. | 220 // Make client disallow only reauth requests. |
228 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 221 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
229 command_line->AppendSwitch( | 222 command_line->AppendSwitch( |
230 password_manager::switches::kDisallowAutofillSyncCredentialForReauth); | 223 password_manager::switches::kDisallowAutofillSyncCredentialForReauth); |
231 scoped_ptr<TestChromePasswordManagerClient> client( | 224 scoped_ptr<MockChromePasswordManagerClient> client( |
232 new TestChromePasswordManagerClient(web_contents())); | 225 new MockChromePasswordManagerClient(web_contents())); |
233 autofill::PasswordForm form; | 226 autofill::PasswordForm form; |
234 | 227 |
235 client->set_is_sync_account_credential(false); | 228 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) |
| 229 .WillRepeatedly(Return(false)); |
236 NavigateAndCommit( | 230 NavigateAndCommit( |
237 GURL("https://accounts.google.com/login?rart=123&continue=blah")); | 231 GURL("https://accounts.google.com/login?rart=123&continue=blah")); |
238 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | 232 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); |
239 | 233 |
240 client->set_is_sync_account_credential(true); | 234 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) |
| 235 .WillRepeatedly(Return(true)); |
241 NavigateAndCommit( | 236 NavigateAndCommit( |
242 GURL("https://accounts.google.com/login?rart=123&continue=blah")); | 237 GURL("https://accounts.google.com/login?rart=123&continue=blah")); |
243 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); | 238 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); |
244 | 239 |
245 // This counts as a reauth url, though a valid URL should have a value for | 240 // This counts as a reauth url, though a valid URL should have a value for |
246 // "rart" | 241 // "rart" |
247 NavigateAndCommit(GURL("https://accounts.google.com/addlogin?rart")); | 242 NavigateAndCommit(GURL("https://accounts.google.com/addlogin?rart")); |
248 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); | 243 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); |
249 | 244 |
250 NavigateAndCommit(GURL("https://accounts.google.com/login?param=123")); | 245 NavigateAndCommit(GURL("https://accounts.google.com/login?param=123")); |
251 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | 246 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); |
252 | 247 |
253 NavigateAndCommit(GURL("https://site.com/login?rart=678")); | 248 NavigateAndCommit(GURL("https://site.com/login?rart=678")); |
254 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | 249 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); |
255 } | 250 } |
256 | 251 |
257 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult) { | 252 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult) { |
258 // Normally the client should allow any credentials through, even if they | 253 // Normally the client should allow any credentials through, even if they |
259 // are the sync credential. | 254 // are the sync credential. |
260 scoped_ptr<TestChromePasswordManagerClient> client( | 255 scoped_ptr<MockChromePasswordManagerClient> client( |
261 new TestChromePasswordManagerClient(web_contents())); | 256 new MockChromePasswordManagerClient(web_contents())); |
262 autofill::PasswordForm form; | 257 autofill::PasswordForm form; |
263 client->set_is_sync_account_credential(true); | 258 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) |
| 259 .WillRepeatedly(Return(true)); |
264 NavigateAndCommit(GURL("https://accounts.google.com/Login")); | 260 NavigateAndCommit(GURL("https://accounts.google.com/Login")); |
265 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); | 261 EXPECT_FALSE(client->ShouldFilterAutofillResult(form)); |
266 | 262 |
267 // Adding disallow switch should cause sync credential to be filtered. | 263 // Adding disallow switch should cause sync credential to be filtered. |
268 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 264 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
269 command_line->AppendSwitch( | 265 command_line->AppendSwitch( |
270 password_manager::switches::kDisallowAutofillSyncCredential); | 266 password_manager::switches::kDisallowAutofillSyncCredential); |
271 client.reset(new TestChromePasswordManagerClient(web_contents())); | 267 client.reset(new MockChromePasswordManagerClient(web_contents())); |
272 client->set_is_sync_account_credential(true); | 268 EXPECT_CALL(*client, IsSyncAccountCredential(_, _)) |
| 269 .WillRepeatedly(Return(true)); |
273 NavigateAndCommit(GURL("https://accounts.google.com/Login")); | 270 NavigateAndCommit(GURL("https://accounts.google.com/Login")); |
274 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); | 271 EXPECT_TRUE(client->ShouldFilterAutofillResult(form)); |
275 } | 272 } |
276 | 273 |
277 TEST_F(ChromePasswordManagerClientTest, | 274 TEST_F(ChromePasswordManagerClientTest, |
278 IsPasswordManagerEnabledForCurrentPage) { | 275 IsPasswordManagerEnabledForCurrentPage) { |
279 ChromePasswordManagerClient* client = GetClient(); | 276 ChromePasswordManagerClient* client = GetClient(); |
280 NavigateAndCommit( | 277 NavigateAndCommit( |
281 GURL("https://accounts.google.com/ServiceLogin?continue=" | 278 GURL("https://accounts.google.com/ServiceLogin?continue=" |
282 "https://passwords.google.com/settings&rart=123")); | 279 "https://passwords.google.com/settings&rart=123")); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 | 378 |
382 // Again, without a custom passphrase. | 379 // Again, without a custom passphrase. |
383 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase()) | 380 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase()) |
384 .WillRepeatedly(Return(false)); | 381 .WillRepeatedly(Return(false)); |
385 | 382 |
386 EXPECT_FALSE( | 383 EXPECT_FALSE( |
387 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE)); | 384 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE)); |
388 EXPECT_FALSE(client->IsPasswordSyncEnabled( | 385 EXPECT_FALSE(client->IsPasswordSyncEnabled( |
389 password_manager::WITHOUT_CUSTOM_PASSPHRASE)); | 386 password_manager::WITHOUT_CUSTOM_PASSPHRASE)); |
390 } | 387 } |
OLD | NEW |