summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/autofill_ie_toolbar_import_win_unittest.cc
blob: f1b065de45a7b3a33b38372753286990dde4f0fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/autofill_ie_toolbar_import_win.h"

#include <stddef.h>

#include "base/stl_util.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/os_crypt/os_crypt.h"
#include "components/os_crypt/os_crypt_mocker.h"
#include "testing/gtest/include/gtest/gtest.h"

#include <windows.h>

using base::win::RegKey;

namespace autofill {

// Defined in autofill_ie_toolbar_import_win.cc. Not exposed in the header file.
bool ImportCurrentUserProfiles(const std::string& app_locale,
                               std::vector<AutofillProfile>* profiles,
                               std::vector<CreditCard>* credit_cards);

namespace {

const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests";
const wchar_t kUnitTestUserOverrideSubKey[] =
    L"SOFTWARE\\Chromium Unit Tests\\HKCU Override";

const wchar_t kProfileKey[] =
    L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Profiles";
const wchar_t kCreditCardKey[] =
    L"Software\\Google\\Google Toolbar\\4.0\\Autofill\\Credit Cards";
const wchar_t kPasswordHashValue[] = L"password_hash";
const wchar_t kSaltValue[] = L"salt";

struct ValueDescription {
  wchar_t const* const value_name;
  wchar_t const* const value;
};

ValueDescription profile1[] = {
  { L"name_first", L"John" },
  { L"name_middle", L"Herman" },
  { L"name_last", L"Doe" },
  { L"email", L"jdoe@test.com" },
  { L"company_name", L"Testcompany" },
  { L"phone_home_number", L"555-5555" },
  { L"phone_home_city_code", L"650" },
  { L"phone_home_country_code", L"1" },
};

ValueDescription profile2[] = {
  { L"name_first", L"Jane" },
  { L"name_last", L"Doe" },
  { L"email", L"janedoe@test.com" },
  { L"company_name", L"Testcompany" },
};

ValueDescription credit_card[] = {
    {L"credit_card_name_full", L"Tommy Gun"},
    // "4111111111111111" encrypted:
    {L"credit_card_number",
     L"\xE53F\x19AB\xC1BF\xC9EB\xECCC\x9BDA\x8515"
     L"\xE14D\x6852\x80A8\x50A3\x4375\xFD9F\x1E07"
     L"\x790E\x7336\xB773\xAF33\x93EA\xB846\xEC89"
     L"\x265C\xD0E6\x4E23\xB75F\x7983"},
    {L"credit_card_exp_month", L"11"},
    {L"credit_card_exp_4_digit_year", L"2011"},
};

ValueDescription empty_salt = {
  kSaltValue,
  L"\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA\xB\xC\xD\xE\xF\x10\x11\x12\x13\x14"
};

ValueDescription empty_password = {
  kPasswordHashValue, L""
};

ValueDescription protected_salt = {
  kSaltValue, L"\x4854\xB906\x9C7C\x50A6\x4376\xFD9D\x1E02"
};

ValueDescription protected_password = {
  kPasswordHashValue, L"\x18B7\xE586\x459B\x7457\xA066\x3842\x71DA"
};

void EncryptAndWrite(RegKey* key, const ValueDescription* value) {
  std::string data;
  size_t data_size = (lstrlen(value->value) + 1) * sizeof(wchar_t);
  data.resize(data_size);
  memcpy(&data[0], value->value, data_size);

  std::string encrypted_data;
  OSCrypt::EncryptString(data, &encrypted_data);
  EXPECT_EQ(ERROR_SUCCESS, key->WriteValue(value->value_name,
      &encrypted_data[0], encrypted_data.size(), REG_BINARY));
}

void CreateSubkey(RegKey* key, wchar_t const* subkey_name,
                  const ValueDescription* values, size_t values_size) {
  RegKey subkey;
  subkey.Create(key->Handle(), subkey_name, KEY_ALL_ACCESS);
  EXPECT_TRUE(subkey.Valid());
  for (size_t i = 0; i < values_size; ++i)
    EncryptAndWrite(&subkey, values + i);
}

}  // namespace

class AutofillIeToolbarImportTest : public testing::Test {
 public:
  AutofillIeToolbarImportTest();

  // testing::Test method overrides:
  void SetUp() override;
  void TearDown() override;

 private:
  RegKey temp_hkcu_hive_key_;

  DISALLOW_COPY_AND_ASSIGN(AutofillIeToolbarImportTest);
};

AutofillIeToolbarImportTest::AutofillIeToolbarImportTest() {
}

void AutofillIeToolbarImportTest::SetUp() {
  OSCryptMocker::SetUp();
  temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER,
                             kUnitTestUserOverrideSubKey,
                             KEY_ALL_ACCESS);
  EXPECT_TRUE(temp_hkcu_hive_key_.Valid());
  EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER,
                                                temp_hkcu_hive_key_.Handle()));
}

