autofill_table_unittest.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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 home_profile.set_language_code("en"); 654 655 Time pre_creation_time = Time::Now(); 656 EXPECT_TRUE(table_->AddAutofillProfile(home_profile)); 657 Time post_creation_time = Time::Now(); 658 659 // Get the 'Home' profile. 660 AutofillProfile* db_profile; 661 ASSERT_TRUE(table_->GetAutofillProfile(home_profile.guid(), &db_profile)); 662 EXPECT_EQ(home_profile, *db_profile); 663 sql::Statement s_home(db_->GetSQLConnection()->GetUniqueStatement( 664 "SELECT date_modified " 665 "FROM autofill_profiles WHERE guid=?")); 666 s_home.BindString(0, home_profile.guid()); 667 ASSERT_TRUE(s_home.is_valid()); 668 ASSERT_TRUE(s_home.Step()); 669 EXPECT_GE(s_home.ColumnInt64(0), pre_creation_time.ToTimeT()); 670 EXPECT_LE(s_home.ColumnInt64(0), post_creation_time.ToTimeT()); 671 EXPECT_FALSE(s_home.Step()); 672 delete db_profile; 673 674 // Add a 'Billing' profile. 675 AutofillProfile billing_profile = home_profile; 676 billing_profile.set_guid(base::GenerateGUID()); 677 billing_profile.set_origin("https://www.example.com/"); 678 billing_profile.SetRawInfo(ADDRESS_HOME_LINE1, 679 ASCIIToUTF16("5678 Bottom Street")); 680 billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("suite 3")); 681 682 pre_creation_time = Time::Now(); 683 EXPECT_TRUE(table_->AddAutofillProfile(billing_profile)); 684 post_creation_time = Time::Now(); 685 686 // Get the 'Billing' profile. 687 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 688 EXPECT_EQ(billing_profile, *db_profile); 689 sql::Statement s_billing(db_->GetSQLConnection()->GetUniqueStatement( 690 "SELECT date_modified FROM autofill_profiles WHERE guid=?")); 691 s_billing.BindString(0, billing_profile.guid()); 692 ASSERT_TRUE(s_billing.is_valid()); 693 ASSERT_TRUE(s_billing.Step()); 694 EXPECT_GE(s_billing.ColumnInt64(0), pre_creation_time.ToTimeT()); 695 EXPECT_LE(s_billing.ColumnInt64(0), post_creation_time.ToTimeT()); 696 EXPECT_FALSE(s_billing.Step()); 697 delete db_profile; 698 699 // Update the 'Billing' profile, name only. 700 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane")); 701 Time pre_modification_time = Time::Now(); 702 EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile)); 703 Time post_modification_time = Time::Now(); 704 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 705 EXPECT_EQ(billing_profile, *db_profile); 706 sql::Statement s_billing_updated(db_->GetSQLConnection()->GetUniqueStatement( 707 "SELECT date_modified FROM autofill_profiles WHERE guid=?")); 708 s_billing_updated.BindString(0, billing_profile.guid()); 709 ASSERT_TRUE(s_billing_updated.is_valid()); 710 ASSERT_TRUE(s_billing_updated.Step()); 711 EXPECT_GE(s_billing_updated.ColumnInt64(0), 712 pre_modification_time.ToTimeT()); 713 EXPECT_LE(s_billing_updated.ColumnInt64(0), 714 post_modification_time.ToTimeT()); 715 EXPECT_FALSE(s_billing_updated.Step()); 716 delete db_profile; 717 718 // Update the 'Billing' profile. 719 billing_profile.set_origin("Chrome settings"); 720 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Janice")); 721 billing_profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("C.")); 722 billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Joplin")); 723 billing_profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("jane@singer.com")); 724 billing_profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Indy")); 725 billing_profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("Open Road")); 726 billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("Route 66")); 727 billing_profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 728 ASCIIToUTF16("District 9")); 729 billing_profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("NFA")); 730 billing_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("NY")); 731 billing_profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("10011")); 732 billing_profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, ASCIIToUTF16("123456")); 733 billing_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 734 billing_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, 735 ASCIIToUTF16("18181230000")); 736 Time pre_modification_time_2 = Time::Now(); 737 EXPECT_TRUE(table_->UpdateAutofillProfile(billing_profile)); 738 Time post_modification_time_2 = Time::Now(); 739 ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 740 EXPECT_EQ(billing_profile, *db_profile); 741 sql::Statement s_billing_updated_2( 742 db_->GetSQLConnection()->GetUniqueStatement( 743 "SELECT date_modified FROM autofill_profiles WHERE guid=?")); 744 s_billing_updated_2.BindString(0, billing_profile.guid()); 745 ASSERT_TRUE(s_billing_updated_2.is_valid()); 746 ASSERT_TRUE(s_billing_updated_2.Step()); 747 EXPECT_GE(s_billing_updated_2.ColumnInt64(0), 748 pre_modification_time_2.ToTimeT()); 749 EXPECT_LE(s_billing_updated_2.ColumnInt64(0), 750 post_modification_time_2.ToTimeT()); 751 EXPECT_FALSE(s_billing_updated_2.Step()); 752 delete db_profile; 753 754 // Remove the 'Billing' profile. 755 EXPECT_TRUE(table_->RemoveAutofillProfile(billing_profile.guid())); 756 EXPECT_FALSE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); 757} 758 759TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { 760 AutofillProfile p; 761 const base::string16 kJohnDoe(ASCIIToUTF16("John Doe")); 762 const base::string16 kJohnPDoe(ASCIIToUTF16("John P. Doe")); 763 std::vector<base::string16> set_values; 764 set_values.push_back(kJohnDoe); 765 set_values.push_back(kJohnPDoe); 766 p.SetRawMultiInfo(NAME_FULL, set_values); 767 768 EXPECT_TRUE(table_->AddAutofillProfile(p)); 769 770 AutofillProfile* db_profile; 771 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 772 EXPECT_EQ(p, *db_profile); 773 EXPECT_EQ(0, p.Compare(*db_profile)); 774 delete db_profile; 775 776 // Update the values. 777 const base::string16 kNoOne(ASCIIToUTF16("No One")); 778 set_values[1] = kNoOne; 779 p.SetRawMultiInfo(NAME_FULL, set_values); 780 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 781 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 782 EXPECT_EQ(p, *db_profile); 783 EXPECT_EQ(0, p.Compare(*db_profile)); 784 delete db_profile; 785 786 // Delete values. 787 set_values.clear(); 788 p.SetRawMultiInfo(NAME_FULL, set_values); 789 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 790 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 791 EXPECT_EQ(p, *db_profile); 792 EXPECT_EQ(0, p.Compare(*db_profile)); 793 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(NAME_FULL)); 794 delete db_profile; 795} 796 797TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { 798 AutofillProfile p; 799 const base::string16 kJohnDoe(ASCIIToUTF16("john@doe.com")); 800 const base::string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com")); 801 std::vector<base::string16> set_values; 802 set_values.push_back(kJohnDoe); 803 set_values.push_back(kJohnPDoe); 804 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 805 806 EXPECT_TRUE(table_->AddAutofillProfile(p)); 807 808 AutofillProfile* db_profile; 809 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 810 EXPECT_EQ(p, *db_profile); 811 EXPECT_EQ(0, p.Compare(*db_profile)); 812 delete db_profile; 813 814 // Update the values. 815 const base::string16 kNoOne(ASCIIToUTF16("no@one.com")); 816 set_values[1] = kNoOne; 817 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 818 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 819 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 820 EXPECT_EQ(p, *db_profile); 821 EXPECT_EQ(0, p.Compare(*db_profile)); 822 delete db_profile; 823 824 // Delete values. 825 set_values.clear(); 826 p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); 827 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 828 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 829 EXPECT_EQ(p, *db_profile); 830 EXPECT_EQ(0, p.Compare(*db_profile)); 831 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS)); 832 delete db_profile; 833} 834 835TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { 836 AutofillProfile p; 837 const base::string16 kJohnDoe(ASCIIToUTF16("4151112222")); 838 const base::string16 kJohnPDoe(ASCIIToUTF16("4151113333")); 839 std::vector<base::string16> set_values; 840 set_values.push_back(kJohnDoe); 841 set_values.push_back(kJohnPDoe); 842 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 843 844 EXPECT_TRUE(table_->AddAutofillProfile(p)); 845 846 AutofillProfile* db_profile; 847 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 848 EXPECT_EQ(p, *db_profile); 849 EXPECT_EQ(0, p.Compare(*db_profile)); 850 delete db_profile; 851 852 // Update the values. 853 const base::string16 kNoOne(ASCIIToUTF16("4151110000")); 854 set_values[1] = kNoOne; 855 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 856 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 857 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 858 EXPECT_EQ(p, *db_profile); 859 EXPECT_EQ(0, p.Compare(*db_profile)); 860 delete db_profile; 861 862 // Delete values. 863 set_values.clear(); 864 p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); 865 EXPECT_TRUE(table_->UpdateAutofillProfile(p)); 866 ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); 867 EXPECT_EQ(p, *db_profile); 868 EXPECT_EQ(0, p.Compare(*db_profile)); 869 EXPECT_EQ(base::string16(), db_profile->GetRawInfo(EMAIL_ADDRESS)); 870 delete db_profile; 871} 872 873TEST_F(AutofillTableTest, AutofillProfileTrash) { 874 std::vector<std::string> guids; 875 table_->GetAutofillProfilesInTrash(&guids); 876 EXPECT_TRUE(guids.empty()); 877 878 ASSERT_TRUE(table_->AddAutofillGUIDToTrash( 879 "00000000-0000-0000-0000-000000000000")); 880 ASSERT_TRUE(table_->AddAutofillGUIDToTrash( 881 "00000000-0000-0000-0000-000000000001")); 882 ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); 883 EXPECT_EQ(2UL, guids.size()); 884 EXPECT_EQ("00000000-0000-0000-0000-000000000000", guids[0]); 885 EXPECT_EQ("00000000-0000-0000-0000-000000000001", guids[1]); 886 887 ASSERT_TRUE(table_->EmptyAutofillProfilesTrash()); 888 ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); 889 EXPECT_TRUE(guids.empty()); 890} 891 892TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) { 893 std::vector<std::string> guids; 894 table_->GetAutofillProfilesInTrash(&guids); 895 EXPECT_TRUE(guids.empty()); 896 897 AutofillProfile profile; 898 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 899 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 900 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 901 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz")); 902 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1 Main St")); 903 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 904 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 905 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 906 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 907 908 // Mark this profile as in the trash. This stops |AddAutofillProfile| from 909 // adding it. 910 EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid())); 911 EXPECT_TRUE(table_->AddAutofillProfile(profile)); 912 AutofillProfile* added_profile = NULL; 913 EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &added_profile)); 914 EXPECT_EQ(static_cast<AutofillProfile*>(NULL), added_profile); 915 916 // Add the profile for real this time. 917 EXPECT_TRUE(table_->EmptyAutofillProfilesTrash()); 918 EXPECT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); 919 EXPECT_TRUE(guids.empty()); 920 EXPECT_TRUE(table_->AddAutofillProfile(profile)); 921 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), 922 &added_profile)); 923 ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile); 924 delete added_profile; 925 926 // Mark this profile as in the trash. This stops |UpdateAutofillProfileMulti| 927 // from updating it. In normal operation a profile should not be both in the 928 // trash and in the profiles table simultaneously. 929 EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid())); 930 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane")); 931 EXPECT_TRUE(table_->UpdateAutofillProfile(profile)); 932 AutofillProfile* updated_profile = NULL; 933 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &updated_profile)); 934 ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile); 935 EXPECT_EQ(ASCIIToUTF16("John"), updated_profile->GetRawInfo(NAME_FIRST)); 936 delete updated_profile; 937 938 // Try to delete the trashed profile. This stops |RemoveAutofillProfile| from 939 // deleting it. In normal operation deletion is done by migration step, and 940 // removal from trash is done by |WebDataService|. |RemoveAutofillProfile| 941 // does remove the item from the trash if it is found however, so that if 942 // other clients remove it (via Sync say) then it is gone and doesn't need to 943 // be processed further by |WebDataService|. 944 EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid())); 945 AutofillProfile* removed_profile = NULL; 946 EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &removed_profile)); 947 EXPECT_FALSE(table_->IsAutofillGUIDInTrash(profile.guid())); 948 ASSERT_NE(static_cast<AutofillProfile*>(NULL), removed_profile); 949 delete removed_profile; 950 951 // Check that emptying the trash now allows removal to occur. 952 EXPECT_TRUE(table_->EmptyAutofillProfilesTrash()); 953 EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid())); 954 removed_profile = NULL; 955 EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &removed_profile)); 956 EXPECT_EQ(static_cast<AutofillProfile*>(NULL), removed_profile); 957} 958 959TEST_F(AutofillTableTest, CreditCard) { 960 // Add a 'Work' credit card. 961 CreditCard work_creditcard; 962 work_creditcard.set_origin("https://www.example.com/"); 963 work_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 964 work_creditcard.SetRawInfo(CREDIT_CARD_NUMBER, 965 ASCIIToUTF16("1234567890123456")); 966 work_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); 967 work_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, 968 ASCIIToUTF16("2013")); 969 970 Time pre_creation_time = Time::Now(); 971 EXPECT_TRUE(table_->AddCreditCard(work_creditcard)); 972 Time post_creation_time = Time::Now(); 973 974 // Get the 'Work' credit card. 975 CreditCard* db_creditcard; 976 ASSERT_TRUE(table_->GetCreditCard(work_creditcard.guid(), &db_creditcard)); 977 EXPECT_EQ(work_creditcard, *db_creditcard); 978 sql::Statement s_work(db_->GetSQLConnection()->GetUniqueStatement( 979 "SELECT guid, name_on_card, expiration_month, expiration_year, " 980 "card_number_encrypted, date_modified " 981 "FROM credit_cards WHERE guid=?")); 982 s_work.BindString(0, work_creditcard.guid()); 983 ASSERT_TRUE(s_work.is_valid()); 984 ASSERT_TRUE(s_work.Step()); 985 EXPECT_GE(s_work.ColumnInt64(5), pre_creation_time.ToTimeT()); 986 EXPECT_LE(s_work.ColumnInt64(5), post_creation_time.ToTimeT()); 987 EXPECT_FALSE(s_work.Step()); 988 delete db_creditcard; 989 990 // Add a 'Target' credit card. 991 CreditCard target_creditcard; 992 target_creditcard.set_origin(std::string()); 993 target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 994 target_creditcard.SetRawInfo(CREDIT_CARD_NUMBER, 995 ASCIIToUTF16("1111222233334444")); 996 target_creditcard.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("06")); 997 target_creditcard.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, 998 ASCIIToUTF16("2012")); 999 1000 pre_creation_time = Time::Now(); 1001 EXPECT_TRUE(table_->AddCreditCard(target_creditcard)); 1002 post_creation_time = Time::Now(); 1003 ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); 1004 EXPECT_EQ(target_creditcard, *db_creditcard); 1005 sql::Statement s_target(db_->GetSQLConnection()->GetUniqueStatement( 1006 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1007 "card_number_encrypted, date_modified " 1008 "FROM credit_cards WHERE guid=?")); 1009 s_target.BindString(0, target_creditcard.guid()); 1010 ASSERT_TRUE(s_target.is_valid()); 1011 ASSERT_TRUE(s_target.Step()); 1012 EXPECT_GE(s_target.ColumnInt64(5), pre_creation_time.ToTimeT()); 1013 EXPECT_LE(s_target.ColumnInt64(5), post_creation_time.ToTimeT()); 1014 EXPECT_FALSE(s_target.Step()); 1015 delete db_creditcard; 1016 1017 // Update the 'Target' credit card. 1018 target_creditcard.set_origin("Interactive Autofill dialog"); 1019 target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Charles Grady")); 1020 Time pre_modification_time = Time::Now(); 1021 EXPECT_TRUE(table_->UpdateCreditCard(target_creditcard)); 1022 Time post_modification_time = Time::Now(); 1023 ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); 1024 EXPECT_EQ(target_creditcard, *db_creditcard); 1025 sql::Statement s_target_updated(db_->GetSQLConnection()->GetUniqueStatement( 1026 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1027 "card_number_encrypted, date_modified " 1028 "FROM credit_cards WHERE guid=?")); 1029 s_target_updated.BindString(0, target_creditcard.guid()); 1030 ASSERT_TRUE(s_target_updated.is_valid()); 1031 ASSERT_TRUE(s_target_updated.Step()); 1032 EXPECT_GE(s_target_updated.ColumnInt64(5), pre_modification_time.ToTimeT()); 1033 EXPECT_LE(s_target_updated.ColumnInt64(5), post_modification_time.ToTimeT()); 1034 EXPECT_FALSE(s_target_updated.Step()); 1035 delete db_creditcard; 1036 1037 // Remove the 'Target' credit card. 1038 EXPECT_TRUE(table_->RemoveCreditCard(target_creditcard.guid())); 1039 EXPECT_FALSE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); 1040} 1041 1042TEST_F(AutofillTableTest, UpdateAutofillProfile) { 1043 // Add a profile to the db. 1044 AutofillProfile profile; 1045 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 1046 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 1047 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1048 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@example.com")); 1049 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google")); 1050 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way")); 1051 profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5")); 1052 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 1053 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1054 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 1055 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 1056 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); 1057 profile.set_language_code("en"); 1058 table_->AddAutofillProfile(profile); 1059 1060 // Set a mocked value for the profile's creation time. 1061 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1062 sql::Statement s_mock_creation_date( 1063 db_->GetSQLConnection()->GetUniqueStatement( 1064 "UPDATE autofill_profiles SET date_modified = ?")); 1065 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1066 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1067 ASSERT_TRUE(s_mock_creation_date.Run()); 1068 1069 // Get the profile. 1070 AutofillProfile* tmp_profile; 1071 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1072 scoped_ptr<AutofillProfile> db_profile(tmp_profile); 1073 EXPECT_EQ(profile, *db_profile); 1074 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1075 "SELECT date_modified FROM autofill_profiles")); 1076 ASSERT_TRUE(s_original.is_valid()); 1077 ASSERT_TRUE(s_original.Step()); 1078 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1079 EXPECT_FALSE(s_original.Step()); 1080 1081 // Now, update the profile and save the update to the database. 1082 // The modification date should change to reflect the update. 1083 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz")); 1084 table_->UpdateAutofillProfile(profile); 1085 1086 // Get the profile. 1087 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1088 db_profile.reset(tmp_profile); 1089 EXPECT_EQ(profile, *db_profile); 1090 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1091 "SELECT date_modified FROM autofill_profiles")); 1092 ASSERT_TRUE(s_updated.is_valid()); 1093 ASSERT_TRUE(s_updated.Step()); 1094 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1095 EXPECT_FALSE(s_updated.Step()); 1096 1097 // Set a mocked value for the profile's modification time. 1098 const time_t mock_modification_date = Time::Now().ToTimeT() - 7; 1099 sql::Statement s_mock_modification_date( 1100 db_->GetSQLConnection()->GetUniqueStatement( 1101 "UPDATE autofill_profiles SET date_modified = ?")); 1102 ASSERT_TRUE(s_mock_modification_date.is_valid()); 1103 s_mock_modification_date.BindInt64(0, mock_modification_date); 1104 ASSERT_TRUE(s_mock_modification_date.Run()); 1105 1106 // Finally, call into |UpdateAutofillProfile()| without changing the 1107 // profile. The modification date should not change. 1108 table_->UpdateAutofillProfile(profile); 1109 1110 // Get the profile. 1111 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1112 db_profile.reset(tmp_profile); 1113 EXPECT_EQ(profile, *db_profile); 1114 sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement( 1115 "SELECT date_modified FROM autofill_profiles")); 1116 ASSERT_TRUE(s_unchanged.is_valid()); 1117 ASSERT_TRUE(s_unchanged.Step()); 1118 EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0)); 1119 EXPECT_FALSE(s_unchanged.Step()); 1120} 1121 1122TEST_F(AutofillTableTest, UpdateCreditCard) { 1123 // Add a credit card to the db. 1124 CreditCard credit_card; 1125 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 1126 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456")); 1127 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); 1128 credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013")); 1129 table_->AddCreditCard(credit_card); 1130 1131 // Set a mocked value for the credit card's creation time. 1132 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1133 sql::Statement s_mock_creation_date( 1134 db_->GetSQLConnection()->GetUniqueStatement( 1135 "UPDATE credit_cards SET date_modified = ?")); 1136 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1137 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1138 ASSERT_TRUE(s_mock_creation_date.Run()); 1139 1140 // Get the credit card. 1141 CreditCard* tmp_credit_card; 1142 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1143 scoped_ptr<CreditCard> db_credit_card(tmp_credit_card); 1144 EXPECT_EQ(credit_card, *db_credit_card); 1145 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1146 "SELECT date_modified FROM credit_cards")); 1147 ASSERT_TRUE(s_original.is_valid()); 1148 ASSERT_TRUE(s_original.Step()); 1149 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1150 EXPECT_FALSE(s_original.Step()); 1151 1152 // Now, update the credit card and save the update to the database. 1153 // The modification date should change to reflect the update. 1154 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("01")); 1155 table_->UpdateCreditCard(credit_card); 1156 1157 // Get the credit card. 1158 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1159 db_credit_card.reset(tmp_credit_card); 1160 EXPECT_EQ(credit_card, *db_credit_card); 1161 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1162 "SELECT date_modified FROM credit_cards")); 1163 ASSERT_TRUE(s_updated.is_valid()); 1164 ASSERT_TRUE(s_updated.Step()); 1165 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1166 EXPECT_FALSE(s_updated.Step()); 1167 1168 // Set a mocked value for the credit card's modification time. 1169 const time_t mock_modification_date = Time::Now().ToTimeT() - 7; 1170 sql::Statement s_mock_modification_date( 1171 db_->GetSQLConnection()->GetUniqueStatement( 1172 "UPDATE credit_cards SET date_modified = ?")); 1173 ASSERT_TRUE(s_mock_modification_date.is_valid()); 1174 s_mock_modification_date.BindInt64(0, mock_modification_date); 1175 ASSERT_TRUE(s_mock_modification_date.Run()); 1176 1177 // Finally, call into |UpdateCreditCard()| without changing the credit card. 1178 // The modification date should not change. 1179 table_->UpdateCreditCard(credit_card); 1180 1181 // Get the credit card. 1182 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1183 db_credit_card.reset(tmp_credit_card); 1184 EXPECT_EQ(credit_card, *db_credit_card); 1185 sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement( 1186 "SELECT date_modified FROM credit_cards")); 1187 ASSERT_TRUE(s_unchanged.is_valid()); 1188 ASSERT_TRUE(s_unchanged.Step()); 1189 EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0)); 1190 EXPECT_FALSE(s_unchanged.Step()); 1191} 1192 1193TEST_F(AutofillTableTest, UpdateProfileOriginOnly) { 1194 // Add a profile to the db. 1195 AutofillProfile profile; 1196 profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); 1197 profile.SetRawInfo(NAME_MIDDLE, ASCIIToUTF16("Q.")); 1198 profile.SetRawInfo(NAME_LAST, ASCIIToUTF16("Smith")); 1199 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@example.com")); 1200 profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Google")); 1201 profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("1234 Apple Way")); 1202 profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("unit 5")); 1203 profile.SetRawInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Los Angeles")); 1204 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("CA")); 1205 profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); 1206 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); 1207 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); 1208 table_->AddAutofillProfile(profile); 1209 1210 // Set a mocked value for the profile's creation time. 1211 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1212 sql::Statement s_mock_creation_date( 1213 db_->GetSQLConnection()->GetUniqueStatement( 1214 "UPDATE autofill_profiles SET date_modified = ?")); 1215 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1216 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1217 ASSERT_TRUE(s_mock_creation_date.Run()); 1218 1219 // Get the profile. 1220 AutofillProfile* tmp_profile; 1221 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1222 scoped_ptr<AutofillProfile> db_profile(tmp_profile); 1223 EXPECT_EQ(profile, *db_profile); 1224 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1225 "SELECT date_modified FROM autofill_profiles")); 1226 ASSERT_TRUE(s_original.is_valid()); 1227 ASSERT_TRUE(s_original.Step()); 1228 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1229 EXPECT_FALSE(s_original.Step()); 1230 1231 // Now, update just the profile's origin and save the update to the database. 1232 // The modification date should change to reflect the update. 1233 profile.set_origin("https://www.example.com/"); 1234 table_->UpdateAutofillProfile(profile); 1235 1236 // Get the profile. 1237 ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); 1238 db_profile.reset(tmp_profile); 1239 EXPECT_EQ(profile, *db_profile); 1240 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1241 "SELECT date_modified FROM autofill_profiles")); 1242 ASSERT_TRUE(s_updated.is_valid()); 1243 ASSERT_TRUE(s_updated.Step()); 1244 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1245 EXPECT_FALSE(s_updated.Step()); 1246} 1247 1248TEST_F(AutofillTableTest, UpdateCreditCardOriginOnly) { 1249 // Add a credit card to the db. 1250 CreditCard credit_card; 1251 credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); 1252 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456")); 1253 credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); 1254 credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013")); 1255 table_->AddCreditCard(credit_card); 1256 1257 // Set a mocked value for the credit card's creation time. 1258 const time_t kMockCreationDate = Time::Now().ToTimeT() - 13; 1259 sql::Statement s_mock_creation_date( 1260 db_->GetSQLConnection()->GetUniqueStatement( 1261 "UPDATE credit_cards SET date_modified = ?")); 1262 ASSERT_TRUE(s_mock_creation_date.is_valid()); 1263 s_mock_creation_date.BindInt64(0, kMockCreationDate); 1264 ASSERT_TRUE(s_mock_creation_date.Run()); 1265 1266 // Get the credit card. 1267 CreditCard* tmp_credit_card; 1268 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1269 scoped_ptr<CreditCard> db_credit_card(tmp_credit_card); 1270 EXPECT_EQ(credit_card, *db_credit_card); 1271 sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( 1272 "SELECT date_modified FROM credit_cards")); 1273 ASSERT_TRUE(s_original.is_valid()); 1274 ASSERT_TRUE(s_original.Step()); 1275 EXPECT_EQ(kMockCreationDate, s_original.ColumnInt64(0)); 1276 EXPECT_FALSE(s_original.Step()); 1277 1278 // Now, update just the credit card's origin and save the update to the 1279 // database. The modification date should change to reflect the update. 1280 credit_card.set_origin("https://www.example.com/"); 1281 table_->UpdateCreditCard(credit_card); 1282 1283 // Get the credit card. 1284 ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); 1285 db_credit_card.reset(tmp_credit_card); 1286 EXPECT_EQ(credit_card, *db_credit_card); 1287 sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( 1288 "SELECT date_modified FROM credit_cards")); 1289 ASSERT_TRUE(s_updated.is_valid()); 1290 ASSERT_TRUE(s_updated.Step()); 1291 EXPECT_LT(kMockCreationDate, s_updated.ColumnInt64(0)); 1292 EXPECT_FALSE(s_updated.Step()); 1293} 1294 1295TEST_F(AutofillTableTest, RemoveAutofillDataModifiedBetween) { 1296 // Populate the autofill_profiles and credit_cards tables. 1297 ASSERT_TRUE(db_->GetSQLConnection()->Execute( 1298 "INSERT INTO autofill_profiles (guid, date_modified) " 1299 "VALUES('00000000-0000-0000-0000-000000000000', 11);" 1300 "INSERT INTO autofill_profiles (guid, date_modified) " 1301 "VALUES('00000000-0000-0000-0000-000000000001', 21);" 1302 "INSERT INTO autofill_profiles (guid, date_modified) " 1303 "VALUES('00000000-0000-0000-0000-000000000002', 31);" 1304 "INSERT INTO autofill_profiles (guid, date_modified) " 1305 "VALUES('00000000-0000-0000-0000-000000000003', 41);" 1306 "INSERT INTO autofill_profiles (guid, date_modified) " 1307 "VALUES('00000000-0000-0000-0000-000000000004', 51);" 1308 "INSERT INTO autofill_profiles (guid, date_modified) " 1309 "VALUES('00000000-0000-0000-0000-000000000005', 61);" 1310 "INSERT INTO credit_cards (guid, date_modified) " 1311 "VALUES('00000000-0000-0000-0000-000000000006', 17);" 1312 "INSERT INTO credit_cards (guid, date_modified) " 1313 "VALUES('00000000-0000-0000-0000-000000000007', 27);" 1314 "INSERT INTO credit_cards (guid, date_modified) " 1315 "VALUES('00000000-0000-0000-0000-000000000008', 37);" 1316 "INSERT INTO credit_cards (guid, date_modified) " 1317 "VALUES('00000000-0000-0000-0000-000000000009', 47);" 1318 "INSERT INTO credit_cards (guid, date_modified) " 1319 "VALUES('00000000-0000-0000-0000-000000000010', 57);" 1320 "INSERT INTO credit_cards (guid, date_modified) " 1321 "VALUES('00000000-0000-0000-0000-000000000011', 67);")); 1322 1323 // Remove all entries modified in the bounded time range [17,41). 1324 std::vector<std::string> profile_guids; 1325 std::vector<std::string> credit_card_guids; 1326 table_->RemoveAutofillDataModifiedBetween( 1327 Time::FromTimeT(17), Time::FromTimeT(41), 1328 &profile_guids, &credit_card_guids); 1329 ASSERT_EQ(2UL, profile_guids.size()); 1330 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profile_guids[0]); 1331 EXPECT_EQ("00000000-0000-0000-0000-000000000002", profile_guids[1]); 1332 sql::Statement s_autofill_profiles_bounded( 1333 db_->GetSQLConnection()->GetUniqueStatement( 1334 "SELECT date_modified FROM autofill_profiles")); 1335 ASSERT_TRUE(s_autofill_profiles_bounded.is_valid()); 1336 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1337 EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0)); 1338 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1339 EXPECT_EQ(41, s_autofill_profiles_bounded.ColumnInt64(0)); 1340 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1341 EXPECT_EQ(51, s_autofill_profiles_bounded.ColumnInt64(0)); 1342 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1343 EXPECT_EQ(61, s_autofill_profiles_bounded.ColumnInt64(0)); 1344 EXPECT_FALSE(s_autofill_profiles_bounded.Step()); 1345 ASSERT_EQ(3UL, credit_card_guids.size()); 1346 EXPECT_EQ("00000000-0000-0000-0000-000000000006", credit_card_guids[0]); 1347 EXPECT_EQ("00000000-0000-0000-0000-000000000007", credit_card_guids[1]); 1348 EXPECT_EQ("00000000-0000-0000-0000-000000000008", credit_card_guids[2]); 1349 sql::Statement s_credit_cards_bounded( 1350 db_->GetSQLConnection()->GetUniqueStatement( 1351 "SELECT date_modified FROM credit_cards")); 1352 ASSERT_TRUE(s_credit_cards_bounded.is_valid()); 1353 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1354 EXPECT_EQ(47, s_credit_cards_bounded.ColumnInt64(0)); 1355 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1356 EXPECT_EQ(57, s_credit_cards_bounded.ColumnInt64(0)); 1357 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1358 EXPECT_EQ(67, s_credit_cards_bounded.ColumnInt64(0)); 1359 EXPECT_FALSE(s_credit_cards_bounded.Step()); 1360 1361 // Remove all entries modified on or after time 51 (unbounded range). 1362 table_->RemoveAutofillDataModifiedBetween( 1363 Time::FromTimeT(51), Time(), 1364 &profile_guids, &credit_card_guids); 1365 ASSERT_EQ(2UL, profile_guids.size()); 1366 EXPECT_EQ("00000000-0000-0000-0000-000000000004", profile_guids[0]); 1367 EXPECT_EQ("00000000-0000-0000-0000-000000000005", profile_guids[1]); 1368 sql::Statement s_autofill_profiles_unbounded( 1369 db_->GetSQLConnection()->GetUniqueStatement( 1370 "SELECT date_modified FROM autofill_profiles")); 1371 ASSERT_TRUE(s_autofill_profiles_unbounded.is_valid()); 1372 ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); 1373 EXPECT_EQ(11, s_autofill_profiles_unbounded.ColumnInt64(0)); 1374 ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); 1375 EXPECT_EQ(41, s_autofill_profiles_unbounded.ColumnInt64(0)); 1376 EXPECT_FALSE(s_autofill_profiles_unbounded.Step()); 1377 ASSERT_EQ(2UL, credit_card_guids.size()); 1378 EXPECT_EQ("00000000-0000-0000-0000-000000000010", credit_card_guids[0]); 1379 EXPECT_EQ("00000000-0000-0000-0000-000000000011", credit_card_guids[1]); 1380 sql::Statement s_credit_cards_unbounded( 1381 db_->GetSQLConnection()->GetUniqueStatement( 1382 "SELECT date_modified FROM credit_cards")); 1383 ASSERT_TRUE(s_credit_cards_unbounded.is_valid()); 1384 ASSERT_TRUE(s_credit_cards_unbounded.Step()); 1385 EXPECT_EQ(47, s_credit_cards_unbounded.ColumnInt64(0)); 1386 EXPECT_FALSE(s_credit_cards_unbounded.Step()); 1387 1388 // Remove all remaining entries. 1389 table_->RemoveAutofillDataModifiedBetween( 1390 Time(), Time(), 1391 &profile_guids, &credit_card_guids); 1392 ASSERT_EQ(2UL, profile_guids.size()); 1393 EXPECT_EQ("00000000-0000-0000-0000-000000000000", profile_guids[0]); 1394 EXPECT_EQ("00000000-0000-0000-0000-000000000003", profile_guids[1]); 1395 sql::Statement s_autofill_profiles_empty( 1396 db_->GetSQLConnection()->GetUniqueStatement( 1397 "SELECT date_modified FROM autofill_profiles")); 1398 ASSERT_TRUE(s_autofill_profiles_empty.is_valid()); 1399 EXPECT_FALSE(s_autofill_profiles_empty.Step()); 1400 ASSERT_EQ(1UL, credit_card_guids.size()); 1401 EXPECT_EQ("00000000-0000-0000-0000-000000000009", credit_card_guids[0]); 1402 sql::Statement s_credit_cards_empty( 1403 db_->GetSQLConnection()->GetUniqueStatement( 1404 "SELECT date_modified FROM credit_cards")); 1405 ASSERT_TRUE(s_credit_cards_empty.is_valid()); 1406 EXPECT_FALSE(s_credit_cards_empty.Step()); 1407} 1408 1409TEST_F(AutofillTableTest, RemoveOriginURLsModifiedBetween) { 1410 // Populate the autofill_profiles and credit_cards tables. 1411 ASSERT_TRUE(db_->GetSQLConnection()->Execute( 1412 "INSERT INTO autofill_profiles (guid, origin, date_modified) " 1413 "VALUES('00000000-0000-0000-0000-000000000000', '', 11);" 1414 "INSERT INTO autofill_profiles (guid, origin, date_modified) " 1415 "VALUES('00000000-0000-0000-0000-000000000001', " 1416 " 'https://www.example.com/', 21);" 1417 "INSERT INTO autofill_profiles (guid, origin, date_modified) " 1418 "VALUES('00000000-0000-0000-0000-000000000002', 'Chrome settings', 31);" 1419 "INSERT INTO credit_cards (guid, origin, date_modified) " 1420 "VALUES('00000000-0000-0000-0000-000000000003', '', 17);" 1421 "INSERT INTO credit_cards (guid, origin, date_modified) " 1422 "VALUES('00000000-0000-0000-0000-000000000004', " 1423 " 'https://www.example.com/', 27);" 1424 "INSERT INTO credit_cards (guid, origin, date_modified) " 1425 "VALUES('00000000-0000-0000-0000-000000000005', 'Chrome settings', " 1426 " 37);")); 1427 1428 // Remove all origin URLs set in the bounded time range [21,27). 1429 ScopedVector<AutofillProfile> profiles; 1430 table_->RemoveOriginURLsModifiedBetween( 1431 Time::FromTimeT(21), Time::FromTimeT(27), &profiles); 1432 ASSERT_EQ(1UL, profiles.size()); 1433 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profiles[0]->guid()); 1434 sql::Statement s_autofill_profiles_bounded( 1435 db_->GetSQLConnection()->GetUniqueStatement( 1436 "SELECT date_modified, origin FROM autofill_profiles")); 1437 ASSERT_TRUE(s_autofill_profiles_bounded.is_valid()); 1438 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1439 EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0)); 1440 EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1)); 1441 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1442 EXPECT_EQ(21, s_autofill_profiles_bounded.ColumnInt64(0)); 1443 EXPECT_EQ(std::string(), s_autofill_profiles_bounded.ColumnString(1)); 1444 ASSERT_TRUE(s_autofill_profiles_bounded.Step()); 1445 EXPECT_EQ(31, s_autofill_profiles_bounded.ColumnInt64(0)); 1446 EXPECT_EQ("Chrome settings", s_autofill_profiles_bounded.ColumnString(1)); 1447 sql::Statement s_credit_cards_bounded( 1448 db_->GetSQLConnection()->GetUniqueStatement( 1449 "SELECT date_modified, origin FROM credit_cards")); 1450 ASSERT_TRUE(s_credit_cards_bounded.is_valid()); 1451 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1452 EXPECT_EQ(17, s_credit_cards_bounded.ColumnInt64(0)); 1453 EXPECT_EQ(std::string(), s_credit_cards_bounded.ColumnString(1)); 1454 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1455 EXPECT_EQ(27, s_credit_cards_bounded.ColumnInt64(0)); 1456 EXPECT_EQ("https://www.example.com/", 1457 s_credit_cards_bounded.ColumnString(1)); 1458 ASSERT_TRUE(s_credit_cards_bounded.Step()); 1459 EXPECT_EQ(37, s_credit_cards_bounded.ColumnInt64(0)); 1460 EXPECT_EQ("Chrome settings", s_credit_cards_bounded.ColumnString(1)); 1461 1462 // Remove all origin URLS. 1463 profiles.clear(); 1464 table_->RemoveOriginURLsModifiedBetween(Time(), Time(), &profiles); 1465 EXPECT_EQ(0UL, profiles.size()); 1466 sql::Statement s_autofill_profiles_all( 1467 db_->GetSQLConnection()->GetUniqueStatement( 1468 "SELECT date_modified, origin FROM autofill_profiles")); 1469 ASSERT_TRUE(s_autofill_profiles_all.is_valid()); 1470 ASSERT_TRUE(s_autofill_profiles_all.Step()); 1471 EXPECT_EQ(11, s_autofill_profiles_all.ColumnInt64(0)); 1472 EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1)); 1473 ASSERT_TRUE(s_autofill_profiles_all.Step()); 1474 EXPECT_EQ(21, s_autofill_profiles_all.ColumnInt64(0)); 1475 EXPECT_EQ(std::string(), s_autofill_profiles_all.ColumnString(1)); 1476 ASSERT_TRUE(s_autofill_profiles_all.Step()); 1477 EXPECT_EQ(31, s_autofill_profiles_all.ColumnInt64(0)); 1478 EXPECT_EQ("Chrome settings", s_autofill_profiles_all.ColumnString(1)); 1479 sql::Statement s_credit_cards_all( 1480 db_->GetSQLConnection()->GetUniqueStatement( 1481 "SELECT date_modified, origin FROM credit_cards")); 1482 ASSERT_TRUE(s_credit_cards_all.is_valid()); 1483 ASSERT_TRUE(s_credit_cards_all.Step()); 1484 EXPECT_EQ(17, s_credit_cards_all.ColumnInt64(0)); 1485 EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1)); 1486 ASSERT_TRUE(s_credit_cards_all.Step()); 1487 EXPECT_EQ(27, s_credit_cards_all.ColumnInt64(0)); 1488 EXPECT_EQ(std::string(), s_credit_cards_all.ColumnString(1)); 1489 ASSERT_TRUE(s_credit_cards_all.Step()); 1490 EXPECT_EQ(37, s_credit_cards_all.ColumnInt64(0)); 1491 EXPECT_EQ("Chrome settings", s_credit_cards_all.ColumnString(1)); 1492} 1493 1494TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_NoResults) { 1495 std::vector<AutofillEntry> entries; 1496 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1497 1498 EXPECT_EQ(0U, entries.size()); 1499} 1500 1501TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) { 1502 AutofillChangeList changes; 1503 std::map<std::string, std::vector<Time> > name_value_times_map; 1504 1505 time_t start = 0; 1506 std::vector<Time> timestamps1; 1507 FormFieldData field; 1508 field.name = ASCIIToUTF16("Name"); 1509 field.value = ASCIIToUTF16("Superman"); 1510 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1511 Time::FromTimeT(start))); 1512 timestamps1.push_back(Time::FromTimeT(start)); 1513 std::string key1("NameSuperman"); 1514 name_value_times_map.insert( 1515 std::pair<std::string, std::vector<Time> >(key1, timestamps1)); 1516 1517 AutofillEntrySet expected_entries(CompareAutofillEntries); 1518 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman")); 1519 AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back()); 1520 1521 expected_entries.insert(ae1); 1522 1523 std::vector<AutofillEntry> entries; 1524 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1525 AutofillEntrySet entry_set(entries.begin(), entries.end(), 1526 CompareAutofillEntries); 1527 1528 CompareAutofillEntrySets(entry_set, expected_entries); 1529} 1530 1531TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { 1532 AutofillChangeList changes; 1533 std::map<std::string, std::vector<Time> > name_value_times_map; 1534 time_t start = 0; 1535 1536 std::vector<Time> timestamps1; 1537 FormFieldData field; 1538 field.name = ASCIIToUTF16("Name"); 1539 field.value = ASCIIToUTF16("Superman"); 1540 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1541 Time::FromTimeT(start))); 1542 timestamps1.push_back(Time::FromTimeT(start)); 1543 std::string key1("NameSuperman"); 1544 name_value_times_map.insert( 1545 std::pair<std::string, std::vector<Time> >(key1, timestamps1)); 1546 1547 ++start; 1548 std::vector<Time> timestamps2; 1549 field.name = ASCIIToUTF16("Name"); 1550 field.value = ASCIIToUTF16("Clark Kent"); 1551 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1552 Time::FromTimeT(start))); 1553 timestamps2.push_back(Time::FromTimeT(start)); 1554 std::string key2("NameClark Kent"); 1555 name_value_times_map.insert( 1556 std::pair<std::string, std::vector<Time> >(key2, timestamps2)); 1557 1558 AutofillEntrySet expected_entries(CompareAutofillEntries); 1559 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman")); 1560 AutofillKey ak2(ASCIIToUTF16("Name"), ASCIIToUTF16("Clark Kent")); 1561 AutofillEntry ae1(ak1, timestamps1.front(), timestamps1.back()); 1562 AutofillEntry ae2(ak2, timestamps2.front(), timestamps2.back()); 1563 1564 expected_entries.insert(ae1); 1565 expected_entries.insert(ae2); 1566 1567 std::vector<AutofillEntry> entries; 1568 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1569 AutofillEntrySet entry_set(entries.begin(), entries.end(), 1570 CompareAutofillEntries); 1571 1572 CompareAutofillEntrySets(entry_set, expected_entries); 1573} 1574 1575TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) { 1576 AutofillChangeList changes; 1577 std::map<std::string, std::vector<Time> > name_value_times_map; 1578 1579 std::vector<Time> timestamps; 1580 time_t start = 0; 1581 for (int i = 0; i < 2; ++i, ++start) { 1582 FormFieldData field; 1583 field.name = ASCIIToUTF16("Name"); 1584 field.value = ASCIIToUTF16("Superman"); 1585 EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, 1586 Time::FromTimeT(start))); 1587 timestamps.push_back(Time::FromTimeT(start)); 1588 } 1589 1590 std::string key("NameSuperman"); 1591 name_value_times_map.insert( 1592 std::pair<std::string, std::vector<Time> >(key, timestamps)); 1593 1594 AutofillEntrySet expected_entries(CompareAutofillEntries); 1595 AutofillKey ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman")); 1596 AutofillEntry ae1(ak1, timestamps.front(), timestamps.back()); 1597 1598 expected_entries.insert(ae1); 1599 1600 std::vector<AutofillEntry> entries; 1601 ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); 1602 AutofillEntrySet entry_set(entries.begin(), entries.end(), 1603 CompareAutofillEntries); 1604 1605 CompareAutofillEntrySets(entry_set, expected_entries); 1606} 1607 1608} // namespace autofill 1609