| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "chrome/browser/password_manager/login_database.h" | 12 #include "chrome/browser/password_manager/login_database.h" |
| 13 #if defined(OS_MACOSX) | |
| 14 #include "chrome/browser/password_manager/login_database_mac.h" | |
| 15 #endif | |
| 16 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 17 #include "webkit/glue/password_form.h" | 14 #include "webkit/glue/password_form.h" |
| 18 | 15 |
| 19 using webkit_glue::PasswordForm; | 16 using webkit_glue::PasswordForm; |
| 20 | 17 |
| 21 class LoginDatabaseTest : public testing::Test { | 18 class LoginDatabaseTest : public testing::Test { |
| 22 protected: | 19 protected: |
| 23 virtual void SetUp() { | 20 virtual void SetUp() { |
| 24 PathService::Get(chrome::DIR_TEST_DATA, &file_); | 21 PathService::Get(chrome::DIR_TEST_DATA, &file_); |
| 25 const std::string test_db = | 22 const std::string test_db = |
| 26 "TestMetadataStoreMacDatabase" + | 23 "TestMetadataStoreMacDatabase" + |
| 27 Int64ToString(base::Time::Now().ToInternalValue()) + ".db"; | 24 Int64ToString(base::Time::Now().ToInternalValue()) + ".db"; |
| 28 file_ = file_.AppendASCII(test_db); | 25 file_ = file_.AppendASCII(test_db); |
| 29 file_util::Delete(file_, false); | 26 file_util::Delete(file_, false); |
| 30 } | 27 } |
| 31 | 28 |
| 32 virtual void TearDown() { | 29 virtual void TearDown() { |
| 33 file_util::Delete(file_, false); | 30 file_util::Delete(file_, false); |
| 34 } | 31 } |
| 35 | 32 |
| 36 FilePath file_; | 33 FilePath file_; |
| 37 }; | 34 }; |
| 38 | 35 |
| 39 // Returns the correct concrete subclass for the platform. Caller is responsible | |
| 40 // for delete-ing the return object. | |
| 41 static LoginDatabase* CreateLoginDatabase() { | |
| 42 #if defined(OS_MACOSX) | |
| 43 return new LoginDatabaseMac(); | |
| 44 #else | |
| 45 return NULL; | |
| 46 #endif | |
| 47 } | |
| 48 | |
| 49 TEST_F(LoginDatabaseTest, Logins) { | 36 TEST_F(LoginDatabaseTest, Logins) { |
| 50 scoped_ptr<LoginDatabase> db(CreateLoginDatabase()); | 37 scoped_ptr<LoginDatabase> db(new LoginDatabase()); |
| 51 if (!db.get()) | |
| 52 return; | |
| 53 | 38 |
| 54 ASSERT_TRUE(db->Init(file_)); | 39 ASSERT_TRUE(db->Init(file_)); |
| 55 | 40 |
| 56 std::vector<PasswordForm*> result; | 41 std::vector<PasswordForm*> result; |
| 57 | 42 |
| 58 // Verify the database is empty. | 43 // Verify the database is empty. |
| 59 EXPECT_TRUE(db->GetAutofillableLogins(&result)); | 44 EXPECT_TRUE(db->GetAutofillableLogins(&result)); |
| 60 EXPECT_EQ(0U, result.size()); | 45 EXPECT_EQ(0U, result.size()); |
| 61 | 46 |
| 62 // Example password form. | 47 // Example password form. |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 189 } |
| 205 | 190 |
| 206 static void ClearResults(std::vector<PasswordForm*>* results) { | 191 static void ClearResults(std::vector<PasswordForm*>* results) { |
| 207 for (size_t i = 0; i < results->size(); ++i) { | 192 for (size_t i = 0; i < results->size(); ++i) { |
| 208 delete (*results)[i]; | 193 delete (*results)[i]; |
| 209 } | 194 } |
| 210 results->clear(); | 195 results->clear(); |
| 211 } | 196 } |
| 212 | 197 |
| 213 TEST_F(LoginDatabaseTest, ClearPrivateData_SavedPasswords) { | 198 TEST_F(LoginDatabaseTest, ClearPrivateData_SavedPasswords) { |
| 214 scoped_ptr<LoginDatabase> db(CreateLoginDatabase()); | 199 scoped_ptr<LoginDatabase> db(new LoginDatabase()); |
| 215 if (!db.get()) | |
| 216 return; | |
| 217 | 200 |
| 218 EXPECT_TRUE(db->Init(file_)); | 201 EXPECT_TRUE(db->Init(file_)); |
| 219 | 202 |
| 220 std::vector<PasswordForm*> result; | 203 std::vector<PasswordForm*> result; |
| 221 | 204 |
| 222 // Verify the database is empty. | 205 // Verify the database is empty. |
| 223 EXPECT_TRUE(db->GetAutofillableLogins(&result)); | 206 EXPECT_TRUE(db->GetAutofillableLogins(&result)); |
| 224 EXPECT_EQ(0U, result.size()); | 207 EXPECT_EQ(0U, result.size()); |
| 225 | 208 |
| 226 base::Time now = base::Time::Now(); | 209 base::Time now = base::Time::Now(); |
| 227 base::TimeDelta one_day = base::TimeDelta::FromDays(1); | 210 base::TimeDelta one_day = base::TimeDelta::FromDays(1); |
| 228 | 211 |
| 229 // Create one with a 0 time. | 212 // Create one with a 0 time. |
| 230 EXPECT_TRUE(AddTimestampedLogin(db.get(), "1", "foo1", base::Time())); | 213 EXPECT_TRUE(AddTimestampedLogin(db.get(), "1", "foo1", base::Time())); |
| 231 // Create one for now and +/- 1 day. | 214 // Create one for now and +/- 1 day. |
| 232 EXPECT_TRUE(AddTimestampedLogin(db.get(), "2", "foo2", now - one_day)); | 215 EXPECT_TRUE(AddTimestampedLogin(db.get(), "2", "foo2", now - one_day)); |
| 233 EXPECT_TRUE(AddTimestampedLogin(db.get(), "3", "foo3", now)); | 216 EXPECT_TRUE(AddTimestampedLogin(db.get(), "3", "foo3", now)); |
| 234 EXPECT_TRUE(AddTimestampedLogin(db.get(), "4", "foo4", now + one_day)); | 217 EXPECT_TRUE(AddTimestampedLogin(db.get(), "4", "foo4", now + one_day)); |
| 235 | 218 |
| 236 // Verify inserts worked. | 219 // Verify inserts worked. |
| 237 EXPECT_TRUE(db->GetAutofillableLogins(&result)); | 220 EXPECT_TRUE(db->GetAutofillableLogins(&result)); |
| 238 EXPECT_EQ(4U, result.size()); | 221 EXPECT_EQ(4U, result.size()); |
| 239 ClearResults(&result); | 222 ClearResults(&result); |
| 240 | 223 |
| 224 // Get everything from today's date and on. |
| 225 EXPECT_TRUE(db->GetLoginsCreatedBetween(now, base::Time(), &result)); |
| 226 EXPECT_EQ(2U, result.size()); |
| 227 ClearResults(&result); |
| 228 |
| 241 // Delete everything from today's date and on. | 229 // Delete everything from today's date and on. |
| 242 db->RemoveLoginsCreatedBetween(now, base::Time()); | 230 db->RemoveLoginsCreatedBetween(now, base::Time()); |
| 243 | 231 |
| 244 // Should have deleted half of what we inserted. | 232 // Should have deleted half of what we inserted. |
| 245 EXPECT_TRUE(db->GetAutofillableLogins(&result)); | 233 EXPECT_TRUE(db->GetAutofillableLogins(&result)); |
| 246 EXPECT_EQ(2U, result.size()); | 234 EXPECT_EQ(2U, result.size()); |
| 247 ClearResults(&result); | 235 ClearResults(&result); |
| 248 | 236 |
| 249 // Delete with 0 date (should delete all). | 237 // Delete with 0 date (should delete all). |
| 250 db->RemoveLoginsCreatedBetween(base::Time(), base::Time()); | 238 db->RemoveLoginsCreatedBetween(base::Time(), base::Time()); |
| 251 | 239 |
| 252 // Verify nothing is left. | 240 // Verify nothing is left. |
| 253 EXPECT_TRUE(db->GetAutofillableLogins(&result)); | 241 EXPECT_TRUE(db->GetAutofillableLogins(&result)); |
| 254 EXPECT_EQ(0U, result.size()); | 242 EXPECT_EQ(0U, result.size()); |
| 255 } | 243 } |
| 256 | 244 |
| 257 TEST_F(LoginDatabaseTest, BlacklistedLogins) { | 245 TEST_F(LoginDatabaseTest, BlacklistedLogins) { |
| 258 scoped_ptr<LoginDatabase> db(CreateLoginDatabase()); | 246 scoped_ptr<LoginDatabase> db(new LoginDatabase()); |
| 259 if (!db.get()) | |
| 260 return; | |
| 261 | 247 |
| 262 EXPECT_TRUE(db->Init(file_)); | 248 EXPECT_TRUE(db->Init(file_)); |
| 263 std::vector<PasswordForm*> result; | 249 std::vector<PasswordForm*> result; |
| 264 | 250 |
| 265 // Verify the database is empty. | 251 // Verify the database is empty. |
| 266 EXPECT_TRUE(db->GetBlacklistLogins(&result)); | 252 EXPECT_TRUE(db->GetBlacklistLogins(&result)); |
| 267 ASSERT_EQ(0U, result.size()); | 253 ASSERT_EQ(0U, result.size()); |
| 268 | 254 |
| 269 // Save a form as blacklisted. | 255 // Save a form as blacklisted. |
| 270 PasswordForm form; | 256 PasswordForm form; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 287 // GetLogins should give the blacklisted result. | 273 // GetLogins should give the blacklisted result. |
| 288 EXPECT_TRUE(db->GetLogins(form, &result)); | 274 EXPECT_TRUE(db->GetLogins(form, &result)); |
| 289 EXPECT_EQ(1U, result.size()); | 275 EXPECT_EQ(1U, result.size()); |
| 290 ClearResults(&result); | 276 ClearResults(&result); |
| 291 | 277 |
| 292 // So should GetAllBlacklistedLogins. | 278 // So should GetAllBlacklistedLogins. |
| 293 EXPECT_TRUE(db->GetBlacklistLogins(&result)); | 279 EXPECT_TRUE(db->GetBlacklistLogins(&result)); |
| 294 EXPECT_EQ(1U, result.size()); | 280 EXPECT_EQ(1U, result.size()); |
| 295 ClearResults(&result); | 281 ClearResults(&result); |
| 296 } | 282 } |
| OLD | NEW |