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 "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 "sync/util/data_encryption_win.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@test.com" }, | |
45 { L"company_name", L"Testcompany" }, | |
46 { L"phone_home_number", L"555-5555" }, | |
47 { L"phone_home_city_code", L"650" }, | |
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@test.com" }, | |
55 { L"company_name", L"Testcompany" }, | |
56 }; | |
57 | |
58 ValueDescription credit_card[] = { | |
59 { L"credit_card_name", L"Tommy Gun" }, | |
60 // "4111111111111111" encrypted: | |
61 { L"credit_card_number", L"\xE53F\x19AB\xC1BF\xC9EB\xECCC\x9BDA\x8515" | |
62 L"\xE14D\x6852\x80A8\x50A3\x4375\xFD9F\x1E07" | |
63 L"\x790E\x7336\xB773\xAF33\x93EA\xB846\xEC89" | |
64 L"\x265C\xD0E6\x4E23\xB75F\x7983" }, | |
65 { L"credit_card_exp_month", L"11" }, | |
66 { L"credit_card_exp_4_digit_year", L"2011" }, | |
67 }; | |
68 | |
69 ValueDescription empty_salt = { | |
70 L"salt", L"\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA\xB\xC\xD\xE\xF\x10\x11\x12\x13\x14" | |
71 }; | |
72 | |
73 ValueDescription empty_password = { | |
74 L"password_hash", L"" | |
75 }; | |
76 | |
77 ValueDescription protected_salt = { | |
78 L"salt", L"\x4854\xB906\x9C7C\x50A6\x4376\xFD9D\x1E02" | |
79 }; | |
80 | |
81 ValueDescription protected_password = { | |
82 L"password_hash", L"\x18B7\xE586\x459B\x7457\xA066\x3842\x71DA" | |
83 }; | |
84 | |
85 void EncryptAndWrite(RegKey* key, const ValueDescription* value) { | |
86 std::string data; | |
87 size_t data_size = (lstrlen(value->value) + 1) * sizeof(wchar_t); | |
88 data.resize(data_size); | |
89 memcpy(&data[0], value->value, data_size); | |
90 | |
91 std::vector<uint8> encrypted_data = syncer::EncryptData(data); | |
92 EXPECT_EQ(ERROR_SUCCESS, key->WriteValue(value->value_name, | |
93 &encrypted_data[0], encrypted_data.size(), REG_BINARY)); | |
94 } | |
95 | |
96 void CreateSubkey(RegKey* key, wchar_t const* subkey_name, | |
97 const ValueDescription* values, size_t values_size) { | |
98 RegKey subkey; | |
99 subkey.Create(key->Handle(), subkey_name, KEY_ALL_ACCESS); | |
100 EXPECT_TRUE(subkey.Valid()); | |
101 for (size_t i = 0; i < values_size; ++i) | |
102 EncryptAndWrite(&subkey, values + i); | |
103 } | |
104 | |
105 } // namespace | |
106 | |
107 class AutofillIeToolbarImportTest : public testing::Test { | |
108 public: | |
109 AutofillIeToolbarImportTest(); | |
110 | |
111 // testing::Test method overrides: | |
112 virtual void SetUp(); | |
113 virtual void TearDown(); | |
114 | |
115 private: | |
116 RegKey temp_hkcu_hive_key_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(AutofillIeToolbarImportTest); | |
119 }; | |
120 | |
121 AutofillIeToolbarImportTest::AutofillIeToolbarImportTest() { | |
122 } | |
123 | |
124 void AutofillIeToolbarImportTest::SetUp() { | |
125 temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER, | |
126 kUnitTestUserOverrideSubKey, | |
127 KEY_ALL_ACCESS); | |
128 EXPECT_TRUE(temp_hkcu_hive_key_.Valid()); | |
129 EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER, | |
130 temp_hkcu_hive_key_.Handle())); | |
131 } | |
132 | |
133 void AutofillIeToolbarImportTest::TearDown() { | |
134 EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER, NULL)); | |
135 temp_hkcu_hive_key_.Close(); | |
136 RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS); | |
137 key.DeleteKey(L""); | |
138 } | |
139 | |
140 TEST_F(AutofillIeToolbarImportTest, TestAutofillImport) { | |
141 RegKey profile_key; | |
142 profile_key.Create(HKEY_CURRENT_USER, kProfileKey, KEY_ALL_ACCESS); | |
143 EXPECT_TRUE(profile_key.Valid()); | |
144 | |
145 CreateSubkey(&profile_key, L"0", profile1, arraysize(profile1)); | |
146 CreateSubkey(&profile_key, L"1", profile2, arraysize(profile2)); | |
147 | |
148 RegKey cc_key; | |
149 cc_key.Create(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS); | |
150 EXPECT_TRUE(cc_key.Valid()); | |
151 CreateSubkey(&cc_key, L"0", credit_card, arraysize(credit_card)); | |
152 EncryptAndWrite(&cc_key, &empty_password); | |
153 EncryptAndWrite(&cc_key, &empty_salt); | |
154 | |
155 profile_key.Close(); | |
156 cc_key.Close(); | |
157 | |
158 std::vector<AutofillProfile> profiles; | |
159 std::vector<CreditCard> credit_cards; | |
160 EXPECT_TRUE(ImportCurrentUserProfiles(&profiles, &credit_cards)); | |
161 ASSERT_EQ(2U, profiles.size()); | |
162 // The profiles are read in reverse order. | |
163 EXPECT_EQ(profile1[0].value, profiles[1].GetRawInfo(NAME_FIRST)); | |
164 EXPECT_EQ(profile1[1].value, profiles[1].GetRawInfo(NAME_MIDDLE)); | |
165 EXPECT_EQ(profile1[2].value, profiles[1].GetRawInfo(NAME_LAST)); | |
166 EXPECT_EQ(profile1[3].value, profiles[1].GetRawInfo(EMAIL_ADDRESS)); | |
167 EXPECT_EQ(profile1[4].value, profiles[1].GetRawInfo(COMPANY_NAME)); | |
168 EXPECT_EQ(profile1[7].value, | |
169 profiles[1].GetInfo(PHONE_HOME_COUNTRY_CODE, "US")); | |
170 EXPECT_EQ(profile1[6].value, profiles[1].GetInfo(PHONE_HOME_CITY_CODE, "US")); | |
171 EXPECT_EQ(L"5555555", profiles[1].GetInfo(PHONE_HOME_NUMBER, "US")); | |
172 EXPECT_EQ(L"+1 650-555-5555", | |
173 profiles[1].GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | |
174 | |
175 EXPECT_EQ(profile2[0].value, profiles[0].GetRawInfo(NAME_FIRST)); | |
176 EXPECT_EQ(profile2[1].value, profiles[0].GetRawInfo(NAME_LAST)); | |
177 EXPECT_EQ(profile2[2].value, profiles[0].GetRawInfo(EMAIL_ADDRESS)); | |
178 EXPECT_EQ(profile2[3].value, profiles[0].GetRawInfo(COMPANY_NAME)); | |
179 | |
180 ASSERT_EQ(1U, credit_cards.size()); | |
181 EXPECT_EQ(credit_card[0].value, credit_cards[0].GetRawInfo(CREDIT_CARD_NAME)); | |
182 EXPECT_EQ(L"4111111111111111", | |
183 credit_cards[0].GetRawInfo(CREDIT_CARD_NUMBER)); | |
184 EXPECT_EQ(credit_card[2].value, | |
185 credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_MONTH)); | |
186 EXPECT_EQ(credit_card[3].value, | |
187 credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
188 | |
189 // Mock password encrypted cc. | |
190 cc_key.Open(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS); | |
191 EXPECT_TRUE(cc_key.Valid()); | |
192 EncryptAndWrite(&cc_key, &protected_password); | |
193 EncryptAndWrite(&cc_key, &protected_salt); | |
194 cc_key.Close(); | |
195 | |
196 profiles.clear(); | |
197 credit_cards.clear(); | |
198 EXPECT_TRUE(ImportCurrentUserProfiles(&profiles, &credit_cards)); | |
199 // Profiles are not protected. | |
200 EXPECT_EQ(2U, profiles.size()); | |
201 // Credit cards are. | |
202 EXPECT_EQ(0U, credit_cards.size()); | |
203 } | |
204 | |
OLD | NEW |