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