autofill_table_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <vector> 6 7#include "base/file_util.h" 8#include "base/files/scoped_temp_dir.h" 9#include "base/guid.h" 10#include "base/path_service.h" 11#include "base/strings/string_number_conversions.h" 12#include "base/strings/string_util.h" 13#include "base/strings/utf_string_conversions.h" 14#include "base/time/time.h" 15#include "components/autofill/core/browser/autofill_profile.h" 16#include "components/autofill/core/browser/autofill_type.h" 17#include "components/autofill/core/browser/credit_card.h" 18#include "components/autofill/core/browser/webdata/autofill_change.h" 19#include "components/autofill/core/browser/webdata/autofill_entry.h" 20#include "components/autofill/core/browser/webdata/autofill_table.h" 21#include "components/autofill/core/common/form_field_data.h" 22#include "components/os_crypt/os_crypt.h" 23#include "components/webdata/common/web_database.h" 24#include "sql/statement.h" 25#include "testing/gtest/include/gtest/gtest.h" 26 27using base::ASCIIToUTF16; 28using base::Time; 29using base::TimeDelta; 30 31namespace autofill { 32 33// So we can compare AutofillKeys with EXPECT_EQ(). 34std::ostream& operator<<(std::ostream& os, const AutofillKey& key) { 35 return os << base::UTF16ToASCII(key.name()) << ", " 36 << base::UTF16ToASCII(key.value()); 37} 38 39// So we can compare AutofillChanges with EXPECT_EQ(). 40std::ostream& operator<<(std::ostream& os, const AutofillChange& change) { 41 switch (change.type()) { 42 case AutofillChange::ADD: { 43 os << "ADD"; 44 break; 45 } 46 case AutofillChange::UPDATE: { 47 os << "UPDATE"; 48 break; 49 } 50 case AutofillChange::REMOVE: { 51 os << "REMOVE"; 52 break; 53 } 54 } 55 return os << " " << change.key(); 56} 57 58namespace { 59 60typedef std::set<AutofillEntry, 61 bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet; 62typedef AutofillEntrySet::iterator AutofillEntrySetIterator; 63 64bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) { 65 int compVal = a.key().name().compare(b.key().name()); 66 if (compVal != 0) 67 return compVal < 0; 68 69 compVal = a.key().value().compare(b.key().value()); 70 if (compVal != 0) 71 return compVal < 0; 72 73 if (a.date_created() != b.date_created()) 74 return a.date_created() < b.date_created(); 75 76 return a.date_last_used() < b.date_last_used(); 77} 78 79AutofillEntry MakeAutofillEntry(const char* name, 80 const char* value, 81 time_t date_created, 82 time_t date_last_used) { 83 if (date_last_used < 0) 84 date_last_used = date_created; 85 return AutofillEntry(AutofillKey(ASCIIToUTF16(name), ASCIIToUTF16(value)), 86 Time::FromTimeT(date_created), 87 Time::FromTimeT(date_last_used)); 88} 89 90// Checks |actual| and |expected| contain the same elements. 91void CompareAutofillEntrySets(const AutofillEntrySet& actual, 92 const AutofillEntrySet& expected) { 93 ASSERT_EQ(expected.size(), actual.size()); 94 size_t count = 0; 95 for (AutofillEntrySet::const_iterator it = actual.begin(); 96 it != actual.end(); ++it) { 97 count += expected.count(*it); 98 } 99 EXPECT_EQ(actual.size(), count); 100} 101 102int GetAutofillEntryCount(const base::string16& name, 103 const base::string16& value, 104 WebDatabase* db) { 105 sql::Statement s(db->GetSQLConnection()->GetUniqueStatement( 106 "SELECT count FROM autofill WHERE name = ? AND value = ?")); 107 s.BindString16(0, name); 108 s.BindString16(1, value); 109 s.Step(); 110 return s.ColumnInt(0); 111} 112 113} // namespace 114 115class AutofillTableTest : public testing::Test { 116 public: 117 AutofillTableTest() {} 118 virtual ~AutofillTableTest() {} 119 120 protected: 121 virtual void SetUp() { 122#if defined(OS_MACOSX) 123 OSCrypt::UseMockKeychain(true); 124#endif 125 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 126 file_ = temp_dir_.path().AppendASCII("TestWebDatabase"); 127 128 table_.reset(new AutofillTable("en-US")); 129 db_.reset(new WebDatabase); 130 db_->AddTable(table_.get()); 131 ASSERT_EQ(sql::INIT_OK, db_->Init(file_)); 132 } 133 134 base::FilePath file_; 135 base::ScopedTempDir temp_dir_; 136 scoped_ptr<AutofillTable> table_; 137 scoped_ptr<WebDatabase> db_; 138 139 private: 140 DISALLOW_COPY_AND_ASSIGN(AutofillTableTest); 141}; 142 143TEST_F(AutofillTableTest, Autofill) { 144 Time t1 = Time::Now(); 145 146 // Simulate the submission of a handful of entries in a field called "Name", 147 // some more often than others. 148 AutofillChangeList changes; 149 FormFieldData field; 150 field.name = ASCIIToUTF16("Name"); 151 field.value = ASCIIToUTF16("Superman"); 152 base::Time now = base::Time::Now(); 153 base::TimeDelta two_seconds = base::TimeDelta::FromSeconds(2); 154 EXPECT_FALSE(table_->HasFormElements()); 155 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); 156 EXPECT_TRUE(table_->HasFormElements()); 157 std::vector<base::string16> v; 158 for (int i = 0; i < 5; ++i) { 159 field.value = ASCIIToUTF16("Clark Kent"); 160 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 161 now + i * two_seconds)); 162 } 163 for (int i = 0; i < 3; ++i) { 164 field.value = ASCIIToUTF16("Clark Sutter"); 165 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 166 now + i * two_seconds)); 167 } 168 for (int i = 0; i < 2; ++i) { 169 field.name = ASCIIToUTF16("Favorite Color"); 170 field.value = ASCIIToUTF16("Green"); 171 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 172 now + i * two_seconds)); 173 } 174 175 // We have added the name Clark Kent 5 times, so count should be 5. 176 EXPECT_EQ(5, GetAutofillEntryCount(ASCIIToUTF16("Name"), 177 ASCIIToUTF16("Clark Kent"), db_.get())); 178 179 // Storing in the data base should be case sensitive, so there should be no 180 // database entry for clark kent lowercase. 181 EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"), 182 ASCIIToUTF16("clark kent"), db_.get())); 183 184 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("Favorite Color"), 185 ASCIIToUTF16("Green"), db_.get())); 186 187 // This is meant to get a list of suggestions for Name. The empty prefix 188 // in the second argument means it should return all suggestions for a name 189 // no matter what they start with. The order that the names occur in the list 190 // should be decreasing order by count. 191 EXPECT_TRUE(table_->GetFormValuesForElementName( 192 ASCIIToUTF16("Name"), base::string16(), &v, 6)); 193 EXPECT_EQ(3U, v.size()); 194 if (v.size() == 3) { 195 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]); 196 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]); 197 EXPECT_EQ(ASCIIToUTF16("Superman"), v[2]); 198 } 199 200 // If we query again limiting the list size to 1, we should only get the most 201 // frequent entry. 202 EXPECT_TRUE(table_->GetFormValuesForElementName( 203 ASCIIToUTF16("Name"), base::string16(), &v, 1)); 204 EXPECT_EQ(1U, v.size()); 205 if (v.size() == 1) { 206 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]); 207 } 208 209 // Querying for suggestions given a prefix is case-insensitive, so the prefix 210 // "cLa" shoud get suggestions for both Clarks. 211 EXPECT_TRUE(table_->GetFormValuesForElementName( 212 ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v, 6)); 213 EXPECT_EQ(2U, v.size()); 214 if (v.size() == 2) { 215 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]); 216 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]); 217 } 218 219 // Removing all elements since the beginning of this function should remove 220 // everything from the database. 221 changes.clear(); 222 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, Time(), &changes)); 223 224 const AutofillChange kExpectedChanges[] = { 225 AutofillChange(AutofillChange::REMOVE, 226 AutofillKey(ASCIIToUTF16("Name"), 227 ASCIIToUTF16("Superman"))), 228 AutofillChange(AutofillChange::REMOVE, 229 AutofillKey(ASCIIToUTF16("Name"), 230 ASCIIToUTF16("Clark Kent"))), 231 AutofillChange(AutofillChange::REMOVE, 232 AutofillKey(ASCIIToUTF16("Name"), 233 ASCIIToUTF16("Clark Sutter"))), 234 AutofillChange(AutofillChange::REMOVE, 235 AutofillKey(ASCIIToUTF16("Favorite Color"), 236 ASCIIToUTF16("Green"))), 237 }; 238 EXPECT_EQ(arraysize(kExpectedChanges), changes.size()); 239 for (size_t i = 0; i < arraysize(kExpectedChanges); ++i) { 240 EXPECT_EQ(kExpectedChanges[i], changes[i]); 241 } 242 243 EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"), 244 ASCIIToUTF16("Clark Kent"), db_.get())); 245 246 EXPECT_TRUE(table_->GetFormValuesForElementName( 247 ASCIIToUTF16("Name"), base::string16(), &v, 6)); 248 EXPECT_EQ(0U, v.size()); 249 250 // Now add some values with empty strings. 251 const base::string16 kValue = ASCIIToUTF16(" toto "); 252 field.name = ASCIIToUTF16("blank"); 253 field.value = base::string16(); 254 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); 255 field.name = ASCIIToUTF16("blank"); 256 field.value = ASCIIToUTF16(" "); 257 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); 258 field.name = ASCIIToUTF16("blank"); 259 field.value = ASCIIToUTF16(" "); 260 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); 261 field.name = ASCIIToUTF16("blank"); 262 field.value = kValue; 263 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); 264 265 // They should be stored normally as the DB layer does not check for empty 266 // values. 267 v.clear(); 268 EXPECT_TRUE(table_->GetFormValuesForElementName( 269 ASCIIToUTF16("blank"), base::string16(), &v, 10)); 270 EXPECT_EQ(4U, v.size()); 271} 272 273TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) { 274 TimeDelta one_day(TimeDelta::FromDays(1)); 275 Time t1 = Time::Now(); 276 Time t2 = t1 + one_day; 277 278 AutofillChangeList changes; 279 FormFieldData field; 280 field.name = ASCIIToUTF16("Name"); 281 field.value = ASCIIToUTF16("Superman"); 282 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1)); 283 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t2)); 284 285 changes.clear(); 286 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, t2, &changes)); 287 ASSERT_EQ(1U, changes.size()); 288 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, 289 AutofillKey(ASCIIToUTF16("Name"), 290 ASCIIToUTF16("Superman"))), 291 changes[0]); 292 changes.clear(); 293 294 EXPECT_TRUE( 295 table_->RemoveFormElementsAddedBetween(t2, t2 + one_day, &changes)); 296 ASSERT_EQ(1U, changes.size()); 297 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE, 298 AutofillKey(ASCIIToUTF16("Name"), 299 ASCIIToUTF16("Superman"))), 300 changes[0]); 301} 302 303TEST_F(AutofillTableTest, Autofill_AddChanges) { 304 TimeDelta one_day(TimeDelta::FromDays(1)); 305 Time t1 = Time::Now(); 306 Time t2 = t1 + one_day; 307 308 AutofillChangeList changes; 309 FormFieldData field; 310 field.name = ASCIIToUTF16("Name"); 311 field.value = ASCIIToUTF16("Superman"); 312 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1)); 313 ASSERT_EQ(1U, changes.size()); 314 EXPECT_EQ(AutofillChange(AutofillChange::ADD, 315 AutofillKey(ASCIIToUTF16("Name"), 316 ASCIIToUTF16("Superman"))), 317 changes[0]); 318 319 changes.clear(); 320 EXPECT_TRUE( 321 table_->AddFormFieldValueTime(field, &changes, t2)); 322 ASSERT_EQ(1U, changes.size()); 323 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, 324 AutofillKey(ASCIIToUTF16("Name"), 325 ASCIIToUTF16("Superman"))), 326 changes[0]); 327} 328 329TEST_F(AutofillTableTest, Autofill_UpdateOneWithOneTimestamp) { 330 AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, -1)); 331 std::vector<AutofillEntry> entries; 332 entries.push_back(entry); 333 ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); 334 335 EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"), 336 db_.get())); 337 338 std::vector<AutofillEntry> all_entries; 339 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); 340 ASSERT_EQ(1U, all_entries.size()); 341 EXPECT_EQ(entry, all_entries[0]); 342} 343 344TEST_F(AutofillTableTest, Autofill_UpdateOneWithTwoTimestamps) { 345 AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2)); 346 std::vector<AutofillEntry> entries; 347 entries.push_back(entry); 348 ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); 349 350 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"), 351 db_.get())); 352 353 std::vector<AutofillEntry> all_entries; 354 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); 355 ASSERT_EQ(1U, all_entries.size()); 356 EXPECT_EQ(entry, all_entries[0]); 357} 358 359TEST_F(AutofillTableTest, Autofill_GetAutofillTimestamps) { 360 AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2)); 361 std::vector<AutofillEntry> entries; 362 entries.push_back(entry); 363 ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); 364 365 Time date_created, date_last_used; 366 ASSERT_TRUE(table_->GetAutofillTimestamps(ASCIIToUTF16("foo"), 367 ASCIIToUTF16("bar"), 368 &date_created, 369 &date_last_used)); 370 EXPECT_EQ(Time::FromTimeT(1), date_created); 371 EXPECT_EQ(Time::FromTimeT(2), date_last_used); 372} 373 374TEST_F(AutofillTableTest, Autofill_UpdateTwo) { 375 AutofillEntry entry0(MakeAutofillEntry("foo", "bar0", 1, -1)); 376 AutofillEntry entry1(MakeAutofillEntry("foo", "bar1", 2, 3)); 377 std::vector<AutofillEntry> entries; 378 entries.push_back(entry0); 379 entries.push_back(entry1); 380 ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); 381 382 EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar0"), 383 db_.get())); 384 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar1"), 385 db_.get())); 386} 387 388TEST_F(AutofillTableTest, Autofill_UpdateReplace) { 389 AutofillChangeList changes; 390 // Add a form field. This will be replaced. 391 FormFieldData field; 392 field.name = ASCIIToUTF16("Name"); 393 field.value = ASCIIToUTF16("Superman"); 394 EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); 395 396 AutofillEntry entry(MakeAutofillEntry("Name", "Superman", 1, 2)); 397 std::vector<AutofillEntry> entries; 398 entries.push_back(entry); 399 ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); 400 401 std::vector<AutofillEntry> all_entries; 402 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); 403 ASSERT_EQ(1U, all_entries.size()); 404 EXPECT_EQ(entry, all_entries[0]); 405} 406 407TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) { 408 Time t = Time::Now(); 409 AutofillEntry existing( 410 MakeAutofillEntry("Name", "Superman", t.ToTimeT(), -1)); 411 412 AutofillChangeList changes; 413 // Add a form field. This will NOT be replaced. 414 FormFieldData field; 415 field.name = existing.key().name(); 416 field.value = existing.key().value(); 417 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t)); 418 AutofillEntry entry(MakeAutofillEntry("Name", "Clark Kent", 1, 2)); 419 std::vector<AutofillEntry> entries; 420 entries.push_back(entry); 421 ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); 422 423 std::vector<AutofillEntry> all_entries; 424 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); 425 ASSERT_EQ(2U, all_entries.size()); 426 AutofillEntrySet expected_entries(all_entries.begin(), 427 all_entries.end(), 428 CompareAutofillEntries); 429 EXPECT_EQ(1U, expected_entries.count(existing)); 430 EXPECT_EQ(1U, expected_entries.count(entry)); 431} 432 433TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) { 434 Time t = Time::Now(); 435 436 // Add multiple values for "firstname" and "lastname" names. Test that only 437 // first value of each gets added. Related to security issue: 438 // http://crbug.com/51727. 439 std::vector<FormFieldData> elements; 440 FormFieldData field; 441 field.name = ASCIIToUTF16("firstname"); 442 field.value = ASCIIToUTF16("Joe"); 443 elements.push_back(field); 444 445 field.name = ASCIIToUTF16("firstname"); 446 field.value = ASCIIToUTF16("Jane"); 447 elements.push_back(field); 448 449 field.name = ASCIIToUTF16("lastname"); 450 field.value = ASCIIToUTF16("Smith"); 451 elements.push_back(field); 452 453 field.name = ASCIIToUTF16("lastname"); 454 field.value = ASCIIToUTF16("Jones"); 455 elements.push_back(field); 456 457 std::vector<AutofillChange> changes; 458 table_->AddFormFieldValuesTime(elements, &changes, t); 459 460 ASSERT_EQ(2U, changes.size()); 461 EXPECT_EQ(changes[0], AutofillChange(AutofillChange::ADD, 462 AutofillKey(ASCIIToUTF16("firstname"), 463 ASCIIToUTF16("Joe")))); 464 EXPECT_EQ(changes[1], AutofillChange(AutofillChange::ADD, 465 AutofillKey(ASCIIToUTF16("lastname"), 466 ASCIIToUTF16("Smith")))); 467 468 std::vector<AutofillEntry> all_entries; 469 ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); 470 ASSERT_EQ(2U, all_entries.size()); 471} 472 473TEST_F(AutofillTableTest, 474 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore) { 475 // Add an entry used only before the targetted range. 476 AutofillChangeList changes; 477 FormFieldData field; 478 field.name = ASCIIToUTF16("Name"); 479 field.value = ASCIIToUTF16("Superman"); 480 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 481 base::Time::FromTimeT(10))); 482 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 483 base::Time::FromTimeT(20))); 484 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 485 base::Time::FromTimeT(30))); 486 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 487 base::Time::FromTimeT(40))); 488 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 489 base::Time::FromTimeT(50))); 490 491 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 492 493 changes.clear(); 494 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(51), 495 base::Time::FromTimeT(60), 496 &changes)); 497 EXPECT_TRUE(changes.empty()); 498 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 499} 500 501TEST_F(AutofillTableTest, 502 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter) { 503 // Add an entry used only after the targetted range. 504 AutofillChangeList changes; 505 FormFieldData field; 506 field.name = ASCIIToUTF16("Name"); 507 field.value = ASCIIToUTF16("Superman"); 508 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 509 base::Time::FromTimeT(50))); 510 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 511 base::Time::FromTimeT(60))); 512 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 513 base::Time::FromTimeT(70))); 514 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 515 base::Time::FromTimeT(80))); 516 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 517 base::Time::FromTimeT(90))); 518 519 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 520 521 changes.clear(); 522 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40), 523 base::Time::FromTimeT(50), 524 &changes)); 525 EXPECT_TRUE(changes.empty()); 526 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 527} 528 529TEST_F(AutofillTableTest, 530 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring) { 531 // Add an entry used entirely during the targetted range. 532 AutofillChangeList changes; 533 FormFieldData field; 534 field.name = ASCIIToUTF16("Name"); 535 field.value = ASCIIToUTF16("Superman"); 536 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 537 base::Time::FromTimeT(10))); 538 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 539 base::Time::FromTimeT(20))); 540 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 541 base::Time::FromTimeT(30))); 542 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 543 base::Time::FromTimeT(40))); 544 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 545 base::Time::FromTimeT(50))); 546 547 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 548 549 changes.clear(); 550 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(10), 551 base::Time::FromTimeT(51), 552 &changes)); 553 ASSERT_EQ(1U, changes.size()); 554 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE, 555 AutofillKey(field.name, field.value)), 556 changes[0]); 557 EXPECT_EQ(0, GetAutofillEntryCount(field.name, field.value, db_.get())); 558} 559 560TEST_F(AutofillTableTest, 561 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring) { 562 // Add an entry used both before and during the targetted range. 563 AutofillChangeList changes; 564 FormFieldData field; 565 field.name = ASCIIToUTF16("Name"); 566 field.value = ASCIIToUTF16("Superman"); 567 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 568 base::Time::FromTimeT(10))); 569 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 570 base::Time::FromTimeT(20))); 571 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 572 base::Time::FromTimeT(30))); 573 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 574 base::Time::FromTimeT(40))); 575 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 576 base::Time::FromTimeT(50))); 577 578 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 579 580 changes.clear(); 581 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40), 582 base::Time::FromTimeT(60), 583 &changes)); 584 ASSERT_EQ(1U, changes.size()); 585 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, 586 AutofillKey(field.name, field.value)), 587 changes[0]); 588 EXPECT_EQ(4, GetAutofillEntryCount(field.name, field.value, db_.get())); 589 base::Time date_created, date_last_used; 590 EXPECT_TRUE( 591 table_->GetAutofillTimestamps(field.name, field.value, 592 &date_created, &date_last_used)); 593 EXPECT_EQ(base::Time::FromTimeT(10), date_created); 594 EXPECT_EQ(base::Time::FromTimeT(39), date_last_used); 595} 596 597TEST_F(AutofillTableTest, 598 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter) { 599 // Add an entry used both during and after the targetted range. 600 AutofillChangeList changes; 601 FormFieldData field; 602 field.name = ASCIIToUTF16("Name"); 603 field.value = ASCIIToUTF16("Superman"); 604 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 605 base::Time::FromTimeT(50))); 606 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 607 base::Time::FromTimeT(60))); 608 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 609 base::Time::FromTimeT(70))); 610 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 611 base::Time::FromTimeT(80))); 612 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 613 base::Time::FromTimeT(90))); 614 615 EXPECT_EQ(5, GetAutofillEntryCount(field.name, field.value, db_.get())); 616 617 changes.clear(); 618 EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40), 619 base::Time::FromTimeT(80), 620 &changes)); 621 ASSERT_EQ(1U, changes.size()); 622 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, 623 AutofillKey(field.name, field.value)), 624 changes[0]); 625 EXPECT_EQ(2, GetAutofillEntryCount(field.name, field.value, db_.get())); 626 base::Time date_created, date_last_used; 627 EXPECT_TRUE( 628 table_->GetAutofillTimestamps(field.name, field.value, 629 &date_created, &date_last_used)); 630 EXPECT_EQ(base::Time::FromTimeT(80), date_created); 631 EXPECT_EQ(base::Time::FromTimeT(90), date_last_used); 632} 633 634TEST_F(AutofillTableTest, AutofillProfile) { 635 // Add a 'Home' profile. 636 AutofillProfile home_profile; 637 home_profile.set_origin(std::string()); 638 home_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 639 home_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 640 home_profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 641 home_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz")); 642 home_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google")); 643 home_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way")); 644 home_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5")); 645 home_profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 646 ASCIIToUTF16("Beverly Hills")); 647 home_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 648 home_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 649 home_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 650 home_profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, ASCIIToUTF16("MAGIC ###")); 651 home_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 652 home_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); 653 654 Time pre_creation_time = Time::Now(); 655 EXPECT_TRUE(table_->AddAutofillProfile(home_profile)); 656 Time post_creation_time = Time::Now(); 657 658 // Get the 'Home' profile. 659 AutofillProfile* db_profile; 660 ASSERT_TRUE(table_->GetAutofillProfile(home_profile.guid(), &db_profile)); 661 EXPECT_EQ(home_profile, *db_profile); 662 sql::Statement s_home(db_->GetSQLConnection()->GetUniqueStatement( 663 "SELECT date_modified " 664 "FROM autofill_profiles WHERE guid=?")); 665 s_home.BindString(0, home_profile.guid()); 666 ASSERT_TRUE(s_home.is_valid()); 667 ASSERT_TRUE(s_home.Step()); 668 EXPECT_GE(s_home.ColumnInt64(0), pre_creation_time.ToTimeT()); 669 EXPECT_LE(s_home.ColumnInt64(0), post_creation_time.ToTimeT()); 670 EXPECT_FALSE(s_home.Step()); 671 delete db_profile; 672 673 // Add a 'Billing' profile. 674 AutofillProfile billing_profile = home_profile; 675 billing_profile.set_guid(base::GenerateGUID()); 676 billing_profile.set_origin("https://www.example.com/"); 677 billing_profile.SetRawInfo(ADDRESS_HOME_LINE1, 678 ASCIIToUTF16("5678 Bottom Street")); 679 billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("suite 3")); 680 681 pre_creation_time = Time::Now(); 682 EXPECT_TRUE(table_->AddAutofillProfile(billing_profile)); 683 post_creation_time = Time::Now(); 684 685 // Get the 'Billing' profile. 686 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 687 EXPECT_EQ(billing_profile, *db_profile); 688 sql::Statement s_billing(db_->GetSQLConnection()->GetUniqueStatement( 689 "SELECT date_modified FROM autofill_profiles WHERE guid=?")); 690 s_billing.BindString(0, billing_profile.guid()); 691 ASSERT_TRUE(s_billing.is_valid()); 692 ASSERT_TRUE(s_billing.Step()); 693 EXPECT_GE(s_billing.ColumnInt64(0), pre_creation_time.ToTimeT()); 694 EXPECT_LE(s_billing.ColumnInt64(0), post_creation_time.ToTimeT()); 695 EXPECT_FALSE(s_billing.Step()); 696 delete db_profile; 697 698 // Update the 'Billing' profile, name only. 699 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane")); 700 Time pre_modification_time = Time::Now(); 701 EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile)); 702 Time post_modification_time = Time::Now(); 703 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 704 EXPECT_EQ(billing_profile, *db_profile); 705 sql::Statement s_billing_updated(db_->GetSQLConnection()->GetUniqueStatement( 706 "SELECT date_modified FROM autofill_profiles WHERE guid=?")); 707 s_billing_updated.BindString(0, billing_profile.guid()); 708 ASSERT_TRUE(s_billing_updated.is_valid()); 709 ASSERT_TRUE(s_billing_updated.Step()); 710 EXPECT_GE(s_billing_updated.ColumnInt64(0), 711 pre_modification_time.ToTimeT()); 712 EXPECT_LE(s_billing_updated.ColumnInt64(0), 713 post_modification_time.ToTimeT()); 714 EXPECT_FALSE(s_billing_updated.Step()); 715 delete db_profile; 716 717 // Update the 'Billing' profile. 718 billing_profile.set_origin("Chrome settings"); 719 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Janice")); 720 billing_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("C.")); 721 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Joplin")); 722 billing_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("jane@singer.com")); 723 billing_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Indy")); 724 billing_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("Open Road")); 725 billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("Route 66")); 726 billing_profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 727 ASCIIToUTF16("District 9")); 728 billing_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("NFA")); 729 billing_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("NY")); 730 billing_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("10011")); 731 billing_profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, ASCIIToUTF16("123456")); 732 billing_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 733 billing_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, 734 ASCIIToUTF16("18181230000")); 735 Time pre_modification_time_2 = Time::Now(); 736 EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile)); 737 Time post_modification_time_2 = Time::Now(); 738 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 739 EXPECT_EQ(billing_profile, *db_profile); 740 sql::Statement s_billing_updated_2( 741 db_->GetSQLConnection()->GetUniqueStatement( 742 "SELECT date_modified FROM autofill_profiles WHERE guid=?")); 743 s_billing_updated_2.BindString(0, billing_profile.guid()); 744 ASSERT_TRUE(s_billing_updated_2.is_valid()); 745 ASSERT_TRUE(s_billing_updated_2.Step()); 746 EXPECT_GE(s_billing_updated_2.ColumnInt64(0), 747 pre_modification_time_2.ToTimeT()); 748 EXPECT_LE(s_billing_updated_2.ColumnInt64(0), 749 post_modification_time_2.ToTimeT()); 750 EXPECT_FALSE(s_billing_updated_2.Step()); 751 delete db_profile; 752 753 // Remove the 'Billing' profile. 754 EXPECT_TRUE(table_->RemoveAutofillProfile(billing_profile.guid())); 755 EXPECT_FALSE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 756} 757 758TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { 759 AutofillProfile p; 760 const base::string16 kJohnDoe(ASCIIToUTF16("John Doe")); 761 const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe")); 762 std::vector<base::string16> set_values; 763 set_values.push_back(kJohnDoe); 764 set_values.push_back(kJohnPDoe); 765 p.SetRawMultiInfo(NAME_FULL, set_values); 766 767 EXPECT_TRUE(table_->AddAutofillProfile(p)); 768 769 AutofillProfile* db_profile; 770 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 771 EXPECT_EQ(p, *db_profile); 772 EXPECT_EQ(0, p.Compare(*db_profile)); 773 delete db_profile; 774 775 // Update the values. 776 const base::string16 kNoOne(ASCIIToUTF16("No One")); 777 set_values[1] = kNoOne; 778 p.SetRawMultiInfo(NAME_FULL, set_values); 779 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 780 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 781 EXPECT_EQ(p, *db_profile); 782 EXPECT_EQ(0, p.Compare(*db_profile)); 783 delete db_profile; 784 785 // Delete values. 786 set_values.clear(); 787 p.SetRawMultiInfo(NAME_FULL, set_values); 788 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 789 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 790 EXPECT_EQ(p, *db_profile); 791 EXPECT_EQ(0, p.Compare(*db_profile)); 792 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(NAME_FULL)); 793 delete db_profile; 794} 795 796TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { 797 AutofillProfile p; 798 const base::string16 kJohnDoe(ASCIIToUTF16("john@doe.com")); 799 const base::string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com")); 800 std::vector<base::string16> set_values; 801 set_values.push_back(kJohnDoe); 802 set_values.push_back(kJohnPDoe); 803 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 804 805 EXPECT_TRUE(table_->AddAutofillProfile(p)); 806 807 AutofillProfile* db_profile; 808 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 809 EXPECT_EQ(p, *db_profile); 810 EXPECT_EQ(0, p.Compare(*db_profile)); 811 delete db_profile; 812 813 // Update the values. 814 const base::string16 kNoOne(ASCIIToUTF16("no@one.com")); 815 set_values[1] = kNoOne; 816 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 817 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 818 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 819 EXPECT_EQ(p, *db_profile); 820 EXPECT_EQ(0, p.Compare(*db_profile)); 821 delete db_profile; 822 823 // Delete values. 824 set_values.clear(); 825 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 826 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 827 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 828 EXPECT_EQ(p, *db_profile); 829 EXPECT_EQ(0, p.Compare(*db_profile)); 830 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS)); 831 delete db_profile; 832} 833 834TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { 835 AutofillProfile p; 836 const base::string16 kJohnDoe(ASCIIToUTF16("4151112222")); 837 const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333")); 838 std::vector<base::string16> set_values; 839 set_values.push_back(kJohnDoe); 840 set_values.push_back(kJohnPDoe); 841 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 842 843 EXPECT_TRUE(table_->AddAutofillProfile(p)); 844 845 AutofillProfile* db_profile; 846 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 847 EXPECT_EQ(p, *db_profile); 848 EXPECT_EQ(0, p.Compare(*db_profile)); 849 delete db_profile; 850 851 // Update the values. 852 const base::string16 kNoOne(ASCIIToUTF16("4151110000")); 853 set_values[1] = kNoOne; 854 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 855 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 856 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 857 EXPECT_EQ(p, *db_profile); 858 EXPECT_EQ(0, p.Compare(*db_profile)); 859 delete db_profile; 860 861 // Delete values. 862 set_values.clear(); 863 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 864 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 865 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 866 EXPECT_EQ(p, *db_profile); 867 EXPECT_EQ(0, p.Compare(*db_profile)); 868 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS)); 869 delete db_profile; 870} 871 872TEST_F(AutofillTableTest, AutofillProfileTrash) { 873 std::vector<std::string> guids; 874 table_->GetAutofillProfilesInTrash(&guids); 875 EXPECT_TRUE(guids.empty()); 876 877 ASSERT_TRUE(table_->AddAutofillGUIDToTrash( 878 "00000000-0000-0000-0000-000000000000")); 879 ASSERT_TRUE(table_->AddAutofillGUIDToTrash( 880 "00000000-0000-0000-0000-000000000001")); 881 ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); 882 EXPECT_EQ(2UL, guids.size()); 883 EXPECT_EQ("00000000-0000-0000-0000-000000000000", guids[0]); 884 EXPECT_EQ("00000000-0000-0000-0000-000000000001", guids[1]); 885 886 ASSERT_TRUE(table_->EmptyAutofillProfilesTrash()); 887 ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); 888 EXPECT_TRUE(guids.empty()); 889} 890 891TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) { 892 std::vector<std::string> guids; 893 table_->GetAutofillProfilesInTrash(&guids); 894 EXPECT_TRUE(guids.empty()); 895 896 AutofillProfile profile; 897 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 898 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 899 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 900 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz")); 901 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1 Main St")); 902 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 903 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 904 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 905 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 906 907 // Mark this profile as in the trash. This stops |AddAutofillProfile| from 908 // adding it. 909 EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid())); 910 EXPECT_TRUE(table_->AddAutofillProfile(profile)); 911 AutofillProfile* added_profile = NULL; 912 EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &added_profile)); 913 EXPECT_EQ(static_cast<AutofillProfile*>(NULL), added_profile); 914 915 // Add the profile for real this time. 916 EXPECT_TRUE(table_->EmptyAutofillProfilesTrash()); 917 EXPECT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); 918 EXPECT_TRUE(guids.empty()); 919 EXPECT_TRUE(table_->AddAutofillProfile(profile)); 920 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), 921 &added_profile)); 922 ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile); 923 delete added_profile; 924 925 // Mark this profile as in the trash. This stops |UpdateAutofillProfileMulti| 926 // from updating it. In normal operation a profile should not be both in the 927 // trash and in the profiles table simultaneously. 928 EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid())); 929 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane")); 930 EXPECT_TRUE(table_->UpdateAutofillProfile(profile)); 931 AutofillProfile* updated_profile = NULL; 932 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &updated_profile)); 933 ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile); 934 EXPECT_EQ(ASCIIToUTF16("John"), updated_profile->GetRawInfo(NAME_FIRST)); 935 delete updated_profile; 936 937 // Try to delete the trashed profile. This stops |RemoveAutofillProfile| from 938 // deleting it. In normal operation deletion is done by migration step, and 939 // removal from trash is done by |WebDataService|. |RemoveAutofillProfile| 940 // does remove the item from the trash if it is found however, so that if 941 // other clients remove it (via Sync say) then it is gone and doesn't need to 942 // be processed further by |WebDataService|. 943 EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid())); 944 AutofillProfile* removed_profile = NULL; 945 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &removed_profile)); 946 EXPECT_FALSE(table_->IsAutofillGUIDInTrash(profile.guid())); 947 ASSERT_NE(static_cast<AutofillProfile*>(NULL), removed_profile); 948 delete removed_profile; 949 950 // Check that emptying the trash now allows removal to occur. 951 EXPECT_TRUE(table_->EmptyAutofillProfilesTrash()); 952 EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid())); 953 removed_profile = NULL; 954 EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &removed_profile)); 955 EXPECT_EQ(static_cast<AutofillProfile*>(NULL), removed_profile); 956} 957 958TEST_F(AutofillTableTest, CreditCard) { 959 // Add a 'Work' credit card. 960 CreditCard work_creditcard; 961 work_creditcard.set_origin("https://www.example.com/"); 962 work_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 963 work_creditcard.SetRawInfo(CREDIT_CARD_NUMBER, 964 ASCIIToUTF16("1234567890123456")); 965 work_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); 966 work_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, 967 ASCIIToUTF16("2013")); 968 969 Time pre_creation_time = Time::Now(); 970 EXPECT_TRUE(table_->AddCreditCard(work_creditcard)); 971 Time post_creation_time = Time::Now(); 972 973 // Get the 'Work' credit card. 974 CreditCard* db_creditcard; 975 ASSERT_TRUE(table_->GetCreditCard(work_creditcard.guid(), &db_creditcard)); 976 EXPECT_EQ(work_creditcard, *db_creditcard); 977 sql::Statement s_work(db_->GetSQLConnection()->GetUniqueStatement( 978 "SELECT guid, name_on_card, expiration_month, expiration_year, " 979 "card_number_encrypted, date_modified " 980 "FROM credit_cards WHERE guid=?")); 981 s_work.BindString(0, work_creditcard.guid()); 982 ASSERT_TRUE(s_work.is_valid()); 983 ASSERT_TRUE(s_work.Step()); 984 EXPECT_GE(s_work.ColumnInt64(5), pre_creation_time.ToTimeT()); 985 EXPECT_LE(s_work.ColumnInt64(5), post_creation_time.ToTimeT()); 986 EXPECT_FALSE(s_work.Step()); 987 delete db_creditcard; 988 989 // Add a 'Target' credit card. 990 CreditCard target_creditcard; 991 target_creditcard.set_origin(std::string()); 992 target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 993 target_creditcard.SetRawInfo(CREDIT_CARD_NUMBER, 994 ASCIIToUTF16("1111222233334444")); 995 target_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("06")); 996 target_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, 997 ASCIIToUTF16("2012")); 998 999 pre_creation_time = Time::Now(); 1000 EXPECT_TRUE(table_->AddCreditCard(target_creditcard)); 1001 post_creation_time = Time::Now(); 1002 ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); 1003 EXPECT_EQ(target_creditcard, *db_creditcard); 1004 sql::Statement s_target(db_->GetSQLConnection()->GetUniqueStatement( 1005 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1006 "card_number_encrypted, date_modified " 1007 "FROM credit_cards WHERE guid=?")); 1008 s_target.BindString(0, target_creditcard.guid()); 1009 ASSERT_TRUE(s_target.is_valid()); 1010 ASSERT_TRUE(s_target.Step()); 1011 EXPECT_GE(s_target.ColumnInt64(5), pre_creation_time.ToTimeT()); 1012 EXPECT_LE(s_target.ColumnInt64(5), post_creation_time.ToTimeT()); 1013 EXPECT_FALSE(s_target.Step()); 1014 delete db_creditcard; 1015 1016 // Update the 'Target' credit card. 1017 target_creditcard.set_origin("Interactive Autofill dialog"); 1018 target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Charles Grady")); 1019 Time pre_modification_time = Time::Now(); 1020 EXPECT_TRUE(table_->UpdateCreditCard(target_creditcard)); 1021 Time post_modification_time = Time::Now(); 1022 ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); 1023 EXPECT_EQ(target_creditcard, *db_creditcard); 1024 sql::Statement s_target_updated(db_->GetSQLConnection()->GetUniqueStatement( 1025 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1026 "card_number_encrypted, date_modified " 1027 "FROM credit_cards WHERE guid=?")); 1028 s_target_updated.BindString(0, target_creditcard.guid()); 1029 ASSERT_TRUE(s_target_updated.is_valid()); 1030 ASSERT_TRUE(s_target_updated.Step()); 1031 EXPECT_GE(s_target_updated.ColumnInt64(5), pre_modification_time.ToTimeT()); 1032 EXPECT_LE(s_target_updated.ColumnInt64(5), post_modification_time.ToTimeT()); 1033 EXPECT_FALSE(s_target_updated.Step()); 1034 delete db_creditcard; 1035 1036 // Remove the 'Target' credit card. 1037 EXPECT_TRUE(table_->RemoveCreditCard(target_creditcard.guid())); 1038 EXPECT_FALSE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); 1039} 1040 1041TEST_F(AutofillTableTest, UpdateAutofillProfile) { 1042 // Add a profile to the db. 1043 AutofillProfile profile; 1044 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 1045 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 1046 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1047 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@example.com")); 1048 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google")); 1049 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way")); 1050 profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5")); 1051 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 1052 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1053 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 1054 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 1055 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); 1056 table_->AddAutofillProfile(profile); 1057 1058 // Set a mocked value for the profile's creation time. 1059 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1060 sql::Statement s_mock_creation_date( 1061 db_->GetSQLConnection()->GetUniqueStatement( 1062 "UPDATE autofill_profiles SET date_modified = ?")); 1063 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1064 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1065 ASSERT_TRUE(s_mock_creation_date.Run()); 1066 1067 // Get the profile. 1068 AutofillProfile* tmp_profile; 1069 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1070 scoped_ptr<AutofillProfile> db_profile(tmp_profile); 1071 EXPECT_EQ(profile, *db_profile); 1072 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1073 "SELECT date_modified FROM autofill_profiles")); 1074 ASSERT_TRUE(s_original.is_valid()); 1075 ASSERT_TRUE(s_original.Step()); 1076 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1077 EXPECT_FALSE(s_original.Step()); 1078 1079 // Now, update the profile and save the update to the database. 1080 // The modification date should change to reflect the update. 1081 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz")); 1082 table_->UpdateAutofillProfile(profile); 1083 1084 // Get the profile. 1085 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1086 db_profile.reset(tmp_profile); 1087 EXPECT_EQ(profile, *db_profile); 1088 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1089 "SELECT date_modified FROM autofill_profiles")); 1090 ASSERT_TRUE(s_updated.is_valid()); 1091 ASSERT_TRUE(s_updated.Step()); 1092 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1093 EXPECT_FALSE(s_updated.Step()); 1094 1095 // Set a mocked value for the profile's modification time. 1096 const time_t mock_modification_date = Time::Now().ToTimeT() - 7; 1097 sql::Statement s_mock_modification_date( 1098 db_->GetSQLConnection()->GetUniqueStatement( 1099 "UPDATE autofill_profiles SET date_modified = ?")); 1100 ASSERT_TRUE(s_mock_modification_date.is_valid()); 1101 s_mock_modification_date.BindInt64(0, mock_modification_date); 1102 ASSERT_TRUE(s_mock_modification_date.Run()); 1103 1104 // Finally, call into |UpdateAutofillProfile()| without changing the 1105 // profile. The modification date should not change. 1106 table_->UpdateAutofillProfile(profile); 1107 1108 // Get the profile. 1109 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1110 db_profile.reset(tmp_profile); 1111 EXPECT_EQ(profile, *db_profile); 1112 sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement( 1113 "SELECT date_modified FROM autofill_profiles")); 1114 ASSERT_TRUE(s_unchanged.is_valid()); 1115 ASSERT_TRUE(s_unchanged.Step()); 1116 EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0)); 1117 EXPECT_FALSE(s_unchanged.Step()); 1118} 1119 1120TEST_F(AutofillTableTest, UpdateCreditCard) { 1121 // Add a credit card to the db. 1122 CreditCard credit_card; 1123 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 1124 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456")); 1125 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); 1126 credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013")); 1127 table_->AddCreditCard(credit_card); 1128 1129 // Set a mocked value for the credit card's creation time. 1130 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1131 sql::Statement s_mock_creation_date( 1132 db_->GetSQLConnection()->GetUniqueStatement( 1133 "UPDATE credit_cards SET date_modified = ?")); 1134 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1135 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1136 ASSERT_TRUE(s_mock_creation_date.Run()); 1137 1138 // Get the credit card. 1139 CreditCard* tmp_credit_card; 1140 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1141 scoped_ptr<CreditCard> db_credit_card(tmp_credit_card); 1142 EXPECT_EQ(credit_card, *db_credit_card); 1143 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1144 "SELECT date_modified FROM credit_cards")); 1145 ASSERT_TRUE(s_original.is_valid()); 1146 ASSERT_TRUE(s_original.Step()); 1147 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1148 EXPECT_FALSE(s_original.Step()); 1149 1150 // Now, update the credit card and save the update to the database. 1151 // The modification date should change to reflect the update. 1152 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("01")); 1153 table_->UpdateCreditCard(credit_card); 1154 1155 // Get the credit card. 1156 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1157 db_credit_card.reset(tmp_credit_card); 1158 EXPECT_EQ(credit_card, *db_credit_card); 1159 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1160 "SELECT date_modified FROM credit_cards")); 1161 ASSERT_TRUE(s_updated.is_valid()); 1162 ASSERT_TRUE(s_updated.Step()); 1163 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1164 EXPECT_FALSE(s_updated.Step()); 1165 1166 // Set a mocked value for the credit card's modification time. 1167 const time_t mock_modification_date = Time::Now().ToTimeT() - 7; 1168 sql::Statement s_mock_modification_date( 1169 db_->GetSQLConnection()->GetUniqueStatement( 1170 "UPDATE credit_cards SET date_modified = ?")); 1171 ASSERT_TRUE(s_mock_modification_date.is_valid()); 1172 s_mock_modification_date.BindInt64(0, mock_modification_date); 1173 ASSERT_TRUE(s_mock_modification_date.Run()); 1174 1175 // Finally, call into |UpdateCreditCard()| without changing the credit card. 1176 // The modification date should not change. 1177 table_->UpdateCreditCard(credit_card); 1178 1179 // Get the credit card. 1180 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1181 db_credit_card.reset(tmp_credit_card); 1182 EXPECT_EQ(credit_card, *db_credit_card); 1183 sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement( 1184 "SELECT date_modified FROM credit_cards")); 1185 ASSERT_TRUE(s_unchanged.is_valid()); 1186 ASSERT_TRUE(s_unchanged.Step()); 1187 EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0)); 1188 EXPECT_FALSE(s_unchanged.Step()); 1189} 1190 1191TEST_F(AutofillTableTest, UpdateProfileOriginOnly) { 1192 // Add a profile to the db. 1193 AutofillProfile profile; 1194 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 1195 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 1196 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1197 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@example.com")); 1198 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google")); 1199 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way")); 1200 profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5")); 1201 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 1202 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1203 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 1204 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 1205 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); 1206 table_->AddAutofillProfile(profile); 1207 1208 // Set a mocked value for the profile's creation time. 1209 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1210 sql::Statement s_mock_creation_date( 1211 db_->GetSQLConnection()->GetUniqueStatement( 1212 "UPDATE autofill_profiles SET date_modified = ?")); 1213 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1214 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1215 ASSERT_TRUE(s_mock_creation_date.Run()); 1216 1217 // Get the profile. 1218 AutofillProfile* tmp_profile; 1219 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1220 scoped_ptr<AutofillProfile> db_profile(tmp_profile); 1221 EXPECT_EQ(profile, *db_profile); 1222 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1223 "SELECT date_modified FROM autofill_profiles")); 1224 ASSERT_TRUE(s_original.is_valid()); 1225 ASSERT_TRUE(s_original.Step()); 1226 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1227 EXPECT_FALSE(s_original.Step()); 1228 1229 // Now, update just the profile's origin and save the update to the database. 1230 // The modification date should change to reflect the update. 1231 profile.set_origin("https://www.example.com/"); 1232 table_->UpdateAutofillProfile(profile); 1233 1234 // Get the profile. 1235 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1236 db_profile.reset(tmp_profile); 1237 EXPECT_EQ(profile, *db_profile); 1238 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1239 "SELECT date_modified FROM autofill_profiles")); 1240 ASSERT_TRUE(s_updated.is_valid()); 1241 ASSERT_TRUE(s_updated.Step()); 1242 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1243 EXPECT_FALSE(s_updated.Step()); 1244} 1245 1246TEST_F(AutofillTableTest, UpdateCreditCardOriginOnly) { 1247 // Add a credit card to the db. 1248 CreditCard credit_card; 1249 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 1250 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456")); 1251 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); 1252 credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013")); 1253 table_->AddCreditCard(credit_card); 1254 1255 // Set a mocked value for the credit card's creation time. 1256 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1257 sql::Statement s_mock_creation_date( 1258 db_->GetSQLConnection()->GetUniqueStatement( 1259 "UPDATE credit_cards SET date_modified = ?")); 1260 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1261 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1262 ASSERT_TRUE(s_mock_creation_date.Run()); 1263 1264 // Get the credit card. 1265 CreditCard* tmp_credit_card; 1266 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1267 scoped_ptr<CreditCard> db_credit_card(tmp_credit_card); 1268 EXPECT_EQ(credit_card, *db_credit_card); 1269 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1270 "SELECT date_modified FROM credit_cards")); 1271 ASSERT_TRUE(s_original.is_valid()); 1272 ASSERT_TRUE(s_original.Step()); 1273 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1274 EXPECT_FALSE(s_original.Step()); 1275 1276 // Now, update just the credit card's origin and save the update to the 1277 // database. The modification date should change to reflect the update. 1278 credit_card.set_origin("https://www.example.com/"); 1279 table_->UpdateCreditCard(credit_card); 1280 1281 // Get the credit card. 1282 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1283 db_credit_card.reset(tmp_credit_card); 1284 EXPECT_EQ(credit_card, *db_credit_card); 1285 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1286 "SELECT date_modified FROM credit_cards")); 1287 ASSERT_TRUE(s_updated.is_valid()); 1288 ASSERT_TRUE(s_updated.Step()); 1289 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1290 EXPECT_FALSE(s_updated.Step()); 1291} 1292 1293TEST_F(AutofillTableTest, RemoveAutofillDataModifiedBetween) { 1294 // Populate the autofill_profiles and credit_cards tables. 1295 ASSERT_TRUE(db_->GetSQLConnection()->Execute( 1296 "INSERT INTO autofill_profiles (guid, date_modified) " 1297 "VALUES('00000000-0000-0000-0000-000000000000', 11);" 1298 "INSERT INTO autofill_profiles (guid, date_modified) " 1299 "VALUES('00000000-0000-0000-0000-000000000001', 21);" 1300 "INSERT INTO autofill_profiles (guid, date_modified) " 1301 "VALUES('00000000-0000-0000-0000-000000000002', 31);" 1302 "INSERT INTO autofill_profiles (guid, date_modified) " 1303 "VALUES('00000000-0000-0000-0000-000000000003', 41);" 1304 "INSERT INTO autofill_profiles (guid, date_modified) " 1305 "VALUES('00000000-0000-0000-0000-000000000004', 51);" 1306 "INSERT INTO autofill_profiles (guid, date_modified) " 1307 "VALUES('00000000-0000-0000-0000-000000000005', 61);" 1308 "INSERT INTO credit_cards (guid, date_modified) " 1309 "VALUES('00000000-0000-0000-0000-000000000006', 17);" 1310 "INSERT INTO credit_cards (guid, date_modified) " 1311 "VALUES('00000000-0000-0000-0000-000000000007', 27);" 1312 "INSERT INTO credit_cards (guid, date_modified) " 1313 "VALUES('00000000-0000-0000-0000-000000000008', 37);" 1314 "INSERT INTO credit_cards (guid, date_modified) " 1315 "VALUES('00000000-0000-0000-0000-000000000009', 47);" 1316 "INSERT INTO credit_cards (guid, date_modified) " 1317 "VALUES('00000000-0000-0000-0000-000000000010', 57);" 1318 "INSERT INTO credit_cards (guid, date_modified) " 1319 "VALUES('00000000-0000-0000-0000-000000000011', 67);")); 1320 1321 // Remove all entries modified in the bounded time range [17,41). 1322 std::vector<std::string> profile_guids; 1323 std::vector<std::string> credit_card_guids; 1324 table_->RemoveAutofillDataModifiedBetween( 1325 Time::FromTimeT(17), Time::FromTimeT(41), 1326 &profile_guids, &credit_card_guids); 1327 ASSERT_EQ(2UL, profile_guids.size()); 1328 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profile_guids[0]); 1329 EXPECT_EQ("00000000-0000-0000-0000-000000000002", profile_guids[1]); 1330 sql::Statement s_autofill_profiles_bounded( 1331 db_->GetSQLConnection()->GetUniqueStatement( 1332 "SELECT date_modified FROM autofill_profiles")); 1333 ASSERT_TRUE(s_autofill_profiles_bounded.is_valid()); 1334 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1335 EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0)); 1336 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1337 EXPECT_EQ(41, s_autofill_profiles_bounded.ColumnInt64(0)); 1338 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1339 EXPECT_EQ(51, s_autofill_profiles_bounded.ColumnInt64(0)); 1340 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1341 EXPECT_EQ(61, s_autofill_profiles_bounded.ColumnInt64(0)); 1342 EXPECT_FALSE(s_autofill_profiles_bounded.Step()); 1343 ASSERT_EQ(3UL, credit_card_guids.size()); 1344 EXPECT_EQ("00000000-0000-0000-0000-000000000006", credit_card_guids[0]); 1345 EXPECT_EQ("00000000-0000-0000-0000-000000000007", credit_card_guids[1]); 1346 EXPECT_EQ("00000000-0000-0000-0000-000000000008", credit_card_guids[2]); 1347 sql::Statement s_credit_cards_bounded( 1348 db_->GetSQLConnection()->GetUniqueStatement( 1349 "SELECT date_modified FROM credit_cards")); 1350 ASSERT_TRUE(s_credit_cards_bounded.is_valid()); 1351 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1352 EXPECT_EQ(47, s_credit_cards_bounded.ColumnInt64(0)); 1353 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1354 EXPECT_EQ(57, s_credit_cards_bounded.ColumnInt64(0)); 1355 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1356 EXPECT_EQ(67, s_credit_cards_bounded.ColumnInt64(0)); 1357 EXPECT_FALSE(s_credit_cards_bounded.Step()); 1358 1359 // Remove all entries modified on or after time 51 (unbounded range). 1360 table_->RemoveAutofillDataModifiedBetween( 1361 Time::FromTimeT(51), Time(), 1362 &profile_guids, &credit_card_guids); 1363 ASSERT_EQ(2UL, profile_guids.size()); 1364 EXPECT_EQ("00000000-0000-0000-0000-000000000004", profile_guids[0]); 1365 EXPECT_EQ("00000000-0000-0000-0000-000000000005", profile_guids[1]); 1366 sql::Statement s_autofill_profiles_unbounded( 1367 db_->GetSQLConnection()->GetUniqueStatement( 1368 "SELECT date_modified FROM autofill_profiles")); 1369 ASSERT_TRUE(s_autofill_profiles_unbounded.is_valid()); 1370 ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); 1371 EXPECT_EQ(11, s_autofill_profiles_unbounded.ColumnInt64(0)); 1372 ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); 1373 EXPECT_EQ(41, s_autofill_profiles_unbounded.ColumnInt64(0)); 1374 EXPECT_FALSE(s_autofill_profiles_unbounded.Step()); 1375 ASSERT_EQ(2UL, credit_card_guids.size()); 1376 EXPECT_EQ("00000000-0000-0000-0000-000000000010", credit_card_guids[0]); 1377 EXPECT_EQ("00000000-0000-0000-0000-000000000011", credit_card_guids[1]); 1378 sql::Statement s_credit_cards_unbounded( 1379 db_->GetSQLConnection()->GetUniqueStatement( 1380 "SELECT date_modified FROM credit_cards")); 1381 ASSERT_TRUE(s_credit_cards_unbounded.is_valid()); 1382 ASSERT_TRUE(s_credit_cards_unbounded.Step()); 1383 EXPECT_EQ(47, s_credit_cards_unbounded.ColumnInt64(0)); 1384 EXPECT_FALSE(s_credit_cards_unbounded.Step()); 1385 1386 // Remove all remaining entries. 1387 table_->RemoveAutofillDataModifiedBetween( 1388 Time(), Time(), 1389 &profile_guids, &credit_card_guids); 1390 ASSERT_EQ(2UL, profile_guids.size()); 1391 EXPECT_EQ("00000000-0000-0000-0000-000000000000", profile_guids[0]); 1392 EXPECT_EQ("00000000-0000-0000-0000-000000000003", profile_guids[1]); 1393 sql::Statement s_autofill_profiles_empty( 1394 db_->GetSQLConnection()->GetUniqueStatement( 1395 "SELECT date_modified FROM autofill_profiles")); 1396 ASSERT_TRUE(s_autofill_profiles_empty.is_valid()); 1397 EXPECT_FALSE(s_autofill_profiles_empty.Step()); 1398 ASSERT_EQ(1UL, credit_card_guids.size()); 1399 EXPECT_EQ("00000000-0000-0000-0000-000000000009", credit_card_guids[0]); 1400 sql::Statement s_credit_cards_empty( 1401 db_->GetSQLConnection()->GetUniqueStatement( 1402 "SELECT date_modified FROM credit_cards")); 1403 ASSERT_TRUE(s_credit_cards_empty.is_valid()); 1404 EXPECT_FALSE(s_credit_cards_empty.Step()); 1405} 1406 1407TEST_F(AutofillTableTest, RemoveOriginURLsModifiedBetween) { 1408 // Populate the autofill_profiles and credit_cards tables. 1409 ASSERT_TRUE(db_->GetSQLConnection()->Execute( 1410 "INSERT INTO autofill_profiles (guid, origin, date_modified) " 1411 "VALUES('00000000-0000-0000-0000-000000000000', '', 11);" 1412 "INSERT INTO autofill_profiles (guid, origin, date_modified) " 1413 "VALUES('00000000-0000-0000-0000-000000000001', " 1414 " 'https://www.example.com/', 21);" 1415 "INSERT INTO autofill_profiles (guid, origin, date_modified) " 1416 "VALUES('00000000-0000-0000-0000-000000000002', 'Chrome settings', 31);" 1417 "INSERT INTO credit_cards (guid, origin, date_modified) " 1418 "VALUES('00000000-0000-0000-0000-000000000003', '', 17);" 1419 "INSERT INTO credit_cards (guid, origin, date_modified) " 1420 "VALUES('00000000-0000-0000-0000-000000000004', " 1421 " 'https://www.example.com/', 27);" 1422 "INSERT INTO credit_cards (guid, origin, date_modified) " 1423 "VALUES('00000000-0000-0000-0000-000000000005', 'Chrome settings', " 1424 " 37);")); 1425 1426 // Remove all origin URLs set in the bounded time range [21,27). 1427 ScopedVector<AutofillProfile> profiles; 1428 table_->RemoveOriginURLsModifiedBetween( 1429 Time::FromTimeT(21), Time::FromTimeT(27), &profiles); 1430 ASSERT_EQ(1UL, profiles.size()); 1431 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profiles[0]->guid()); 1432 sql::Statement s_autofill_profiles_bounded( 1433 db_->GetSQLConnection()->GetUniqueStatement( 1434 "SELECT date_modified, origin FROM autofill_profiles")); 1435 ASSERT_TRUE(s_autofill_profiles_bounded.is_valid()); 1436 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1437 EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0)); 1438 EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1)); 1439 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1440 EXPECT_EQ(21, s_autofill_profiles_bounded.ColumnInt64(0)); 1441 EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1)); 1442 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1443 EXPECT_EQ(31, s_autofill_profiles_bounded.ColumnInt64(0)); 1444 EXPECT_EQ("Chrome settings", s_autofill_profiles_bounded.ColumnString(1)); 1445 sql::Statement s_credit_cards_bounded( 1446 db_->GetSQLConnection()->GetUniqueStatement( 1447 "SELECT date_modified, origin FROM credit_cards")); 1448 ASSERT_TRUE(s_credit_cards_bounded.is_valid()); 1449 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1450 EXPECT_EQ(17, s_credit_cards_bounded.ColumnInt64(0)); 1451 EXPECT_EQ(std::string(), s_credit_cards_bounded.ColumnString(1)); 1452 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1453 EXPECT_EQ(27, s_credit_cards_bounded.ColumnInt64(0)); 1454 EXPECT_EQ("https://www.example.com/", 1455 s_credit_cards_bounded.ColumnString(1)); 1456 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1457 EXPECT_EQ(37, s_credit_cards_bounded.ColumnInt64(0)); 1458 EXPECT_EQ("Chrome settings", s_credit_cards_bounded.ColumnString(1)); 1459 1460 // Remove all origin URLS. 1461 profiles.clear(); 1462 table_->RemoveOriginURLsModifiedBetween(Time(), Time(), &profiles); 1463 EXPECT_EQ(0UL, profiles.size()); 1464 sql::Statement s_autofill_profiles_all( 1465 db_->GetSQLConnection()->GetUniqueStatement( 1466 "SELECT date_modified, origin FROM autofill_profiles")); 1467 ASSERT_TRUE(s_autofill_profiles_all.is_valid()); 1468 ASSERT_TRUE(s_autofill_profiles_all.Step()); 1469 EXPECT_EQ(11, s_autofill_profiles_all.ColumnInt64(0)); 1470 EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1)); 1471 ASSERT_TRUE(s_autofill_profiles_all.Step()); 1472 EXPECT_EQ(21, s_autofill_profiles_all.ColumnInt64(0)); 1473 EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1)); 1474 ASSERT_TRUE(s_autofill_profiles_all.Step()); 1475 EXPECT_EQ(31, s_autofill_profiles_all.ColumnInt64(0)); 1476 EXPECT_EQ("Chrome settings", s_autofill_profiles_all.ColumnString(1)); 1477 sql::Statement s_credit_cards_all( 1478 db_->GetSQLConnection()->GetUniqueStatement( 1479 "SELECT date_modified, origin FROM credit_cards")); 1480 ASSERT_TRUE(s_credit_cards_all.is_valid()); 1481 ASSERT_TRUE(s_credit_cards_all.Step()); 1482 EXPECT_EQ(17, s_credit_cards_all.ColumnInt64(0)); 1483 EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1)); 1484 ASSERT_TRUE(s_credit_cards_all.Step()); 1485 EXPECT_EQ(27, s_credit_cards_all.ColumnInt64(0)); 1486 EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1)); 1487 ASSERT_TRUE(s_credit_cards_all.Step()); 1488 EXPECT_EQ(37, s_credit_cards_all.ColumnInt64(0)); 1489 EXPECT_EQ("Chrome settings", s_credit_cards_all.ColumnString(1)); 1490} 1491 1492TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_NoResults) { 1493 std::vector<AutofillEntry> entries; 1494 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1495 1496 EXPECT_EQ(0U, entries.size()); 1497} 1498 1499TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) { 1500 AutofillChangeList changes; 1501 std::map<std::string, std::vector<Time> > name_value_times_map; 1502 1503 time_t start = 0; 1504 std::vector<Time> timestamps1; 1505 FormFieldData field; 1506 field.name = ASCIIToUTF16("Name"); 1507 field.value = ASCIIToUTF16("Superman"); 1508 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1509 Time::FromTimeT(start))); 1510 timestamps1.push_back(Time::FromTimeT(start)); 1511 std::string key1("NameSuperman"); 1512 name_value_times_map.insert( 1513 std::pair<std::string, std::vector<Time> >(key1, timestamps1)); 1514 1515 AutofillEntrySet expected_entries(CompareAutofillEntries); 1516 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman")); 1517 AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back()); 1518 1519 expected_entries.insert(ae1); 1520 1521 std::vector<AutofillEntry> entries; 1522 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1523 AutofillEntrySet entry_set(entries.begin(), entries.end(), 1524 CompareAutofillEntries); 1525 1526 CompareAutofillEntrySets(entry_set, expected_entries); 1527} 1528 1529TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { 1530 AutofillChangeList changes; 1531 std::map<std::string, std::vector<Time> > name_value_times_map; 1532 time_t start = 0; 1533 1534 std::vector<Time> timestamps1; 1535 FormFieldData field; 1536 field.name = ASCIIToUTF16("Name"); 1537 field.value = ASCIIToUTF16("Superman"); 1538 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1539 Time::FromTimeT(start))); 1540 timestamps1.push_back(Time::FromTimeT(start)); 1541 std::string key1("NameSuperman"); 1542 name_value_times_map.insert( 1543 std::pair<std::string, std::vector<Time> >(key1, timestamps1)); 1544 1545 ++start; 1546 std::vector<Time> timestamps2; 1547 field.name = ASCIIToUTF16("Name"); 1548 field.value = ASCIIToUTF16("Clark Kent"); 1549 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1550 Time::FromTimeT(start))); 1551 timestamps2.push_back(Time::FromTimeT(start)); 1552 std::string key2("NameClark Kent"); 1553 name_value_times_map.insert( 1554 std::pair<std::string, std::vector<Time> >(key2, timestamps2)); 1555 1556 AutofillEntrySet expected_entries(CompareAutofillEntries); 1557 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman")); 1558 AutofillKey ak2(ASCIIToUTF16("Name"), ASCIIToUTF16("Clark Kent")); 1559 AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back()); 1560 AutofillEntry ae2(ak2, timestamps2.front(), timestamps2.back()); 1561 1562 expected_entries.insert(ae1); 1563 expected_entries.insert(ae2); 1564 1565 std::vector<AutofillEntry> entries; 1566 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1567 AutofillEntrySet entry_set(entries.begin(), entries.end(), 1568 CompareAutofillEntries); 1569 1570 CompareAutofillEntrySets(entry_set, expected_entries); 1571} 1572 1573TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) { 1574 AutofillChangeList changes; 1575 std::map<std::string, std::vector<Time> > name_value_times_map; 1576 1577 std::vector<Time> timestamps; 1578 time_t start = 0; 1579 for (int i = 0; i < 2; ++i, ++start) { 1580 FormFieldData field; 1581 field.name = ASCIIToUTF16("Name"); 1582 field.value = ASCIIToUTF16("Superman"); 1583 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1584 Time::FromTimeT(start))); 1585 timestamps.push_back(Time::FromTimeT(start)); 1586 } 1587 1588 std::string key("NameSuperman"); 1589 name_value_times_map.insert( 1590 std::pair<std::string, std::vector<Time> >(key, timestamps)); 1591 1592 AutofillEntrySet expected_entries(CompareAutofillEntries); 1593 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman")); 1594 AutofillEntry ae1(ak1, timestamps.front(), timestamps.back()); 1595 1596 expected_entries.insert(ae1); 1597 1598 std::vector<AutofillEntry> entries; 1599 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1600 AutofillEntrySet entry_set(entries.begin(), entries.end(), 1601 CompareAutofillEntries); 1602 1603 CompareAutofillEntrySets(entry_set, expected_entries); 1604} 1605 1606} // namespace autofill 1607