| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/browsing_data/passwords_counter.h" | 5 #include "chrome/browser/browsing_data/passwords_counter.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/password_manager/password_store_factory.h" | 9 #include "chrome/browser/password_manager/password_store_factory.h" |
| 10 #include "chrome/browser/sync/test/integration/passwords_helper.h" | 10 #include "chrome/browser/sync/test/integration/passwords_helper.h" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "components/autofill/core/common/password_form.h" | 14 #include "components/autofill/core/common/password_form.h" |
| 15 #include "components/browsing_data/browsing_data_utils.h" |
| 16 #include "components/browsing_data/pref_names.h" |
| 15 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 using autofill::PasswordForm; | 21 using autofill::PasswordForm; |
| 20 | 22 |
| 21 class PasswordsCounterTest : public InProcessBrowserTest, | 23 class PasswordsCounterTest : public InProcessBrowserTest, |
| 22 public password_manager::PasswordStore::Observer { | 24 public password_manager::PasswordStore::Observer { |
| 23 public: | 25 public: |
| 24 void SetUpOnMainThread() override { | 26 void SetUpOnMainThread() override { |
| 25 time_ = base::Time::Now(); | 27 time_ = base::Time::Now(); |
| 26 store_ = PasswordStoreFactory::GetForProfile( | 28 store_ = PasswordStoreFactory::GetForProfile( |
| 27 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); | 29 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); |
| 28 SetPasswordsDeletionPref(true); | 30 SetPasswordsDeletionPref(true); |
| 29 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 31 SetDeletionPeriodPref(browsing_data::EVERYTHING); |
| 30 } | 32 } |
| 31 | 33 |
| 32 void AddLogin(const std::string& origin, | 34 void AddLogin(const std::string& origin, |
| 33 const std::string& username, | 35 const std::string& username, |
| 34 bool blacklisted) { | 36 bool blacklisted) { |
| 35 // Add login and wait until the password store actually changes. | 37 // Add login and wait until the password store actually changes. |
| 36 // on the database thread. | 38 // on the database thread. |
| 37 passwords_helper::AddLogin( | 39 passwords_helper::AddLogin( |
| 38 store_.get(), CreateCredentials(origin, username, blacklisted)); | 40 store_.get(), CreateCredentials(origin, username, blacklisted)); |
| 39 base::RunLoop().RunUntilIdle(); | 41 base::RunLoop().RunUntilIdle(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 59 void OnLoginsChanged( | 61 void OnLoginsChanged( |
| 60 const password_manager::PasswordStoreChangeList& changes) override { | 62 const password_manager::PasswordStoreChangeList& changes) override { |
| 61 run_loop_->Quit(); | 63 run_loop_->Quit(); |
| 62 } | 64 } |
| 63 | 65 |
| 64 void SetPasswordsDeletionPref(bool value) { | 66 void SetPasswordsDeletionPref(bool value) { |
| 65 browser()->profile()->GetPrefs()->SetBoolean( | 67 browser()->profile()->GetPrefs()->SetBoolean( |
| 66 prefs::kDeletePasswords, value); | 68 prefs::kDeletePasswords, value); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { | 71 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
| 70 browser()->profile()->GetPrefs()->SetInteger( | 72 browser()->profile()->GetPrefs()->SetInteger( |
| 71 prefs::kDeleteTimePeriod, static_cast<int>(period)); | 73 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void RevertTimeInDays(int days) { | 76 void RevertTimeInDays(int days) { |
| 75 time_ -= base::TimeDelta::FromDays(days); | 77 time_ -= base::TimeDelta::FromDays(days); |
| 76 } | 78 } |
| 77 | 79 |
| 78 void WaitForCounting() { | 80 void WaitForCounting() { |
| 79 if (finished_) | 81 if (finished_) |
| 80 return; | 82 return; |
| 81 run_loop_.reset(new base::RunLoop()); | 83 run_loop_.reset(new base::RunLoop()); |
| 82 run_loop_->Run(); | 84 run_loop_->Run(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 BrowsingDataCounter::ResultInt GetResult() { | 87 browsing_data::BrowsingDataCounter::ResultInt GetResult() { |
| 86 DCHECK(finished_); | 88 DCHECK(finished_); |
| 87 return result_; | 89 return result_; |
| 88 } | 90 } |
| 89 | 91 |
| 90 void Callback(std::unique_ptr<BrowsingDataCounter::Result> result) { | 92 void Callback( |
| 93 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 91 finished_ = result->Finished(); | 94 finished_ = result->Finished(); |
| 92 | 95 |
| 93 if (finished_) { | 96 if (finished_) { |
| 94 result_ = static_cast<BrowsingDataCounter::FinishedResult*>( | 97 result_ = |
| 95 result.get())->Value(); | 98 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>( |
| 99 result.get()) |
| 100 ->Value(); |
| 96 } | 101 } |
| 97 | 102 |
| 98 if (run_loop_ && finished_) | 103 if (run_loop_ && finished_) |
| 99 run_loop_->Quit(); | 104 run_loop_->Quit(); |
| 100 } | 105 } |
| 101 | 106 |
| 102 private: | 107 private: |
| 103 PasswordForm CreateCredentials(const std::string& origin, | 108 PasswordForm CreateCredentials(const std::string& origin, |
| 104 const std::string& username, | 109 const std::string& username, |
| 105 bool blacklisted) { | 110 bool blacklisted) { |
| 106 PasswordForm result; | 111 PasswordForm result; |
| 107 result.signon_realm = origin; | 112 result.signon_realm = origin; |
| 108 result.origin = GURL(origin); | 113 result.origin = GURL(origin); |
| 109 result.username_value = base::ASCIIToUTF16(username); | 114 result.username_value = base::ASCIIToUTF16(username); |
| 110 result.password_value = base::ASCIIToUTF16("hunter2"); | 115 result.password_value = base::ASCIIToUTF16("hunter2"); |
| 111 result.blacklisted_by_user = blacklisted; | 116 result.blacklisted_by_user = blacklisted; |
| 112 result.date_created = time_; | 117 result.date_created = time_; |
| 113 return result; | 118 return result; |
| 114 } | 119 } |
| 115 | 120 |
| 116 scoped_refptr<password_manager::PasswordStore> store_; | 121 scoped_refptr<password_manager::PasswordStore> store_; |
| 117 | 122 |
| 118 std::unique_ptr<base::RunLoop> run_loop_; | 123 std::unique_ptr<base::RunLoop> run_loop_; |
| 119 base::Time time_; | 124 base::Time time_; |
| 120 | 125 |
| 121 bool finished_; | 126 bool finished_; |
| 122 BrowsingDataCounter::ResultInt result_; | 127 browsing_data::BrowsingDataCounter::ResultInt result_; |
| 123 }; | 128 }; |
| 124 | 129 |
| 125 // Tests that the counter correctly counts each individual credential on | 130 // Tests that the counter correctly counts each individual credential on |
| 126 // the same domain. | 131 // the same domain. |
| 127 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { | 132 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { |
| 128 AddLogin("https://www.google.com", "user1", false); | 133 AddLogin("https://www.google.com", "user1", false); |
| 129 AddLogin("https://www.google.com", "user2", false); | 134 AddLogin("https://www.google.com", "user2", false); |
| 130 AddLogin("https://www.google.com", "user3", false); | 135 AddLogin("https://www.google.com", "user3", false); |
| 131 AddLogin("https://www.chrome.com", "user1", false); | 136 AddLogin("https://www.chrome.com", "user1", false); |
| 132 AddLogin("https://www.chrome.com", "user2", false); | 137 AddLogin("https://www.chrome.com", "user2", false); |
| 133 | 138 |
| 134 PasswordsCounter counter; | 139 Profile* profile = browser()->profile(); |
| 135 counter.Init(browser()->profile(), | 140 PasswordsCounter counter(profile); |
| 136 base::Bind(&PasswordsCounterTest::Callback, | 141 counter.Init(profile->GetPrefs(), base::Bind(&PasswordsCounterTest::Callback, |
| 137 base::Unretained(this))); | 142 base::Unretained(this))); |
| 138 counter.Restart(); | 143 counter.Restart(); |
| 139 | 144 |
| 140 WaitForCounting(); | 145 WaitForCounting(); |
| 141 EXPECT_EQ(5u, GetResult()); | 146 EXPECT_EQ(5u, GetResult()); |
| 142 } | 147 } |
| 143 | 148 |
| 144 // Tests that the counter doesn't count blacklisted entries. | 149 // Tests that the counter doesn't count blacklisted entries. |
| 145 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { | 150 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { |
| 146 AddLogin("https://www.google.com", "user1", false); | 151 AddLogin("https://www.google.com", "user1", false); |
| 147 AddLogin("https://www.google.com", "user2", true); | 152 AddLogin("https://www.google.com", "user2", true); |
| 148 AddLogin("https://www.chrome.com", "user3", true); | 153 AddLogin("https://www.chrome.com", "user3", true); |
| 149 | 154 |
| 150 PasswordsCounter counter; | 155 Profile* profile = browser()->profile(); |
| 151 counter.Init(browser()->profile(), | 156 PasswordsCounter counter(profile); |
| 152 base::Bind(&PasswordsCounterTest::Callback, | 157 counter.Init(profile->GetPrefs(), base::Bind(&PasswordsCounterTest::Callback, |
| 153 base::Unretained(this))); | 158 base::Unretained(this))); |
| 154 counter.Restart(); | 159 counter.Restart(); |
| 155 | 160 |
| 156 WaitForCounting(); | 161 WaitForCounting(); |
| 157 EXPECT_EQ(1u, GetResult()); | 162 EXPECT_EQ(1u, GetResult()); |
| 158 } | 163 } |
| 159 | 164 |
| 160 // Tests that the counter starts counting automatically when the deletion | 165 // Tests that the counter starts counting automatically when the deletion |
| 161 // pref changes to true. | 166 // pref changes to true. |
| 162 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { | 167 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { |
| 163 SetPasswordsDeletionPref(false); | 168 SetPasswordsDeletionPref(false); |
| 164 AddLogin("https://www.google.com", "user", false); | 169 AddLogin("https://www.google.com", "user", false); |
| 165 AddLogin("https://www.chrome.com", "user", false); | 170 AddLogin("https://www.chrome.com", "user", false); |
| 166 | 171 |
| 167 PasswordsCounter counter; | 172 Profile* profile = browser()->profile(); |
| 168 counter.Init(browser()->profile(), | 173 PasswordsCounter counter(profile); |
| 169 base::Bind(&PasswordsCounterTest::Callback, | 174 counter.Init(profile->GetPrefs(), base::Bind(&PasswordsCounterTest::Callback, |
| 170 base::Unretained(this))); | 175 base::Unretained(this))); |
| 171 SetPasswordsDeletionPref(true); | 176 SetPasswordsDeletionPref(true); |
| 172 | 177 |
| 173 WaitForCounting(); | 178 WaitForCounting(); |
| 174 EXPECT_EQ(2u, GetResult()); | 179 EXPECT_EQ(2u, GetResult()); |
| 175 } | 180 } |
| 176 | 181 |
| 177 // Tests that the counter does not count passwords if the deletion | 182 // Tests that the counter does not count passwords if the deletion |
| 178 // preference is false. | 183 // preference is false. |
| 179 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefIsFalse) { | 184 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefIsFalse) { |
| 180 SetPasswordsDeletionPref(false); | 185 SetPasswordsDeletionPref(false); |
| 181 AddLogin("https://www.google.com", "user", false); | 186 AddLogin("https://www.google.com", "user", false); |
| 182 | 187 |
| 183 PasswordsCounter counter; | 188 Profile* profile = browser()->profile(); |
| 184 counter.Init(browser()->profile(), | 189 PasswordsCounter counter(profile); |
| 185 base::Bind(&PasswordsCounterTest::Callback, | 190 counter.Init(profile->GetPrefs(), base::Bind(&PasswordsCounterTest::Callback, |
| 186 base::Unretained(this))); | 191 base::Unretained(this))); |
| 187 counter.Restart(); | 192 counter.Restart(); |
| 188 | 193 |
| 189 EXPECT_FALSE(counter.cancelable_task_tracker()->HasTrackedTasks()); | 194 EXPECT_FALSE(counter.cancelable_task_tracker()->HasTrackedTasks()); |
| 190 } | 195 } |
| 191 | 196 |
| 192 // Tests that the counter starts counting automatically when | 197 // Tests that the counter starts counting automatically when |
| 193 // the password store changes. | 198 // the password store changes. |
| 194 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) { | 199 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) { |
| 195 AddLogin("https://www.google.com", "user", false); | 200 AddLogin("https://www.google.com", "user", false); |
| 196 | 201 |
| 197 PasswordsCounter counter; | 202 Profile* profile = browser()->profile(); |
| 198 counter.Init(browser()->profile(), | 203 PasswordsCounter counter(profile); |
| 199 base::Bind(&PasswordsCounterTest::Callback, | 204 counter.Init(profile->GetPrefs(), base::Bind(&PasswordsCounterTest::Callback, |
| 200 base::Unretained(this))); | 205 base::Unretained(this))); |
| 201 counter.Restart(); | 206 counter.Restart(); |
| 202 | 207 |
| 203 WaitForCounting(); | 208 WaitForCounting(); |
| 204 EXPECT_EQ(1u, GetResult()); | 209 EXPECT_EQ(1u, GetResult()); |
| 205 | 210 |
| 206 AddLogin("https://www.chrome.com", "user", false); | 211 AddLogin("https://www.chrome.com", "user", false); |
| 207 WaitForCounting(); | 212 WaitForCounting(); |
| 208 EXPECT_EQ(2u, GetResult()); | 213 EXPECT_EQ(2u, GetResult()); |
| 209 | 214 |
| 210 RemoveLogin("https://www.chrome.com", "user", false); | 215 RemoveLogin("https://www.chrome.com", "user", false); |
| 211 WaitForCounting(); | 216 WaitForCounting(); |
| 212 EXPECT_EQ(1u, GetResult()); | 217 EXPECT_EQ(1u, GetResult()); |
| 213 } | 218 } |
| 214 | 219 |
| 215 // Tests that changing the deletion period restarts the counting, and that | 220 // Tests that changing the deletion period restarts the counting, and that |
| 216 // the result takes login creation dates into account. | 221 // the result takes login creation dates into account. |
| 217 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PeriodChanged) { | 222 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PeriodChanged) { |
| 218 AddLogin("https://www.google.com", "user", false); | 223 AddLogin("https://www.google.com", "user", false); |
| 219 RevertTimeInDays(2); | 224 RevertTimeInDays(2); |
| 220 AddLogin("https://example.com", "user1", false); | 225 AddLogin("https://example.com", "user1", false); |
| 221 RevertTimeInDays(3); | 226 RevertTimeInDays(3); |
| 222 AddLogin("https://example.com", "user2", false); | 227 AddLogin("https://example.com", "user2", false); |
| 223 RevertTimeInDays(30); | 228 RevertTimeInDays(30); |
| 224 AddLogin("https://www.chrome.com", "user", false); | 229 AddLogin("https://www.chrome.com", "user", false); |
| 225 | 230 |
| 226 PasswordsCounter counter; | 231 Profile* profile = browser()->profile(); |
| 227 counter.Init(browser()->profile(), | 232 PasswordsCounter counter(profile); |
| 228 base::Bind(&PasswordsCounterTest::Callback, | 233 counter.Init(profile->GetPrefs(), base::Bind(&PasswordsCounterTest::Callback, |
| 229 base::Unretained(this))); | 234 base::Unretained(this))); |
| 230 | 235 |
| 231 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); | 236 SetDeletionPeriodPref(browsing_data::LAST_HOUR); |
| 232 WaitForCounting(); | 237 WaitForCounting(); |
| 233 EXPECT_EQ(1u, GetResult()); | 238 EXPECT_EQ(1u, GetResult()); |
| 234 | 239 |
| 235 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY); | 240 SetDeletionPeriodPref(browsing_data::LAST_DAY); |
| 236 WaitForCounting(); | 241 WaitForCounting(); |
| 237 EXPECT_EQ(1u, GetResult()); | 242 EXPECT_EQ(1u, GetResult()); |
| 238 | 243 |
| 239 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK); | 244 SetDeletionPeriodPref(browsing_data::LAST_WEEK); |
| 240 WaitForCounting(); | 245 WaitForCounting(); |
| 241 EXPECT_EQ(3u, GetResult()); | 246 EXPECT_EQ(3u, GetResult()); |
| 242 | 247 |
| 243 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS); | 248 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS); |
| 244 WaitForCounting(); | 249 WaitForCounting(); |
| 245 EXPECT_EQ(3u, GetResult()); | 250 EXPECT_EQ(3u, GetResult()); |
| 246 | 251 |
| 247 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 252 SetDeletionPeriodPref(browsing_data::EVERYTHING); |
| 248 WaitForCounting(); | 253 WaitForCounting(); |
| 249 EXPECT_EQ(4u, GetResult()); | 254 EXPECT_EQ(4u, GetResult()); |
| 250 } | 255 } |
| 251 | 256 |
| 252 } // namespace | 257 } // namespace |
| OLD | NEW |