OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/core/browser/webdata/autofill_table.h" | 5 #include "components/autofill/core/browser/webdata/autofill_table.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <tuple> | 9 #include <tuple> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/guid.h" | 15 #include "base/guid.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
24 #include "components/autofill/core/browser/autofill_profile.h" | 24 #include "components/autofill/core/browser/autofill_profile.h" |
25 #include "components/autofill/core/browser/autofill_test_utils.h" | 25 #include "components/autofill/core/browser/autofill_test_utils.h" |
26 #include "components/autofill/core/browser/autofill_type.h" | 26 #include "components/autofill/core/browser/autofill_type.h" |
27 #include "components/autofill/core/browser/credit_card.h" | 27 #include "components/autofill/core/browser/credit_card.h" |
28 #include "components/autofill/core/browser/test_autofill_clock.h" | |
28 #include "components/autofill/core/browser/webdata/autofill_change.h" | 29 #include "components/autofill/core/browser/webdata/autofill_change.h" |
29 #include "components/autofill/core/browser/webdata/autofill_entry.h" | 30 #include "components/autofill/core/browser/webdata/autofill_entry.h" |
30 #include "components/autofill/core/common/autofill_constants.h" | 31 #include "components/autofill/core/common/autofill_constants.h" |
31 #include "components/autofill/core/common/autofill_switches.h" | 32 #include "components/autofill/core/common/autofill_switches.h" |
32 #include "components/autofill/core/common/autofill_util.h" | 33 #include "components/autofill/core/common/autofill_util.h" |
33 #include "components/autofill/core/common/form_field_data.h" | 34 #include "components/autofill/core/common/form_field_data.h" |
34 #include "components/os_crypt/os_crypt_mocker.h" | 35 #include "components/os_crypt/os_crypt_mocker.h" |
35 #include "components/sync/protocol/entity_metadata.pb.h" | 36 #include "components/sync/protocol/entity_metadata.pb.h" |
36 #include "components/sync/protocol/model_type_state.pb.h" | 37 #include "components/sync/protocol/model_type_state.pb.h" |
37 #include "components/webdata/common/web_database.h" | 38 #include "components/webdata/common/web_database.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
68 case AutofillChange::REMOVE: { | 69 case AutofillChange::REMOVE: { |
69 os << "REMOVE"; | 70 os << "REMOVE"; |
70 break; | 71 break; |
71 } | 72 } |
72 } | 73 } |
73 return os << " " << change.key(); | 74 return os << " " << change.key(); |
74 } | 75 } |
75 | 76 |
76 namespace { | 77 namespace { |
77 | 78 |
79 const base::Time kArbitraryTime = base::Time::FromDoubleT(25); | |
80 const base::Time kSomeLaterTime = base::Time::FromDoubleT(1000); | |
81 const base::Time kMuchLaterTime = base::Time::FromDoubleT(5000); | |
82 | |
78 typedef std::set<AutofillEntry, | 83 typedef std::set<AutofillEntry, |
79 bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet; | 84 bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet; |
80 typedef AutofillEntrySet::iterator AutofillEntrySetIterator; | 85 typedef AutofillEntrySet::iterator AutofillEntrySetIterator; |
81 | 86 |
82 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) { | 87 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) { |
83 return std::tie(a.key().name(), a.key().value(), | 88 return std::tie(a.key().name(), a.key().value(), |
84 a.date_created(), a.date_last_used()) < | 89 a.date_created(), a.date_last_used()) < |
85 std::tie(b.key().name(), b.key().value(), | 90 std::tie(b.key().name(), b.key().value(), |
86 b.date_created(), b.date_last_used()); | 91 b.date_created(), b.date_last_used()); |
87 } | 92 } |
(...skipping 1650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1738 table_->GetServerCreditCards(&outputs); | 1743 table_->GetServerCreditCards(&outputs); |
1739 ASSERT_EQ(1u, outputs.size()); | 1744 ASSERT_EQ(1u, outputs.size()); |
1740 EXPECT_TRUE(outputs[0]->record_type() == CreditCard::MASKED_SERVER_CARD); | 1745 EXPECT_TRUE(outputs[0]->record_type() == CreditCard::MASKED_SERVER_CARD); |
1741 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1746 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
1742 EXPECT_EQ(ASCIIToUTF16("1111"), outputs[0]->GetRawInfo(CREDIT_CARD_NUMBER)); | 1747 EXPECT_EQ(ASCIIToUTF16("1111"), outputs[0]->GetRawInfo(CREDIT_CARD_NUMBER)); |
1743 | 1748 |
1744 outputs.clear(); | 1749 outputs.clear(); |
1745 } | 1750 } |
1746 | 1751 |
1747 TEST_F(AutofillTableTest, SetServerCardUpdateUsageStatsAndBillingAddress) { | 1752 TEST_F(AutofillTableTest, SetServerCardUpdateUsageStatsAndBillingAddress) { |
1753 TestAutofillClock test_clock; | |
1754 test_clock.SetNow(kArbitraryTime); | |
1755 | |
1748 // Add a masked card. | 1756 // Add a masked card. |
1749 CreditCard masked_card(CreditCard::MASKED_SERVER_CARD, "a123"); | 1757 CreditCard masked_card(CreditCard::MASKED_SERVER_CARD, "a123"); |
1750 masked_card.SetRawInfo(CREDIT_CARD_NAME_FULL, | 1758 masked_card.SetRawInfo(CREDIT_CARD_NAME_FULL, |
1751 ASCIIToUTF16("Paul F. Tompkins")); | 1759 ASCIIToUTF16("Paul F. Tompkins")); |
1752 masked_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1")); | 1760 masked_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1")); |
1753 masked_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2020")); | 1761 masked_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2020")); |
1754 masked_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1111")); | 1762 masked_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1111")); |
1755 masked_card.set_billing_address_id("1"); | 1763 masked_card.set_billing_address_id("1"); |
1756 masked_card.SetTypeForMaskedCard(kVisaCard); | 1764 masked_card.SetTypeForMaskedCard(kVisaCard); |
1757 | 1765 |
1758 std::vector<CreditCard> inputs; | 1766 std::vector<CreditCard> inputs; |
1759 inputs.push_back(masked_card); | 1767 inputs.push_back(masked_card); |
1760 test::SetServerCreditCards(table_.get(), inputs); | 1768 test::SetServerCreditCards(table_.get(), inputs); |
1761 | 1769 |
1762 std::vector<std::unique_ptr<CreditCard>> outputs; | 1770 std::vector<std::unique_ptr<CreditCard>> outputs; |
1763 table_->GetServerCreditCards(&outputs); | 1771 table_->GetServerCreditCards(&outputs); |
1764 ASSERT_EQ(1u, outputs.size()); | 1772 ASSERT_EQ(1u, outputs.size()); |
1765 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1773 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
1766 EXPECT_EQ(1U, outputs[0]->use_count()); | 1774 EXPECT_EQ(1U, outputs[0]->use_count()); |
1767 EXPECT_NE(base::Time(), outputs[0]->use_date()); | 1775 EXPECT_EQ(kArbitraryTime, outputs[0]->use_date()); |
1768 // We don't track modification date for server cards. It should always be | 1776 // We don't track modification date for server cards. It should always be |
1769 // base::Time(). | 1777 // base::Time(). |
1770 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1778 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
1771 outputs.clear(); | 1779 outputs.clear(); |
1772 | 1780 |
1773 // Update the usage stats; make sure they're reflected in GetServerProfiles. | 1781 // Update the usage stats; make sure they're reflected in GetServerProfiles. |
1774 inputs.back().set_use_count(4U); | 1782 inputs.back().set_use_count(4U); |
1775 inputs.back().set_use_date(base::Time()); | 1783 inputs.back().set_use_date(kSomeLaterTime); |
1776 inputs.back().set_billing_address_id("2"); | 1784 inputs.back().set_billing_address_id("2"); |
1777 table_->UpdateServerCardMetadata(inputs.back()); | 1785 table_->UpdateServerCardMetadata(inputs.back()); |
1778 table_->GetServerCreditCards(&outputs); | 1786 table_->GetServerCreditCards(&outputs); |
1779 ASSERT_EQ(1u, outputs.size()); | 1787 ASSERT_EQ(1u, outputs.size()); |
1780 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1788 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
1781 EXPECT_EQ(4U, outputs[0]->use_count()); | 1789 EXPECT_EQ(4U, outputs[0]->use_count()); |
1782 EXPECT_EQ(base::Time(), outputs[0]->use_date()); | 1790 EXPECT_EQ(kSomeLaterTime, outputs[0]->use_date()); |
1783 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1791 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
1784 EXPECT_EQ("2", outputs[0]->billing_address_id()); | 1792 EXPECT_EQ("2", outputs[0]->billing_address_id()); |
1785 outputs.clear(); | 1793 outputs.clear(); |
1786 | 1794 |
1787 // Setting the cards again shouldn't delete the usage stats. | 1795 // Setting the cards shouldn't delete the usage stats. |
1796 inputs.back().set_use_count(1000U); | |
1797 inputs.back().set_use_date(kMuchLaterTime); | |
1788 table_->SetServerCreditCards(inputs); | 1798 table_->SetServerCreditCards(inputs); |
1789 table_->GetServerCreditCards(&outputs); | 1799 table_->GetServerCreditCards(&outputs); |
1790 ASSERT_EQ(1u, outputs.size()); | 1800 ASSERT_EQ(1u, outputs.size()); |
1791 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1801 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
1802 // The use stats should not be modified. | |
1792 EXPECT_EQ(4U, outputs[0]->use_count()); | 1803 EXPECT_EQ(4U, outputs[0]->use_count()); |
1793 EXPECT_EQ(base::Time(), outputs[0]->use_date()); | 1804 EXPECT_EQ(kSomeLaterTime, outputs[0]->use_date()); |
1794 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1805 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
1795 EXPECT_EQ("2", outputs[0]->billing_address_id()); | 1806 EXPECT_EQ("2", outputs[0]->billing_address_id()); |
1796 outputs.clear(); | 1807 outputs.clear(); |
1797 | 1808 |
1798 // Set a card list where the card is missing --- this should clear metadata. | 1809 // Set a card list where the card is missing --- this should clear metadata. |
1799 CreditCard masked_card2(CreditCard::MASKED_SERVER_CARD, "b456"); | 1810 CreditCard masked_card2(CreditCard::MASKED_SERVER_CARD, "b456"); |
1800 inputs.back() = masked_card2; | 1811 inputs.back() = masked_card2; |
1801 table_->SetServerCreditCards(inputs); | 1812 table_->SetServerCreditCards(inputs); |
Mathieu
2017/02/26 23:43:12
is this useful?
sebsg
2017/02/27 13:54:53
It clears all the data. It is there to show that i
| |
1802 | 1813 |
1803 // Back to the original card list. | 1814 // Back to the original card list. |
1804 inputs.back() = masked_card; | 1815 inputs.back() = masked_card; |
1816 // Update the metadata | |
1817 inputs.back().set_use_count(1000U); | |
Mathieu
2017/02/26 23:43:12
this seems to be tested on 1783?
sebsg
2017/02/27 13:54:53
Done.
| |
1818 inputs.back().set_use_date(kMuchLaterTime); | |
1805 table_->SetServerCreditCards(inputs); | 1819 table_->SetServerCreditCards(inputs); |
1820 table_->UpdateServerCardMetadata(inputs.back()); | |
1806 table_->GetServerCreditCards(&outputs); | 1821 table_->GetServerCreditCards(&outputs); |
1807 ASSERT_EQ(1u, outputs.size()); | 1822 ASSERT_EQ(1u, outputs.size()); |
1808 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1823 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
1809 EXPECT_EQ(1U, outputs[0]->use_count()); | 1824 // The use stats should have been updated. |
1810 EXPECT_NE(base::Time(), outputs[0]->use_date()); | 1825 EXPECT_EQ(1000U, outputs[0]->use_count()); |
1826 EXPECT_EQ(kMuchLaterTime, outputs[0]->use_date()); | |
1811 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1827 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
1812 EXPECT_EQ("1", outputs[0]->billing_address_id()); | 1828 EXPECT_EQ("1", outputs[0]->billing_address_id()); |
1813 outputs.clear(); | 1829 outputs.clear(); |
1814 } | 1830 } |
1815 | 1831 |
1816 TEST_F(AutofillTableTest, SetServerProfile) { | 1832 TEST_F(AutofillTableTest, SetServerProfile) { |
1817 AutofillProfile one(AutofillProfile::SERVER_PROFILE, "a123"); | 1833 AutofillProfile one(AutofillProfile::SERVER_PROFILE, "a123"); |
1818 std::vector<AutofillProfile> inputs; | 1834 std::vector<AutofillProfile> inputs; |
1819 inputs.push_back(one); | 1835 inputs.push_back(one); |
1820 table_->SetServerProfiles(inputs); | 1836 table_->SetServerProfiles(inputs); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2101 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement( | 2117 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement( |
2102 "INSERT OR REPLACE INTO autofill_model_type_state " | 2118 "INSERT OR REPLACE INTO autofill_model_type_state " |
2103 "(rowid, value) VALUES(1, ?)")); | 2119 "(rowid, value) VALUES(1, ?)")); |
2104 s.BindString(0, "unparseable"); | 2120 s.BindString(0, "unparseable"); |
2105 EXPECT_TRUE(s.Run()); | 2121 EXPECT_TRUE(s.Run()); |
2106 | 2122 |
2107 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); | 2123 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); |
2108 } | 2124 } |
2109 | 2125 |
2110 } // namespace autofill | 2126 } // namespace autofill |
OLD | NEW |