1 // Copyright (c) 2011 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 "chrome/browser/autofill/autofill_ie_toolbar_import_win.h" 6 7 #include "base/basictypes.h" 8 #include "base/string16.h" 9 #include "base/win/registry.h" 10 #include "chrome/browser/autofill/autofill_profile.h" 11 #include "chrome/browser/autofill/credit_card.h" 12 #include "chrome/browser/autofill/field_types.h" 13 #include "chrome/browser/sync/util/data_encryption.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 using base::win::RegKey; 17 18 // Defined in autofill_ie_toolbar_import_win.cc. Not exposed in the header file. 19 bool ImportCurrentUserProfiles(std::vector<AutofillProfile>* profiles, 20 std::vector<CreditCard>* credit_cards); 21 22 namespace { 23 24 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; 25 const wchar_t kUnitTestUserOverrideSubKey[] = 26 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; 27 28 const wchar_t* const kProfileKey = 29 L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Profiles"; 30 const wchar_t* const kCreditCardKey = 31 L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Credit Cards"; 32 const wchar_t* const kPasswordHashValue = L"password_hash"; 33 const wchar_t* const kSaltValue = L"salt"; 34 35 struct ValueDescription { 36 wchar_t const* const value_name; 37 wchar_t const* const value; 38 }; 39 40 ValueDescription profile1[] = { 41 { L"name_first", L"John" }, 42 { L"name_middle", L"Herman" }, 43 { L"name_last", L"Doe" }, 44 { L"email", L"jdoe (at) test.com" }, 45 { L"company_name", L"Testcompany" }, 46 { L"phone_home_number", L"555-5555" }, 47 { L"phone_home_city_code", L"444" }, 48 { L"phone_home_country_code", L"1" }, 49 }; 50 51 ValueDescription profile2[] = { 52 { L"name_first", L"Jane" }, 53 { L"name_last", L"Doe" }, 54 { L"email", L"janedoe (at) test.com" }, 55 { L"company_name", L"Testcompany" }, 56 { L"phone_fax_number", L"555-6666" }, 57 { L"phone_fax_city_code", L"777" }, 58 { L"phone_fax_country_code", L"2" }, 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 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 = 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(&profiles, &credit_cards)); 164 ASSERT_EQ(profiles.size(), 2); 165 // The profiles are read in reverse order. 166 EXPECT_EQ(profiles[1].GetInfo(NAME_FIRST), profile1[0].value); 167 EXPECT_EQ(profiles[1].GetInfo(NAME_MIDDLE), profile1[1].value); 168 EXPECT_EQ(profiles[1].GetInfo(NAME_LAST), profile1[2].value); 169 EXPECT_EQ(profiles[1].GetInfo(EMAIL_ADDRESS), profile1[3].value); 170 EXPECT_EQ(profiles[1].GetInfo(COMPANY_NAME), profile1[4].value); 171 EXPECT_EQ(profiles[1].GetInfo(PHONE_HOME_COUNTRY_CODE), profile1[7].value); 172 EXPECT_EQ(profiles[1].GetInfo(PHONE_HOME_CITY_CODE), profile1[6].value); 173 EXPECT_EQ(profiles[1].GetInfo(PHONE_HOME_NUMBER), L"5555555"); 174 EXPECT_EQ(profiles[1].GetInfo(PHONE_HOME_WHOLE_NUMBER), L"14445555555"); 175 176 EXPECT_EQ(profiles[0].GetInfo(NAME_FIRST), profile2[0].value); 177 EXPECT_EQ(profiles[0].GetInfo(NAME_LAST), profile2[1].value); 178 EXPECT_EQ(profiles[0].GetInfo(EMAIL_ADDRESS), profile2[2].value); 179 EXPECT_EQ(profiles[0].GetInfo(COMPANY_NAME), profile2[3].value); 180 EXPECT_EQ(profiles[0].GetInfo(PHONE_FAX_COUNTRY_CODE), profile2[6].value); 181 EXPECT_EQ(profiles[0].GetInfo(PHONE_FAX_CITY_CODE), profile2[5].value); 182 EXPECT_EQ(profiles[0].GetInfo(PHONE_FAX_NUMBER), L"5556666"); 183 EXPECT_EQ(profiles[0].GetInfo(PHONE_FAX_WHOLE_NUMBER), L"27775556666"); 184 185 ASSERT_EQ(credit_cards.size(), 1); 186 EXPECT_EQ(credit_cards[0].GetInfo(CREDIT_CARD_NAME), credit_card[0].value); 187 EXPECT_EQ(credit_cards[0].GetInfo(CREDIT_CARD_NUMBER), L"4111111111111111"); 188 EXPECT_EQ(credit_cards[0].GetInfo(CREDIT_CARD_EXP_MONTH), 189 credit_card[2].value); 190 EXPECT_EQ(credit_cards[0].GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR), 191 credit_card[3].value); 192 193 // Mock password encrypted cc. 194 cc_key.Open(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS); 195 EXPECT_TRUE(cc_key.Valid()); 196 EncryptAndWrite(&cc_key, &protected_password); 197 EncryptAndWrite(&cc_key, &protected_salt); 198 cc_key.Close(); 199 200 profiles.clear(); 201 credit_cards.clear(); 202 EXPECT_TRUE(ImportCurrentUserProfiles(&profiles, &credit_cards)); 203 // Profiles are not protected. 204 EXPECT_EQ(profiles.size(), 2); 205 // Credit cards are. 206 EXPECT_EQ(credit_cards.size(), 0); 207 } 208 209