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 "base/basictypes.h"
8#include "base/strings/string16.h"
9#include "base/win/registry.h"
10#include "components/autofill/core/browser/autofill_profile.h"
11#include "components/autofill/core/browser/credit_card.h"
12#include "components/autofill/core/browser/field_types.h"
13#include "components/os_crypt/os_crypt.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16using base::win::RegKey;
17
18namespace autofill {
19
20// Defined in autofill_ie_toolbar_import_win.cc. Not exposed in the header file.
21bool ImportCurrentUserProfiles(const std::string& app_locale,
22                               std::vector<AutofillProfile>* profiles,
23                               std::vector<CreditCard>* credit_cards);
24
25namespace {
26
27const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests";
28const wchar_t kUnitTestUserOverrideSubKey[] =
29    L"SOFTWARE\\Chromium Unit Tests\\HKCU Override";
30
31const wchar_t* const kProfileKey =
32    L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Profiles";
33const wchar_t* const kCreditCardKey =
34    L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Credit Cards";
35const wchar_t* const kPasswordHashValue = L"password_hash";
36const wchar_t* const kSaltValue = L"salt";
37
38struct ValueDescription {
39  wchar_t const* const value_name;
40  wchar_t const* const value;
41};
42
43ValueDescription 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
54ValueDescription 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
61ValueDescription 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
72ValueDescription 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
76ValueDescription empty_password = {
77  L"password_hash", L""
78};
79
80ValueDescription protected_salt = {
81  L"salt", L"\x4854\xB906\x9C7C\x50A6\x4376\xFD9D\x1E02"
82};
83
84ValueDescription protected_password = {
85  L"password_hash", L"\x18B7\xE586\x459B\x7457\xA066\x3842\x71DA"
86};
87
88void 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::string encrypted_data;
95  OSCrypt::EncryptString(data, &encrypted_data);
96  EXPECT_EQ(ERROR_SUCCESS, key->WriteValue(value->value_name,
97      &encrypted_data[0], encrypted_data.size(), REG_BINARY));
98}
99
100void CreateSubkey(RegKey* key, wchar_t const* subkey_name,
101                  const ValueDescription* values, size_t values_size) {
102  RegKey subkey;
103  subkey.Create(key->Handle(), subkey_name, KEY_ALL_ACCESS);
104  EXPECT_TRUE(subkey.Valid());
105  for (size_t i = 0; i < values_size; ++i)
106    EncryptAndWrite(&subkey, values + i);
107}
108
109}  // namespace
110
111class AutofillIeToolbarImportTest : public testing::Test {
112 public:
113  AutofillIeToolbarImportTest();
114
115  // testing::Test method overrides:
116  virtual void SetUp();
117  virtual void TearDown();
118
119 private:
120  RegKey temp_hkcu_hive_key_;
121
122  DISALLOW_COPY_AND_ASSIGN(AutofillIeToolbarImportTest);
123};
124
125AutofillIeToolbarImportTest::AutofillIeToolbarImportTest() {
126}
127
128void AutofillIeToolbarImportTest::SetUp() {
129  temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER,
130                             kUnitTestUserOverrideSubKey,
131                             KEY_ALL_ACCESS);
132  EXPECT_TRUE(temp_hkcu_hive_key_.Valid());
133  EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER,
134                                                temp_hkcu_hive_key_.Handle()));
135}
136
137void AutofillIeToolbarImportTest::TearDown() {
138  EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER, NULL));
139  temp_hkcu_hive_key_.Close();
140  RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS);
141  key.DeleteKey(L"");
142}
143
144TEST_F(AutofillIeToolbarImportTest, TestAutofillImport) {
145  RegKey profile_key;
146  profile_key.Create(HKEY_CURRENT_USER, kProfileKey, KEY_ALL_ACCESS);
147  EXPECT_TRUE(profile_key.Valid());
148
149  CreateSubkey(&profile_key, L"0", profile1, arraysize(profile1));
150  CreateSubkey(&profile_key, L"1", profile2, arraysize(profile2));
151
152  RegKey cc_key;
153  cc_key.Create(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS);
154  EXPECT_TRUE(cc_key.Valid());
155  CreateSubkey(&cc_key, L"0", credit_card, arraysize(credit_card));
156  EncryptAndWrite(&cc_key, &empty_password);
157  EncryptAndWrite(&cc_key, &empty_salt);
158
159  profile_key.Close();
160  cc_key.Close();
161
162  std::vector<AutofillProfile> profiles;
163  std::vector<CreditCard> credit_cards;
164  EXPECT_TRUE(ImportCurrentUserProfiles("en-US", &profiles, &credit_cards));
165  ASSERT_EQ(2U, profiles.size());
166  // The profiles are read in reverse order.
167  EXPECT_EQ(profile1[0].value, profiles[1].GetRawInfo(NAME_FIRST));
168  EXPECT_EQ(profile1[1].value, profiles[1].GetRawInfo(NAME_MIDDLE));
169  EXPECT_EQ(profile1[2].value, profiles[1].GetRawInfo(NAME_LAST));
170  EXPECT_EQ(profile1[3].value, profiles[1].GetRawInfo(EMAIL_ADDRESS));
171  EXPECT_EQ(profile1[4].value, profiles[1].GetRawInfo(COMPANY_NAME));
172  EXPECT_EQ(profile1[7].value,
173            profiles[1].GetInfo(AutofillType(PHONE_HOME_COUNTRY_CODE), "US"));
174  EXPECT_EQ(profile1[6].value,
175            profiles[1].GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
176  EXPECT_EQ(L"5555555",
177            profiles[1].GetInfo(AutofillType(PHONE_HOME_NUMBER), "US"));
178  EXPECT_EQ(L"1 650-555-5555", profiles[1].GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
179
180  EXPECT_EQ(profile2[0].value, profiles[0].GetRawInfo(NAME_FIRST));
181  EXPECT_EQ(profile2[1].value, profiles[0].GetRawInfo(NAME_LAST));
182  EXPECT_EQ(profile2[2].value, profiles[0].GetRawInfo(EMAIL_ADDRESS));
183  EXPECT_EQ(profile2[3].value, profiles[0].GetRawInfo(COMPANY_NAME));
184
185  ASSERT_EQ(1U, credit_cards.size());
186  EXPECT_EQ(credit_card[0].value, credit_cards[0].GetRawInfo(CREDIT_CARD_NAME));
187  EXPECT_EQ(L"4111111111111111",
188            credit_cards[0].GetRawInfo(CREDIT_CARD_NUMBER));
189  EXPECT_EQ(credit_card[2].value,
190            credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_MONTH));
191  EXPECT_EQ(credit_card[3].value,
192            credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
193
194  // Mock password encrypted cc.
195  cc_key.Open(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS);
196  EXPECT_TRUE(cc_key.Valid());
197  EncryptAndWrite(&cc_key, &protected_password);
198  EncryptAndWrite(&cc_key, &protected_salt);
199  cc_key.Close();
200
201  profiles.clear();
202  credit_cards.clear();
203  EXPECT_TRUE(ImportCurrentUserProfiles("en-US", &profiles, &credit_cards));
204  // Profiles are not protected.
205  EXPECT_EQ(2U, profiles.size());
206  // Credit cards are.
207  EXPECT_EQ(0U, credit_cards.size());
208}
209
210}  // namespace autofill
211