| 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" | |
| 29 #include "components/autofill/core/browser/webdata/autofill_change.h" | 28 #include "components/autofill/core/browser/webdata/autofill_change.h" |
| 30 #include "components/autofill/core/browser/webdata/autofill_entry.h" | 29 #include "components/autofill/core/browser/webdata/autofill_entry.h" |
| 31 #include "components/autofill/core/common/autofill_constants.h" | 30 #include "components/autofill/core/common/autofill_constants.h" |
| 32 #include "components/autofill/core/common/autofill_switches.h" | 31 #include "components/autofill/core/common/autofill_switches.h" |
| 33 #include "components/autofill/core/common/autofill_util.h" | 32 #include "components/autofill/core/common/autofill_util.h" |
| 34 #include "components/autofill/core/common/form_field_data.h" | 33 #include "components/autofill/core/common/form_field_data.h" |
| 35 #include "components/os_crypt/os_crypt_mocker.h" | 34 #include "components/os_crypt/os_crypt_mocker.h" |
| 36 #include "components/sync/protocol/entity_metadata.pb.h" | 35 #include "components/sync/protocol/entity_metadata.pb.h" |
| 37 #include "components/sync/protocol/model_type_state.pb.h" | 36 #include "components/sync/protocol/model_type_state.pb.h" |
| 38 #include "components/webdata/common/web_database.h" | 37 #include "components/webdata/common/web_database.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 69 case AutofillChange::REMOVE: { | 68 case AutofillChange::REMOVE: { |
| 70 os << "REMOVE"; | 69 os << "REMOVE"; |
| 71 break; | 70 break; |
| 72 } | 71 } |
| 73 } | 72 } |
| 74 return os << " " << change.key(); | 73 return os << " " << change.key(); |
| 75 } | 74 } |
| 76 | 75 |
| 77 namespace { | 76 namespace { |
| 78 | 77 |
| 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 | |
| 83 typedef std::set<AutofillEntry, | 78 typedef std::set<AutofillEntry, |
| 84 bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet; | 79 bool (*)(const AutofillEntry&, const AutofillEntry&)> AutofillEntrySet; |
| 85 typedef AutofillEntrySet::iterator AutofillEntrySetIterator; | 80 typedef AutofillEntrySet::iterator AutofillEntrySetIterator; |
| 86 | 81 |
| 87 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) { | 82 bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) { |
| 88 return std::tie(a.key().name(), a.key().value(), | 83 return std::tie(a.key().name(), a.key().value(), |
| 89 a.date_created(), a.date_last_used()) < | 84 a.date_created(), a.date_last_used()) < |
| 90 std::tie(b.key().name(), b.key().value(), | 85 std::tie(b.key().name(), b.key().value(), |
| 91 b.date_created(), b.date_last_used()); | 86 b.date_created(), b.date_last_used()); |
| 92 } | 87 } |
| (...skipping 1650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1743 table_->GetServerCreditCards(&outputs); | 1738 table_->GetServerCreditCards(&outputs); |
| 1744 ASSERT_EQ(1u, outputs.size()); | 1739 ASSERT_EQ(1u, outputs.size()); |
| 1745 EXPECT_TRUE(outputs[0]->record_type() == CreditCard::MASKED_SERVER_CARD); | 1740 EXPECT_TRUE(outputs[0]->record_type() == CreditCard::MASKED_SERVER_CARD); |
| 1746 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1741 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
| 1747 EXPECT_EQ(ASCIIToUTF16("1111"), outputs[0]->GetRawInfo(CREDIT_CARD_NUMBER)); | 1742 EXPECT_EQ(ASCIIToUTF16("1111"), outputs[0]->GetRawInfo(CREDIT_CARD_NUMBER)); |
| 1748 | 1743 |
| 1749 outputs.clear(); | 1744 outputs.clear(); |
| 1750 } | 1745 } |
| 1751 | 1746 |
| 1752 TEST_F(AutofillTableTest, SetServerCardUpdateUsageStatsAndBillingAddress) { | 1747 TEST_F(AutofillTableTest, SetServerCardUpdateUsageStatsAndBillingAddress) { |
| 1753 TestAutofillClock test_clock; | |
| 1754 test_clock.SetNow(kArbitraryTime); | |
| 1755 | |
| 1756 // Add a masked card. | 1748 // Add a masked card. |
| 1757 CreditCard masked_card(CreditCard::MASKED_SERVER_CARD, "a123"); | 1749 CreditCard masked_card(CreditCard::MASKED_SERVER_CARD, "a123"); |
| 1758 masked_card.SetRawInfo(CREDIT_CARD_NAME_FULL, | 1750 masked_card.SetRawInfo(CREDIT_CARD_NAME_FULL, |
| 1759 ASCIIToUTF16("Paul F. Tompkins")); | 1751 ASCIIToUTF16("Paul F. Tompkins")); |
| 1760 masked_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1")); | 1752 masked_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1")); |
| 1761 masked_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2020")); | 1753 masked_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2020")); |
| 1762 masked_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1111")); | 1754 masked_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1111")); |
| 1763 masked_card.set_billing_address_id("1"); | 1755 masked_card.set_billing_address_id("1"); |
| 1764 masked_card.SetTypeForMaskedCard(kVisaCard); | 1756 masked_card.SetTypeForMaskedCard(kVisaCard); |
| 1765 | 1757 |
| 1766 std::vector<CreditCard> inputs; | 1758 std::vector<CreditCard> inputs; |
| 1767 inputs.push_back(masked_card); | 1759 inputs.push_back(masked_card); |
| 1768 test::SetServerCreditCards(table_.get(), inputs); | 1760 test::SetServerCreditCards(table_.get(), inputs); |
| 1769 | 1761 |
| 1770 std::vector<std::unique_ptr<CreditCard>> outputs; | 1762 std::vector<std::unique_ptr<CreditCard>> outputs; |
| 1771 table_->GetServerCreditCards(&outputs); | 1763 table_->GetServerCreditCards(&outputs); |
| 1772 ASSERT_EQ(1u, outputs.size()); | 1764 ASSERT_EQ(1u, outputs.size()); |
| 1773 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1765 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
| 1774 EXPECT_EQ(1U, outputs[0]->use_count()); | 1766 EXPECT_EQ(1U, outputs[0]->use_count()); |
| 1775 EXPECT_EQ(kArbitraryTime, outputs[0]->use_date()); | 1767 EXPECT_NE(base::Time(), outputs[0]->use_date()); |
| 1776 // We don't track modification date for server cards. It should always be | 1768 // We don't track modification date for server cards. It should always be |
| 1777 // base::Time(). | 1769 // base::Time(). |
| 1778 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1770 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
| 1779 outputs.clear(); | 1771 outputs.clear(); |
| 1780 | 1772 |
| 1781 // Update the usage stats; make sure they're reflected in GetServerProfiles. | 1773 // Update the usage stats; make sure they're reflected in GetServerProfiles. |
| 1782 inputs.back().set_use_count(4U); | 1774 inputs.back().set_use_count(4U); |
| 1783 inputs.back().set_use_date(kSomeLaterTime); | 1775 inputs.back().set_use_date(base::Time()); |
| 1784 inputs.back().set_billing_address_id("2"); | 1776 inputs.back().set_billing_address_id("2"); |
| 1785 table_->UpdateServerCardMetadata(inputs.back()); | 1777 table_->UpdateServerCardMetadata(inputs.back()); |
| 1786 table_->GetServerCreditCards(&outputs); | 1778 table_->GetServerCreditCards(&outputs); |
| 1787 ASSERT_EQ(1u, outputs.size()); | 1779 ASSERT_EQ(1u, outputs.size()); |
| 1788 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1780 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
| 1789 EXPECT_EQ(4U, outputs[0]->use_count()); | 1781 EXPECT_EQ(4U, outputs[0]->use_count()); |
| 1790 EXPECT_EQ(kSomeLaterTime, outputs[0]->use_date()); | 1782 EXPECT_EQ(base::Time(), outputs[0]->use_date()); |
| 1791 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1783 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
| 1792 EXPECT_EQ("2", outputs[0]->billing_address_id()); | 1784 EXPECT_EQ("2", outputs[0]->billing_address_id()); |
| 1793 outputs.clear(); | 1785 outputs.clear(); |
| 1794 | 1786 |
| 1795 // Setting the cards shouldn't delete the usage stats. | 1787 // Setting the cards again shouldn't delete the usage stats. |
| 1796 inputs.back().set_use_count(1000U); | |
| 1797 inputs.back().set_use_date(kMuchLaterTime); | |
| 1798 table_->SetServerCreditCards(inputs); | 1788 table_->SetServerCreditCards(inputs); |
| 1799 table_->GetServerCreditCards(&outputs); | 1789 table_->GetServerCreditCards(&outputs); |
| 1800 ASSERT_EQ(1u, outputs.size()); | 1790 ASSERT_EQ(1u, outputs.size()); |
| 1801 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1791 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
| 1802 // The use stats should not be modified. | |
| 1803 EXPECT_EQ(4U, outputs[0]->use_count()); | 1792 EXPECT_EQ(4U, outputs[0]->use_count()); |
| 1804 EXPECT_EQ(kSomeLaterTime, outputs[0]->use_date()); | 1793 EXPECT_EQ(base::Time(), outputs[0]->use_date()); |
| 1805 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1794 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
| 1806 EXPECT_EQ("2", outputs[0]->billing_address_id()); | 1795 EXPECT_EQ("2", outputs[0]->billing_address_id()); |
| 1807 outputs.clear(); | 1796 outputs.clear(); |
| 1808 | 1797 |
| 1809 // Set a card list where the card is missing --- this should clear metadata. | 1798 // Set a card list where the card is missing --- this should clear metadata. |
| 1810 CreditCard masked_card2(CreditCard::MASKED_SERVER_CARD, "b456"); | 1799 CreditCard masked_card2(CreditCard::MASKED_SERVER_CARD, "b456"); |
| 1811 inputs.back() = masked_card2; | 1800 inputs.back() = masked_card2; |
| 1812 table_->SetServerCreditCards(inputs); | 1801 table_->SetServerCreditCards(inputs); |
| 1813 | 1802 |
| 1814 // Back to the original card list. | 1803 // Back to the original card list. |
| 1815 inputs.back() = masked_card; | 1804 inputs.back() = masked_card; |
| 1816 table_->SetServerCreditCards(inputs); | 1805 table_->SetServerCreditCards(inputs); |
| 1817 table_->UpdateServerCardMetadata(inputs.back()); | |
| 1818 table_->GetServerCreditCards(&outputs); | 1806 table_->GetServerCreditCards(&outputs); |
| 1819 ASSERT_EQ(1u, outputs.size()); | 1807 ASSERT_EQ(1u, outputs.size()); |
| 1820 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); | 1808 EXPECT_EQ(masked_card.server_id(), outputs[0]->server_id()); |
| 1821 // The use stats should have stayed the same. | |
| 1822 EXPECT_EQ(1U, outputs[0]->use_count()); | 1809 EXPECT_EQ(1U, outputs[0]->use_count()); |
| 1823 EXPECT_EQ(kArbitraryTime, outputs[0]->use_date()); | 1810 EXPECT_NE(base::Time(), outputs[0]->use_date()); |
| 1824 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); | 1811 EXPECT_EQ(base::Time(), outputs[0]->modification_date()); |
| 1825 EXPECT_EQ("1", outputs[0]->billing_address_id()); | 1812 EXPECT_EQ("1", outputs[0]->billing_address_id()); |
| 1826 outputs.clear(); | 1813 outputs.clear(); |
| 1827 } | 1814 } |
| 1828 | 1815 |
| 1829 TEST_F(AutofillTableTest, SetServerProfile) { | 1816 TEST_F(AutofillTableTest, SetServerProfile) { |
| 1830 AutofillProfile one(AutofillProfile::SERVER_PROFILE, "a123"); | 1817 AutofillProfile one(AutofillProfile::SERVER_PROFILE, "a123"); |
| 1831 std::vector<AutofillProfile> inputs; | 1818 std::vector<AutofillProfile> inputs; |
| 1832 inputs.push_back(one); | 1819 inputs.push_back(one); |
| 1833 table_->SetServerProfiles(inputs); | 1820 table_->SetServerProfiles(inputs); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2114 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement( | 2101 sql::Statement s(db_->GetSQLConnection()->GetUniqueStatement( |
| 2115 "INSERT OR REPLACE INTO autofill_model_type_state " | 2102 "INSERT OR REPLACE INTO autofill_model_type_state " |
| 2116 "(rowid, value) VALUES(1, ?)")); | 2103 "(rowid, value) VALUES(1, ?)")); |
| 2117 s.BindString(0, "unparseable"); | 2104 s.BindString(0, "unparseable"); |
| 2118 EXPECT_TRUE(s.Run()); | 2105 EXPECT_TRUE(s.Run()); |
| 2119 | 2106 |
| 2120 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); | 2107 EXPECT_FALSE(table_->GetAllSyncMetadata(syncer::AUTOFILL, &metadata_batch)); |
| 2121 } | 2108 } |
| 2122 | 2109 |
| 2123 } // namespace autofill | 2110 } // namespace autofill |
| OLD | NEW |