| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/platform_file.h" | 9 #include "base/platform_file.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/password_manager/password_form_data.h" | 17 #include "chrome/browser/password_manager/password_form_data.h" |
| 18 #include "chrome/browser/password_manager/password_store_change.h" | 18 #include "chrome/browser/password_manager/password_store_change.h" |
| 19 #include "chrome/browser/password_manager/password_store_consumer.h" | 19 #include "chrome/browser/password_manager/password_store_consumer.h" |
| 20 #include "chrome/browser/password_manager/password_store_x.h" | 20 #include "chrome/browser/password_manager/password_store_x.h" |
| 21 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/browser/webdata/web_data_service.h" | |
| 23 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/test/base/testing_browser_process.h" | 24 #include "chrome/test/base/testing_browser_process.h" |
| 26 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 27 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
| 28 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
| 29 #include "content/public/browser/notification_source.h" | 28 #include "content/public/browser/notification_source.h" |
| 30 #include "content/test/notification_observer_mock.h" | 29 #include "content/test/notification_observer_mock.h" |
| 31 #include "content/test/test_browser_thread.h" | 30 #include "content/test/test_browser_thread.h" |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 46 | 45 |
| 47 namespace { | 46 namespace { |
| 48 | 47 |
| 49 class MockPasswordStoreConsumer : public PasswordStoreConsumer { | 48 class MockPasswordStoreConsumer : public PasswordStoreConsumer { |
| 50 public: | 49 public: |
| 51 MOCK_METHOD2(OnPasswordStoreRequestDone, | 50 MOCK_METHOD2(OnPasswordStoreRequestDone, |
| 52 void(CancelableRequestProvider::Handle, | 51 void(CancelableRequestProvider::Handle, |
| 53 const std::vector<PasswordForm*>&)); | 52 const std::vector<PasswordForm*>&)); |
| 54 }; | 53 }; |
| 55 | 54 |
| 56 class MockWebDataServiceConsumer : public WebDataServiceConsumer { | |
| 57 public: | |
| 58 MOCK_METHOD2(OnWebDataServiceRequestDone, void(WebDataService::Handle, | |
| 59 const WDTypedResult*)); | |
| 60 }; | |
| 61 | |
| 62 // This class will add and remove a mock notification observer from | 55 // This class will add and remove a mock notification observer from |
| 63 // the DB thread. | 56 // the DB thread. |
| 64 class DBThreadObserverHelper | 57 class DBThreadObserverHelper |
| 65 : public base::RefCountedThreadSafe<DBThreadObserverHelper, | 58 : public base::RefCountedThreadSafe<DBThreadObserverHelper, |
| 66 BrowserThread::DeleteOnDBThread> { | 59 BrowserThread::DeleteOnDBThread> { |
| 67 public: | 60 public: |
| 68 DBThreadObserverHelper() : done_event_(true, false) {} | 61 DBThreadObserverHelper() : done_event_(true, false) {} |
| 69 | 62 |
| 70 void Init(PasswordStore* password_store) { | 63 void Init(PasswordStore* password_store) { |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 264 } |
| 272 | 265 |
| 273 virtual void SetUp() { | 266 virtual void SetUp() { |
| 274 ASSERT_TRUE(db_thread_.Start()); | 267 ASSERT_TRUE(db_thread_.Start()); |
| 275 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 268 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 276 | 269 |
| 277 profile_.reset(new TestingProfile()); | 270 profile_.reset(new TestingProfile()); |
| 278 | 271 |
| 279 login_db_.reset(new LoginDatabase()); | 272 login_db_.reset(new LoginDatabase()); |
| 280 ASSERT_TRUE(login_db_->Init(temp_dir_.path().Append("login_test"))); | 273 ASSERT_TRUE(login_db_->Init(temp_dir_.path().Append("login_test"))); |
| 281 | |
| 282 wds_ = new WebDataService(); | |
| 283 ASSERT_TRUE(wds_->Init(temp_dir_.path())); | |
| 284 } | 274 } |
| 285 | 275 |
| 286 virtual void TearDown() { | 276 virtual void TearDown() { |
| 287 wds_->Shutdown(); | |
| 288 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 277 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 289 MessageLoop::current()->Run(); | 278 MessageLoop::current()->Run(); |
| 290 db_thread_.Stop(); | 279 db_thread_.Stop(); |
| 291 } | 280 } |
| 292 | 281 |
| 293 PasswordStoreX::NativeBackend* GetBackend() { | 282 PasswordStoreX::NativeBackend* GetBackend() { |
| 294 switch (GetParam()) { | 283 switch (GetParam()) { |
| 295 case FAILING_BACKEND: | 284 case FAILING_BACKEND: |
| 296 return new FailingBackend(); | 285 return new FailingBackend(); |
| 297 case WORKING_BACKEND: | 286 case WORKING_BACKEND: |
| 298 return new MockBackend(); | 287 return new MockBackend(); |
| 299 default: | 288 default: |
| 300 return NULL; | 289 return NULL; |
| 301 } | 290 } |
| 302 } | 291 } |
| 303 | 292 |
| 304 MessageLoopForUI message_loop_; | 293 MessageLoopForUI message_loop_; |
| 305 content::TestBrowserThread ui_thread_; | 294 content::TestBrowserThread ui_thread_; |
| 306 // PasswordStore, WDS schedule work on this thread. | 295 // PasswordStore, WDS schedule work on this thread. |
| 307 content::TestBrowserThread db_thread_; | 296 content::TestBrowserThread db_thread_; |
| 308 | 297 |
| 309 scoped_ptr<LoginDatabase> login_db_; | 298 scoped_ptr<LoginDatabase> login_db_; |
| 310 scoped_ptr<TestingProfile> profile_; | 299 scoped_ptr<TestingProfile> profile_; |
| 311 scoped_refptr<WebDataService> wds_; | |
| 312 ScopedTempDir temp_dir_; | 300 ScopedTempDir temp_dir_; |
| 313 }; | 301 }; |
| 314 | 302 |
| 315 ACTION(STLDeleteElements0) { | 303 ACTION(STLDeleteElements0) { |
| 316 STLDeleteContainerPointers(arg0.begin(), arg0.end()); | 304 STLDeleteContainerPointers(arg0.begin(), arg0.end()); |
| 317 } | 305 } |
| 318 | 306 |
| 319 ACTION(QuitUIMessageLoop) { | 307 ACTION(QuitUIMessageLoop) { |
| 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 321 MessageLoop::current()->Quit(); | 309 MessageLoop::current()->Quit(); |
| 322 } | 310 } |
| 323 | 311 |
| 324 MATCHER(EmptyWDResult, "") { | 312 TEST_P(PasswordStoreXTest, Notifications) { |
| 325 return static_cast<const WDResult<std::vector<PasswordForm*> >*>( | |
| 326 arg)->GetValue().empty(); | |
| 327 } | |
| 328 | |
| 329 TEST_P(PasswordStoreXTest, WDSMigration) { | |
| 330 VectorOfForms expected_autofillable; | |
| 331 InitExpectedForms(true, 5, &expected_autofillable); | |
| 332 | |
| 333 VectorOfForms expected_blacklisted; | |
| 334 InitExpectedForms(false, 5, &expected_blacklisted); | |
| 335 | |
| 336 // Populate the WDS with logins that should be migrated. | |
| 337 for (VectorOfForms::iterator it = expected_autofillable.begin(); | |
| 338 it != expected_autofillable.end(); ++it) { | |
| 339 wds_->AddLogin(**it); | |
| 340 } | |
| 341 for (VectorOfForms::iterator it = expected_blacklisted.begin(); | |
| 342 it != expected_blacklisted.end(); ++it) { | |
| 343 wds_->AddLogin(**it); | |
| 344 } | |
| 345 | |
| 346 // The WDS schedules tasks to run on the DB thread so we schedule yet another | |
| 347 // task to notify us that it's safe to carry on with the test. | |
| 348 WaitableEvent done(false, false); | |
| 349 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 350 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | |
| 351 done.Wait(); | |
| 352 | |
| 353 // Initializing the PasswordStore should trigger a migration. | |
| 354 scoped_refptr<PasswordStoreX> store( | 313 scoped_refptr<PasswordStoreX> store( |
| 355 new PasswordStoreX(login_db_.release(), | 314 new PasswordStoreX(login_db_.release(), |
| 356 profile_.get(), | 315 profile_.get(), |
| 357 wds_.get(), | |
| 358 GetBackend())); | 316 GetBackend())); |
| 359 store->Init(); | 317 store->Init(); |
| 360 | 318 |
| 361 // Check that the migration preference has not been initialized. | |
| 362 ASSERT_TRUE(NULL == profile_->GetPrefs()->FindPreference( | |
| 363 prefs::kLoginDatabaseMigrated)); | |
| 364 | |
| 365 // Again, the WDS schedules tasks to run on the DB thread, so schedule a task | |
| 366 // to signal us when it is safe to continue. | |
| 367 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 368 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | |
| 369 done.Wait(); | |
| 370 | |
| 371 // Let the WDS callbacks proceed so the logins can be migrated. | |
| 372 MessageLoop::current()->RunAllPending(); | |
| 373 | |
| 374 MockPasswordStoreConsumer consumer; | |
| 375 | |
| 376 // Make sure we quit the MessageLoop even if the test fails. | |
| 377 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) | |
| 378 .WillByDefault(QuitUIMessageLoop()); | |
| 379 | |
| 380 // The autofillable forms should have been migrated from the WDS to the login | |
| 381 // database. | |
| 382 EXPECT_CALL(consumer, | |
| 383 OnPasswordStoreRequestDone(_, | |
| 384 ContainsAllPasswordForms(expected_autofillable))) | |
| 385 .WillOnce(DoAll(WithArg<1>(STLDeleteElements0()), QuitUIMessageLoop())); | |
| 386 | |
| 387 store->GetAutofillableLogins(&consumer); | |
| 388 MessageLoop::current()->Run(); | |
| 389 | |
| 390 // The blacklisted forms should have been migrated from the WDS to the login | |
| 391 // database. | |
| 392 EXPECT_CALL(consumer, | |
| 393 OnPasswordStoreRequestDone(_, | |
| 394 ContainsAllPasswordForms(expected_blacklisted))) | |
| 395 .WillOnce(DoAll(WithArg<1>(STLDeleteElements0()), QuitUIMessageLoop())); | |
| 396 | |
| 397 store->GetBlacklistLogins(&consumer); | |
| 398 MessageLoop::current()->Run(); | |
| 399 | |
| 400 // Check that the migration updated the migrated preference. | |
| 401 ASSERT_TRUE(profile_->GetPrefs()->GetBoolean(prefs::kLoginDatabaseMigrated)); | |
| 402 | |
| 403 MockWebDataServiceConsumer wds_consumer; | |
| 404 | |
| 405 // No autofillable logins should be left in the WDS. | |
| 406 EXPECT_CALL(wds_consumer, | |
| 407 OnWebDataServiceRequestDone(_, EmptyWDResult())); | |
| 408 | |
| 409 wds_->GetAutofillableLogins(&wds_consumer); | |
| 410 | |
| 411 // Wait for the WDS methods to execute on the DB thread. | |
| 412 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 413 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | |
| 414 done.Wait(); | |
| 415 | |
| 416 // Handle the callback from the WDS. | |
| 417 MessageLoop::current()->RunAllPending(); | |
| 418 | |
| 419 // Likewise, no blacklisted logins should be left in the WDS. | |
| 420 EXPECT_CALL(wds_consumer, | |
| 421 OnWebDataServiceRequestDone(_, EmptyWDResult())); | |
| 422 | |
| 423 wds_->GetBlacklistLogins(&wds_consumer); | |
| 424 | |
| 425 // Wait for the WDS methods to execute on the DB thread. | |
| 426 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 427 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | |
| 428 done.Wait(); | |
| 429 | |
| 430 // Handle the callback from the WDS. | |
| 431 MessageLoop::current()->RunAllPending(); | |
| 432 | |
| 433 STLDeleteElements(&expected_autofillable); | |
| 434 STLDeleteElements(&expected_blacklisted); | |
| 435 | |
| 436 // Public in PasswordStore, protected in PasswordStoreX. | |
| 437 static_cast<PasswordStore*>(store)->ShutdownOnUIThread(); | |
| 438 } | |
| 439 | |
| 440 TEST_P(PasswordStoreXTest, WDSMigrationAlreadyDone) { | |
| 441 PasswordFormData wds_data[] = { | |
| 442 { PasswordForm::SCHEME_HTML, | |
| 443 "http://bar.example.com", | |
| 444 "http://bar.example.com/origin", | |
| 445 "http://bar.example.com/action", | |
| 446 L"submit_element", | |
| 447 L"username_element", | |
| 448 L"password_element", | |
| 449 L"username_value", | |
| 450 L"password_value", | |
| 451 true, false, 1 }, | |
| 452 }; | |
| 453 | |
| 454 VectorOfForms unexpected_autofillable; | |
| 455 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(wds_data); ++i) { | |
| 456 unexpected_autofillable.push_back( | |
| 457 CreatePasswordFormFromData(wds_data[i])); | |
| 458 } | |
| 459 | |
| 460 // Populate the WDS with logins that should be migrated. | |
| 461 for (VectorOfForms::iterator it = unexpected_autofillable.begin(); | |
| 462 it != unexpected_autofillable.end(); ++it) { | |
| 463 wds_->AddLogin(**it); | |
| 464 } | |
| 465 | |
| 466 // The WDS schedules tasks to run on the DB thread so we schedule yet another | |
| 467 // task to notify us that it's safe to carry on with the test. | |
| 468 WaitableEvent done(false, false); | |
| 469 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
| 470 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | |
| 471 done.Wait(); | |
| 472 | |
| 473 // Pretend that the migration has already taken place. | |
| 474 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated, | |
| 475 true, | |
| 476 PrefService::UNSYNCABLE_PREF); | |
| 477 | |
| 478 // Initializing the PasswordStore shouldn't trigger a migration. | |
| 479 scoped_refptr<PasswordStoreX> store( | |
| 480 new PasswordStoreX(login_db_.release(), | |
| 481 profile_.get(), | |
| 482 wds_.get(), | |
| 483 GetBackend())); | |
| 484 store->Init(); | |
| 485 | |
| 486 MockPasswordStoreConsumer consumer; | |
| 487 // Make sure we quit the MessageLoop even if the test fails. | |
| 488 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) | |
| 489 .WillByDefault(QuitUIMessageLoop()); | |
| 490 | |
| 491 // No forms should be migrated. | |
| 492 VectorOfForms empty; | |
| 493 EXPECT_CALL(consumer, | |
| 494 OnPasswordStoreRequestDone(_, | |
| 495 ContainsAllPasswordForms(empty))) | |
| 496 .WillOnce(QuitUIMessageLoop()); | |
| 497 | |
| 498 store->GetAutofillableLogins(&consumer); | |
| 499 MessageLoop::current()->Run(); | |
| 500 | |
| 501 STLDeleteElements(&unexpected_autofillable); | |
| 502 | |
| 503 // Public in PasswordStore, protected in PasswordStoreX. | |
| 504 static_cast<PasswordStore*>(store)->ShutdownOnUIThread(); | |
| 505 } | |
| 506 | |
| 507 TEST_P(PasswordStoreXTest, Notifications) { | |
| 508 // Pretend that the migration has already taken place. | |
| 509 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated, | |
| 510 true, | |
| 511 PrefService::UNSYNCABLE_PREF); | |
| 512 | |
| 513 // Initializing the PasswordStore shouldn't trigger a migration. | |
| 514 scoped_refptr<PasswordStoreX> store( | |
| 515 new PasswordStoreX(login_db_.release(), | |
| 516 profile_.get(), | |
| 517 wds_.get(), | |
| 518 GetBackend())); | |
| 519 store->Init(); | |
| 520 | |
| 521 PasswordFormData form_data = | 319 PasswordFormData form_data = |
| 522 { PasswordForm::SCHEME_HTML, | 320 { PasswordForm::SCHEME_HTML, |
| 523 "http://bar.example.com", | 321 "http://bar.example.com", |
| 524 "http://bar.example.com/origin", | 322 "http://bar.example.com/origin", |
| 525 "http://bar.example.com/action", | 323 "http://bar.example.com/action", |
| 526 L"submit_element", | 324 L"submit_element", |
| 527 L"username_element", | 325 L"username_element", |
| 528 L"password_element", | 326 L"password_element", |
| 529 L"username_value", | 327 L"username_value", |
| 530 L"password_value", | 328 L"password_value", |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 WaitableEvent done(false, false); | 434 WaitableEvent done(false, false); |
| 637 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 435 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 638 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | 436 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); |
| 639 done.Wait(); | 437 done.Wait(); |
| 640 | 438 |
| 641 // Get the new size of the login DB file. We expect it to be larger. | 439 // Get the new size of the login DB file. We expect it to be larger. |
| 642 base::PlatformFileInfo db_file_full_info; | 440 base::PlatformFileInfo db_file_full_info; |
| 643 ASSERT_TRUE(file_util::GetFileInfo(login_db_file, &db_file_full_info)); | 441 ASSERT_TRUE(file_util::GetFileInfo(login_db_file, &db_file_full_info)); |
| 644 EXPECT_GT(db_file_full_info.size, db_file_start_info.size); | 442 EXPECT_GT(db_file_full_info.size, db_file_start_info.size); |
| 645 | 443 |
| 646 // Pretend that the WDS migration has already taken place. | |
| 647 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated, | |
| 648 true, | |
| 649 PrefService::UNSYNCABLE_PREF); | |
| 650 | |
| 651 // Initializing the PasswordStore shouldn't trigger a native migration (yet). | 444 // Initializing the PasswordStore shouldn't trigger a native migration (yet). |
| 652 scoped_refptr<PasswordStoreX> store( | 445 scoped_refptr<PasswordStoreX> store( |
| 653 new PasswordStoreX(login_db_.release(), | 446 new PasswordStoreX(login_db_.release(), |
| 654 profile_.get(), | 447 profile_.get(), |
| 655 wds_.get(), | |
| 656 GetBackend())); | 448 GetBackend())); |
| 657 store->Init(); | 449 store->Init(); |
| 658 | 450 |
| 659 MockPasswordStoreConsumer consumer; | 451 MockPasswordStoreConsumer consumer; |
| 660 | 452 |
| 661 // Make sure we quit the MessageLoop even if the test fails. | 453 // Make sure we quit the MessageLoop even if the test fails. |
| 662 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) | 454 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) |
| 663 .WillByDefault(QuitUIMessageLoop()); | 455 .WillByDefault(QuitUIMessageLoop()); |
| 664 | 456 |
| 665 // The autofillable forms should have been migrated to the native backend. | 457 // The autofillable forms should have been migrated to the native backend. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 | 537 |
| 746 INSTANTIATE_TEST_CASE_P(NoBackend, | 538 INSTANTIATE_TEST_CASE_P(NoBackend, |
| 747 PasswordStoreXTest, | 539 PasswordStoreXTest, |
| 748 testing::Values(NO_BACKEND)); | 540 testing::Values(NO_BACKEND)); |
| 749 INSTANTIATE_TEST_CASE_P(FailingBackend, | 541 INSTANTIATE_TEST_CASE_P(FailingBackend, |
| 750 PasswordStoreXTest, | 542 PasswordStoreXTest, |
| 751 testing::Values(FAILING_BACKEND)); | 543 testing::Values(FAILING_BACKEND)); |
| 752 INSTANTIATE_TEST_CASE_P(WorkingBackend, | 544 INSTANTIATE_TEST_CASE_P(WorkingBackend, |
| 753 PasswordStoreXTest, | 545 PasswordStoreXTest, |
| 754 testing::Values(WORKING_BACKEND)); | 546 testing::Values(WORKING_BACKEND)); |
| OLD | NEW |