OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/affiliated_match_helper.h" | 5 #include "components/password_manager/core/browser/affiliated_match_helper.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "components/password_manager/core/browser/affiliation_service.h" | 20 #include "components/password_manager/core/browser/affiliation_service.h" |
21 #include "components/password_manager/core/browser/affiliation_utils.h" | 21 #include "components/password_manager/core/browser/affiliation_utils.h" |
22 #include "components/password_manager/core/browser/test_password_store.h" | 22 #include "components/password_manager/core/browser/test_password_store.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
25 | 25 |
26 namespace password_manager { | 26 namespace password_manager { |
27 | 27 |
28 namespace { | 28 namespace { |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 const char* origin) { | 159 const char* origin) { |
160 return {autofill::PasswordForm::SCHEME_HTML, signon_realm, | 160 return {autofill::PasswordForm::SCHEME_HTML, signon_realm, |
161 origin ? GURL(origin) : GURL()}; | 161 origin ? GURL(origin) : GURL()}; |
162 } | 162 } |
163 | 163 |
164 } // namespace | 164 } // namespace |
165 | 165 |
166 class AffiliatedMatchHelperTest : public testing::Test { | 166 class AffiliatedMatchHelperTest : public testing::Test { |
167 public: | 167 public: |
168 AffiliatedMatchHelperTest() | 168 AffiliatedMatchHelperTest() |
169 : waiting_task_runner_(new base::TestSimpleTaskRunner), | 169 : expecting_result_callback_(false), mock_affiliation_service_(nullptr) {} |
170 expecting_result_callback_(false), | |
171 mock_affiliation_service_(nullptr) {} | |
172 ~AffiliatedMatchHelperTest() override {} | 170 ~AffiliatedMatchHelperTest() override {} |
173 | 171 |
174 protected: | 172 protected: |
175 void RunDeferredInitialization() { | 173 void RunDeferredInitialization() { |
176 base::TimeDelta expected_init_delay = base::TimeDelta::FromSeconds( | 174 mock_time_task_runner_->RunUntilIdle(); |
177 AffiliatedMatchHelper::kInitializationDelayOnStartupInSeconds); | 175 ASSERT_EQ(AffiliatedMatchHelper::kInitializationDelayOnStartup, |
178 ASSERT_TRUE(waiting_task_runner()->HasPendingTask()); | 176 mock_time_task_runner_->NextPendingTaskDelay()); |
179 ASSERT_EQ(expected_init_delay, | 177 mock_time_task_runner_->FastForwardBy( |
180 waiting_task_runner()->NextPendingTaskDelay()); | 178 AffiliatedMatchHelper::kInitializationDelayOnStartup); |
181 waiting_task_runner()->RunUntilIdle(); | 179 } |
182 base::RunLoop().RunUntilIdle(); | 180 |
| 181 void RunUntilIdle() { |
| 182 // TODO(gab): Add support for base::RunLoop().RunUntilIdle() in scope of |
| 183 // ScopedMockTimeMessageLoopTaskRunner and use it instead of this helper |
| 184 // method. |
| 185 mock_time_task_runner_->RunUntilIdle(); |
183 } | 186 } |
184 | 187 |
185 void AddLogin(const autofill::PasswordForm& form) { | 188 void AddLogin(const autofill::PasswordForm& form) { |
186 password_store_->AddLogin(form); | 189 password_store_->AddLogin(form); |
187 base::RunLoop().RunUntilIdle(); | 190 RunUntilIdle(); |
188 } | 191 } |
189 | 192 |
190 void UpdateLoginWithPrimaryKey( | 193 void UpdateLoginWithPrimaryKey( |
191 const autofill::PasswordForm& new_form, | 194 const autofill::PasswordForm& new_form, |
192 const autofill::PasswordForm& old_primary_key) { | 195 const autofill::PasswordForm& old_primary_key) { |
193 password_store_->UpdateLoginWithPrimaryKey(new_form, old_primary_key); | 196 password_store_->UpdateLoginWithPrimaryKey(new_form, old_primary_key); |
194 base::RunLoop().RunUntilIdle(); | 197 RunUntilIdle(); |
195 } | 198 } |
196 | 199 |
197 void RemoveLogin(const autofill::PasswordForm& form) { | 200 void RemoveLogin(const autofill::PasswordForm& form) { |
198 password_store_->RemoveLogin(form); | 201 password_store_->RemoveLogin(form); |
199 base::RunLoop().RunUntilIdle(); | 202 RunUntilIdle(); |
200 } | 203 } |
201 | 204 |
202 void AddAndroidAndNonAndroidTestLogins() { | 205 void AddAndroidAndNonAndroidTestLogins() { |
203 AddLogin(GetTestAndroidCredentials(kTestAndroidRealmAlpha3)); | 206 AddLogin(GetTestAndroidCredentials(kTestAndroidRealmAlpha3)); |
204 AddLogin(GetTestAndroidCredentials(kTestAndroidRealmBeta2)); | 207 AddLogin(GetTestAndroidCredentials(kTestAndroidRealmBeta2)); |
205 AddLogin(GetTestBlacklistedAndroidCredentials(kTestAndroidRealmBeta3)); | 208 AddLogin(GetTestBlacklistedAndroidCredentials(kTestAndroidRealmBeta3)); |
206 AddLogin(GetTestAndroidCredentials(kTestAndroidRealmGamma)); | 209 AddLogin(GetTestAndroidCredentials(kTestAndroidRealmGamma)); |
207 | 210 |
208 AddLogin(GetTestAndroidCredentials(kTestWebRealmAlpha1)); | 211 AddLogin(GetTestAndroidCredentials(kTestWebRealmAlpha1)); |
209 AddLogin(GetTestAndroidCredentials(kTestWebRealmAlpha2)); | 212 AddLogin(GetTestAndroidCredentials(kTestWebRealmAlpha2)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 kTestAndroidFacetURIGamma); | 252 kTestAndroidFacetURIGamma); |
250 } | 253 } |
251 | 254 |
252 std::vector<std::string> GetAffiliatedAndroidRealms( | 255 std::vector<std::string> GetAffiliatedAndroidRealms( |
253 const PasswordStore::FormDigest& observed_form) { | 256 const PasswordStore::FormDigest& observed_form) { |
254 expecting_result_callback_ = true; | 257 expecting_result_callback_ = true; |
255 match_helper()->GetAffiliatedAndroidRealms( | 258 match_helper()->GetAffiliatedAndroidRealms( |
256 observed_form, | 259 observed_form, |
257 base::Bind(&AffiliatedMatchHelperTest::OnAffiliatedRealmsCallback, | 260 base::Bind(&AffiliatedMatchHelperTest::OnAffiliatedRealmsCallback, |
258 base::Unretained(this))); | 261 base::Unretained(this))); |
259 base::RunLoop().RunUntilIdle(); | 262 RunUntilIdle(); |
260 EXPECT_FALSE(expecting_result_callback_); | 263 EXPECT_FALSE(expecting_result_callback_); |
261 return last_result_realms_; | 264 return last_result_realms_; |
262 } | 265 } |
263 | 266 |
264 std::vector<std::string> GetAffiliatedWebRealms( | 267 std::vector<std::string> GetAffiliatedWebRealms( |
265 const PasswordStore::FormDigest& android_form) { | 268 const PasswordStore::FormDigest& android_form) { |
266 expecting_result_callback_ = true; | 269 expecting_result_callback_ = true; |
267 match_helper()->GetAffiliatedWebRealms( | 270 match_helper()->GetAffiliatedWebRealms( |
268 android_form, | 271 android_form, |
269 base::Bind(&AffiliatedMatchHelperTest::OnAffiliatedRealmsCallback, | 272 base::Bind(&AffiliatedMatchHelperTest::OnAffiliatedRealmsCallback, |
270 base::Unretained(this))); | 273 base::Unretained(this))); |
271 base::RunLoop().RunUntilIdle(); | 274 RunUntilIdle(); |
272 EXPECT_FALSE(expecting_result_callback_); | 275 EXPECT_FALSE(expecting_result_callback_); |
273 return last_result_realms_; | 276 return last_result_realms_; |
274 } | 277 } |
275 | 278 |
276 std::vector<std::unique_ptr<autofill::PasswordForm>> | 279 std::vector<std::unique_ptr<autofill::PasswordForm>> |
277 InjectAffiliatedWebRealms( | 280 InjectAffiliatedWebRealms( |
278 std::vector<std::unique_ptr<autofill::PasswordForm>> forms) { | 281 std::vector<std::unique_ptr<autofill::PasswordForm>> forms) { |
279 expecting_result_callback_ = true; | 282 expecting_result_callback_ = true; |
280 match_helper()->InjectAffiliatedWebRealms( | 283 match_helper()->InjectAffiliatedWebRealms( |
281 std::move(forms), | 284 std::move(forms), |
282 base::Bind(&AffiliatedMatchHelperTest::OnFormsCallback, | 285 base::Bind(&AffiliatedMatchHelperTest::OnFormsCallback, |
283 base::Unretained(this))); | 286 base::Unretained(this))); |
284 base::RunLoop().RunUntilIdle(); | 287 RunUntilIdle(); |
285 EXPECT_FALSE(expecting_result_callback_); | 288 EXPECT_FALSE(expecting_result_callback_); |
286 return std::move(last_result_forms_); | 289 return std::move(last_result_forms_); |
287 } | 290 } |
288 | 291 |
289 void DestroyMatchHelper() { match_helper_.reset(); } | 292 void DestroyMatchHelper() { match_helper_.reset(); } |
290 | 293 |
291 base::TestSimpleTaskRunner* waiting_task_runner() { | |
292 return waiting_task_runner_.get(); | |
293 } | |
294 | |
295 TestPasswordStore* password_store() { return password_store_.get(); } | 294 TestPasswordStore* password_store() { return password_store_.get(); } |
296 | 295 |
297 MockAffiliationService* mock_affiliation_service() { | 296 MockAffiliationService* mock_affiliation_service() { |
298 return mock_affiliation_service_; | 297 return mock_affiliation_service_; |
299 } | 298 } |
300 | 299 |
301 AffiliatedMatchHelper* match_helper() { return match_helper_.get(); } | 300 AffiliatedMatchHelper* match_helper() { return match_helper_.get(); } |
302 | 301 |
303 private: | 302 private: |
304 void OnAffiliatedRealmsCallback( | 303 void OnAffiliatedRealmsCallback( |
(...skipping 13 matching lines...) Expand all Loading... |
318 // testing::Test: | 317 // testing::Test: |
319 void SetUp() override { | 318 void SetUp() override { |
320 std::unique_ptr<MockAffiliationService> service( | 319 std::unique_ptr<MockAffiliationService> service( |
321 new MockAffiliationService()); | 320 new MockAffiliationService()); |
322 mock_affiliation_service_ = service.get(); | 321 mock_affiliation_service_ = service.get(); |
323 | 322 |
324 password_store_ = new TestPasswordStore; | 323 password_store_ = new TestPasswordStore; |
325 | 324 |
326 match_helper_.reset( | 325 match_helper_.reset( |
327 new AffiliatedMatchHelper(password_store_.get(), std::move(service))); | 326 new AffiliatedMatchHelper(password_store_.get(), std::move(service))); |
328 match_helper_->SetTaskRunnerUsedForWaitingForTesting(waiting_task_runner_); | |
329 } | 327 } |
330 | 328 |
331 void TearDown() override { | 329 void TearDown() override { |
332 match_helper_.reset(); | 330 match_helper_.reset(); |
333 password_store_->ShutdownOnUIThread(); | 331 password_store_->ShutdownOnUIThread(); |
334 password_store_ = nullptr; | 332 password_store_ = nullptr; |
335 } | 333 } |
336 | 334 |
337 scoped_refptr<base::TestSimpleTaskRunner> waiting_task_runner_; | |
338 base::MessageLoop message_loop_; | 335 base::MessageLoop message_loop_; |
| 336 base::ScopedMockTimeMessageLoopTaskRunner mock_time_task_runner_; |
| 337 |
339 std::vector<std::string> last_result_realms_; | 338 std::vector<std::string> last_result_realms_; |
340 std::vector<std::unique_ptr<autofill::PasswordForm>> last_result_forms_; | 339 std::vector<std::unique_ptr<autofill::PasswordForm>> last_result_forms_; |
341 bool expecting_result_callback_; | 340 bool expecting_result_callback_; |
342 | 341 |
343 scoped_refptr<TestPasswordStore> password_store_; | 342 scoped_refptr<TestPasswordStore> password_store_; |
344 std::unique_ptr<AffiliatedMatchHelper> match_helper_; | 343 std::unique_ptr<AffiliatedMatchHelper> match_helper_; |
345 | 344 |
346 // Owned by |match_helper_|. | 345 // Owned by |match_helper_|. |
347 MockAffiliationService* mock_affiliation_service_; | 346 MockAffiliationService* mock_affiliation_service_; |
348 | 347 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 AffiliatedMatchHelper::IsValidAndroidCredential(android_credential)); | 496 AffiliatedMatchHelper::IsValidAndroidCredential(android_credential)); |
498 } | 497 } |
499 | 498 |
500 // Verifies that affiliations for Android applications with pre-existing | 499 // Verifies that affiliations for Android applications with pre-existing |
501 // credentials on start-up are prefetched. | 500 // credentials on start-up are prefetched. |
502 TEST_F(AffiliatedMatchHelperTest, | 501 TEST_F(AffiliatedMatchHelperTest, |
503 PrefetchAffiliationsForPreexistingAndroidCredentialsOnStartup) { | 502 PrefetchAffiliationsForPreexistingAndroidCredentialsOnStartup) { |
504 AddAndroidAndNonAndroidTestLogins(); | 503 AddAndroidAndNonAndroidTestLogins(); |
505 | 504 |
506 match_helper()->Initialize(); | 505 match_helper()->Initialize(); |
507 base::RunLoop().RunUntilIdle(); | 506 RunUntilIdle(); |
508 | 507 |
509 ExpectPrefetchForAndroidTestLogins(); | 508 ExpectPrefetchForAndroidTestLogins(); |
510 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); | 509 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); |
511 } | 510 } |
512 | 511 |
513 // Stores credentials for Android applications between Initialize() and | 512 // Stores credentials for Android applications between Initialize() and |
514 // DoDeferredInitialization(). Verifies that corresponding affiliation | 513 // DoDeferredInitialization(). Verifies that corresponding affiliation |
515 // information gets prefetched. | 514 // information gets prefetched. |
516 TEST_F(AffiliatedMatchHelperTest, | 515 TEST_F(AffiliatedMatchHelperTest, |
517 PrefetchAffiliationsForAndroidCredentialsAddedInInitializationDelay) { | 516 PrefetchAffiliationsForAndroidCredentialsAddedInInitializationDelay) { |
518 match_helper()->Initialize(); | 517 match_helper()->Initialize(); |
519 base::RunLoop().RunUntilIdle(); | 518 RunUntilIdle(); |
520 | 519 |
521 AddAndroidAndNonAndroidTestLogins(); | 520 AddAndroidAndNonAndroidTestLogins(); |
522 | 521 |
523 ExpectPrefetchForAndroidTestLogins(); | 522 ExpectPrefetchForAndroidTestLogins(); |
524 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); | 523 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); |
525 } | 524 } |
526 | 525 |
527 // Stores credentials for Android applications after DoDeferredInitialization(). | 526 // Stores credentials for Android applications after DoDeferredInitialization(). |
528 // Verifies that corresponding affiliation information gets prefetched. | 527 // Verifies that corresponding affiliation information gets prefetched. |
529 TEST_F(AffiliatedMatchHelperTest, | 528 TEST_F(AffiliatedMatchHelperTest, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 autofill::PasswordForm android_form( | 586 autofill::PasswordForm android_form( |
588 GetTestAndroidCredentials(kTestAndroidRealmAlpha3)); | 587 GetTestAndroidCredentials(kTestAndroidRealmAlpha3)); |
589 AddLogin(android_form); | 588 AddLogin(android_form); |
590 | 589 |
591 // Store two credentials before initialization. | 590 // Store two credentials before initialization. |
592 autofill::PasswordForm android_form2(android_form); | 591 autofill::PasswordForm android_form2(android_form); |
593 android_form2.username_value = base::ASCIIToUTF16("JohnDoe2"); | 592 android_form2.username_value = base::ASCIIToUTF16("JohnDoe2"); |
594 AddLogin(android_form2); | 593 AddLogin(android_form2); |
595 | 594 |
596 match_helper()->Initialize(); | 595 match_helper()->Initialize(); |
597 base::RunLoop().RunUntilIdle(); | 596 RunUntilIdle(); |
598 | 597 |
599 // Store one credential between initialization and deferred initialization. | 598 // Store one credential between initialization and deferred initialization. |
600 autofill::PasswordForm android_form3(android_form); | 599 autofill::PasswordForm android_form3(android_form); |
601 android_form3.username_value = base::ASCIIToUTF16("JohnDoe3"); | 600 android_form3.username_value = base::ASCIIToUTF16("JohnDoe3"); |
602 AddLogin(android_form3); | 601 AddLogin(android_form3); |
603 | 602 |
604 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); | 603 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); |
605 | 604 |
606 // Store one credential after deferred initialization. | 605 // Store one credential after deferred initialization. |
607 autofill::PasswordForm android_form4(android_form); | 606 autofill::PasswordForm android_form4(android_form); |
608 android_form4.username_value = base::ASCIIToUTF16("JohnDoe4"); | 607 android_form4.username_value = base::ASCIIToUTF16("JohnDoe4"); |
609 AddLogin(android_form4); | 608 AddLogin(android_form4); |
610 | 609 |
611 for (size_t i = 0; i < 4; ++i) { | 610 for (size_t i = 0; i < 4; ++i) { |
612 mock_affiliation_service()->ExpectCallToCancelPrefetch( | 611 mock_affiliation_service()->ExpectCallToCancelPrefetch( |
613 kTestAndroidFacetURIAlpha3); | 612 kTestAndroidFacetURIAlpha3); |
614 mock_affiliation_service()->ExpectCallToTrimCacheForFacet( | 613 mock_affiliation_service()->ExpectCallToTrimCacheForFacet( |
615 kTestAndroidFacetURIAlpha3); | 614 kTestAndroidFacetURIAlpha3); |
616 } | 615 } |
617 | 616 |
618 RemoveLogin(android_form); | 617 RemoveLogin(android_form); |
619 RemoveLogin(android_form2); | 618 RemoveLogin(android_form2); |
620 RemoveLogin(android_form3); | 619 RemoveLogin(android_form3); |
621 RemoveLogin(android_form4); | 620 RemoveLogin(android_form4); |
622 } | 621 } |
623 | 622 |
624 TEST_F(AffiliatedMatchHelperTest, DestroyBeforeDeferredInitialization) { | 623 TEST_F(AffiliatedMatchHelperTest, DestroyBeforeDeferredInitialization) { |
625 match_helper()->Initialize(); | 624 match_helper()->Initialize(); |
626 base::RunLoop().RunUntilIdle(); | 625 RunUntilIdle(); |
627 DestroyMatchHelper(); | 626 DestroyMatchHelper(); |
628 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); | 627 ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization()); |
629 } | 628 } |
630 | 629 |
631 } // namespace password_manager | 630 } // namespace password_manager |
OLD | NEW |