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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 enum BackendType { | 232 enum BackendType { |
233 NO_BACKEND, | 233 NO_BACKEND, |
234 FAILING_BACKEND, | 234 FAILING_BACKEND, |
235 WORKING_BACKEND | 235 WORKING_BACKEND |
236 }; | 236 }; |
237 | 237 |
238 class PasswordStoreXTest : public testing::TestWithParam<BackendType> { | 238 class PasswordStoreXTest : public testing::TestWithParam<BackendType> { |
239 protected: | 239 protected: |
240 void SetUp() override { | 240 void SetUp() override { |
241 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 241 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
242 | |
243 login_db_.reset(new password_manager::LoginDatabase()); | |
244 ASSERT_TRUE(login_db_->Init(temp_dir_.path().Append("login_test"))); | |
245 } | 242 } |
246 | 243 |
247 void TearDown() override { base::RunLoop().RunUntilIdle(); } | 244 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
248 | 245 |
| 246 base::FilePath test_login_db_file_path() const { |
| 247 return temp_dir_.path().Append(FILE_PATH_LITERAL("login_test")); |
| 248 } |
| 249 |
249 PasswordStoreX::NativeBackend* GetBackend() { | 250 PasswordStoreX::NativeBackend* GetBackend() { |
250 switch (GetParam()) { | 251 switch (GetParam()) { |
251 case FAILING_BACKEND: | 252 case FAILING_BACKEND: |
252 return new FailingBackend(); | 253 return new FailingBackend(); |
253 case WORKING_BACKEND: | 254 case WORKING_BACKEND: |
254 return new MockBackend(); | 255 return new MockBackend(); |
255 default: | 256 default: |
256 return NULL; | 257 return NULL; |
257 } | 258 } |
258 } | 259 } |
259 | 260 |
260 content::TestBrowserThreadBundle thread_bundle_; | 261 content::TestBrowserThreadBundle thread_bundle_; |
261 | 262 |
262 scoped_ptr<password_manager::LoginDatabase> login_db_; | |
263 base::ScopedTempDir temp_dir_; | 263 base::ScopedTempDir temp_dir_; |
264 }; | 264 }; |
265 | 265 |
266 ACTION(STLDeleteElements0) { | 266 ACTION(STLDeleteElements0) { |
267 STLDeleteContainerPointers(arg0.begin(), arg0.end()); | 267 STLDeleteContainerPointers(arg0.begin(), arg0.end()); |
268 } | 268 } |
269 | 269 |
270 TEST_P(PasswordStoreXTest, Notifications) { | 270 TEST_P(PasswordStoreXTest, Notifications) { |
271 scoped_refptr<PasswordStoreX> store( | 271 scoped_refptr<PasswordStoreX> store( |
272 new PasswordStoreX(base::MessageLoopProxy::current(), | 272 new PasswordStoreX(base::MessageLoopProxy::current(), |
273 base::MessageLoopProxy::current(), | 273 base::MessageLoopProxy::current(), |
274 login_db_.release(), | 274 test_login_db_file_path(), |
275 GetBackend())); | 275 GetBackend())); |
276 store->Init(syncer::SyncableService::StartSyncFlare()); | 276 store->Init(syncer::SyncableService::StartSyncFlare()); |
277 | 277 |
278 password_manager::PasswordFormData form_data = { | 278 password_manager::PasswordFormData form_data = { |
279 PasswordForm::SCHEME_HTML, "http://bar.example.com", | 279 PasswordForm::SCHEME_HTML, "http://bar.example.com", |
280 "http://bar.example.com/origin", "http://bar.example.com/action", | 280 "http://bar.example.com/origin", "http://bar.example.com/action", |
281 L"submit_element", L"username_element", | 281 L"submit_element", L"username_element", |
282 L"password_element", L"username_value", | 282 L"password_element", L"username_value", |
283 L"password_value", true, | 283 L"password_value", true, |
284 false, 1}; | 284 false, 1}; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 store->Shutdown(); | 338 store->Shutdown(); |
339 } | 339 } |
340 | 340 |
341 TEST_P(PasswordStoreXTest, NativeMigration) { | 341 TEST_P(PasswordStoreXTest, NativeMigration) { |
342 VectorOfForms expected_autofillable; | 342 VectorOfForms expected_autofillable; |
343 InitExpectedForms(true, 50, &expected_autofillable); | 343 InitExpectedForms(true, 50, &expected_autofillable); |
344 | 344 |
345 VectorOfForms expected_blacklisted; | 345 VectorOfForms expected_blacklisted; |
346 InitExpectedForms(false, 50, &expected_blacklisted); | 346 InitExpectedForms(false, 50, &expected_blacklisted); |
347 | 347 |
| 348 const base::FilePath login_db_file = test_login_db_file_path(); |
| 349 scoped_ptr<password_manager::LoginDatabase> login_db( |
| 350 new password_manager::LoginDatabase()); |
| 351 ASSERT_TRUE(login_db->Init(login_db_file)); |
| 352 |
348 // Get the initial size of the login DB file, before we populate it. | 353 // Get the initial size of the login DB file, before we populate it. |
349 // This will be used later to make sure it gets back to this size. | 354 // This will be used later to make sure it gets back to this size. |
350 const base::FilePath login_db_file = temp_dir_.path().Append("login_test"); | |
351 base::File::Info db_file_start_info; | 355 base::File::Info db_file_start_info; |
352 ASSERT_TRUE(base::GetFileInfo(login_db_file, &db_file_start_info)); | 356 ASSERT_TRUE(base::GetFileInfo(login_db_file, &db_file_start_info)); |
353 | 357 |
354 password_manager::LoginDatabase* login_db = login_db_.get(); | |
355 | |
356 // Populate the login DB with logins that should be migrated. | 358 // Populate the login DB with logins that should be migrated. |
357 for (VectorOfForms::iterator it = expected_autofillable.begin(); | 359 for (VectorOfForms::iterator it = expected_autofillable.begin(); |
358 it != expected_autofillable.end(); ++it) { | 360 it != expected_autofillable.end(); ++it) { |
359 login_db->AddLogin(**it); | 361 login_db->AddLogin(**it); |
360 } | 362 } |
361 for (VectorOfForms::iterator it = expected_blacklisted.begin(); | 363 for (VectorOfForms::iterator it = expected_blacklisted.begin(); |
362 it != expected_blacklisted.end(); ++it) { | 364 it != expected_blacklisted.end(); ++it) { |
363 login_db->AddLogin(**it); | 365 login_db->AddLogin(**it); |
364 } | 366 } |
365 | 367 |
| 368 login_db.reset(); |
| 369 |
366 // Get the new size of the login DB file. We expect it to be larger. | 370 // Get the new size of the login DB file. We expect it to be larger. |
367 base::File::Info db_file_full_info; | 371 base::File::Info db_file_full_info; |
368 ASSERT_TRUE(base::GetFileInfo(login_db_file, &db_file_full_info)); | 372 ASSERT_TRUE(base::GetFileInfo(login_db_file, &db_file_full_info)); |
369 EXPECT_GT(db_file_full_info.size, db_file_start_info.size); | 373 EXPECT_GT(db_file_full_info.size, db_file_start_info.size); |
370 | 374 |
371 // Initializing the PasswordStore shouldn't trigger a native migration (yet). | 375 // Initializing the PasswordStore shouldn't trigger a native migration (yet). |
372 scoped_refptr<PasswordStoreX> store( | 376 scoped_refptr<PasswordStoreX> store( |
373 new PasswordStoreX(base::MessageLoopProxy::current(), | 377 new PasswordStoreX(base::MessageLoopProxy::current(), |
374 base::MessageLoopProxy::current(), | 378 base::MessageLoopProxy::current(), |
375 login_db_.release(), | 379 login_db_file, |
376 GetBackend())); | 380 GetBackend())); |
377 store->Init(syncer::SyncableService::StartSyncFlare()); | 381 store->Init(syncer::SyncableService::StartSyncFlare()); |
378 | 382 |
379 MockPasswordStoreConsumer consumer; | 383 MockPasswordStoreConsumer consumer; |
380 | 384 |
381 // The autofillable forms should have been migrated to the native backend. | 385 // The autofillable forms should have been migrated to the native backend. |
382 EXPECT_CALL(consumer, | 386 EXPECT_CALL(consumer, |
383 OnGetPasswordStoreResults( | 387 OnGetPasswordStoreResults( |
384 ContainsAllPasswordForms(expected_autofillable))) | 388 ContainsAllPasswordForms(expected_autofillable))) |
385 .WillOnce(WithArg<0>(STLDeleteElements0())); | 389 .WillOnce(WithArg<0>(STLDeleteElements0())); |
(...skipping 17 matching lines...) Expand all Loading... |
403 EXPECT_CALL(ld_return, | 407 EXPECT_CALL(ld_return, |
404 OnLoginDatabaseQueryDone(ContainsAllPasswordForms(empty))); | 408 OnLoginDatabaseQueryDone(ContainsAllPasswordForms(empty))); |
405 } else { | 409 } else { |
406 // The autofillable logins should still be in the login DB. | 410 // The autofillable logins should still be in the login DB. |
407 EXPECT_CALL(ld_return, | 411 EXPECT_CALL(ld_return, |
408 OnLoginDatabaseQueryDone( | 412 OnLoginDatabaseQueryDone( |
409 ContainsAllPasswordForms(expected_autofillable))) | 413 ContainsAllPasswordForms(expected_autofillable))) |
410 .WillOnce(WithArg<0>(STLDeleteElements0())); | 414 .WillOnce(WithArg<0>(STLDeleteElements0())); |
411 } | 415 } |
412 | 416 |
413 LoginDatabaseQueryCallback(login_db, true, &ld_return); | 417 LoginDatabaseQueryCallback(store->login_db(), true, &ld_return); |
414 | 418 |
415 // Wait for the login DB methods to execute. | 419 // Wait for the login DB methods to execute. |
416 base::RunLoop().RunUntilIdle(); | 420 base::RunLoop().RunUntilIdle(); |
417 | 421 |
418 if (GetParam() == WORKING_BACKEND) { | 422 if (GetParam() == WORKING_BACKEND) { |
419 // Likewise, no blacklisted logins should be left in the login DB. | 423 // Likewise, no blacklisted logins should be left in the login DB. |
420 EXPECT_CALL(ld_return, | 424 EXPECT_CALL(ld_return, |
421 OnLoginDatabaseQueryDone(ContainsAllPasswordForms(empty))); | 425 OnLoginDatabaseQueryDone(ContainsAllPasswordForms(empty))); |
422 } else { | 426 } else { |
423 // The blacklisted logins should still be in the login DB. | 427 // The blacklisted logins should still be in the login DB. |
424 EXPECT_CALL(ld_return, | 428 EXPECT_CALL(ld_return, |
425 OnLoginDatabaseQueryDone( | 429 OnLoginDatabaseQueryDone( |
426 ContainsAllPasswordForms(expected_blacklisted))) | 430 ContainsAllPasswordForms(expected_blacklisted))) |
427 .WillOnce(WithArg<0>(STLDeleteElements0())); | 431 .WillOnce(WithArg<0>(STLDeleteElements0())); |
428 } | 432 } |
429 | 433 |
430 LoginDatabaseQueryCallback(login_db, false, &ld_return); | 434 LoginDatabaseQueryCallback(store->login_db(), false, &ld_return); |
431 | 435 |
432 // Wait for the login DB methods to execute. | 436 // Wait for the login DB methods to execute. |
433 base::RunLoop().RunUntilIdle(); | 437 base::RunLoop().RunUntilIdle(); |
434 | 438 |
435 if (GetParam() == WORKING_BACKEND) { | 439 if (GetParam() == WORKING_BACKEND) { |
436 // If the migration succeeded, then not only should there be no logins left | 440 // If the migration succeeded, then not only should there be no logins left |
437 // in the login DB, but also the file should have been deleted and then | 441 // in the login DB, but also the file should have been deleted and then |
438 // recreated. We approximate checking for this by checking that the file | 442 // recreated. We approximate checking for this by checking that the file |
439 // size is equal to the size before we populated it, even though it was | 443 // size is equal to the size before we populated it, even though it was |
440 // larger after populating it. | 444 // larger after populating it. |
(...skipping 10 matching lines...) Expand all Loading... |
451 | 455 |
452 INSTANTIATE_TEST_CASE_P(NoBackend, | 456 INSTANTIATE_TEST_CASE_P(NoBackend, |
453 PasswordStoreXTest, | 457 PasswordStoreXTest, |
454 testing::Values(NO_BACKEND)); | 458 testing::Values(NO_BACKEND)); |
455 INSTANTIATE_TEST_CASE_P(FailingBackend, | 459 INSTANTIATE_TEST_CASE_P(FailingBackend, |
456 PasswordStoreXTest, | 460 PasswordStoreXTest, |
457 testing::Values(FAILING_BACKEND)); | 461 testing::Values(FAILING_BACKEND)); |
458 INSTANTIATE_TEST_CASE_P(WorkingBackend, | 462 INSTANTIATE_TEST_CASE_P(WorkingBackend, |
459 PasswordStoreXTest, | 463 PasswordStoreXTest, |
460 testing::Values(WORKING_BACKEND)); | 464 testing::Values(WORKING_BACKEND)); |
OLD | NEW |