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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 class MockPasswordStoreObserver | 51 class MockPasswordStoreObserver |
52 : public password_manager::PasswordStore::Observer { | 52 : public password_manager::PasswordStore::Observer { |
53 public: | 53 public: |
54 MOCK_METHOD1(OnLoginsChanged, | 54 MOCK_METHOD1(OnLoginsChanged, |
55 void(const password_manager::PasswordStoreChangeList& changes)); | 55 void(const password_manager::PasswordStoreChangeList& changes)); |
56 }; | 56 }; |
57 | 57 |
58 class FailingBackend : public PasswordStoreX::NativeBackend { | 58 class FailingBackend : public PasswordStoreX::NativeBackend { |
59 public: | 59 public: |
60 virtual bool Init() override { return true; } | 60 bool Init() override { return true; } |
61 | 61 |
62 virtual PasswordStoreChangeList AddLogin(const PasswordForm& form) override { | 62 PasswordStoreChangeList AddLogin(const PasswordForm& form) override { |
63 return PasswordStoreChangeList(); | 63 return PasswordStoreChangeList(); |
64 } | 64 } |
65 virtual bool UpdateLogin(const PasswordForm& form, | 65 bool UpdateLogin(const PasswordForm& form, |
66 PasswordStoreChangeList* changes) override { | 66 PasswordStoreChangeList* changes) override { |
67 return false; | 67 return false; |
68 } | 68 } |
69 virtual bool RemoveLogin(const PasswordForm& form) override { return false; } | 69 bool RemoveLogin(const PasswordForm& form) override { return false; } |
70 | 70 |
71 virtual bool RemoveLoginsCreatedBetween( | 71 bool RemoveLoginsCreatedBetween( |
72 base::Time delete_begin, | 72 base::Time delete_begin, |
73 base::Time delete_end, | 73 base::Time delete_end, |
74 password_manager::PasswordStoreChangeList* changes) override { | 74 password_manager::PasswordStoreChangeList* changes) override { |
75 return false; | 75 return false; |
76 } | 76 } |
77 | 77 |
78 virtual bool RemoveLoginsSyncedBetween( | 78 bool RemoveLoginsSyncedBetween( |
79 base::Time delete_begin, | 79 base::Time delete_begin, |
80 base::Time delete_end, | 80 base::Time delete_end, |
81 password_manager::PasswordStoreChangeList* changes) override { | 81 password_manager::PasswordStoreChangeList* changes) override { |
82 return false; | 82 return false; |
83 } | 83 } |
84 | 84 |
85 virtual bool GetLogins(const PasswordForm& form, | 85 bool GetLogins(const PasswordForm& form, PasswordFormList* forms) override { |
86 PasswordFormList* forms) override { | |
87 return false; | 86 return false; |
88 } | 87 } |
89 | 88 |
90 virtual bool GetAutofillableLogins(PasswordFormList* forms) override { | 89 bool GetAutofillableLogins(PasswordFormList* forms) override { return false; } |
91 return false; | 90 bool GetBlacklistLogins(PasswordFormList* forms) override { return false; } |
92 } | |
93 virtual bool GetBlacklistLogins(PasswordFormList* forms) override { | |
94 return false; | |
95 } | |
96 }; | 91 }; |
97 | 92 |
98 class MockBackend : public PasswordStoreX::NativeBackend { | 93 class MockBackend : public PasswordStoreX::NativeBackend { |
99 public: | 94 public: |
100 virtual bool Init() override { return true; } | 95 bool Init() override { return true; } |
101 | 96 |
102 virtual PasswordStoreChangeList AddLogin(const PasswordForm& form) override { | 97 PasswordStoreChangeList AddLogin(const PasswordForm& form) override { |
103 all_forms_.push_back(form); | 98 all_forms_.push_back(form); |
104 PasswordStoreChange change(PasswordStoreChange::ADD, form); | 99 PasswordStoreChange change(PasswordStoreChange::ADD, form); |
105 return PasswordStoreChangeList(1, change); | 100 return PasswordStoreChangeList(1, change); |
106 } | 101 } |
107 | 102 |
108 virtual bool UpdateLogin(const PasswordForm& form, | 103 bool UpdateLogin(const PasswordForm& form, |
109 PasswordStoreChangeList* changes) override { | 104 PasswordStoreChangeList* changes) override { |
110 for (size_t i = 0; i < all_forms_.size(); ++i) | 105 for (size_t i = 0; i < all_forms_.size(); ++i) |
111 if (CompareForms(all_forms_[i], form, true)) { | 106 if (CompareForms(all_forms_[i], form, true)) { |
112 all_forms_[i] = form; | 107 all_forms_[i] = form; |
113 changes->push_back(PasswordStoreChange(PasswordStoreChange::UPDATE, | 108 changes->push_back(PasswordStoreChange(PasswordStoreChange::UPDATE, |
114 form)); | 109 form)); |
115 } | 110 } |
116 return true; | 111 return true; |
117 } | 112 } |
118 | 113 |
119 virtual bool RemoveLogin(const PasswordForm& form) override { | 114 bool RemoveLogin(const PasswordForm& form) override { |
120 for (size_t i = 0; i < all_forms_.size(); ++i) | 115 for (size_t i = 0; i < all_forms_.size(); ++i) |
121 if (CompareForms(all_forms_[i], form, false)) | 116 if (CompareForms(all_forms_[i], form, false)) |
122 erase(i--); | 117 erase(i--); |
123 return true; | 118 return true; |
124 } | 119 } |
125 | 120 |
126 virtual bool RemoveLoginsCreatedBetween( | 121 bool RemoveLoginsCreatedBetween( |
127 base::Time delete_begin, | 122 base::Time delete_begin, |
128 base::Time delete_end, | 123 base::Time delete_end, |
129 password_manager::PasswordStoreChangeList* changes) override { | 124 password_manager::PasswordStoreChangeList* changes) override { |
130 for (size_t i = 0; i < all_forms_.size(); ++i) { | 125 for (size_t i = 0; i < all_forms_.size(); ++i) { |
131 if (delete_begin <= all_forms_[i].date_created && | 126 if (delete_begin <= all_forms_[i].date_created && |
132 (delete_end.is_null() || all_forms_[i].date_created < delete_end)) | 127 (delete_end.is_null() || all_forms_[i].date_created < delete_end)) |
133 erase(i--); | 128 erase(i--); |
134 } | 129 } |
135 return true; | 130 return true; |
136 } | 131 } |
137 | 132 |
138 virtual bool RemoveLoginsSyncedBetween( | 133 bool RemoveLoginsSyncedBetween( |
139 base::Time delete_begin, | 134 base::Time delete_begin, |
140 base::Time delete_end, | 135 base::Time delete_end, |
141 password_manager::PasswordStoreChangeList* changes) override { | 136 password_manager::PasswordStoreChangeList* changes) override { |
142 DCHECK(changes); | 137 DCHECK(changes); |
143 for (size_t i = 0; i < all_forms_.size(); ++i) { | 138 for (size_t i = 0; i < all_forms_.size(); ++i) { |
144 if (delete_begin <= all_forms_[i].date_synced && | 139 if (delete_begin <= all_forms_[i].date_synced && |
145 (delete_end.is_null() || all_forms_[i].date_synced < delete_end)) { | 140 (delete_end.is_null() || all_forms_[i].date_synced < delete_end)) { |
146 changes->push_back(password_manager::PasswordStoreChange( | 141 changes->push_back(password_manager::PasswordStoreChange( |
147 password_manager::PasswordStoreChange::REMOVE, all_forms_[i])); | 142 password_manager::PasswordStoreChange::REMOVE, all_forms_[i])); |
148 erase(i--); | 143 erase(i--); |
149 } | 144 } |
150 } | 145 } |
151 return true; | 146 return true; |
152 } | 147 } |
153 | 148 |
154 virtual bool GetLogins(const PasswordForm& form, | 149 bool GetLogins(const PasswordForm& form, PasswordFormList* forms) override { |
155 PasswordFormList* forms) override { | |
156 for (size_t i = 0; i < all_forms_.size(); ++i) | 150 for (size_t i = 0; i < all_forms_.size(); ++i) |
157 if (all_forms_[i].signon_realm == form.signon_realm) | 151 if (all_forms_[i].signon_realm == form.signon_realm) |
158 forms->push_back(new PasswordForm(all_forms_[i])); | 152 forms->push_back(new PasswordForm(all_forms_[i])); |
159 return true; | 153 return true; |
160 } | 154 } |
161 | 155 |
162 virtual bool GetAutofillableLogins(PasswordFormList* forms) override { | 156 bool GetAutofillableLogins(PasswordFormList* forms) override { |
163 for (size_t i = 0; i < all_forms_.size(); ++i) | 157 for (size_t i = 0; i < all_forms_.size(); ++i) |
164 if (!all_forms_[i].blacklisted_by_user) | 158 if (!all_forms_[i].blacklisted_by_user) |
165 forms->push_back(new PasswordForm(all_forms_[i])); | 159 forms->push_back(new PasswordForm(all_forms_[i])); |
166 return true; | 160 return true; |
167 } | 161 } |
168 | 162 |
169 virtual bool GetBlacklistLogins(PasswordFormList* forms) override { | 163 bool GetBlacklistLogins(PasswordFormList* forms) override { |
170 for (size_t i = 0; i < all_forms_.size(); ++i) | 164 for (size_t i = 0; i < all_forms_.size(); ++i) |
171 if (all_forms_[i].blacklisted_by_user) | 165 if (all_forms_[i].blacklisted_by_user) |
172 forms->push_back(new PasswordForm(all_forms_[i])); | 166 forms->push_back(new PasswordForm(all_forms_[i])); |
173 return true; | 167 return true; |
174 } | 168 } |
175 | 169 |
176 private: | 170 private: |
177 void erase(size_t index) { | 171 void erase(size_t index) { |
178 if (index < all_forms_.size() - 1) | 172 if (index < all_forms_.size() - 1) |
179 all_forms_[index] = all_forms_[all_forms_.size() - 1]; | 173 all_forms_[index] = all_forms_[all_forms_.size() - 1]; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } // anonymous namespace | 234 } // anonymous namespace |
241 | 235 |
242 enum BackendType { | 236 enum BackendType { |
243 NO_BACKEND, | 237 NO_BACKEND, |
244 FAILING_BACKEND, | 238 FAILING_BACKEND, |
245 WORKING_BACKEND | 239 WORKING_BACKEND |
246 }; | 240 }; |
247 | 241 |
248 class PasswordStoreXTest : public testing::TestWithParam<BackendType> { | 242 class PasswordStoreXTest : public testing::TestWithParam<BackendType> { |
249 protected: | 243 protected: |
250 virtual void SetUp() { | 244 void SetUp() override { |
251 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 245 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
252 | 246 |
253 login_db_.reset(new password_manager::LoginDatabase()); | 247 login_db_.reset(new password_manager::LoginDatabase()); |
254 ASSERT_TRUE(login_db_->Init(temp_dir_.path().Append("login_test"))); | 248 ASSERT_TRUE(login_db_->Init(temp_dir_.path().Append("login_test"))); |
255 } | 249 } |
256 | 250 |
257 virtual void TearDown() { | 251 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
258 base::RunLoop().RunUntilIdle(); | |
259 } | |
260 | 252 |
261 PasswordStoreX::NativeBackend* GetBackend() { | 253 PasswordStoreX::NativeBackend* GetBackend() { |
262 switch (GetParam()) { | 254 switch (GetParam()) { |
263 case FAILING_BACKEND: | 255 case FAILING_BACKEND: |
264 return new FailingBackend(); | 256 return new FailingBackend(); |
265 case WORKING_BACKEND: | 257 case WORKING_BACKEND: |
266 return new MockBackend(); | 258 return new MockBackend(); |
267 default: | 259 default: |
268 return NULL; | 260 return NULL; |
269 } | 261 } |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 | 455 |
464 INSTANTIATE_TEST_CASE_P(NoBackend, | 456 INSTANTIATE_TEST_CASE_P(NoBackend, |
465 PasswordStoreXTest, | 457 PasswordStoreXTest, |
466 testing::Values(NO_BACKEND)); | 458 testing::Values(NO_BACKEND)); |
467 INSTANTIATE_TEST_CASE_P(FailingBackend, | 459 INSTANTIATE_TEST_CASE_P(FailingBackend, |
468 PasswordStoreXTest, | 460 PasswordStoreXTest, |
469 testing::Values(FAILING_BACKEND)); | 461 testing::Values(FAILING_BACKEND)); |
470 INSTANTIATE_TEST_CASE_P(WorkingBackend, | 462 INSTANTIATE_TEST_CASE_P(WorkingBackend, |
471 PasswordStoreXTest, | 463 PasswordStoreXTest, |
472 testing::Values(WORKING_BACKEND)); | 464 testing::Values(WORKING_BACKEND)); |
OLD | NEW |