| 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 <stdarg.h> | 5 #include <stdarg.h> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 } | 317 } |
| 318 | 318 |
| 319 } // anonymous namespace | 319 } // anonymous namespace |
| 320 | 320 |
| 321 class NativeBackendGnomeTest : public testing::Test { | 321 class NativeBackendGnomeTest : public testing::Test { |
| 322 protected: | 322 protected: |
| 323 enum UpdateType { // Used in CheckPSLUpdate(). | 323 enum UpdateType { // Used in CheckPSLUpdate(). |
| 324 UPDATE_BY_UPDATELOGIN, | 324 UPDATE_BY_UPDATELOGIN, |
| 325 UPDATE_BY_ADDLOGIN, | 325 UPDATE_BY_ADDLOGIN, |
| 326 }; | 326 }; |
| 327 enum RemoveBetweenMethod { // Used in CheckRemoveLoginsBetween(). |
| 328 CREATED, |
| 329 SYNCED, |
| 330 }; |
| 327 | 331 |
| 328 NativeBackendGnomeTest() | 332 NativeBackendGnomeTest() |
| 329 : ui_thread_(BrowserThread::UI, &message_loop_), | 333 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 330 db_thread_(BrowserThread::DB) { | 334 db_thread_(BrowserThread::DB) { |
| 331 } | 335 } |
| 332 | 336 |
| 333 virtual void SetUp() { | 337 virtual void SetUp() { |
| 334 ASSERT_TRUE(db_thread_.Start()); | 338 ASSERT_TRUE(db_thread_.Start()); |
| 335 | 339 |
| 336 ASSERT_TRUE(MockGnomeKeyringLoader::LoadMockGnomeKeyring()); | 340 ASSERT_TRUE(MockGnomeKeyringLoader::LoadMockGnomeKeyring()); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 EXPECT_FALSE(CheckCredentialAvailability( | 642 EXPECT_FALSE(CheckCredentialAvailability( |
| 639 form_google_, GURL("http://www.google.com/"), scheme, NULL)); | 643 form_google_, GURL("http://www.google.com/"), scheme, NULL)); |
| 640 // Don't match two different non-HTML auth forms with different origin. | 644 // Don't match two different non-HTML auth forms with different origin. |
| 641 EXPECT_FALSE(CheckCredentialAvailability( | 645 EXPECT_FALSE(CheckCredentialAvailability( |
| 642 other_auth_, GURL("http://first.example.com"), scheme, NULL)); | 646 other_auth_, GURL("http://first.example.com"), scheme, NULL)); |
| 643 // Do match non-HTML forms from the same origin. | 647 // Do match non-HTML forms from the same origin. |
| 644 EXPECT_TRUE(CheckCredentialAvailability( | 648 EXPECT_TRUE(CheckCredentialAvailability( |
| 645 other_auth_, GURL("http://www.example.com/"), scheme, NULL)); | 649 other_auth_, GURL("http://www.example.com/"), scheme, NULL)); |
| 646 } | 650 } |
| 647 | 651 |
| 652 void CheckRemoveLoginsBetween(RemoveBetweenMethod date_to_test) { |
| 653 NativeBackendGnome backend(42); |
| 654 backend.Init(); |
| 655 |
| 656 form_google_.date_synced = base::Time(); |
| 657 form_isc_.date_synced = base::Time(); |
| 658 form_google_.date_created = base::Time(); |
| 659 form_isc_.date_created = base::Time(); |
| 660 base::Time now = base::Time::Now(); |
| 661 base::Time next_day = now + base::TimeDelta::FromDays(1); |
| 662 if (date_to_test == CREATED) { |
| 663 // crbug/374132. Remove the next line once it's fixed. |
| 664 next_day = base::Time::FromTimeT(next_day.ToTimeT()); |
| 665 form_google_.date_created = now; |
| 666 form_isc_.date_created = next_day; |
| 667 } else { |
| 668 form_google_.date_synced = now; |
| 669 form_isc_.date_synced = next_day; |
| 670 } |
| 671 |
| 672 BrowserThread::PostTask( |
| 673 BrowserThread::DB, |
| 674 FROM_HERE, |
| 675 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), |
| 676 base::Unretained(&backend), |
| 677 form_google_)); |
| 678 BrowserThread::PostTask( |
| 679 BrowserThread::DB, |
| 680 FROM_HERE, |
| 681 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), |
| 682 base::Unretained(&backend), |
| 683 form_isc_)); |
| 684 |
| 685 PasswordStoreChangeList expected_changes; |
| 686 expected_changes.push_back( |
| 687 PasswordStoreChange(PasswordStoreChange::REMOVE, form_google_)); |
| 688 PasswordStoreChangeList changes; |
| 689 bool (NativeBackendGnome::*method)( |
| 690 base::Time, base::Time, password_manager::PasswordStoreChangeList*) = |
| 691 date_to_test == CREATED |
| 692 ? &NativeBackendGnome::RemoveLoginsCreatedBetween |
| 693 : &NativeBackendGnome::RemoveLoginsSyncedBetween; |
| 694 BrowserThread::PostTaskAndReplyWithResult( |
| 695 BrowserThread::DB, |
| 696 FROM_HERE, |
| 697 base::Bind(method, |
| 698 base::Unretained(&backend), |
| 699 base::Time(), |
| 700 next_day, |
| 701 &changes), |
| 702 base::Bind( |
| 703 &CheckPasswordChangesWithResult, &expected_changes, &changes)); |
| 704 RunBothThreads(); |
| 705 |
| 706 EXPECT_EQ(1u, mock_keyring_items.size()); |
| 707 if (mock_keyring_items.size() > 0) |
| 708 CheckMockKeyringItem(&mock_keyring_items[0], form_isc_, "chrome-42"); |
| 709 |
| 710 // Remove form_isc_. |
| 711 expected_changes.clear(); |
| 712 expected_changes.push_back( |
| 713 PasswordStoreChange(PasswordStoreChange::REMOVE, form_isc_)); |
| 714 BrowserThread::PostTaskAndReplyWithResult( |
| 715 BrowserThread::DB, |
| 716 FROM_HERE, |
| 717 base::Bind(method, |
| 718 base::Unretained(&backend), |
| 719 next_day, |
| 720 base::Time(), |
| 721 &changes), |
| 722 base::Bind( |
| 723 &CheckPasswordChangesWithResult, &expected_changes, &changes)); |
| 724 RunBothThreads(); |
| 725 |
| 726 EXPECT_EQ(0u, mock_keyring_items.size()); |
| 727 } |
| 728 |
| 648 base::MessageLoopForUI message_loop_; | 729 base::MessageLoopForUI message_loop_; |
| 649 content::TestBrowserThread ui_thread_; | 730 content::TestBrowserThread ui_thread_; |
| 650 content::TestBrowserThread db_thread_; | 731 content::TestBrowserThread db_thread_; |
| 651 | 732 |
| 652 // Provide some test forms to avoid having to set them up in each test. | 733 // Provide some test forms to avoid having to set them up in each test. |
| 653 PasswordForm form_google_; | 734 PasswordForm form_google_; |
| 654 PasswordForm form_facebook_; | 735 PasswordForm form_facebook_; |
| 655 PasswordForm form_isc_; | 736 PasswordForm form_isc_; |
| 656 PasswordForm other_auth_; | 737 PasswordForm other_auth_; |
| 657 }; | 738 }; |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 948 | 1029 |
| 949 // Quick check that we got two results back. | 1030 // Quick check that we got two results back. |
| 950 EXPECT_EQ(2u, form_list.size()); | 1031 EXPECT_EQ(2u, form_list.size()); |
| 951 STLDeleteElements(&form_list); | 1032 STLDeleteElements(&form_list); |
| 952 | 1033 |
| 953 EXPECT_EQ(1u, mock_keyring_items.size()); | 1034 EXPECT_EQ(1u, mock_keyring_items.size()); |
| 954 if (mock_keyring_items.size() > 0) | 1035 if (mock_keyring_items.size() > 0) |
| 955 CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42"); | 1036 CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42"); |
| 956 } | 1037 } |
| 957 | 1038 |
| 1039 TEST_F(NativeBackendGnomeTest, RemoveLoginsCreatedBetween) { |
| 1040 CheckRemoveLoginsBetween(CREATED); |
| 1041 } |
| 1042 |
| 958 TEST_F(NativeBackendGnomeTest, RemoveLoginsSyncedBetween) { | 1043 TEST_F(NativeBackendGnomeTest, RemoveLoginsSyncedBetween) { |
| 959 NativeBackendGnome backend(42); | 1044 CheckRemoveLoginsBetween(SYNCED); |
| 960 backend.Init(); | |
| 961 | |
| 962 base::Time now = base::Time::Now(); | |
| 963 base::Time next_day = now + base::TimeDelta::FromDays(1); | |
| 964 form_google_.date_synced = now; | |
| 965 form_isc_.date_synced = next_day; | |
| 966 form_google_.date_created = base::Time(); | |
| 967 form_isc_.date_created = base::Time(); | |
| 968 | |
| 969 BrowserThread::PostTask( | |
| 970 BrowserThread::DB, | |
| 971 FROM_HERE, | |
| 972 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), | |
| 973 base::Unretained(&backend), | |
| 974 form_google_)); | |
| 975 BrowserThread::PostTask( | |
| 976 BrowserThread::DB, | |
| 977 FROM_HERE, | |
| 978 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), | |
| 979 base::Unretained(&backend), | |
| 980 form_isc_)); | |
| 981 | |
| 982 PasswordStoreChangeList expected_changes; | |
| 983 expected_changes.push_back( | |
| 984 PasswordStoreChange(PasswordStoreChange::REMOVE, form_google_)); | |
| 985 PasswordStoreChangeList changes; | |
| 986 BrowserThread::PostTaskAndReplyWithResult( | |
| 987 BrowserThread::DB, | |
| 988 FROM_HERE, | |
| 989 base::Bind(&NativeBackendGnome::RemoveLoginsSyncedBetween, | |
| 990 base::Unretained(&backend), | |
| 991 base::Time(), | |
| 992 next_day, | |
| 993 &changes), | |
| 994 base::Bind(&CheckPasswordChangesWithResult, &expected_changes, &changes)); | |
| 995 RunBothThreads(); | |
| 996 | |
| 997 EXPECT_EQ(1u, mock_keyring_items.size()); | |
| 998 if (mock_keyring_items.size() > 0) | |
| 999 CheckMockKeyringItem(&mock_keyring_items[0], form_isc_, "chrome-42"); | |
| 1000 | |
| 1001 // Remove form_isc_. | |
| 1002 expected_changes.clear(); | |
| 1003 expected_changes.push_back( | |
| 1004 PasswordStoreChange(PasswordStoreChange::REMOVE, form_isc_)); | |
| 1005 BrowserThread::PostTaskAndReplyWithResult( | |
| 1006 BrowserThread::DB, | |
| 1007 FROM_HERE, | |
| 1008 base::Bind(&NativeBackendGnome::RemoveLoginsSyncedBetween, | |
| 1009 base::Unretained(&backend), | |
| 1010 next_day, | |
| 1011 base::Time(), | |
| 1012 &changes), | |
| 1013 base::Bind(&CheckPasswordChangesWithResult, &expected_changes, &changes)); | |
| 1014 RunBothThreads(); | |
| 1015 | |
| 1016 EXPECT_EQ(0u, mock_keyring_items.size()); | |
| 1017 } | 1045 } |
| 1018 | 1046 |
| 1019 // TODO(mdm): add more basic tests here at some point. | 1047 // TODO(mdm): add more basic tests here at some point. |
| OLD | NEW |