void AutofillIeToolbarImportTest::TearDown() {
  EXPECT_EQ(ERROR_SUCCESS, RegOverridePredefKey(HKEY_CURRENT_USER, nullptr));
  temp_hkcu_hive_key_.Close();
  RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS);
  key.DeleteKey(L"");
  OSCryptMocker::TearDown();
}

TEST_F(AutofillIeToolbarImportTest, TestAutofillImport) {
  RegKey profile_key;
  profile_key.Create(HKEY_CURRENT_USER, kProfileKey, KEY_ALL_ACCESS);
  EXPECT_TRUE(profile_key.Valid());

  CreateSubkey(&profile_key, L"0", profile1, base::size(profile1));
  CreateSubkey(&profile_key, L"1", profile2, base::size(profile2));

  RegKey cc_key;
  cc_key.Create(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS);
  EXPECT_TRUE(cc_key.Valid());
  CreateSubkey(&cc_key, L"0", credit_card, base::size(credit_card));
  EncryptAndWrite(&cc_key, &empty_password);
  EncryptAndWrite(&cc_key, &empty_salt);

  profile_key.Close();
  cc_key.Close();

  std::vector<AutofillProfile> profiles;
  std::vector<CreditCard> credit_cards;
  EXPECT_TRUE(ImportCurrentUserProfiles("en-US", &profiles, &credit_cards));
  ASSERT_EQ(2U, profiles.size());
  // The profiles are read in reverse order.
  EXPECT_EQ(base::WideToUTF16(profile1[0].value),
            profiles[1].GetRawInfo(NAME_FIRST));
  EXPECT_EQ(base::WideToUTF16(profile1[1].value),
            profiles[1].GetRawInfo(NAME_MIDDLE));
  EXPECT_EQ(base::WideToUTF16(profile1[2].value),
            profiles[1].GetRawInfo(NAME_LAST));
  EXPECT_EQ(base::WideToUTF16(profile1[3].value),
            profiles[1].GetRawInfo(EMAIL_ADDRESS));
  EXPECT_EQ(base::WideToUTF16(profile1[4].value),
            profiles[1].GetRawInfo(COMPANY_NAME));
  EXPECT_EQ(base::WideToUTF16(profile1[7].value),
            profiles[1].GetInfo(AutofillType(PHONE_HOME_COUNTRY_CODE), "US"));
  EXPECT_EQ(base::WideToUTF16(profile1[6].value),
            profiles[1].GetInfo(AutofillType(PHONE_HOME_CITY_CODE), "US"));
  EXPECT_EQ(STRING16_LITERAL("5555555"),
            profiles[1].GetInfo(AutofillType(PHONE_HOME_NUMBER), "US"));
  EXPECT_EQ(STRING16_LITERAL("1 650-555-5555"),
            profiles[1].GetRawInfo(PHONE_HOME_WHOLE_NUMBER));

  EXPECT_EQ(base::WideToUTF16(profile2[0].value),
            profiles[0].GetRawInfo(NAME_FIRST));
  EXPECT_EQ(base::WideToUTF16(profile2[1].value),
            profiles[0].GetRawInfo(NAME_LAST));
  EXPECT_EQ(base::WideToUTF16(profile2[2].value),
            profiles[0].GetRawInfo(EMAIL_ADDRESS));
  EXPECT_EQ(base::WideToUTF16(profile2[3].value),
            profiles[0].GetRawInfo(COMPANY_NAME));

  ASSERT_EQ(1U, credit_cards.size());
  EXPECT_EQ(base::WideToUTF16(credit_card[0].value),
            credit_cards[0].GetRawInfo(CREDIT_CARD_NAME_FULL));
  EXPECT_EQ(STRING16_LITERAL("4111111111111111"),
            credit_cards[0].GetRawInfo(CREDIT_CARD_NUMBER));
  EXPECT_EQ(base::WideToUTF16(credit_card[2].value),
            credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_MONTH));
  EXPECT_EQ(base::WideToUTF16(credit_card[3].value),
            credit_cards[0].GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));

  // Mock password encrypted cc.
  cc_key.Open(HKEY_CURRENT_USER, kCreditCardKey, KEY_ALL_ACCESS);
  EXPECT_TRUE(cc_key.Valid());
  EncryptAndWrite(&cc_key, &protected_password);
  EncryptAndWrite(&cc_key, &protected_salt);
  cc_key.Close();

  profiles.clear();
  credit_cards.clear();
  EXPECT_TRUE(ImportCurrentUserProfiles("en-US", &profiles, &credit_cards));
  // Profiles are not protected.
  EXPECT_EQ(2U, profiles.size());
  // Credit cards are.
  EXPECT_EQ(0U, credit_cards.size());
}

}  // namespace autofill