Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1096)

Side by Side Diff: components/password_manager/core/browser/login_database_unittest.cc

Issue 299443002: Password Login Database: report correct changes from UpdateLogin(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added UpdateNonexistentLogin tests Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "components/password_manager/core/browser/login_database.h" 5 #include "components/password_manager/core/browser/login_database.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
(...skipping 11 matching lines...) Expand all
22 using ::testing::Eq; 22 using ::testing::Eq;
23 23
24 namespace password_manager { 24 namespace password_manager {
25 namespace { 25 namespace {
26 PasswordStoreChangeList AddChangeForForm(const PasswordForm& form) { 26 PasswordStoreChangeList AddChangeForForm(const PasswordForm& form) {
27 return PasswordStoreChangeList(1, 27 return PasswordStoreChangeList(1,
28 PasswordStoreChange(PasswordStoreChange::ADD, 28 PasswordStoreChange(PasswordStoreChange::ADD,
29 form)); 29 form));
30 } 30 }
31 31
32 PasswordStoreChangeList UpdateChangeForForm(const PasswordForm& form) {
33 return PasswordStoreChangeList(1, PasswordStoreChange(
34 PasswordStoreChange::UPDATE, form));
35 }
36
32 } // namespace 37 } // namespace
33 38
34 // Serialization routines for vectors implemented in login_database.cc. 39 // Serialization routines for vectors implemented in login_database.cc.
35 Pickle SerializeVector(const std::vector<base::string16>& vec); 40 Pickle SerializeVector(const std::vector<base::string16>& vec);
36 std::vector<base::string16> DeserializeVector(const Pickle& pickle); 41 std::vector<base::string16> DeserializeVector(const Pickle& pickle);
37 42
38 class LoginDatabaseTest : public testing::Test { 43 class LoginDatabaseTest : public testing::Test {
39 protected: 44 protected:
40 virtual void SetUp() { 45 virtual void SetUp() {
41 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 delete result[0]; 212 delete result[0];
208 result.clear(); 213 result.clear();
209 214
210 // User changes his password. 215 // User changes his password.
211 PasswordForm form6(form5); 216 PasswordForm form6(form5);
212 form6.password_value = ASCIIToUTF16("test6"); 217 form6.password_value = ASCIIToUTF16("test6");
213 form6.preferred = true; 218 form6.preferred = true;
214 219
215 // We update, and check to make sure it matches the 220 // We update, and check to make sure it matches the
216 // old form, and there is only one record. 221 // old form, and there is only one record.
217 int rows_changed = 0; 222 EXPECT_EQ(UpdateChangeForForm(form6), db_.UpdateLogin(form6));
218 EXPECT_TRUE(db_.UpdateLogin(form6, &rows_changed));
219 EXPECT_EQ(1, rows_changed);
220 // matches 223 // matches
221 EXPECT_TRUE(db_.GetLogins(form5, &result)); 224 EXPECT_TRUE(db_.GetLogins(form5, &result));
222 EXPECT_EQ(1U, result.size()); 225 EXPECT_EQ(1U, result.size());
223 delete result[0]; 226 delete result[0];
224 result.clear(); 227 result.clear();
225 // Only one record. 228 // Only one record.
226 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); 229 EXPECT_TRUE(db_.GetAutofillableLogins(&result));
227 EXPECT_EQ(1U, result.size()); 230 EXPECT_EQ(1U, result.size());
228 // Password element was updated. 231 // Password element was updated.
229 #if defined(OS_MACOSX) && !defined(OS_IOS) 232 #if defined(OS_MACOSX) && !defined(OS_IOS)
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 ClearResults(&result); 744 ClearResults(&result);
742 745
743 // Let's say this login form worked. Now update the stored credentials with 746 // Let's say this login form worked. Now update the stored credentials with
744 // 'action', 'username_element', 'password_element' and 'submit_element' from 747 // 'action', 'username_element', 'password_element' and 'submit_element' from
745 // the encountered form. 748 // the encountered form.
746 PasswordForm completed_form(incomplete_form); 749 PasswordForm completed_form(incomplete_form);
747 completed_form.action = encountered_form.action; 750 completed_form.action = encountered_form.action;
748 completed_form.username_element = encountered_form.username_element; 751 completed_form.username_element = encountered_form.username_element;
749 completed_form.password_element = encountered_form.password_element; 752 completed_form.password_element = encountered_form.password_element;
750 completed_form.submit_element = encountered_form.submit_element; 753 completed_form.submit_element = encountered_form.submit_element;
751 EXPECT_TRUE(db_.UpdateLogin(completed_form, NULL)); 754 EXPECT_EQ(AddChangeForForm(completed_form), db_.AddLogin(completed_form));
755 EXPECT_TRUE(db_.RemoveLogin(incomplete_form));
752 756
753 // Get matches for encountered_form again. 757 // Get matches for encountered_form again.
754 EXPECT_TRUE(db_.GetLogins(encountered_form, &result)); 758 EXPECT_TRUE(db_.GetLogins(encountered_form, &result));
755 ASSERT_EQ(1U, result.size()); 759 ASSERT_EQ(1U, result.size());
756 760
757 // This time we should have all the info available. 761 // This time we should have all the info available.
758 PasswordForm expected_form(completed_form); 762 PasswordForm expected_form(completed_form);
759 #if defined(OS_MACOSX) && !defined(OS_IOS) 763 #if defined(OS_MACOSX) && !defined(OS_IOS)
760 expected_form.password_value.clear(); 764 expected_form.password_value.clear();
761 #endif // OS_MACOSX && !OS_IOS 765 #endif // OS_MACOSX && !OS_IOS
(...skipping 18 matching lines...) Expand all
780 EXPECT_EQ(AddChangeForForm(incomplete_form), db_.AddLogin(incomplete_form)); 784 EXPECT_EQ(AddChangeForForm(incomplete_form), db_.AddLogin(incomplete_form));
781 785
782 // Save a complete version of the previous form. Both forms could exist if 786 // Save a complete version of the previous form. Both forms could exist if
783 // the user created the complete version before importing the incomplete 787 // the user created the complete version before importing the incomplete
784 // version from a different browser. 788 // version from a different browser.
785 PasswordForm complete_form = incomplete_form; 789 PasswordForm complete_form = incomplete_form;
786 complete_form.action = GURL("http://accounts.google.com/Login"); 790 complete_form.action = GURL("http://accounts.google.com/Login");
787 complete_form.username_element = ASCIIToUTF16("username_element"); 791 complete_form.username_element = ASCIIToUTF16("username_element");
788 complete_form.password_element = ASCIIToUTF16("password_element"); 792 complete_form.password_element = ASCIIToUTF16("password_element");
789 complete_form.submit_element = ASCIIToUTF16("submit"); 793 complete_form.submit_element = ASCIIToUTF16("submit");
794
795 // An update fails because the primary key for |complete_form| is different.
796 EXPECT_EQ(PasswordStoreChangeList(), db_.UpdateLogin(complete_form));
790 EXPECT_EQ(AddChangeForForm(complete_form), db_.AddLogin(complete_form)); 797 EXPECT_EQ(AddChangeForForm(complete_form), db_.AddLogin(complete_form));
791 798
792 // Make sure both passwords exist. 799 // Make sure both passwords exist.
793 ScopedVector<autofill::PasswordForm> result; 800 ScopedVector<autofill::PasswordForm> result;
794 EXPECT_TRUE(db_.GetAutofillableLogins(&result.get())); 801 EXPECT_TRUE(db_.GetAutofillableLogins(&result.get()));
795 ASSERT_EQ(2U, result.size()); 802 ASSERT_EQ(2U, result.size());
796 result.clear(); 803 result.clear();
797 804
798 // Simulate the user changing their password. 805 // Simulate the user changing their password.
799 complete_form.password_value = ASCIIToUTF16("new_password"); 806 complete_form.password_value = ASCIIToUTF16("new_password");
800 EXPECT_TRUE(db_.UpdateLogin(complete_form, NULL)); 807 EXPECT_EQ(UpdateChangeForForm(complete_form), db_.UpdateLogin(complete_form));
801 808
802 // Only one updated form should exist now. 809 // Both still exist now.
803 EXPECT_TRUE(db_.GetAutofillableLogins(&result.get())); 810 EXPECT_TRUE(db_.GetAutofillableLogins(&result.get()));
804 ASSERT_EQ(1U, result.size()); 811 ASSERT_EQ(2U, result.size());
805 812
806 PasswordForm expected_form(complete_form);
807 #if defined(OS_MACOSX) && !defined(OS_IOS) 813 #if defined(OS_MACOSX) && !defined(OS_IOS)
808 // On Mac, passwords are not stored in login database, instead they're in 814 // On Mac, passwords are not stored in login database, instead they're in
809 // the keychain. 815 // the keychain.
810 expected_form.password_value.clear(); 816 complete_form.password_value.clear();
817 incomplete_form.password_value.clear();
811 #endif // OS_MACOSX && !OS_IOS 818 #endif // OS_MACOSX && !OS_IOS
812 EXPECT_EQ(expected_form, *result[0]); 819 if (result[0]->username_element.empty())
820 std::swap(result[0], result[1]);
821 EXPECT_EQ(complete_form, *result[0]);
822 EXPECT_EQ(incomplete_form, *result[1]);
813 } 823 }
814 824
815 TEST_F(LoginDatabaseTest, DoubleAdd) { 825 TEST_F(LoginDatabaseTest, DoubleAdd) {
816 PasswordForm form; 826 PasswordForm form;
817 form.origin = GURL("http://accounts.google.com/LoginAuth"); 827 form.origin = GURL("http://accounts.google.com/LoginAuth");
818 form.signon_realm = "http://accounts.google.com/"; 828 form.signon_realm = "http://accounts.google.com/";
819 form.username_value = ASCIIToUTF16("my_username"); 829 form.username_value = ASCIIToUTF16("my_username");
820 form.password_value = ASCIIToUTF16("my_password"); 830 form.password_value = ASCIIToUTF16("my_password");
821 form.ssl_valid = false; 831 form.ssl_valid = false;
822 form.preferred = true; 832 form.preferred = true;
(...skipping 16 matching lines...) Expand all
839 // This tests that sql::Connection::set_restrict_to_user() was called, 849 // This tests that sql::Connection::set_restrict_to_user() was called,
840 // and that function is a noop on non-POSIX platforms in any case. 850 // and that function is a noop on non-POSIX platforms in any case.
841 TEST_F(LoginDatabaseTest, FilePermissions) { 851 TEST_F(LoginDatabaseTest, FilePermissions) {
842 int mode = base::FILE_PERMISSION_MASK; 852 int mode = base::FILE_PERMISSION_MASK;
843 EXPECT_TRUE(base::GetPosixFilePermissions(file_, &mode)); 853 EXPECT_TRUE(base::GetPosixFilePermissions(file_, &mode));
844 EXPECT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); 854 EXPECT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode);
845 } 855 }
846 #endif // defined(OS_POSIX) 856 #endif // defined(OS_POSIX)
847 857
848 } // namespace password_manager 858 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698