OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/password_manager/password_form_manager.h" | 11 #include "chrome/browser/password_manager/password_form_manager.h" |
12 #include "chrome/browser/password_manager/password_manager.h" | 12 #include "chrome/browser/password_manager/password_manager.h" |
13 #include "chrome/browser/password_manager/password_manager_client.h" | 13 #include "chrome/browser/password_manager/password_manager_client.h" |
14 #include "chrome/browser/password_manager/password_manager_driver.h" | 14 #include "chrome/browser/password_manager/password_manager_driver.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
17 #include "components/autofill/core/common/password_form.h" | 17 #include "components/autofill/core/common/password_form.h" |
| 18 #include "components/password_manager/core/browser/mock_password_store.h" |
18 #include "components/password_manager/core/browser/password_store.h" | 19 #include "components/password_manager/core/browser/password_store.h" |
19 #include "components/password_manager/core/browser/test_password_store.h" | 20 #include "components/password_manager/core/browser/test_password_store.h" |
20 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 | 23 |
23 using autofill::PasswordForm; | 24 using autofill::PasswordForm; |
24 using base::ASCIIToUTF16; | 25 using base::ASCIIToUTF16; |
25 using ::testing::_; | 26 using ::testing::_; |
26 using ::testing::Eq; | 27 using ::testing::Eq; |
27 using ::testing::Mock; | 28 using ::testing::Mock; |
| 29 using ::testing::Return; |
28 | 30 |
29 namespace autofill { | 31 namespace autofill { |
30 class AutofillManager; | 32 class AutofillManager; |
31 } | 33 } |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 class MockPasswordManagerDriver : public PasswordManagerDriver { | 37 class MockPasswordManagerDriver : public PasswordManagerDriver { |
36 public: | 38 public: |
37 MockPasswordManagerDriver() {} | 39 MockPasswordManagerDriver() {} |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 saved_match_.preferred = true; | 118 saved_match_.preferred = true; |
117 saved_match_.username_value = ASCIIToUTF16("test@gmail.com"); | 119 saved_match_.username_value = ASCIIToUTF16("test@gmail.com"); |
118 saved_match_.password_value = ASCIIToUTF16("test1"); | 120 saved_match_.password_value = ASCIIToUTF16("test1"); |
119 saved_match_.other_possible_usernames.push_back( | 121 saved_match_.other_possible_usernames.push_back( |
120 ASCIIToUTF16("test2@gmail.com")); | 122 ASCIIToUTF16("test2@gmail.com")); |
121 profile_ = new TestingProfile(); | 123 profile_ = new TestingProfile(); |
122 } | 124 } |
123 | 125 |
124 virtual void TearDown() { | 126 virtual void TearDown() { |
125 delete profile_; | 127 delete profile_; |
| 128 if (mock_store_) |
| 129 mock_store_->Shutdown(); |
| 130 } |
| 131 |
| 132 void InitializeMockStore() { |
| 133 if (!mock_store_) { |
| 134 mock_store_ = new MockPasswordStore(); |
| 135 ASSERT_TRUE(mock_store_); |
| 136 } |
| 137 } |
| 138 |
| 139 MockPasswordStore* mock_store() const { |
| 140 return mock_store_.get(); |
126 } | 141 } |
127 | 142 |
128 PasswordForm* GetPendingCredentials(PasswordFormManager* p) { | 143 PasswordForm* GetPendingCredentials(PasswordFormManager* p) { |
129 return &p->pending_credentials_; | 144 return &p->pending_credentials_; |
130 } | 145 } |
131 | 146 |
132 void SimulateMatchingPhase(PasswordFormManager* p, bool find_match) { | 147 void SimulateMatchingPhase(PasswordFormManager* p, bool find_match) { |
133 // Roll up the state to mock out the matching phase. | 148 // Roll up the state to mock out the matching phase. |
134 p->state_ = PasswordFormManager::POST_MATCHING_PHASE; | 149 p->state_ = PasswordFormManager::POST_MATCHING_PHASE; |
135 if (!find_match) | 150 if (!find_match) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // Owned by the caller of this method. | 186 // Owned by the caller of this method. |
172 PasswordForm* match = new PasswordForm(saved_match_); | 187 PasswordForm* match = new PasswordForm(saved_match_); |
173 match->blacklisted_by_user = blacklisted; | 188 match->blacklisted_by_user = blacklisted; |
174 return match; | 189 return match; |
175 } | 190 } |
176 | 191 |
177 private: | 192 private: |
178 PasswordForm observed_form_; | 193 PasswordForm observed_form_; |
179 PasswordForm saved_match_; | 194 PasswordForm saved_match_; |
180 Profile* profile_; | 195 Profile* profile_; |
| 196 scoped_refptr<MockPasswordStore> mock_store_; |
181 }; | 197 }; |
182 | 198 |
183 TEST_F(PasswordFormManagerTest, TestNewLogin) { | 199 TEST_F(PasswordFormManagerTest, TestNewLogin) { |
184 scoped_ptr<TestPasswordManagerClient> client( | 200 scoped_ptr<TestPasswordManagerClient> client( |
185 new TestPasswordManagerClient(profile(), NULL)); | 201 new TestPasswordManagerClient(profile(), NULL)); |
186 scoped_ptr<MockPasswordManagerDriver> driver; | 202 scoped_ptr<MockPasswordManagerDriver> driver; |
187 PasswordFormManager* manager = new PasswordFormManager( | 203 PasswordFormManager* manager = new PasswordFormManager( |
188 NULL, client.get(), driver.get(), *observed_form(), false); | 204 NULL, client.get(), driver.get(), *observed_form(), false); |
189 | 205 |
190 SimulateMatchingPhase(manager, false); | 206 SimulateMatchingPhase(manager, false); |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 | 669 |
654 SanitizePossibleUsernames(manager.get(), &credentials); | 670 SanitizePossibleUsernames(manager.get(), &credentials); |
655 | 671 |
656 // SSN, duplicate in |other_possible_usernames| and duplicate of | 672 // SSN, duplicate in |other_possible_usernames| and duplicate of |
657 // |username_value| all removed. | 673 // |username_value| all removed. |
658 expected.clear(); | 674 expected.clear(); |
659 expected.push_back(ASCIIToUTF16("duplicate")); | 675 expected.push_back(ASCIIToUTF16("duplicate")); |
660 expected.push_back(ASCIIToUTF16("random")); | 676 expected.push_back(ASCIIToUTF16("random")); |
661 EXPECT_THAT(credentials.other_possible_usernames, Eq(expected)); | 677 EXPECT_THAT(credentials.other_possible_usernames, Eq(expected)); |
662 } | 678 } |
| 679 |
| 680 TEST_F(PasswordFormManagerTest, TestUpdateIncompleteCredentials) { |
| 681 InitializeMockStore(); |
| 682 |
| 683 // We've found this form on a website: |
| 684 PasswordForm encountered_form; |
| 685 encountered_form.origin = GURL("http://accounts.google.com/LoginAuth"); |
| 686 encountered_form.signon_realm = "http://accounts.google.com/"; |
| 687 encountered_form.action = GURL("http://accounts.google.com/Login"); |
| 688 encountered_form.username_element = ASCIIToUTF16("Email"); |
| 689 encountered_form.password_element = ASCIIToUTF16("Passwd"); |
| 690 encountered_form.submit_element = ASCIIToUTF16("signIn"); |
| 691 |
| 692 TestPasswordManagerClient client(profile(), mock_store()); |
| 693 MockPasswordManagerDriver driver; |
| 694 EXPECT_CALL(driver, IsOffTheRecord()).WillRepeatedly(Return(false)); |
| 695 EXPECT_CALL(driver, AllowPasswordGenerationForForm(_)); |
| 696 |
| 697 TestPasswordManager manager(&client); |
| 698 PasswordFormManager form_manager(&manager, |
| 699 &client, |
| 700 &driver, |
| 701 encountered_form, |
| 702 false); |
| 703 |
| 704 const PasswordStore::AuthorizationPromptPolicy auth_policy = |
| 705 PasswordStore::DISALLOW_PROMPT; |
| 706 EXPECT_CALL(*mock_store(), GetLogins(encountered_form, |
| 707 auth_policy, |
| 708 &form_manager)); |
| 709 form_manager.FetchMatchingLoginsFromPasswordStore(auth_policy); |
| 710 |
| 711 // Password store only has these incomplete credentials. |
| 712 PasswordForm* incomplete_form = new PasswordForm(); |
| 713 incomplete_form->origin = GURL("http://accounts.google.com/LoginAuth"); |
| 714 incomplete_form->signon_realm = "http://accounts.google.com/"; |
| 715 incomplete_form->password_value = ASCIIToUTF16("my_password"); |
| 716 incomplete_form->username_value = ASCIIToUTF16("my_username"); |
| 717 incomplete_form->preferred = true; |
| 718 incomplete_form->ssl_valid = false; |
| 719 incomplete_form->scheme = PasswordForm::SCHEME_HTML; |
| 720 |
| 721 // We expect to see this form eventually sent to the Password store. It |
| 722 // has password/username values from the store and 'username_element', |
| 723 // 'password_element', 'submit_element' and 'action' fields copied from |
| 724 // the encountered form. |
| 725 PasswordForm complete_form(*incomplete_form); |
| 726 complete_form.action = encountered_form.action; |
| 727 complete_form.password_element = encountered_form.password_element; |
| 728 complete_form.username_element = encountered_form.username_element; |
| 729 complete_form.submit_element = encountered_form.submit_element; |
| 730 |
| 731 // Feed the incomplete credentials to the manager. |
| 732 std::vector<PasswordForm*> results; |
| 733 results.push_back(incomplete_form); // Takes ownership. |
| 734 form_manager.OnRequestDone(results); |
| 735 |
| 736 form_manager.ProvisionallySave( |
| 737 complete_form, PasswordFormManager::IGNORE_OTHER_POSSIBLE_USERNAMES); |
| 738 // By now that form has been used once. |
| 739 complete_form.times_used = 1; |
| 740 |
| 741 // Check that PasswordStore receives an update request with the complete form. |
| 742 EXPECT_CALL(*mock_store(), UpdateLogin(complete_form)); |
| 743 form_manager.Save(); |
| 744 } |
OLD | NEW |