| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/autofill/browser/autofill_ie_toolbar_import_win.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/strings/string16.h" | |
| 9 #include "base/win/registry.h" | |
| 10 #include "components/autofill/browser/autofill_profile.h" | |
| 11 #include "components/autofill/browser/credit_card.h" | |
| 12 #include "components/autofill/browser/field_types.h" | |
| 13 #include "sync/util/data_encryption_win.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using base::win::RegKey; | |
| 17 | |
| 18 namespace autofill { | |
| 19 | |
| 20 // Defined in autofill_ie_toolbar_import_win.cc. Not exposed in the header file. | |
| 21 bool ImportCurrentUserProfiles(const std::string& app_locale, | |
| 22 std::vector<AutofillProfile>* profiles, | |
| 23 std::vector<CreditCard>* credit_cards); | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; | |
| 28 const wchar_t kUnitTestUserOverrideSubKey[] = | |
| 29 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; | |
| 30 | |
| 31 const wchar_t* const kProfileKey = | |
| 32 L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Profiles"; | |
| 33 const wchar_t* const kCreditCardKey = | |
| 34 L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Credit Cards"; | |
| 35 const wchar_t* const kPasswordHashValue = L"password_hash"; | |
| 36 const wchar_t* const kSaltValue = L"salt"; | |
| 37 | |
| 38 struct ValueDescription { | |
| 39 wchar_t const* const value_name; | |
| 40 wchar_t const* const value; | |
| 41 }; | |
| 42 | |
| 43 ValueDescription profile1[] = { | |
| 44 { L"name_first", L"John" }, | |
| 45 { L"name_middle", L"Herman" }, | |
| 46 { L"name_last", L"Doe" }, | |
| 47 { L"email", L"jdoe@test.com" }, | |
| 48 { L"company_name", L"Testcompany" }, | |
| 49 { L"phone_home_number", L"555-5555" }, | |
| 50 { L"phone_home_city_code", L"650" }, | |
| 51 { L"phone_home_country_code", L"1" }, | |
| 52 }; | |
| 53 | |
| 54 ValueDescription profile2[] = { | |
| 55 { L"name_first", L"Jane" }, | |
| 56 { L"name_last", L"Doe" }, | |
| 57 { L"email", L"janedoe@test.com" }, | |
| 58 { L"company_name", L"Testcompany" }, | |
| 59 }; | |
| 60 | |
| 61 ValueDescription credit_card[] = { | |
| 62 { L"credit_card_name", L"Tommy Gun" }, | |
| 63 // "4111111111111111" encrypted: | |
| 64 { L"credit_card_number", L"\xE53F\x19AB\xC1BF\xC9EB\xECCC\x9BDA\x8515" | |
| 65 L"\xE14D\x6852\x80A8\x50A3\x4375\xFD9F\x1E07" | |
| 66 L"\x790E\x7336\xB773\xAF33\x93EA\xB846\xEC89" | |
| 67 L"\x265C\xD0E6\x4E23\xB75F\x7983" }, | |
| 68 { L"credit_card_exp_month", L"11" }, | |
| 69 { L"credit_card_exp_4_digit_year", L"2011" }, | |
| 70 }; | |
| 71 | |
| 72 ValueDescription empty_salt = { | |
| 73 L"salt", L"\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA\xB\xC\xD\xE\xF\x10\x11\x12\x13\x14" | |
| 74 }; | |
| 75 | |
| 76 ValueDescription empty_password = { | |
| 77 L"password_hash", L"" | |
| 78 }; | |
| 79 | |
| 80 ValueDescription protected_salt = { | |
| 81 L"salt", L"\x4854\xB906\x9C7C\x50A6\x4376\xFD9D\x1E02" | |
| 82 }; | |
| 83 | |
| 84 ValueDescription protected_password = { | |
| 85 L"password_hash", L"\x18B7\xE586\x459B\x7457\xA066\x3842\x71DA" | |
| 86 }; | |
| 87 | |
| 88 void EncryptAndWrite(RegKey* key, const ValueDescription* value) { | |
| 89 std::string data; | |
| 90 size_t data_size = (lstrlen(value->value) + 1) * sizeof(wchar_t); | |
| 91 data.resize(data_size); | |
| 92 memcpy(&data[0], value->value, data_size); | |
| 93 | |
| 94 std::vector<uint8> encrypted_data = syncer::EncryptData(data); | |
| 95 EXPECT_EQ(ERROR_SUCCESS, key->WriteValue(value->value_name, | |
| 96 &encrypted_data[0], encrypted_data.size(), REG_BINARY)); | |
| 97 } | |
| 98 | |
| 99 void CreateSubkey(RegKey* key, wchar_t const* subkey_name, | |
| 100 const ValueDescription* values, size_t values_size) { | |
| 101 RegKey subkey; | |
| 102 subkey.Create(key->Handle(), subkey_name, KEY_ALL_ACCESS); | |
| 103 EXPECT_TRUE(subkey.Valid()); | |
| 104 for (size_t i = 0; i < values_size; ++i) | |
| 105 EncryptAndWrite(&subkey, values + i); | |
| 106 } | |
| 107 | |
| 108 } // namespace | |
| 109 | |
| 110 class AutofillIeToolbarImportTest : public testing::Test { | |
| 111 public: | |
| 112 AutofillIeToolbarImportTest(); | |
| 113 | |
| 114 // testing::Test method overrides: | |
| 115 virtual void SetUp(); | |
| 116 virtual void TearDown(); | |
| 117 | |
| 118 private: | |
| 119 RegKey temp_hkcu_hive_key_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(AutofillIeToolbarImportTest); | |
| 122 }; | |
| 123 | |
| 124 AutofillIeToolbarImportTest::AutofillIeToolbarImportTest() { | |
| 125 } | |
| 126 | |
| 127 void AutofillIeToolbarImportTest::SetUp() { | |
| 128 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | |
| 129 kUnitTestUserOverrideSubKey, | |
| 130 KEY_ALL_ACCESS); | |
| 131 EXPECT_TRUE(temp_hkcu_hive_key_.Valid()); | |
| 132 EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER, | |
| 133 temp_hkcu_hive_key_.Handle())); | |
| 134 } | |
| 135 | |
| 136 void AutofillIeToolbarImportTest::TearDown() { | |
| 137 EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER, NULL)); | |
| 138 temp_hkcu_hive_key_.Close(); | |
| 139 RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS); | |
| 140 key.DeleteKey(L""); | |
| 141 } | |
| 142 | |
| 143 TEST_F(AutofillIeToolbarImportTest, TestAutofillImport) { | |
| 144 RegKey profile_key; | |
| 145 profile_key.Create(HKEY_CURRENT_USER, kProfileKey, KEY_ALL_ACCESS); | |
| 146 EXPECT_TRUE(profile_key.Valid()); | |
| 147 | |
| 148 CreateSubkey(&profile_key, L"0", profile1, arraysize(profile1)); | |
| 149 CreateSubkey(&profile_key, L"1", profile2, arraysize(profile2)); | |
| 150 | |
| 151 RegKey cc_key; | |
| 152 cc_key.Create(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS); | |
| 153 EXPECT_TRUE(cc_key.Valid()); | |
| 154 CreateSubkey(&cc_key, L"0", credit_card, arraysize(credit_card)); | |
| 155 EncryptAndWrite(&cc_key, &empty_password); | |
| 156 EncryptAndWrite(&cc_key, &empty_salt); | |
| 157 | |
| 158 profile_key.Close(); | |
| 159 cc_key.Close(); | |
| 160 | |
| 161 std::vector<AutofillProfile> profiles; | |
| 162 std::vector<CreditCard> credit_cards; | |
| 163 EXPECT_TRUE(ImportCurrentUserProfiles("en-US", &profiles, &credit_cards)); | |
| 164 ASSERT_EQ(2U, profiles.size()); | |
| 165 // The profiles are read in reverse order. | |
| 166 EXPECT_EQ(profile1[0].value, profiles[1].GetRawInfo(NAME_FIRST)); | |
| 167 EXPECT_EQ(profile1[1].value, profiles[1].GetRawInfo(NAME_MIDDLE)); | |
| 168 EXPECT_EQ(profile1[2].value, profiles[1].GetRawInfo(NAME_LAST)); | |
| 169 EXPECT_EQ(profile1[3].value, profiles[1].GetRawInfo(EMAIL_ADDRESS)); | |
| 170 EXPECT_EQ(profile1[4].value, profiles[1].GetRawInfo(COMPANY_NAME)); | |
| 171 EXPECT_EQ(profile1[7].value, | |
| 172 profiles[1].GetInfo(PHONE_HOME_COUNTRY_CODE, "US")); | |
| 173 EXPECT_EQ(profile1[6].value, profiles[1].GetInfo(PHONE_HOME_CITY_CODE, "US")); | |
| 174 EXPECT_EQ(L"5555555", profiles[1].GetInfo(PHONE_HOME_NUMBER, "US")); | |
| 175 EXPECT_EQ(L"+1 650-555-5555", | |
| 176 profiles[1].GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | |
| 177 | |
| 178 EXPECT_EQ(profile2[0].value, profiles[0].GetRawInfo(NAME_FIRST)); | |
| 179 EXPECT_EQ(profile2[1].value, profiles[0].GetRawInfo(NAME_LAST)); | |
| 180 EXPECT_EQ(profile2[2].value, profiles[0].GetRawInfo(EMAIL_ADDRESS)); | |
| 181 EXPECT_EQ(profile2[3].value, profiles[0].GetRawInfo(COMPANY_NAME)); | |
| 182 | |
| 183 ASSERT_EQ(1U, credit_cards.size()); | |
| 184 EXPECT_EQ(credit_card[0].value, credit_cards[0].GetRawInfo(CREDIT_CARD_NAME)); | |
| 185 EXPECT_EQ(L"4111111111111111", | |
| 186 credit_cards[0].GetRawInfo(CREDIT_CARD_NUMBER)); | |
| 187 EXPECT_EQ(credit_card[2].value, | |
| 188 credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_MONTH)); | |
| 189 EXPECT_EQ(credit_card[3].value, | |
| 190 credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
| 191 | |
| 192 // Mock password encrypted cc. | |
| 193 cc_key.Open(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS); | |
| 194 EXPECT_TRUE(cc_key.Valid()); | |
| 195 EncryptAndWrite(&cc_key, &protected_password); | |
| 196 EncryptAndWrite(&cc_key, &protected_salt); | |
| 197 cc_key.Close(); | |
| 198 | |
| 199 profiles.clear(); | |
| 200 credit_cards.clear(); | |
| 201 EXPECT_TRUE(ImportCurrentUserProfiles("en-US", &profiles, &credit_cards)); | |
| 202 // Profiles are not protected. | |
| 203 EXPECT_EQ(2U, profiles.size()); | |
| 204 // Credit cards are. | |
| 205 EXPECT_EQ(0U, credit_cards.size()); | |
| 206 } | |
| 207 | |
| 208 } // namespace autofill | |
| OLD | NEW |