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.
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/guid.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "components/autofill/core/browser/autofill_profile.h"
19 #include "components/autofill/core/browser/autofill_test_utils.h"
20 #include "components/autofill/core/browser/autofill_type.h"
21 #include "components/autofill/core/browser/credit_card.h"
22 #include "components/autofill/core/browser/webdata/autofill_change.h"
23 #include "components/autofill/core/browser/webdata/autofill_entry.h"
24 #include "components/autofill/core/browser/webdata/autofill_table.h"
25 #include "components/autofill/core/common/autofill_switches.h"
26 #include "components/autofill/core/common/autofill_util.h"
27 #include "components/autofill/core/common/form_field_data.h"
28 #include "components/os_crypt/os_crypt.h"
29 #include "components/webdata/common/web_database.h"
30 #include "sql/statement.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using base::ASCIIToUTF16
;
35 using base::TimeDelta
;
39 // So we can compare AutofillKeys with EXPECT_EQ().
40 std::ostream
& operator<<(std::ostream
& os
, const AutofillKey
& key
) {
41 return os
<< base::UTF16ToASCII(key
.name()) << ", "
42 << base::UTF16ToASCII(key
.value());
45 // So we can compare AutofillChanges with EXPECT_EQ().
46 std::ostream
& operator<<(std::ostream
& os
, const AutofillChange
& change
) {
47 switch (change
.type()) {
48 case AutofillChange::ADD
: {
52 case AutofillChange::UPDATE
: {
56 case AutofillChange::REMOVE
: {
61 return os
<< " " << change
.key();
66 typedef std::set
<AutofillEntry
,
67 bool (*)(const AutofillEntry
&, const AutofillEntry
&)> AutofillEntrySet
;
68 typedef AutofillEntrySet::iterator AutofillEntrySetIterator
;
70 bool CompareAutofillEntries(const AutofillEntry
& a
, const AutofillEntry
& b
) {
71 int compVal
= a
.key().name().compare(b
.key().name());
75 compVal
= a
.key().value().compare(b
.key().value());
79 if (a
.date_created() != b
.date_created())
80 return a
.date_created() < b
.date_created();
82 return a
.date_last_used() < b
.date_last_used();
85 AutofillEntry
MakeAutofillEntry(const char* name
,
88 time_t date_last_used
) {
89 if (date_last_used
< 0)
90 date_last_used
= date_created
;
91 return AutofillEntry(AutofillKey(ASCIIToUTF16(name
), ASCIIToUTF16(value
)),
92 Time::FromTimeT(date_created
),
93 Time::FromTimeT(date_last_used
));
96 // Checks |actual| and |expected| contain the same elements.
97 void CompareAutofillEntrySets(const AutofillEntrySet
& actual
,
98 const AutofillEntrySet
& expected
) {
99 ASSERT_EQ(expected
.size(), actual
.size());
101 for (AutofillEntrySet::const_iterator it
= actual
.begin();
102 it
!= actual
.end(); ++it
) {
103 count
+= expected
.count(*it
);
105 EXPECT_EQ(actual
.size(), count
);
108 int GetAutofillEntryCount(const base::string16
& name
,
109 const base::string16
& value
,
111 sql::Statement
s(db
->GetSQLConnection()->GetUniqueStatement(
112 "SELECT count FROM autofill WHERE name = ? AND value = ?"));
113 s
.BindString16(0, name
);
114 s
.BindString16(1, value
);
116 return s
.ColumnInt(0);
121 class AutofillTableTest
: public testing::Test
{
123 AutofillTableTest() {}
124 ~AutofillTableTest() override
{}
127 void SetUp() override
{
128 #if defined(OS_MACOSX)
129 OSCrypt::UseMockKeychain(true);
131 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
132 file_
= temp_dir_
.path().AppendASCII("TestWebDatabase");
134 table_
.reset(new AutofillTable
);
135 db_
.reset(new WebDatabase
);
136 db_
->AddTable(table_
.get());
137 ASSERT_EQ(sql::INIT_OK
, db_
->Init(file_
));
140 base::FilePath file_
;
141 base::ScopedTempDir temp_dir_
;
142 scoped_ptr
<AutofillTable
> table_
;
143 scoped_ptr
<WebDatabase
> db_
;
146 DISALLOW_COPY_AND_ASSIGN(AutofillTableTest
);
149 TEST_F(AutofillTableTest
, Autofill
) {
150 Time t1
= Time::Now();
152 // Simulate the submission of a handful of entries in a field called "Name",
153 // some more often than others.
154 AutofillChangeList changes
;
156 field
.name
= ASCIIToUTF16("Name");
157 field
.value
= ASCIIToUTF16("Superman");
158 base::Time now
= base::Time::Now();
159 base::TimeDelta two_seconds
= base::TimeDelta::FromSeconds(2);
160 EXPECT_FALSE(table_
->HasFormElements());
161 EXPECT_TRUE(table_
->AddFormFieldValue(field
, &changes
));
162 EXPECT_TRUE(table_
->HasFormElements());
163 std::vector
<base::string16
> v
;
164 for (int i
= 0; i
< 5; ++i
) {
165 field
.value
= ASCIIToUTF16("Clark Kent");
166 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
167 now
+ i
* two_seconds
));
169 for (int i
= 0; i
< 3; ++i
) {
170 field
.value
= ASCIIToUTF16("Clark Sutter");
171 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
172 now
+ i
* two_seconds
));
174 for (int i
= 0; i
< 2; ++i
) {
175 field
.name
= ASCIIToUTF16("Favorite Color");
176 field
.value
= ASCIIToUTF16("Green");
177 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
178 now
+ i
* two_seconds
));
181 // We have added the name Clark Kent 5 times, so count should be 5.
182 EXPECT_EQ(5, GetAutofillEntryCount(ASCIIToUTF16("Name"),
183 ASCIIToUTF16("Clark Kent"), db_
.get()));
185 // Storing in the data base should be case sensitive, so there should be no
186 // database entry for clark kent lowercase.
187 EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"),
188 ASCIIToUTF16("clark kent"), db_
.get()));
190 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("Favorite Color"),
191 ASCIIToUTF16("Green"), db_
.get()));
193 // This is meant to get a list of suggestions for Name. The empty prefix
194 // in the second argument means it should return all suggestions for a name
195 // no matter what they start with. The order that the names occur in the list
196 // should be decreasing order by count.
197 EXPECT_TRUE(table_
->GetFormValuesForElementName(
198 ASCIIToUTF16("Name"), base::string16(), &v
, 6));
199 EXPECT_EQ(3U, v
.size());
201 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v
[0]);
202 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v
[1]);
203 EXPECT_EQ(ASCIIToUTF16("Superman"), v
[2]);
206 // If we query again limiting the list size to 1, we should only get the most
208 EXPECT_TRUE(table_
->GetFormValuesForElementName(
209 ASCIIToUTF16("Name"), base::string16(), &v
, 1));
210 EXPECT_EQ(1U, v
.size());
212 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v
[0]);
215 // Querying for suggestions given a prefix is case-insensitive, so the prefix
216 // "cLa" shoud get suggestions for both Clarks.
217 EXPECT_TRUE(table_
->GetFormValuesForElementName(
218 ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v
, 6));
219 EXPECT_EQ(2U, v
.size());
221 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v
[0]);
222 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v
[1]);
225 // Removing all elements since the beginning of this function should remove
226 // everything from the database.
228 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(t1
, Time(), &changes
));
230 const AutofillChange kExpectedChanges
[] = {
231 AutofillChange(AutofillChange::REMOVE
,
232 AutofillKey(ASCIIToUTF16("Name"),
233 ASCIIToUTF16("Superman"))),
234 AutofillChange(AutofillChange::REMOVE
,
235 AutofillKey(ASCIIToUTF16("Name"),
236 ASCIIToUTF16("Clark Kent"))),
237 AutofillChange(AutofillChange::REMOVE
,
238 AutofillKey(ASCIIToUTF16("Name"),
239 ASCIIToUTF16("Clark Sutter"))),
240 AutofillChange(AutofillChange::REMOVE
,
241 AutofillKey(ASCIIToUTF16("Favorite Color"),
242 ASCIIToUTF16("Green"))),
244 EXPECT_EQ(arraysize(kExpectedChanges
), changes
.size());
245 for (size_t i
= 0; i
< arraysize(kExpectedChanges
); ++i
) {
246 EXPECT_EQ(kExpectedChanges
[i
], changes
[i
]);
249 EXPECT_EQ(0, GetAutofillEntryCount(ASCIIToUTF16("Name"),
250 ASCIIToUTF16("Clark Kent"), db_
.get()));
252 EXPECT_TRUE(table_
->GetFormValuesForElementName(
253 ASCIIToUTF16("Name"), base::string16(), &v
, 6));
254 EXPECT_EQ(0U, v
.size());
256 // Now add some values with empty strings.
257 const base::string16 kValue
= ASCIIToUTF16(" toto ");
258 field
.name
= ASCIIToUTF16("blank");
259 field
.value
= base::string16();
260 EXPECT_TRUE(table_
->AddFormFieldValue(field
, &changes
));
261 field
.name
= ASCIIToUTF16("blank");
262 field
.value
= ASCIIToUTF16(" ");
263 EXPECT_TRUE(table_
->AddFormFieldValue(field
, &changes
));
264 field
.name
= ASCIIToUTF16("blank");
265 field
.value
= ASCIIToUTF16(" ");
266 EXPECT_TRUE(table_
->AddFormFieldValue(field
, &changes
));
267 field
.name
= ASCIIToUTF16("blank");
268 field
.value
= kValue
;
269 EXPECT_TRUE(table_
->AddFormFieldValue(field
, &changes
));
271 // They should be stored normally as the DB layer does not check for empty
274 EXPECT_TRUE(table_
->GetFormValuesForElementName(
275 ASCIIToUTF16("blank"), base::string16(), &v
, 10));
276 EXPECT_EQ(4U, v
.size());
279 TEST_F(AutofillTableTest
, Autofill_RemoveBetweenChanges
) {
280 TimeDelta
one_day(TimeDelta::FromDays(1));
281 Time t1
= Time::Now();
282 Time t2
= t1
+ one_day
;
284 AutofillChangeList changes
;
286 field
.name
= ASCIIToUTF16("Name");
287 field
.value
= ASCIIToUTF16("Superman");
288 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
, t1
));
289 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
, t2
));
292 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(t1
, t2
, &changes
));
293 ASSERT_EQ(1U, changes
.size());
294 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE
,
295 AutofillKey(ASCIIToUTF16("Name"),
296 ASCIIToUTF16("Superman"))),
301 table_
->RemoveFormElementsAddedBetween(t2
, t2
+ one_day
, &changes
));
302 ASSERT_EQ(1U, changes
.size());
303 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE
,
304 AutofillKey(ASCIIToUTF16("Name"),
305 ASCIIToUTF16("Superman"))),
309 TEST_F(AutofillTableTest
, Autofill_AddChanges
) {
310 TimeDelta
one_day(TimeDelta::FromDays(1));
311 Time t1
= Time::Now();
312 Time t2
= t1
+ one_day
;
314 AutofillChangeList changes
;
316 field
.name
= ASCIIToUTF16("Name");
317 field
.value
= ASCIIToUTF16("Superman");
318 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
, t1
));
319 ASSERT_EQ(1U, changes
.size());
320 EXPECT_EQ(AutofillChange(AutofillChange::ADD
,
321 AutofillKey(ASCIIToUTF16("Name"),
322 ASCIIToUTF16("Superman"))),
327 table_
->AddFormFieldValueTime(field
, &changes
, t2
));
328 ASSERT_EQ(1U, changes
.size());
329 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE
,
330 AutofillKey(ASCIIToUTF16("Name"),
331 ASCIIToUTF16("Superman"))),
335 TEST_F(AutofillTableTest
, Autofill_UpdateOneWithOneTimestamp
) {
336 AutofillEntry
entry(MakeAutofillEntry("foo", "bar", 1, -1));
337 std::vector
<AutofillEntry
> entries
;
338 entries
.push_back(entry
);
339 ASSERT_TRUE(table_
->UpdateAutofillEntries(entries
));
341 EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"),
344 std::vector
<AutofillEntry
> all_entries
;
345 ASSERT_TRUE(table_
->GetAllAutofillEntries(&all_entries
));
346 ASSERT_EQ(1U, all_entries
.size());
347 EXPECT_EQ(entry
, all_entries
[0]);
350 TEST_F(AutofillTableTest
, Autofill_UpdateOneWithTwoTimestamps
) {
351 AutofillEntry
entry(MakeAutofillEntry("foo", "bar", 1, 2));
352 std::vector
<AutofillEntry
> entries
;
353 entries
.push_back(entry
);
354 ASSERT_TRUE(table_
->UpdateAutofillEntries(entries
));
356 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"),
359 std::vector
<AutofillEntry
> all_entries
;
360 ASSERT_TRUE(table_
->GetAllAutofillEntries(&all_entries
));
361 ASSERT_EQ(1U, all_entries
.size());
362 EXPECT_EQ(entry
, all_entries
[0]);
365 TEST_F(AutofillTableTest
, Autofill_GetAutofillTimestamps
) {
366 AutofillEntry
entry(MakeAutofillEntry("foo", "bar", 1, 2));
367 std::vector
<AutofillEntry
> entries
;
368 entries
.push_back(entry
);
369 ASSERT_TRUE(table_
->UpdateAutofillEntries(entries
));
371 Time date_created
, date_last_used
;
372 ASSERT_TRUE(table_
->GetAutofillTimestamps(ASCIIToUTF16("foo"),
376 EXPECT_EQ(Time::FromTimeT(1), date_created
);
377 EXPECT_EQ(Time::FromTimeT(2), date_last_used
);
380 TEST_F(AutofillTableTest
, Autofill_UpdateTwo
) {
381 AutofillEntry
entry0(MakeAutofillEntry("foo", "bar0", 1, -1));
382 AutofillEntry
entry1(MakeAutofillEntry("foo", "bar1", 2, 3));
383 std::vector
<AutofillEntry
> entries
;
384 entries
.push_back(entry0
);
385 entries
.push_back(entry1
);
386 ASSERT_TRUE(table_
->UpdateAutofillEntries(entries
));
388 EXPECT_EQ(1, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar0"),
390 EXPECT_EQ(2, GetAutofillEntryCount(ASCIIToUTF16("foo"), ASCIIToUTF16("bar1"),
394 TEST_F(AutofillTableTest
, Autofill_UpdateReplace
) {
395 AutofillChangeList changes
;
396 // Add a form field. This will be replaced.
398 field
.name
= ASCIIToUTF16("Name");
399 field
.value
= ASCIIToUTF16("Superman");
400 EXPECT_TRUE(table_
->AddFormFieldValue(field
, &changes
));
402 AutofillEntry
entry(MakeAutofillEntry("Name", "Superman", 1, 2));
403 std::vector
<AutofillEntry
> entries
;
404 entries
.push_back(entry
);
405 ASSERT_TRUE(table_
->UpdateAutofillEntries(entries
));
407 std::vector
<AutofillEntry
> all_entries
;
408 ASSERT_TRUE(table_
->GetAllAutofillEntries(&all_entries
));
409 ASSERT_EQ(1U, all_entries
.size());
410 EXPECT_EQ(entry
, all_entries
[0]);
413 TEST_F(AutofillTableTest
, Autofill_UpdateDontReplace
) {
414 Time t
= Time::Now();
415 AutofillEntry
existing(
416 MakeAutofillEntry("Name", "Superman", t
.ToTimeT(), -1));
418 AutofillChangeList changes
;
419 // Add a form field. This will NOT be replaced.
421 field
.name
= existing
.key().name();
422 field
.value
= existing
.key().value();
423 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
, t
));
424 AutofillEntry
entry(MakeAutofillEntry("Name", "Clark Kent", 1, 2));
425 std::vector
<AutofillEntry
> entries
;
426 entries
.push_back(entry
);
427 ASSERT_TRUE(table_
->UpdateAutofillEntries(entries
));
429 std::vector
<AutofillEntry
> all_entries
;
430 ASSERT_TRUE(table_
->GetAllAutofillEntries(&all_entries
));
431 ASSERT_EQ(2U, all_entries
.size());
432 AutofillEntrySet
expected_entries(all_entries
.begin(),
434 CompareAutofillEntries
);
435 EXPECT_EQ(1U, expected_entries
.count(existing
));
436 EXPECT_EQ(1U, expected_entries
.count(entry
));
439 TEST_F(AutofillTableTest
, Autofill_AddFormFieldValues
) {
440 Time t
= Time::Now();
442 // Add multiple values for "firstname" and "lastname" names. Test that only
443 // first value of each gets added. Related to security issue:
444 // http://crbug.com/51727.
445 std::vector
<FormFieldData
> elements
;
447 field
.name
= ASCIIToUTF16("firstname");
448 field
.value
= ASCIIToUTF16("Joe");
449 elements
.push_back(field
);
451 field
.name
= ASCIIToUTF16("firstname");
452 field
.value
= ASCIIToUTF16("Jane");
453 elements
.push_back(field
);
455 field
.name
= ASCIIToUTF16("lastname");
456 field
.value
= ASCIIToUTF16("Smith");
457 elements
.push_back(field
);
459 field
.name
= ASCIIToUTF16("lastname");
460 field
.value
= ASCIIToUTF16("Jones");
461 elements
.push_back(field
);
463 std::vector
<AutofillChange
> changes
;
464 table_
->AddFormFieldValuesTime(elements
, &changes
, t
);
466 ASSERT_EQ(2U, changes
.size());
467 EXPECT_EQ(changes
[0], AutofillChange(AutofillChange::ADD
,
468 AutofillKey(ASCIIToUTF16("firstname"),
469 ASCIIToUTF16("Joe"))));
470 EXPECT_EQ(changes
[1], AutofillChange(AutofillChange::ADD
,
471 AutofillKey(ASCIIToUTF16("lastname"),
472 ASCIIToUTF16("Smith"))));
474 std::vector
<AutofillEntry
> all_entries
;
475 ASSERT_TRUE(table_
->GetAllAutofillEntries(&all_entries
));
476 ASSERT_EQ(2U, all_entries
.size());
479 TEST_F(AutofillTableTest
,
480 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore
) {
481 // Add an entry used only before the targetted range.
482 AutofillChangeList changes
;
484 field
.name
= ASCIIToUTF16("Name");
485 field
.value
= ASCIIToUTF16("Superman");
486 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
487 base::Time::FromTimeT(10)));
488 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
489 base::Time::FromTimeT(20)));
490 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
491 base::Time::FromTimeT(30)));
492 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
493 base::Time::FromTimeT(40)));
494 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
495 base::Time::FromTimeT(50)));
497 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
500 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(base::Time::FromTimeT(51),
501 base::Time::FromTimeT(60),
503 EXPECT_TRUE(changes
.empty());
504 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
507 TEST_F(AutofillTableTest
,
508 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter
) {
509 // Add an entry used only after the targetted range.
510 AutofillChangeList changes
;
512 field
.name
= ASCIIToUTF16("Name");
513 field
.value
= ASCIIToUTF16("Superman");
514 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
515 base::Time::FromTimeT(50)));
516 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
517 base::Time::FromTimeT(60)));
518 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
519 base::Time::FromTimeT(70)));
520 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
521 base::Time::FromTimeT(80)));
522 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
523 base::Time::FromTimeT(90)));
525 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
528 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
529 base::Time::FromTimeT(50),
531 EXPECT_TRUE(changes
.empty());
532 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
535 TEST_F(AutofillTableTest
,
536 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring
) {
537 // Add an entry used entirely during the targetted range.
538 AutofillChangeList changes
;
540 field
.name
= ASCIIToUTF16("Name");
541 field
.value
= ASCIIToUTF16("Superman");
542 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
543 base::Time::FromTimeT(10)));
544 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
545 base::Time::FromTimeT(20)));
546 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
547 base::Time::FromTimeT(30)));
548 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
549 base::Time::FromTimeT(40)));
550 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
551 base::Time::FromTimeT(50)));
553 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
556 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(base::Time::FromTimeT(10),
557 base::Time::FromTimeT(51),
559 ASSERT_EQ(1U, changes
.size());
560 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE
,
561 AutofillKey(field
.name
, field
.value
)),
563 EXPECT_EQ(0, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
566 TEST_F(AutofillTableTest
,
567 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring
) {
568 // Add an entry used both before and during the targetted range.
569 AutofillChangeList changes
;
571 field
.name
= ASCIIToUTF16("Name");
572 field
.value
= ASCIIToUTF16("Superman");
573 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
574 base::Time::FromTimeT(10)));
575 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
576 base::Time::FromTimeT(20)));
577 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
578 base::Time::FromTimeT(30)));
579 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
580 base::Time::FromTimeT(40)));
581 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
582 base::Time::FromTimeT(50)));
584 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
587 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
588 base::Time::FromTimeT(60),
590 ASSERT_EQ(1U, changes
.size());
591 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE
,
592 AutofillKey(field
.name
, field
.value
)),
594 EXPECT_EQ(4, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
595 base::Time date_created
, date_last_used
;
597 table_
->GetAutofillTimestamps(field
.name
, field
.value
,
598 &date_created
, &date_last_used
));
599 EXPECT_EQ(base::Time::FromTimeT(10), date_created
);
600 EXPECT_EQ(base::Time::FromTimeT(39), date_last_used
);
603 TEST_F(AutofillTableTest
,
604 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter
) {
605 // Add an entry used both during and after the targetted range.
606 AutofillChangeList changes
;
608 field
.name
= ASCIIToUTF16("Name");
609 field
.value
= ASCIIToUTF16("Superman");
610 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
611 base::Time::FromTimeT(50)));
612 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
613 base::Time::FromTimeT(60)));
614 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
615 base::Time::FromTimeT(70)));
616 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
617 base::Time::FromTimeT(80)));
618 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
619 base::Time::FromTimeT(90)));
621 EXPECT_EQ(5, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
624 EXPECT_TRUE(table_
->RemoveFormElementsAddedBetween(base::Time::FromTimeT(40),
625 base::Time::FromTimeT(80),
627 ASSERT_EQ(1U, changes
.size());
628 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE
,
629 AutofillKey(field
.name
, field
.value
)),
631 EXPECT_EQ(2, GetAutofillEntryCount(field
.name
, field
.value
, db_
.get()));
632 base::Time date_created
, date_last_used
;
634 table_
->GetAutofillTimestamps(field
.name
, field
.value
,
635 &date_created
, &date_last_used
));
636 EXPECT_EQ(base::Time::FromTimeT(80), date_created
);
637 EXPECT_EQ(base::Time::FromTimeT(90), date_last_used
);
640 TEST_F(AutofillTableTest
, AutofillProfile
) {
641 // Add a 'Home' profile.
642 AutofillProfile home_profile
;
643 home_profile
.set_origin(std::string());
644 home_profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("John"));
645 home_profile
.SetRawInfo(NAME_MIDDLE
, ASCIIToUTF16("Q."));
646 home_profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
647 home_profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("js@smith.xyz"));
648 home_profile
.SetRawInfo(COMPANY_NAME
, ASCIIToUTF16("Google"));
649 home_profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("1234 Apple Way"));
650 home_profile
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("unit 5"));
651 home_profile
.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY
,
652 ASCIIToUTF16("Beverly Hills"));
653 home_profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("Los Angeles"));
654 home_profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
655 home_profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("90025"));
656 home_profile
.SetRawInfo(ADDRESS_HOME_SORTING_CODE
, ASCIIToUTF16("MAGIC ###"));
657 home_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
658 home_profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("18181234567"));
659 home_profile
.set_language_code("en");
661 Time pre_creation_time
= Time::Now();
662 EXPECT_TRUE(table_
->AddAutofillProfile(home_profile
));
663 Time post_creation_time
= Time::Now();
665 // Get the 'Home' profile.
666 scoped_ptr
<AutofillProfile
> db_profile
=
667 table_
->GetAutofillProfile(home_profile
.guid());
668 ASSERT_TRUE(db_profile
);
669 EXPECT_EQ(home_profile
, *db_profile
);
670 sql::Statement
s_home(db_
->GetSQLConnection()->GetUniqueStatement(
671 "SELECT date_modified "
672 "FROM autofill_profiles WHERE guid=?"));
673 s_home
.BindString(0, home_profile
.guid());
674 ASSERT_TRUE(s_home
.is_valid());
675 ASSERT_TRUE(s_home
.Step());
676 EXPECT_GE(s_home
.ColumnInt64(0), pre_creation_time
.ToTimeT());
677 EXPECT_LE(s_home
.ColumnInt64(0), post_creation_time
.ToTimeT());
678 EXPECT_FALSE(s_home
.Step());
680 // Add a 'Billing' profile.
681 AutofillProfile billing_profile
= home_profile
;
682 billing_profile
.set_guid(base::GenerateGUID());
683 billing_profile
.set_origin("https://www.example.com/");
684 billing_profile
.SetRawInfo(ADDRESS_HOME_LINE1
,
685 ASCIIToUTF16("5678 Bottom Street"));
686 billing_profile
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("suite 3"));
688 pre_creation_time
= Time::Now();
689 EXPECT_TRUE(table_
->AddAutofillProfile(billing_profile
));
690 post_creation_time
= Time::Now();
692 // Get the 'Billing' profile.
693 db_profile
= table_
->GetAutofillProfile(billing_profile
.guid());
694 ASSERT_TRUE(db_profile
);
695 EXPECT_EQ(billing_profile
, *db_profile
);
696 sql::Statement
s_billing(db_
->GetSQLConnection()->GetUniqueStatement(
697 "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
698 s_billing
.BindString(0, billing_profile
.guid());
699 ASSERT_TRUE(s_billing
.is_valid());
700 ASSERT_TRUE(s_billing
.Step());
701 EXPECT_GE(s_billing
.ColumnInt64(0), pre_creation_time
.ToTimeT());
702 EXPECT_LE(s_billing
.ColumnInt64(0), post_creation_time
.ToTimeT());
703 EXPECT_FALSE(s_billing
.Step());
705 // Update the 'Billing' profile, name only.
706 billing_profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Jane"));
707 Time pre_modification_time
= Time::Now();
708 EXPECT_TRUE(table_
->UpdateAutofillProfile(billing_profile
));
709 Time post_modification_time
= Time::Now();
710 db_profile
= table_
->GetAutofillProfile(billing_profile
.guid());
711 ASSERT_TRUE(db_profile
);
712 EXPECT_EQ(billing_profile
, *db_profile
);
713 sql::Statement
s_billing_updated(db_
->GetSQLConnection()->GetUniqueStatement(
714 "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
715 s_billing_updated
.BindString(0, billing_profile
.guid());
716 ASSERT_TRUE(s_billing_updated
.is_valid());
717 ASSERT_TRUE(s_billing_updated
.Step());
718 EXPECT_GE(s_billing_updated
.ColumnInt64(0),
719 pre_modification_time
.ToTimeT());
720 EXPECT_LE(s_billing_updated
.ColumnInt64(0),
721 post_modification_time
.ToTimeT());
722 EXPECT_FALSE(s_billing_updated
.Step());
724 // Update the 'Billing' profile.
725 billing_profile
.set_origin("Chrome settings");
726 billing_profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Janice"));
727 billing_profile
.SetRawInfo(NAME_MIDDLE
, ASCIIToUTF16("C."));
728 billing_profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Joplin"));
729 billing_profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("jane@singer.com"));
730 billing_profile
.SetRawInfo(COMPANY_NAME
, ASCIIToUTF16("Indy"));
731 billing_profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("Open Road"));
732 billing_profile
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("Route 66"));
733 billing_profile
.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY
,
734 ASCIIToUTF16("District 9"));
735 billing_profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("NFA"));
736 billing_profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("NY"));
737 billing_profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("10011"));
738 billing_profile
.SetRawInfo(ADDRESS_HOME_SORTING_CODE
, ASCIIToUTF16("123456"));
739 billing_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
740 billing_profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
,
741 ASCIIToUTF16("18181230000"));
742 Time pre_modification_time_2
= Time::Now();
743 EXPECT_TRUE(table_
->UpdateAutofillProfile(billing_profile
));
744 Time post_modification_time_2
= Time::Now();
745 db_profile
= table_
->GetAutofillProfile(billing_profile
.guid());
746 ASSERT_TRUE(db_profile
);
747 EXPECT_EQ(billing_profile
, *db_profile
);
748 sql::Statement
s_billing_updated_2(
749 db_
->GetSQLConnection()->GetUniqueStatement(
750 "SELECT date_modified FROM autofill_profiles WHERE guid=?"));
751 s_billing_updated_2
.BindString(0, billing_profile
.guid());
752 ASSERT_TRUE(s_billing_updated_2
.is_valid());
753 ASSERT_TRUE(s_billing_updated_2
.Step());
754 EXPECT_GE(s_billing_updated_2
.ColumnInt64(0),
755 pre_modification_time_2
.ToTimeT());
756 EXPECT_LE(s_billing_updated_2
.ColumnInt64(0),
757 post_modification_time_2
.ToTimeT());
758 EXPECT_FALSE(s_billing_updated_2
.Step());
760 // Remove the 'Billing' profile.
761 EXPECT_TRUE(table_
->RemoveAutofillProfile(billing_profile
.guid()));
762 db_profile
= table_
->GetAutofillProfile(billing_profile
.guid());
763 EXPECT_FALSE(db_profile
);
766 TEST_F(AutofillTableTest
, AutofillProfileTrash
) {
767 std::vector
<std::string
> guids
;
768 table_
->GetAutofillProfilesInTrash(&guids
);
769 EXPECT_TRUE(guids
.empty());
771 ASSERT_TRUE(table_
->AddAutofillGUIDToTrash(
772 "00000000-0000-0000-0000-000000000000"));
773 ASSERT_TRUE(table_
->AddAutofillGUIDToTrash(
774 "00000000-0000-0000-0000-000000000001"));
775 ASSERT_TRUE(table_
->GetAutofillProfilesInTrash(&guids
));
776 EXPECT_EQ(2UL, guids
.size());
777 EXPECT_EQ("00000000-0000-0000-0000-000000000000", guids
[0]);
778 EXPECT_EQ("00000000-0000-0000-0000-000000000001", guids
[1]);
780 ASSERT_TRUE(table_
->EmptyAutofillProfilesTrash());
781 ASSERT_TRUE(table_
->GetAutofillProfilesInTrash(&guids
));
782 EXPECT_TRUE(guids
.empty());
785 TEST_F(AutofillTableTest
, AutofillProfileTrashInteraction
) {
786 std::vector
<std::string
> guids
;
787 table_
->GetAutofillProfilesInTrash(&guids
);
788 EXPECT_TRUE(guids
.empty());
790 AutofillProfile profile
;
791 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("John"));
792 profile
.SetRawInfo(NAME_MIDDLE
, ASCIIToUTF16("Q."));
793 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
794 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("js@smith.xyz"));
795 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("1 Main St"));
796 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("Los Angeles"));
797 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
798 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("90025"));
799 profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
801 // Mark this profile as in the trash. This stops |AddAutofillProfile| from
803 EXPECT_TRUE(table_
->AddAutofillGUIDToTrash(profile
.guid()));
804 EXPECT_TRUE(table_
->AddAutofillProfile(profile
));
805 scoped_ptr
<AutofillProfile
> added_profile
=
806 table_
->GetAutofillProfile(profile
.guid());
807 EXPECT_FALSE(added_profile
);
809 // Add the profile for real this time.
810 EXPECT_TRUE(table_
->EmptyAutofillProfilesTrash());
811 EXPECT_TRUE(table_
->GetAutofillProfilesInTrash(&guids
));
812 EXPECT_TRUE(guids
.empty());
813 EXPECT_TRUE(table_
->AddAutofillProfile(profile
));
814 added_profile
= table_
->GetAutofillProfile(profile
.guid());
815 EXPECT_TRUE(added_profile
);
817 // Mark this profile as in the trash. This stops |UpdateAutofillProfileMulti|
818 // from updating it. In normal operation a profile should not be both in the
819 // trash and in the profiles table simultaneously.
820 EXPECT_TRUE(table_
->AddAutofillGUIDToTrash(profile
.guid()));
821 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("Jane"));
822 EXPECT_TRUE(table_
->UpdateAutofillProfile(profile
));
823 scoped_ptr
<AutofillProfile
> updated_profile
=
824 table_
->GetAutofillProfile(profile
.guid());
825 EXPECT_TRUE(updated_profile
);
826 EXPECT_EQ(ASCIIToUTF16("John"), updated_profile
->GetRawInfo(NAME_FIRST
));
828 // Try to delete the trashed profile. This stops |RemoveAutofillProfile| from
829 // deleting it. In normal operation deletion is done by migration step, and
830 // removal from trash is done by |WebDataService|. |RemoveAutofillProfile|
831 // does remove the item from the trash if it is found however, so that if
832 // other clients remove it (via Sync say) then it is gone and doesn't need to
833 // be processed further by |WebDataService|.
834 EXPECT_TRUE(table_
->RemoveAutofillProfile(profile
.guid()));
835 scoped_ptr
<AutofillProfile
> removed_profile
=
836 table_
->GetAutofillProfile(profile
.guid());
837 EXPECT_TRUE(removed_profile
);
838 EXPECT_FALSE(table_
->IsAutofillGUIDInTrash(profile
.guid()));
840 // Check that emptying the trash now allows removal to occur.
841 EXPECT_TRUE(table_
->EmptyAutofillProfilesTrash());
842 EXPECT_TRUE(table_
->RemoveAutofillProfile(profile
.guid()));
843 removed_profile
= table_
->GetAutofillProfile(profile
.guid());
844 EXPECT_FALSE(removed_profile
);
847 TEST_F(AutofillTableTest
, CreditCard
) {
848 // Add a 'Work' credit card.
849 CreditCard work_creditcard
;
850 work_creditcard
.set_origin("https://www.example.com/");
851 work_creditcard
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Jack Torrance"));
852 work_creditcard
.SetRawInfo(CREDIT_CARD_NUMBER
,
853 ASCIIToUTF16("1234567890123456"));
854 work_creditcard
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("04"));
855 work_creditcard
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
,
856 ASCIIToUTF16("2013"));
858 Time pre_creation_time
= Time::Now();
859 EXPECT_TRUE(table_
->AddCreditCard(work_creditcard
));
860 Time post_creation_time
= Time::Now();
862 // Get the 'Work' credit card.
863 scoped_ptr
<CreditCard
> db_creditcard
=
864 table_
->GetCreditCard(work_creditcard
.guid());
865 ASSERT_TRUE(db_creditcard
);
866 EXPECT_EQ(work_creditcard
, *db_creditcard
);
867 sql::Statement
s_work(db_
->GetSQLConnection()->GetUniqueStatement(
868 "SELECT guid, name_on_card, expiration_month, expiration_year, "
869 "card_number_encrypted, date_modified "
870 "FROM credit_cards WHERE guid=?"));
871 s_work
.BindString(0, work_creditcard
.guid());
872 ASSERT_TRUE(s_work
.is_valid());
873 ASSERT_TRUE(s_work
.Step());
874 EXPECT_GE(s_work
.ColumnInt64(5), pre_creation_time
.ToTimeT());
875 EXPECT_LE(s_work
.ColumnInt64(5), post_creation_time
.ToTimeT());
876 EXPECT_FALSE(s_work
.Step());
878 // Add a 'Target' credit card.
879 CreditCard target_creditcard
;
880 target_creditcard
.set_origin(std::string());
881 target_creditcard
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Jack Torrance"));
882 target_creditcard
.SetRawInfo(CREDIT_CARD_NUMBER
,
883 ASCIIToUTF16("1111222233334444"));
884 target_creditcard
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("06"));
885 target_creditcard
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
,
886 ASCIIToUTF16("2012"));
888 pre_creation_time
= Time::Now();
889 EXPECT_TRUE(table_
->AddCreditCard(target_creditcard
));
890 post_creation_time
= Time::Now();
891 db_creditcard
= table_
->GetCreditCard(target_creditcard
.guid());
892 ASSERT_TRUE(db_creditcard
);
893 EXPECT_EQ(target_creditcard
, *db_creditcard
);
894 sql::Statement
s_target(db_
->GetSQLConnection()->GetUniqueStatement(
895 "SELECT guid, name_on_card, expiration_month, expiration_year, "
896 "card_number_encrypted, date_modified "
897 "FROM credit_cards WHERE guid=?"));
898 s_target
.BindString(0, target_creditcard
.guid());
899 ASSERT_TRUE(s_target
.is_valid());
900 ASSERT_TRUE(s_target
.Step());
901 EXPECT_GE(s_target
.ColumnInt64(5), pre_creation_time
.ToTimeT());
902 EXPECT_LE(s_target
.ColumnInt64(5), post_creation_time
.ToTimeT());
903 EXPECT_FALSE(s_target
.Step());
905 // Update the 'Target' credit card.
906 target_creditcard
.set_origin("Interactive Autofill dialog");
907 target_creditcard
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Charles Grady"));
908 Time pre_modification_time
= Time::Now();
909 EXPECT_TRUE(table_
->UpdateCreditCard(target_creditcard
));
910 Time post_modification_time
= Time::Now();
911 db_creditcard
= table_
->GetCreditCard(target_creditcard
.guid());
912 ASSERT_TRUE(db_creditcard
);
913 EXPECT_EQ(target_creditcard
, *db_creditcard
);
914 sql::Statement
s_target_updated(db_
->GetSQLConnection()->GetUniqueStatement(
915 "SELECT guid, name_on_card, expiration_month, expiration_year, "
916 "card_number_encrypted, date_modified "
917 "FROM credit_cards WHERE guid=?"));
918 s_target_updated
.BindString(0, target_creditcard
.guid());
919 ASSERT_TRUE(s_target_updated
.is_valid());
920 ASSERT_TRUE(s_target_updated
.Step());
921 EXPECT_GE(s_target_updated
.ColumnInt64(5), pre_modification_time
.ToTimeT());
922 EXPECT_LE(s_target_updated
.ColumnInt64(5), post_modification_time
.ToTimeT());
923 EXPECT_FALSE(s_target_updated
.Step());
925 // Remove the 'Target' credit card.
926 EXPECT_TRUE(table_
->RemoveCreditCard(target_creditcard
.guid()));
927 db_creditcard
= table_
->GetCreditCard(target_creditcard
.guid());
928 EXPECT_FALSE(db_creditcard
);
931 TEST_F(AutofillTableTest
, UpdateAutofillProfile
) {
932 // Add a profile to the db.
933 AutofillProfile profile
;
934 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("John"));
935 profile
.SetRawInfo(NAME_MIDDLE
, ASCIIToUTF16("Q."));
936 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
937 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("js@example.com"));
938 profile
.SetRawInfo(COMPANY_NAME
, ASCIIToUTF16("Google"));
939 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("1234 Apple Way"));
940 profile
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("unit 5"));
941 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("Los Angeles"));
942 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
943 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("90025"));
944 profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
945 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("18181234567"));
946 profile
.set_language_code("en");
947 table_
->AddAutofillProfile(profile
);
949 // Set a mocked value for the profile's creation time.
950 const time_t kMockCreationDate
= Time::Now().ToTimeT() - 13;
951 sql::Statement
s_mock_creation_date(
952 db_
->GetSQLConnection()->GetUniqueStatement(
953 "UPDATE autofill_profiles SET date_modified = ?"));
954 ASSERT_TRUE(s_mock_creation_date
.is_valid());
955 s_mock_creation_date
.BindInt64(0, kMockCreationDate
);
956 ASSERT_TRUE(s_mock_creation_date
.Run());
959 scoped_ptr
<AutofillProfile
> db_profile
=
960 table_
->GetAutofillProfile(profile
.guid());
961 ASSERT_TRUE(db_profile
);
962 EXPECT_EQ(profile
, *db_profile
);
963 sql::Statement
s_original(db_
->GetSQLConnection()->GetUniqueStatement(
964 "SELECT date_modified FROM autofill_profiles"));
965 ASSERT_TRUE(s_original
.is_valid());
966 ASSERT_TRUE(s_original
.Step());
967 EXPECT_EQ(kMockCreationDate
, s_original
.ColumnInt64(0));
968 EXPECT_FALSE(s_original
.Step());
970 // Now, update the profile and save the update to the database.
971 // The modification date should change to reflect the update.
972 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("js@smith.xyz"));
973 table_
->UpdateAutofillProfile(profile
);
976 db_profile
= table_
->GetAutofillProfile(profile
.guid());
977 ASSERT_TRUE(db_profile
);
978 EXPECT_EQ(profile
, *db_profile
);
979 sql::Statement
s_updated(db_
->GetSQLConnection()->GetUniqueStatement(
980 "SELECT date_modified FROM autofill_profiles"));
981 ASSERT_TRUE(s_updated
.is_valid());
982 ASSERT_TRUE(s_updated
.Step());
983 EXPECT_LT(kMockCreationDate
, s_updated
.ColumnInt64(0));
984 EXPECT_FALSE(s_updated
.Step());
986 // Set a mocked value for the profile's modification time.
987 const time_t mock_modification_date
= Time::Now().ToTimeT() - 7;
988 sql::Statement
s_mock_modification_date(
989 db_
->GetSQLConnection()->GetUniqueStatement(
990 "UPDATE autofill_profiles SET date_modified = ?"));
991 ASSERT_TRUE(s_mock_modification_date
.is_valid());
992 s_mock_modification_date
.BindInt64(0, mock_modification_date
);
993 ASSERT_TRUE(s_mock_modification_date
.Run());
995 // Finally, call into |UpdateAutofillProfile()| without changing the
996 // profile. The modification date should not change.
997 table_
->UpdateAutofillProfile(profile
);
1000 db_profile
= table_
->GetAutofillProfile(profile
.guid());
1001 ASSERT_TRUE(db_profile
);
1002 EXPECT_EQ(profile
, *db_profile
);
1003 sql::Statement
s_unchanged(db_
->GetSQLConnection()->GetUniqueStatement(
1004 "SELECT date_modified FROM autofill_profiles"));
1005 ASSERT_TRUE(s_unchanged
.is_valid());
1006 ASSERT_TRUE(s_unchanged
.Step());
1007 EXPECT_EQ(mock_modification_date
, s_unchanged
.ColumnInt64(0));
1008 EXPECT_FALSE(s_unchanged
.Step());
1011 TEST_F(AutofillTableTest
, UpdateCreditCard
) {
1012 // Add a credit card to the db.
1013 CreditCard credit_card
;
1014 credit_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Jack Torrance"));
1015 credit_card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("1234567890123456"));
1016 credit_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("04"));
1017 credit_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2013"));
1018 table_
->AddCreditCard(credit_card
);
1020 // Set a mocked value for the credit card's creation time.
1021 const time_t kMockCreationDate
= Time::Now().ToTimeT() - 13;
1022 sql::Statement
s_mock_creation_date(
1023 db_
->GetSQLConnection()->GetUniqueStatement(
1024 "UPDATE credit_cards SET date_modified = ?"));
1025 ASSERT_TRUE(s_mock_creation_date
.is_valid());
1026 s_mock_creation_date
.BindInt64(0, kMockCreationDate
);
1027 ASSERT_TRUE(s_mock_creation_date
.Run());
1029 // Get the credit card.
1030 scoped_ptr
<CreditCard
> db_credit_card
=
1031 table_
->GetCreditCard(credit_card
.guid());
1032 ASSERT_TRUE(db_credit_card
);
1033 EXPECT_EQ(credit_card
, *db_credit_card
);
1034 sql::Statement
s_original(db_
->GetSQLConnection()->GetUniqueStatement(
1035 "SELECT date_modified FROM credit_cards"));
1036 ASSERT_TRUE(s_original
.is_valid());
1037 ASSERT_TRUE(s_original
.Step());
1038 EXPECT_EQ(kMockCreationDate
, s_original
.ColumnInt64(0));
1039 EXPECT_FALSE(s_original
.Step());
1041 // Now, update the credit card and save the update to the database.
1042 // The modification date should change to reflect the update.
1043 credit_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("01"));
1044 table_
->UpdateCreditCard(credit_card
);
1046 // Get the credit card.
1047 db_credit_card
= table_
->GetCreditCard(credit_card
.guid());
1048 ASSERT_TRUE(db_credit_card
);
1049 EXPECT_EQ(credit_card
, *db_credit_card
);
1050 sql::Statement
s_updated(db_
->GetSQLConnection()->GetUniqueStatement(
1051 "SELECT date_modified FROM credit_cards"));
1052 ASSERT_TRUE(s_updated
.is_valid());
1053 ASSERT_TRUE(s_updated
.Step());
1054 EXPECT_LT(kMockCreationDate
, s_updated
.ColumnInt64(0));
1055 EXPECT_FALSE(s_updated
.Step());
1057 // Set a mocked value for the credit card's modification time.
1058 const time_t mock_modification_date
= Time::Now().ToTimeT() - 7;
1059 sql::Statement
s_mock_modification_date(
1060 db_
->GetSQLConnection()->GetUniqueStatement(
1061 "UPDATE credit_cards SET date_modified = ?"));
1062 ASSERT_TRUE(s_mock_modification_date
.is_valid());
1063 s_mock_modification_date
.BindInt64(0, mock_modification_date
);
1064 ASSERT_TRUE(s_mock_modification_date
.Run());
1066 // Finally, call into |UpdateCreditCard()| without changing the credit card.
1067 // The modification date should not change.
1068 table_
->UpdateCreditCard(credit_card
);
1070 // Get the credit card.
1071 db_credit_card
= table_
->GetCreditCard(credit_card
.guid());
1072 ASSERT_TRUE(db_credit_card
);
1073 EXPECT_EQ(credit_card
, *db_credit_card
);
1074 sql::Statement
s_unchanged(db_
->GetSQLConnection()->GetUniqueStatement(
1075 "SELECT date_modified FROM credit_cards"));
1076 ASSERT_TRUE(s_unchanged
.is_valid());
1077 ASSERT_TRUE(s_unchanged
.Step());
1078 EXPECT_EQ(mock_modification_date
, s_unchanged
.ColumnInt64(0));
1079 EXPECT_FALSE(s_unchanged
.Step());
1082 TEST_F(AutofillTableTest
, UpdateProfileOriginOnly
) {
1083 // Add a profile to the db.
1084 AutofillProfile profile
;
1085 profile
.SetRawInfo(NAME_FIRST
, ASCIIToUTF16("John"));
1086 profile
.SetRawInfo(NAME_MIDDLE
, ASCIIToUTF16("Q."));
1087 profile
.SetRawInfo(NAME_LAST
, ASCIIToUTF16("Smith"));
1088 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16("js@example.com"));
1089 profile
.SetRawInfo(COMPANY_NAME
, ASCIIToUTF16("Google"));
1090 profile
.SetRawInfo(ADDRESS_HOME_LINE1
, ASCIIToUTF16("1234 Apple Way"));
1091 profile
.SetRawInfo(ADDRESS_HOME_LINE2
, ASCIIToUTF16("unit 5"));
1092 profile
.SetRawInfo(ADDRESS_HOME_CITY
, ASCIIToUTF16("Los Angeles"));
1093 profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("CA"));
1094 profile
.SetRawInfo(ADDRESS_HOME_ZIP
, ASCIIToUTF16("90025"));
1095 profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
1096 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("18181234567"));
1097 table_
->AddAutofillProfile(profile
);
1099 // Set a mocked value for the profile's creation time.
1100 const time_t kMockCreationDate
= Time::Now().ToTimeT() - 13;
1101 sql::Statement
s_mock_creation_date(
1102 db_
->GetSQLConnection()->GetUniqueStatement(
1103 "UPDATE autofill_profiles SET date_modified = ?"));
1104 ASSERT_TRUE(s_mock_creation_date
.is_valid());
1105 s_mock_creation_date
.BindInt64(0, kMockCreationDate
);
1106 ASSERT_TRUE(s_mock_creation_date
.Run());
1109 scoped_ptr
<AutofillProfile
> db_profile
=
1110 table_
->GetAutofillProfile(profile
.guid());
1111 ASSERT_TRUE(db_profile
);
1112 EXPECT_EQ(profile
, *db_profile
);
1113 sql::Statement
s_original(db_
->GetSQLConnection()->GetUniqueStatement(
1114 "SELECT date_modified FROM autofill_profiles"));
1115 ASSERT_TRUE(s_original
.is_valid());
1116 ASSERT_TRUE(s_original
.Step());
1117 EXPECT_EQ(kMockCreationDate
, s_original
.ColumnInt64(0));
1118 EXPECT_FALSE(s_original
.Step());
1120 // Now, update just the profile's origin and save the update to the database.
1121 // The modification date should change to reflect the update.
1122 profile
.set_origin("https://www.example.com/");
1123 table_
->UpdateAutofillProfile(profile
);
1126 db_profile
= table_
->GetAutofillProfile(profile
.guid());
1127 ASSERT_TRUE(db_profile
);
1128 EXPECT_EQ(profile
, *db_profile
);
1129 sql::Statement
s_updated(db_
->GetSQLConnection()->GetUniqueStatement(
1130 "SELECT date_modified FROM autofill_profiles"));
1131 ASSERT_TRUE(s_updated
.is_valid());
1132 ASSERT_TRUE(s_updated
.Step());
1133 EXPECT_LT(kMockCreationDate
, s_updated
.ColumnInt64(0));
1134 EXPECT_FALSE(s_updated
.Step());
1137 TEST_F(AutofillTableTest
, UpdateCreditCardOriginOnly
) {
1138 // Add a credit card to the db.
1139 CreditCard credit_card
;
1140 credit_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Jack Torrance"));
1141 credit_card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("1234567890123456"));
1142 credit_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("04"));
1143 credit_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2013"));
1144 table_
->AddCreditCard(credit_card
);
1146 // Set a mocked value for the credit card's creation time.
1147 const time_t kMockCreationDate
= Time::Now().ToTimeT() - 13;
1148 sql::Statement
s_mock_creation_date(
1149 db_
->GetSQLConnection()->GetUniqueStatement(
1150 "UPDATE credit_cards SET date_modified = ?"));
1151 ASSERT_TRUE(s_mock_creation_date
.is_valid());
1152 s_mock_creation_date
.BindInt64(0, kMockCreationDate
);
1153 ASSERT_TRUE(s_mock_creation_date
.Run());
1155 // Get the credit card.
1156 scoped_ptr
<CreditCard
> db_credit_card
=
1157 table_
->GetCreditCard(credit_card
.guid());
1158 ASSERT_TRUE(db_credit_card
);
1159 EXPECT_EQ(credit_card
, *db_credit_card
);
1160 sql::Statement
s_original(db_
->GetSQLConnection()->GetUniqueStatement(
1161 "SELECT date_modified FROM credit_cards"));
1162 ASSERT_TRUE(s_original
.is_valid());
1163 ASSERT_TRUE(s_original
.Step());
1164 EXPECT_EQ(kMockCreationDate
, s_original
.ColumnInt64(0));
1165 EXPECT_FALSE(s_original
.Step());
1167 // Now, update just the credit card's origin and save the update to the
1168 // database. The modification date should change to reflect the update.
1169 credit_card
.set_origin("https://www.example.com/");
1170 table_
->UpdateCreditCard(credit_card
);
1172 // Get the credit card.
1173 db_credit_card
= table_
->GetCreditCard(credit_card
.guid());
1174 ASSERT_TRUE(db_credit_card
);
1175 EXPECT_EQ(credit_card
, *db_credit_card
);
1176 sql::Statement
s_updated(db_
->GetSQLConnection()->GetUniqueStatement(
1177 "SELECT date_modified FROM credit_cards"));
1178 ASSERT_TRUE(s_updated
.is_valid());
1179 ASSERT_TRUE(s_updated
.Step());
1180 EXPECT_LT(kMockCreationDate
, s_updated
.ColumnInt64(0));
1181 EXPECT_FALSE(s_updated
.Step());
1184 TEST_F(AutofillTableTest
, RemoveAutofillDataModifiedBetween
) {
1185 // Populate the autofill_profiles and credit_cards tables.
1186 ASSERT_TRUE(db_
->GetSQLConnection()->Execute(
1187 "INSERT INTO autofill_profiles (guid, date_modified) "
1188 "VALUES('00000000-0000-0000-0000-000000000000', 11);"
1189 "INSERT INTO autofill_profiles (guid, date_modified) "
1190 "VALUES('00000000-0000-0000-0000-000000000001', 21);"
1191 "INSERT INTO autofill_profiles (guid, date_modified) "
1192 "VALUES('00000000-0000-0000-0000-000000000002', 31);"
1193 "INSERT INTO autofill_profiles (guid, date_modified) "
1194 "VALUES('00000000-0000-0000-0000-000000000003', 41);"
1195 "INSERT INTO autofill_profiles (guid, date_modified) "
1196 "VALUES('00000000-0000-0000-0000-000000000004', 51);"
1197 "INSERT INTO autofill_profiles (guid, date_modified) "
1198 "VALUES('00000000-0000-0000-0000-000000000005', 61);"
1199 "INSERT INTO credit_cards (guid, date_modified) "
1200 "VALUES('00000000-0000-0000-0000-000000000006', 17);"
1201 "INSERT INTO credit_cards (guid, date_modified) "
1202 "VALUES('00000000-0000-0000-0000-000000000007', 27);"
1203 "INSERT INTO credit_cards (guid, date_modified) "
1204 "VALUES('00000000-0000-0000-0000-000000000008', 37);"
1205 "INSERT INTO credit_cards (guid, date_modified) "
1206 "VALUES('00000000-0000-0000-0000-000000000009', 47);"
1207 "INSERT INTO credit_cards (guid, date_modified) "
1208 "VALUES('00000000-0000-0000-0000-000000000010', 57);"
1209 "INSERT INTO credit_cards (guid, date_modified) "
1210 "VALUES('00000000-0000-0000-0000-000000000011', 67);"));
1212 // Remove all entries modified in the bounded time range [17,41).
1213 std::vector
<std::string
> profile_guids
;
1214 std::vector
<std::string
> credit_card_guids
;
1215 table_
->RemoveAutofillDataModifiedBetween(
1216 Time::FromTimeT(17), Time::FromTimeT(41),
1217 &profile_guids
, &credit_card_guids
);
1218 ASSERT_EQ(2UL, profile_guids
.size());
1219 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profile_guids
[0]);
1220 EXPECT_EQ("00000000-0000-0000-0000-000000000002", profile_guids
[1]);
1221 sql::Statement
s_autofill_profiles_bounded(
1222 db_
->GetSQLConnection()->GetUniqueStatement(
1223 "SELECT date_modified FROM autofill_profiles"));
1224 ASSERT_TRUE(s_autofill_profiles_bounded
.is_valid());
1225 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1226 EXPECT_EQ(11, s_autofill_profiles_bounded
.ColumnInt64(0));
1227 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1228 EXPECT_EQ(41, s_autofill_profiles_bounded
.ColumnInt64(0));
1229 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1230 EXPECT_EQ(51, s_autofill_profiles_bounded
.ColumnInt64(0));
1231 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1232 EXPECT_EQ(61, s_autofill_profiles_bounded
.ColumnInt64(0));
1233 EXPECT_FALSE(s_autofill_profiles_bounded
.Step());
1234 ASSERT_EQ(3UL, credit_card_guids
.size());
1235 EXPECT_EQ("00000000-0000-0000-0000-000000000006", credit_card_guids
[0]);
1236 EXPECT_EQ("00000000-0000-0000-0000-000000000007", credit_card_guids
[1]);
1237 EXPECT_EQ("00000000-0000-0000-0000-000000000008", credit_card_guids
[2]);
1238 sql::Statement
s_credit_cards_bounded(
1239 db_
->GetSQLConnection()->GetUniqueStatement(
1240 "SELECT date_modified FROM credit_cards"));
1241 ASSERT_TRUE(s_credit_cards_bounded
.is_valid());
1242 ASSERT_TRUE(s_credit_cards_bounded
.Step());
1243 EXPECT_EQ(47, s_credit_cards_bounded
.ColumnInt64(0));
1244 ASSERT_TRUE(s_credit_cards_bounded
.Step());
1245 EXPECT_EQ(57, s_credit_cards_bounded
.ColumnInt64(0));
1246 ASSERT_TRUE(s_credit_cards_bounded
.Step());
1247 EXPECT_EQ(67, s_credit_cards_bounded
.ColumnInt64(0));
1248 EXPECT_FALSE(s_credit_cards_bounded
.Step());
1250 // Remove all entries modified on or after time 51 (unbounded range).
1251 table_
->RemoveAutofillDataModifiedBetween(
1252 Time::FromTimeT(51), Time(),
1253 &profile_guids
, &credit_card_guids
);
1254 ASSERT_EQ(2UL, profile_guids
.size());
1255 EXPECT_EQ("00000000-0000-0000-0000-000000000004", profile_guids
[0]);
1256 EXPECT_EQ("00000000-0000-0000-0000-000000000005", profile_guids
[1]);
1257 sql::Statement
s_autofill_profiles_unbounded(
1258 db_
->GetSQLConnection()->GetUniqueStatement(
1259 "SELECT date_modified FROM autofill_profiles"));
1260 ASSERT_TRUE(s_autofill_profiles_unbounded
.is_valid());
1261 ASSERT_TRUE(s_autofill_profiles_unbounded
.Step());
1262 EXPECT_EQ(11, s_autofill_profiles_unbounded
.ColumnInt64(0));
1263 ASSERT_TRUE(s_autofill_profiles_unbounded
.Step());
1264 EXPECT_EQ(41, s_autofill_profiles_unbounded
.ColumnInt64(0));
1265 EXPECT_FALSE(s_autofill_profiles_unbounded
.Step());
1266 ASSERT_EQ(2UL, credit_card_guids
.size());
1267 EXPECT_EQ("00000000-0000-0000-0000-000000000010", credit_card_guids
[0]);
1268 EXPECT_EQ("00000000-0000-0000-0000-000000000011", credit_card_guids
[1]);
1269 sql::Statement
s_credit_cards_unbounded(
1270 db_
->GetSQLConnection()->GetUniqueStatement(
1271 "SELECT date_modified FROM credit_cards"));
1272 ASSERT_TRUE(s_credit_cards_unbounded
.is_valid());
1273 ASSERT_TRUE(s_credit_cards_unbounded
.Step());
1274 EXPECT_EQ(47, s_credit_cards_unbounded
.ColumnInt64(0));
1275 EXPECT_FALSE(s_credit_cards_unbounded
.Step());
1277 // Remove all remaining entries.
1278 table_
->RemoveAutofillDataModifiedBetween(
1280 &profile_guids
, &credit_card_guids
);
1281 ASSERT_EQ(2UL, profile_guids
.size());
1282 EXPECT_EQ("00000000-0000-0000-0000-000000000000", profile_guids
[0]);
1283 EXPECT_EQ("00000000-0000-0000-0000-000000000003", profile_guids
[1]);
1284 sql::Statement
s_autofill_profiles_empty(
1285 db_
->GetSQLConnection()->GetUniqueStatement(
1286 "SELECT date_modified FROM autofill_profiles"));
1287 ASSERT_TRUE(s_autofill_profiles_empty
.is_valid());
1288 EXPECT_FALSE(s_autofill_profiles_empty
.Step());
1289 ASSERT_EQ(1UL, credit_card_guids
.size());
1290 EXPECT_EQ("00000000-0000-0000-0000-000000000009", credit_card_guids
[0]);
1291 sql::Statement
s_credit_cards_empty(
1292 db_
->GetSQLConnection()->GetUniqueStatement(
1293 "SELECT date_modified FROM credit_cards"));
1294 ASSERT_TRUE(s_credit_cards_empty
.is_valid());
1295 EXPECT_FALSE(s_credit_cards_empty
.Step());
1298 TEST_F(AutofillTableTest
, RemoveOriginURLsModifiedBetween
) {
1299 // Populate the autofill_profiles and credit_cards tables.
1300 ASSERT_TRUE(db_
->GetSQLConnection()->Execute(
1301 "INSERT INTO autofill_profiles (guid, origin, date_modified) "
1302 "VALUES('00000000-0000-0000-0000-000000000000', '', 11);"
1303 "INSERT INTO autofill_profiles (guid, origin, date_modified) "
1304 "VALUES('00000000-0000-0000-0000-000000000001', "
1305 " 'https://www.example.com/', 21);"
1306 "INSERT INTO autofill_profiles (guid, origin, date_modified) "
1307 "VALUES('00000000-0000-0000-0000-000000000002', 'Chrome settings', 31);"
1308 "INSERT INTO credit_cards (guid, origin, date_modified) "
1309 "VALUES('00000000-0000-0000-0000-000000000003', '', 17);"
1310 "INSERT INTO credit_cards (guid, origin, date_modified) "
1311 "VALUES('00000000-0000-0000-0000-000000000004', "
1312 " 'https://www.example.com/', 27);"
1313 "INSERT INTO credit_cards (guid, origin, date_modified) "
1314 "VALUES('00000000-0000-0000-0000-000000000005', 'Chrome settings', "
1317 // Remove all origin URLs set in the bounded time range [21,27).
1318 ScopedVector
<AutofillProfile
> profiles
;
1319 table_
->RemoveOriginURLsModifiedBetween(
1320 Time::FromTimeT(21), Time::FromTimeT(27), &profiles
);
1321 ASSERT_EQ(1UL, profiles
.size());
1322 EXPECT_EQ("00000000-0000-0000-0000-000000000001", profiles
[0]->guid());
1323 sql::Statement
s_autofill_profiles_bounded(
1324 db_
->GetSQLConnection()->GetUniqueStatement(
1325 "SELECT date_modified, origin FROM autofill_profiles"));
1326 ASSERT_TRUE(s_autofill_profiles_bounded
.is_valid());
1327 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1328 EXPECT_EQ(11, s_autofill_profiles_bounded
.ColumnInt64(0));
1329 EXPECT_EQ(std::string(), s_autofill_profiles_bounded
.ColumnString(1));
1330 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1331 EXPECT_EQ(21, s_autofill_profiles_bounded
.ColumnInt64(0));
1332 EXPECT_EQ(std::string(), s_autofill_profiles_bounded
.ColumnString(1));
1333 ASSERT_TRUE(s_autofill_profiles_bounded
.Step());
1334 EXPECT_EQ(31, s_autofill_profiles_bounded
.ColumnInt64(0));
1335 EXPECT_EQ("Chrome settings", s_autofill_profiles_bounded
.ColumnString(1));
1336 sql::Statement
s_credit_cards_bounded(
1337 db_
->GetSQLConnection()->GetUniqueStatement(
1338 "SELECT date_modified, origin FROM credit_cards"));
1339 ASSERT_TRUE(s_credit_cards_bounded
.is_valid());
1340 ASSERT_TRUE(s_credit_cards_bounded
.Step());
1341 EXPECT_EQ(17, s_credit_cards_bounded
.ColumnInt64(0));
1342 EXPECT_EQ(std::string(), s_credit_cards_bounded
.ColumnString(1));
1343 ASSERT_TRUE(s_credit_cards_bounded
.Step());
1344 EXPECT_EQ(27, s_credit_cards_bounded
.ColumnInt64(0));
1345 EXPECT_EQ("https://www.example.com/",
1346 s_credit_cards_bounded
.ColumnString(1));
1347 ASSERT_TRUE(s_credit_cards_bounded
.Step());
1348 EXPECT_EQ(37, s_credit_cards_bounded
.ColumnInt64(0));
1349 EXPECT_EQ("Chrome settings", s_credit_cards_bounded
.ColumnString(1));
1351 // Remove all origin URLS.
1353 table_
->RemoveOriginURLsModifiedBetween(Time(), Time(), &profiles
);
1354 EXPECT_EQ(0UL, profiles
.size());
1355 sql::Statement
s_autofill_profiles_all(
1356 db_
->GetSQLConnection()->GetUniqueStatement(
1357 "SELECT date_modified, origin FROM autofill_profiles"));
1358 ASSERT_TRUE(s_autofill_profiles_all
.is_valid());
1359 ASSERT_TRUE(s_autofill_profiles_all
.Step());
1360 EXPECT_EQ(11, s_autofill_profiles_all
.ColumnInt64(0));
1361 EXPECT_EQ(std::string(), s_autofill_profiles_all
.ColumnString(1));
1362 ASSERT_TRUE(s_autofill_profiles_all
.Step());
1363 EXPECT_EQ(21, s_autofill_profiles_all
.ColumnInt64(0));
1364 EXPECT_EQ(std::string(), s_autofill_profiles_all
.ColumnString(1));
1365 ASSERT_TRUE(s_autofill_profiles_all
.Step());
1366 EXPECT_EQ(31, s_autofill_profiles_all
.ColumnInt64(0));
1367 EXPECT_EQ("Chrome settings", s_autofill_profiles_all
.ColumnString(1));
1368 sql::Statement
s_credit_cards_all(
1369 db_
->GetSQLConnection()->GetUniqueStatement(
1370 "SELECT date_modified, origin FROM credit_cards"));
1371 ASSERT_TRUE(s_credit_cards_all
.is_valid());
1372 ASSERT_TRUE(s_credit_cards_all
.Step());
1373 EXPECT_EQ(17, s_credit_cards_all
.ColumnInt64(0));
1374 EXPECT_EQ(std::string(), s_credit_cards_all
.ColumnString(1));
1375 ASSERT_TRUE(s_credit_cards_all
.Step());
1376 EXPECT_EQ(27, s_credit_cards_all
.ColumnInt64(0));
1377 EXPECT_EQ(std::string(), s_credit_cards_all
.ColumnString(1));
1378 ASSERT_TRUE(s_credit_cards_all
.Step());
1379 EXPECT_EQ(37, s_credit_cards_all
.ColumnInt64(0));
1380 EXPECT_EQ("Chrome settings", s_credit_cards_all
.ColumnString(1));
1383 TEST_F(AutofillTableTest
, Autofill_GetAllAutofillEntries_NoResults
) {
1384 std::vector
<AutofillEntry
> entries
;
1385 ASSERT_TRUE(table_
->GetAllAutofillEntries(&entries
));
1387 EXPECT_EQ(0U, entries
.size());
1390 TEST_F(AutofillTableTest
, Autofill_GetAllAutofillEntries_OneResult
) {
1391 AutofillChangeList changes
;
1392 std::map
<std::string
, std::vector
<Time
> > name_value_times_map
;
1395 std::vector
<Time
> timestamps1
;
1396 FormFieldData field
;
1397 field
.name
= ASCIIToUTF16("Name");
1398 field
.value
= ASCIIToUTF16("Superman");
1399 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
1400 Time::FromTimeT(start
)));
1401 timestamps1
.push_back(Time::FromTimeT(start
));
1402 std::string
key1("NameSuperman");
1403 name_value_times_map
.insert(
1404 std::pair
<std::string
, std::vector
<Time
> >(key1
, timestamps1
));
1406 AutofillEntrySet
expected_entries(CompareAutofillEntries
);
1407 AutofillKey
ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1408 AutofillEntry
ae1(ak1
, timestamps1
.front(), timestamps1
.back());
1410 expected_entries
.insert(ae1
);
1412 std::vector
<AutofillEntry
> entries
;
1413 ASSERT_TRUE(table_
->GetAllAutofillEntries(&entries
));
1414 AutofillEntrySet
entry_set(entries
.begin(), entries
.end(),
1415 CompareAutofillEntries
);
1417 CompareAutofillEntrySets(entry_set
, expected_entries
);
1420 TEST_F(AutofillTableTest
, Autofill_GetAllAutofillEntries_TwoDistinct
) {
1421 AutofillChangeList changes
;
1422 std::map
<std::string
, std::vector
<Time
> > name_value_times_map
;
1425 std::vector
<Time
> timestamps1
;
1426 FormFieldData field
;
1427 field
.name
= ASCIIToUTF16("Name");
1428 field
.value
= ASCIIToUTF16("Superman");
1429 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
1430 Time::FromTimeT(start
)));
1431 timestamps1
.push_back(Time::FromTimeT(start
));
1432 std::string
key1("NameSuperman");
1433 name_value_times_map
.insert(
1434 std::pair
<std::string
, std::vector
<Time
> >(key1
, timestamps1
));
1437 std::vector
<Time
> timestamps2
;
1438 field
.name
= ASCIIToUTF16("Name");
1439 field
.value
= ASCIIToUTF16("Clark Kent");
1440 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
1441 Time::FromTimeT(start
)));
1442 timestamps2
.push_back(Time::FromTimeT(start
));
1443 std::string
key2("NameClark Kent");
1444 name_value_times_map
.insert(
1445 std::pair
<std::string
, std::vector
<Time
> >(key2
, timestamps2
));
1447 AutofillEntrySet
expected_entries(CompareAutofillEntries
);
1448 AutofillKey
ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1449 AutofillKey
ak2(ASCIIToUTF16("Name"), ASCIIToUTF16("Clark Kent"));
1450 AutofillEntry
ae1(ak1
, timestamps1
.front(), timestamps1
.back());
1451 AutofillEntry
ae2(ak2
, timestamps2
.front(), timestamps2
.back());
1453 expected_entries
.insert(ae1
);
1454 expected_entries
.insert(ae2
);
1456 std::vector
<AutofillEntry
> entries
;
1457 ASSERT_TRUE(table_
->GetAllAutofillEntries(&entries
));
1458 AutofillEntrySet
entry_set(entries
.begin(), entries
.end(),
1459 CompareAutofillEntries
);
1461 CompareAutofillEntrySets(entry_set
, expected_entries
);
1464 TEST_F(AutofillTableTest
, Autofill_GetAllAutofillEntries_TwoSame
) {
1465 AutofillChangeList changes
;
1466 std::map
<std::string
, std::vector
<Time
> > name_value_times_map
;
1468 std::vector
<Time
> timestamps
;
1470 for (int i
= 0; i
< 2; ++i
, ++start
) {
1471 FormFieldData field
;
1472 field
.name
= ASCIIToUTF16("Name");
1473 field
.value
= ASCIIToUTF16("Superman");
1474 EXPECT_TRUE(table_
->AddFormFieldValueTime(field
, &changes
,
1475 Time::FromTimeT(start
)));
1476 timestamps
.push_back(Time::FromTimeT(start
));
1479 std::string
key("NameSuperman");
1480 name_value_times_map
.insert(
1481 std::pair
<std::string
, std::vector
<Time
> >(key
, timestamps
));
1483 AutofillEntrySet
expected_entries(CompareAutofillEntries
);
1484 AutofillKey
ak1(ASCIIToUTF16("Name"), ASCIIToUTF16("Superman"));
1485 AutofillEntry
ae1(ak1
, timestamps
.front(), timestamps
.back());
1487 expected_entries
.insert(ae1
);
1489 std::vector
<AutofillEntry
> entries
;
1490 ASSERT_TRUE(table_
->GetAllAutofillEntries(&entries
));
1491 AutofillEntrySet
entry_set(entries
.begin(), entries
.end(),
1492 CompareAutofillEntries
);
1494 CompareAutofillEntrySets(entry_set
, expected_entries
);
1497 TEST_F(AutofillTableTest
, SetGetServerCards
) {
1498 std::vector
<CreditCard
> inputs
;
1499 inputs
.push_back(CreditCard(CreditCard::FULL_SERVER_CARD
, "a123"));
1500 inputs
[0].SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Paul F. Tompkins"));
1501 inputs
[0].SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
1502 inputs
[0].SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2020"));
1503 inputs
[0].SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("4111111111111111"));
1506 CreditCard(CreditCard::MASKED_SERVER_CARD
, "b456"));
1507 inputs
[1].SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Rick Roman"));
1508 inputs
[1].SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("12"));
1509 inputs
[1].SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("1997"));
1510 inputs
[1].SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("1111"));
1511 inputs
[1].SetTypeForMaskedCard(kVisaCard
);
1512 inputs
[1].SetServerStatus(CreditCard::EXPIRED
);
1514 test::SetServerCreditCards(table_
.get(), inputs
);
1516 std::vector
<CreditCard
*> outputs
;
1517 ASSERT_TRUE(table_
->GetServerCreditCards(&outputs
));
1518 ASSERT_EQ(inputs
.size(), outputs
.size());
1520 // Ordering isn't guaranteed, so fix the ordering if it's backwards.
1521 if (outputs
[1]->server_id() == inputs
[0].server_id())
1522 std::swap(outputs
[0], outputs
[1]);
1524 // GUIDs for server cards are dynamically generated so will be different
1525 // after reading from the DB. Check they're valid, but otherwise don't count
1526 // them in the comparison.
1527 inputs
[0].set_guid(std::string());
1528 inputs
[1].set_guid(std::string());
1529 outputs
[0]->set_guid(std::string());
1530 outputs
[1]->set_guid(std::string());
1532 EXPECT_EQ(inputs
[0], *outputs
[0]);
1533 EXPECT_EQ(inputs
[1], *outputs
[1]);
1535 EXPECT_EQ(CreditCard::OK
, outputs
[0]->GetServerStatus());
1536 EXPECT_EQ(CreditCard::EXPIRED
, outputs
[1]->GetServerStatus());
1538 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1541 TEST_F(AutofillTableTest
, MaskUnmaskServerCards
) {
1542 base::string16
masked_number(ASCIIToUTF16("1111"));
1543 std::vector
<CreditCard
> inputs
;
1544 inputs
.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD
, "a123"));
1545 inputs
[0].SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Jay Johnson"));
1546 inputs
[0].SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
1547 inputs
[0].SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2020"));
1548 inputs
[0].SetRawInfo(CREDIT_CARD_NUMBER
, masked_number
);
1549 inputs
[0].SetTypeForMaskedCard(kVisaCard
);
1550 test::SetServerCreditCards(table_
.get(), inputs
);
1552 // Unmask the number. The full number should be available.
1553 base::string16
full_number(ASCIIToUTF16("4111111111111111"));
1554 ASSERT_TRUE(table_
->UnmaskServerCreditCard(inputs
[0],
1557 std::vector
<CreditCard
*> outputs
;
1558 table_
->GetServerCreditCards(&outputs
);
1559 ASSERT_EQ(1u, outputs
.size());
1560 EXPECT_TRUE(CreditCard::FULL_SERVER_CARD
== outputs
[0]->record_type());
1561 EXPECT_EQ(full_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1563 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1566 // Re-mask the number, we should only get the last 4 digits out.
1567 ASSERT_TRUE(table_
->MaskServerCreditCard(inputs
[0].server_id()));
1568 table_
->GetServerCreditCards(&outputs
);
1569 ASSERT_EQ(1u, outputs
.size());
1570 EXPECT_TRUE(CreditCard::MASKED_SERVER_CARD
== outputs
[0]->record_type());
1571 EXPECT_EQ(masked_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1573 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1577 // Calling SetServerCreditCards should replace all existing cards, but unmasked
1578 // cards should not be re-masked.
1579 TEST_F(AutofillTableTest
, SetServerCardModify
) {
1580 // Add a masked card.
1581 CreditCard
masked_card(CreditCard::MASKED_SERVER_CARD
, "a123");
1582 masked_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Paul F. Tompkins"));
1583 masked_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
1584 masked_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2020"));
1585 masked_card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("1111"));
1586 masked_card
.SetTypeForMaskedCard(kVisaCard
);
1588 std::vector
<CreditCard
> inputs
;
1589 inputs
.push_back(masked_card
);
1590 test::SetServerCreditCards(table_
.get(), inputs
);
1593 base::string16 full_number
= ASCIIToUTF16("4111111111111111");
1594 table_
->UnmaskServerCreditCard(masked_card
, full_number
);
1596 // The card should now be unmasked.
1597 std::vector
<CreditCard
*> outputs
;
1598 table_
->GetServerCreditCards(&outputs
);
1599 ASSERT_EQ(1u, outputs
.size());
1600 EXPECT_TRUE(outputs
[0]->record_type() == CreditCard::FULL_SERVER_CARD
);
1601 EXPECT_EQ(full_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1603 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1606 // Call set again with the masked number.
1607 inputs
[0] = masked_card
;
1608 test::SetServerCreditCards(table_
.get(), inputs
);
1610 // The card should stay unmasked.
1611 table_
->GetServerCreditCards(&outputs
);
1612 ASSERT_EQ(1u, outputs
.size());
1613 EXPECT_TRUE(outputs
[0]->record_type() == CreditCard::FULL_SERVER_CARD
);
1614 EXPECT_EQ(full_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1616 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1619 // Set inputs that do not include our old card.
1620 CreditCard
random_card(CreditCard::MASKED_SERVER_CARD
, "b456");
1621 random_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Rick Roman"));
1622 random_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("12"));
1623 random_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("1997"));
1624 random_card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("2222"));
1625 random_card
.SetTypeForMaskedCard(kVisaCard
);
1626 inputs
[0] = random_card
;
1627 test::SetServerCreditCards(table_
.get(), inputs
);
1629 // We should have only the new card, the other one should have been deleted.
1630 table_
->GetServerCreditCards(&outputs
);
1631 ASSERT_EQ(1u, outputs
.size());
1632 EXPECT_TRUE(outputs
[0]->record_type() == CreditCard::MASKED_SERVER_CARD
);
1633 EXPECT_EQ(random_card
.server_id(), outputs
[0]->server_id());
1634 EXPECT_EQ(ASCIIToUTF16("2222"), outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1636 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1639 // Putting back the original card masked should make it masked (this tests
1640 // that the unmasked data was really deleted).
1641 inputs
[0] = masked_card
;
1642 test::SetServerCreditCards(table_
.get(), inputs
);
1643 table_
->GetServerCreditCards(&outputs
);
1644 ASSERT_EQ(1u, outputs
.size());
1645 EXPECT_TRUE(outputs
[0]->record_type() == CreditCard::MASKED_SERVER_CARD
);
1646 EXPECT_EQ(masked_card
.server_id(), outputs
[0]->server_id());
1647 EXPECT_EQ(ASCIIToUTF16("1111"), outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1649 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1653 TEST_F(AutofillTableTest
, SetServerCardUpdateUsageStats
) {
1654 // Add a masked card.
1655 CreditCard
masked_card(CreditCard::MASKED_SERVER_CARD
, "a123");
1656 masked_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Paul F. Tompkins"));
1657 masked_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
1658 masked_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2020"));
1659 masked_card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("1111"));
1660 masked_card
.SetTypeForMaskedCard(kVisaCard
);
1662 std::vector
<CreditCard
> inputs
;
1663 inputs
.push_back(masked_card
);
1664 test::SetServerCreditCards(table_
.get(), inputs
);
1666 ScopedVector
<CreditCard
> outputs
;
1667 table_
->GetServerCreditCards(&outputs
.get());
1668 ASSERT_EQ(1u, outputs
.size());
1669 EXPECT_EQ(masked_card
.server_id(), outputs
[0]->server_id());
1670 EXPECT_EQ(0U, outputs
[0]->use_count());
1671 EXPECT_EQ(base::Time(), outputs
[0]->use_date());
1674 // Update the usage stats; make sure they're reflected in GetServerProfiles.
1675 inputs
.back().set_use_count(4U);
1676 inputs
.back().set_use_date(base::Time::Now());
1677 table_
->UpdateServerCardUsageStats(inputs
.back());
1678 table_
->GetServerCreditCards(&outputs
.get());
1679 ASSERT_EQ(1u, outputs
.size());
1680 EXPECT_EQ(masked_card
.server_id(), outputs
[0]->server_id());
1681 EXPECT_EQ(4U, outputs
[0]->use_count());
1682 EXPECT_NE(base::Time(), outputs
[0]->use_date());
1685 // Setting the cards again shouldn't delete the usage stats.
1686 table_
->SetServerCreditCards(inputs
);
1687 table_
->GetServerCreditCards(&outputs
.get());
1688 ASSERT_EQ(1u, outputs
.size());
1689 EXPECT_EQ(masked_card
.server_id(), outputs
[0]->server_id());
1690 EXPECT_EQ(4U, outputs
[0]->use_count());
1691 EXPECT_NE(base::Time(), outputs
[0]->use_date());
1694 // Set a card list where the card is missing --- this should clear metadata.
1695 CreditCard
masked_card2(CreditCard::MASKED_SERVER_CARD
, "b456");
1696 inputs
.back() = masked_card2
;
1697 table_
->SetServerCreditCards(inputs
);
1699 // Back to the original card list.
1700 inputs
.back() = masked_card
;
1701 table_
->SetServerCreditCards(inputs
);
1702 table_
->GetServerCreditCards(&outputs
.get());
1703 ASSERT_EQ(1u, outputs
.size());
1704 EXPECT_EQ(masked_card
.server_id(), outputs
[0]->server_id());
1705 EXPECT_EQ(0U, outputs
[0]->use_count());
1706 EXPECT_EQ(base::Time(), outputs
[0]->use_date());
1710 TEST_F(AutofillTableTest
, SetServerProfile
) {
1711 AutofillProfile
one(AutofillProfile::SERVER_PROFILE
, "a123");
1712 std::vector
<AutofillProfile
> inputs
;
1713 inputs
.push_back(one
);
1714 table_
->SetServerProfiles(inputs
);
1716 std::vector
<AutofillProfile
*> outputs
;
1717 table_
->GetServerProfiles(&outputs
);
1718 ASSERT_EQ(1u, outputs
.size());
1719 EXPECT_EQ(one
.server_id(), outputs
[0]->server_id());
1721 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1724 // Set a different profile.
1725 AutofillProfile
two(AutofillProfile::SERVER_PROFILE
, "b456");
1727 table_
->SetServerProfiles(inputs
);
1729 // The original one should have been replaced.
1730 table_
->GetServerProfiles(&outputs
);
1731 ASSERT_EQ(1u, outputs
.size());
1732 EXPECT_EQ(two
.server_id(), outputs
[0]->server_id());
1734 STLDeleteContainerPointers(outputs
.begin(), outputs
.end());
1738 TEST_F(AutofillTableTest
, SetServerProfileUpdateUsageStats
) {
1739 AutofillProfile
one(AutofillProfile::SERVER_PROFILE
, "a123");
1740 std::vector
<AutofillProfile
> inputs
;
1741 inputs
.push_back(one
);
1742 table_
->SetServerProfiles(inputs
);
1744 ScopedVector
<AutofillProfile
> outputs
;
1745 table_
->GetServerProfiles(&outputs
.get());
1746 ASSERT_EQ(1u, outputs
.size());
1747 EXPECT_EQ(one
.server_id(), outputs
[0]->server_id());
1748 EXPECT_EQ(0U, outputs
[0]->use_count());
1749 EXPECT_EQ(base::Time(), outputs
[0]->use_date());
1752 // Update the usage stats; make sure they're reflected in GetServerProfiles.
1753 inputs
.back().set_use_count(4U);
1754 inputs
.back().set_use_date(base::Time::Now());
1755 table_
->UpdateServerAddressUsageStats(inputs
.back());
1756 table_
->GetServerProfiles(&outputs
.get());
1757 ASSERT_EQ(1u, outputs
.size());
1758 EXPECT_EQ(one
.server_id(), outputs
[0]->server_id());
1759 EXPECT_EQ(4U, outputs
[0]->use_count());
1760 EXPECT_NE(base::Time(), outputs
[0]->use_date());
1763 // Setting the profiles again shouldn't delete the usage stats.
1764 table_
->SetServerProfiles(inputs
);
1765 table_
->GetServerProfiles(&outputs
.get());
1766 ASSERT_EQ(1u, outputs
.size());
1767 EXPECT_EQ(one
.server_id(), outputs
[0]->server_id());
1768 EXPECT_EQ(4U, outputs
[0]->use_count());
1769 EXPECT_NE(base::Time(), outputs
[0]->use_date());
1772 // Set a null profile list --- this should clear metadata.
1773 table_
->SetServerProfiles(std::vector
<AutofillProfile
>());
1774 // Reset the old profile list and see the metadata is reset.
1775 table_
->SetServerProfiles(inputs
);
1776 table_
->GetServerProfiles(&outputs
.get());
1777 ASSERT_EQ(1u, outputs
.size());
1778 EXPECT_EQ(one
.server_id(), outputs
[0]->server_id());
1779 EXPECT_EQ(0U, outputs
[0]->use_count());
1780 EXPECT_EQ(base::Time(), outputs
[0]->use_date());
1784 // Tests that deleting time ranges re-masks server credit cards that were
1785 // unmasked in that time.
1786 TEST_F(AutofillTableTest
, DeleteUnmaskedCard
) {
1787 // This isn't the exact unmasked time, since the database will use the
1788 // current time that it is called. The code below has to be approximate.
1789 base::Time unmasked_time
= base::Time::Now();
1791 // Add a masked card.
1792 base::string16 masked_number
= ASCIIToUTF16("1111");
1793 CreditCard
masked_card(CreditCard::MASKED_SERVER_CARD
, "a123");
1794 masked_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("Paul F. Tompkins"));
1795 masked_card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
1796 masked_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2020"));
1797 masked_card
.SetRawInfo(CREDIT_CARD_NUMBER
, masked_number
);
1798 masked_card
.SetTypeForMaskedCard(kVisaCard
);
1800 std::vector
<CreditCard
> inputs
;
1801 inputs
.push_back(masked_card
);
1802 table_
->SetServerCreditCards(inputs
);
1805 base::string16 full_number
= ASCIIToUTF16("4111111111111111");
1806 table_
->UnmaskServerCreditCard(masked_card
, full_number
);
1808 // Delete data in a range a year in the future.
1809 std::vector
<std::string
> profile_guids
;
1810 std::vector
<std::string
> credit_card_guids
;
1811 ASSERT_TRUE(table_
->RemoveAutofillDataModifiedBetween(
1812 unmasked_time
+ base::TimeDelta::FromDays(365),
1813 unmasked_time
+ base::TimeDelta::FromDays(530),
1814 &profile_guids
, &credit_card_guids
));
1816 // This should not affect the unmasked card (should be unmasked).
1817 ScopedVector
<CreditCard
> outputs
;
1818 ASSERT_TRUE(table_
->GetServerCreditCards(&outputs
.get()));
1819 ASSERT_EQ(1u, outputs
.size());
1820 EXPECT_EQ(CreditCard::FULL_SERVER_CARD
, outputs
[0]->record_type());
1821 EXPECT_EQ(full_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1824 // Delete data in the range of the last 24 hours.
1825 // Fudge |now| to make sure it's strictly greater than the |now| that
1826 // the database uses.
1827 base::Time now
= base::Time::Now() + base::TimeDelta::FromSeconds(1);
1828 ASSERT_TRUE(table_
->RemoveAutofillDataModifiedBetween(
1829 now
- base::TimeDelta::FromDays(1), now
,
1830 &profile_guids
, &credit_card_guids
));
1832 // This should re-mask.
1833 ASSERT_TRUE(table_
->GetServerCreditCards(&outputs
.get()));
1834 ASSERT_EQ(1u, outputs
.size());
1835 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD
, outputs
[0]->record_type());
1836 EXPECT_EQ(masked_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1839 // Unmask again, the card should be back.
1840 table_
->UnmaskServerCreditCard(masked_card
, full_number
);
1841 ASSERT_TRUE(table_
->GetServerCreditCards(&outputs
.get()));
1842 ASSERT_EQ(1u, outputs
.size());
1843 EXPECT_EQ(CreditCard::FULL_SERVER_CARD
, outputs
[0]->record_type());
1844 EXPECT_EQ(full_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1848 ASSERT_TRUE(table_
->RemoveAutofillDataModifiedBetween(
1849 base::Time(), base::Time::Max(), &profile_guids
, &credit_card_guids
));
1851 // Should be masked again.
1852 ASSERT_TRUE(table_
->GetServerCreditCards(&outputs
.get()));
1853 ASSERT_EQ(1u, outputs
.size());
1854 EXPECT_EQ(CreditCard::MASKED_SERVER_CARD
, outputs
[0]->record_type());
1855 EXPECT_EQ(masked_number
, outputs
[0]->GetRawInfo(CREDIT_CARD_NUMBER
));
1859 TEST_F(AutofillTableTest
, GetFormValuesForElementName_SubstringMatchEnabled
) {
1860 // Token matching is currently behind a flag.
1861 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1862 autofill::switches::kEnableSuggestionsWithSubstringMatch
);
1864 const size_t kMaxCount
= 2;
1866 const char* const field_suggestion
[kMaxCount
];
1867 const char* const field_contents
;
1868 size_t expected_suggestion_count
;
1869 const char* const expected_suggestion
[kMaxCount
];
1871 {{"user.test", "test_user"}, "TEST", 2, {"test_user", "user.test"}},
1872 {{"user test", "test-user"}, "user", 2, {"user test", "test-user"}},
1873 {{"user test", "test-rest"}, "user", 1, {"user test", nullptr}},
1874 {{"user@test", "test_user"}, "user@t", 1, {"user@test", nullptr}},
1875 {{"user.test", "test_user"}, "er.tes", 0, {nullptr, nullptr}},
1876 {{"user test", "test_user"}, "_ser", 0, {nullptr, nullptr}},
1877 {{"user.test", "test_user"}, "%ser", 0, {nullptr, nullptr}},
1878 {{"user.test", "test_user"},
1879 "; DROP TABLE autofill;",
1881 {nullptr, nullptr}},
1884 for (size_t i
= 0; i
< arraysize(kTestCases
); ++i
) {
1885 SCOPED_TRACE(testing::Message()
1886 << "suggestion = " << kTestCases
[i
].field_suggestion
[0]
1887 << ", contents = " << kTestCases
[i
].field_contents
);
1889 Time t1
= Time::Now();
1891 // Simulate the submission of a handful of entries in a field called "Name".
1892 AutofillChangeList changes
;
1893 FormFieldData field
;
1894 for (size_t k
= 0; k
< kMaxCount
; ++k
) {
1895 field
.name
= ASCIIToUTF16("Name");
1896 field
.value
= ASCIIToUTF16(kTestCases
[i
].field_suggestion
[k
]);
1897 table_
->AddFormFieldValue(field
, &changes
);
1900 std::vector
<base::string16
> v
;
1901 table_
->GetFormValuesForElementName(
1902 ASCIIToUTF16("Name"), ASCIIToUTF16(kTestCases
[i
].field_contents
), &v
,
1905 EXPECT_EQ(kTestCases
[i
].expected_suggestion_count
, v
.size());
1906 for (size_t j
= 0; j
< kTestCases
[i
].expected_suggestion_count
; ++j
) {
1907 EXPECT_EQ(ASCIIToUTF16(kTestCases
[i
].expected_suggestion
[j
]), v
[j
]);
1911 table_
->RemoveFormElementsAddedBetween(t1
, Time(), &changes
);
1915 } // namespace autofill