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