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.
5 #include "base/json/json_reader.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "components/autofill/content/browser/wallet/full_wallet.h"
11 #include "components/autofill/content/browser/wallet/required_action.h"
12 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
13 #include "components/autofill/core/browser/autofill_type.h"
14 #include "components/autofill/core/browser/field_types.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using base::ASCIIToUTF16;
21 const char kFullWalletValidResponse[] =
23 " \"expiration_month\":12,"
24 " \"expiration_year\":3000,"
27 " \"billing_address\":"
29 " \"phone_number\":\"phone_number\","
30 " \"postal_address\":"
32 " \"recipient_name\":\"recipient_name\","
35 " \"address_line_1\","
38 " \"locality_name\":\"locality_name\","
39 " \"dependent_locality_name\":\"dependent_locality_name\","
40 " \"administrative_area_name\":\"admin_area_name\","
41 " \"postal_code_number\":\"postal_code_number\","
42 " \"sorting_code\":\"sorting_code\","
43 " \"country_name_code\":\"US\","
44 " \"language_code\":\"language_code\""
47 " \"shipping_address\":"
49 " \"id\":\"address_id\","
50 " \"phone_number\":\"ship_phone_number\","
51 " \"postal_address\":"
53 " \"recipient_name\":\"ship_recipient_name\","
56 " \"ship_address_line_1\","
57 " \"ship_address_line_2\""
59 " \"locality_name\":\"ship_locality_name\","
60 " \"dependent_locality_name\":\"ship_dependent_locality_name\","
61 " \"administrative_area_name\":\"ship_admin_area_name\","
62 " \"postal_code_number\":\"ship_postal_code_number\","
63 " \"sorting_code\":\"ship_sorting_code\","
64 " \"country_name_code\":\"US\","
65 " \"language_code\":\"ship_language_code\""
68 " \"required_action\":"
73 const char kFullWalletMissingExpirationMonth[] =
75 " \"expiration_year\":2012,"
78 " \"billing_address\":"
81 " \"phone_number\":\"phone_number\","
82 " \"postal_address\":"
84 " \"recipient_name\":\"recipient_name\","
87 " \"address_line_1\","
90 " \"locality_name\":\"locality_name\","
91 " \"administrative_area_name\":\"administrative_area_name\","
92 " \"postal_code_number\":\"postal_code_number\","
93 " \"country_name_code\":\"country_name_code\","
94 " \"language_code\":\"language_code\""
97 " \"shipping_address\":"
99 " \"id\":\"address_id\","
100 " \"phone_number\":\"ship_phone_number\","
101 " \"postal_address\":"
103 " \"recipient_name\":\"ship_recipient_name\","
106 " \"ship_address_line_1\","
107 " \"ship_address_line_2\""
109 " \"locality_name\":\"ship_locality_name\","
110 " \"administrative_area_name\":\"ship_admin_area_name\","
111 " \"postal_code_number\":\"ship_postal_code_number\","
112 " \"country_name_code\":\"ship_country_name_code\","
113 " \"language_code\":\"ship_language_code\""
116 " \"required_action\":"
121 const char kFullWalletMissingExpirationYear[] =
123 " \"expiration_month\":12,"
125 " \"rest\":\"rest\","
126 " \"billing_address\":"
129 " \"phone_number\":\"phone_number\","
130 " \"postal_address\":"
132 " \"recipient_name\":\"recipient_name\","
135 " \"address_line_1\","
136 " \"address_line_2\""
138 " \"locality_name\":\"locality_name\","
139 " \"administrative_area_name\":\"administrative_area_name\","
140 " \"postal_code_number\":\"postal_code_number\","
141 " \"country_name_code\":\"country_name_code\","
142 " \"language_code\":\"language_code\""
145 " \"shipping_address\":"
147 " \"id\":\"address_id\","
148 " \"phone_number\":\"ship_phone_number\","
149 " \"postal_address\":"
151 " \"recipient_name\":\"ship_recipient_name\","
154 " \"ship_address_line_1\","
155 " \"ship_address_line_2\""
157 " \"locality_name\":\"ship_locality_name\","
158 " \"administrative_area_name\":\"ship_admin_area_name\","
159 " \"postal_code_number\":\"ship_postal_code_number\","
160 " \"country_name_code\":\"ship_country_name_code\","
161 " \"language_code\":\"ship_language_code\""
164 " \"required_action\":"
169 const char kFullWalletMissingIin[] =
171 " \"expiration_month\":12,"
172 " \"expiration_year\":2012,"
173 " \"rest\":\"rest\","
174 " \"billing_address\":"
177 " \"phone_number\":\"phone_number\","
178 " \"postal_address\":"
180 " \"recipient_name\":\"recipient_name\","
183 " \"address_line_1\","
184 " \"address_line_2\""
186 " \"locality_name\":\"locality_name\","
187 " \"administrative_area_name\":\"administrative_area_name\","
188 " \"postal_code_number\":\"postal_code_number\","
189 " \"country_name_code\":\"country_name_code\","
190 " \"language_code\":\"language_code\""
193 " \"shipping_address\":"
195 " \"id\":\"address_id\","
196 " \"phone_number\":\"ship_phone_number\","
197 " \"postal_address\":"
199 " \"recipient_name\":\"ship_recipient_name\","
202 " \"ship_address_line_1\","
203 " \"ship_address_line_2\""
205 " \"locality_name\":\"ship_locality_name\","
206 " \"administrative_area_name\":\"ship_admin_area_name\","
207 " \"postal_code_number\":\"ship_postal_code_number\","
208 " \"country_name_code\":\"ship_country_name_code\","
209 " \"language_code\":\"language_code\""
212 " \"required_action\":"
217 const char kFullWalletMissingRest[] =
219 " \"expiration_month\":12,"
220 " \"expiration_year\":2012,"
222 " \"billing_address\":"
225 " \"phone_number\":\"phone_number\","
226 " \"postal_address\":"
228 " \"recipient_name\":\"recipient_name\","
231 " \"address_line_1\","
232 " \"address_line_2\""
234 " \"locality_name\":\"locality_name\","
235 " \"administrative_area_name\":\"administrative_area_name\","
236 " \"postal_code_number\":\"postal_code_number\","
237 " \"country_name_code\":\"country_name_code\","
238 " \"language_code\":\"language_code\""
241 " \"shipping_address\":"
243 " \"id\":\"address_id\","
244 " \"phone_number\":\"ship_phone_number\","
245 " \"postal_address\":"
247 " \"recipient_name\":\"ship_recipient_name\","
250 " \"ship_address_line_1\","
251 " \"ship_address_line_2\""
253 " \"locality_name\":\"ship_locality_name\","
254 " \"administrative_area_name\":\"ship_admin_area_name\","
255 " \"postal_code_number\":\"ship_postal_code_number\","
256 " \"country_name_code\":\"ship_country_name_code\","
257 " \"language_code\":\"ship_language_code\""
260 " \"required_action\":"
265 const char kFullWalletMissingBillingAddress[] =
267 " \"expiration_month\":12,"
268 " \"expiration_year\":2012,"
270 " \"rest\":\"rest\","
271 " \"shipping_address\":"
273 " \"id\":\"address_id\","
274 " \"phone_number\":\"ship_phone_number\","
275 " \"postal_address\":"
277 " \"recipient_name\":\"ship_recipient_name\","
280 " \"ship_address_line_1\","
281 " \"ship_address_line_2\""
283 " \"locality_name\":\"ship_locality_name\","
284 " \"administrative_area_name\":\"ship_admin_area_name\","
285 " \"postal_code_number\":\"ship_postal_code_number\","
286 " \"country_name_code\":\"ship_country_name_code\","
287 " \"language_code\":\"ship_language_code\""
290 " \"required_action\":"
295 const char kFullWalletWithRequiredActions[] =
297 " \"required_action\":"
299 " \"CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS\","
300 " \"update_EXPIRATION_date\","
302 " \" REQuIrE_PHONE_NumBER\t\n\r \""
306 const char kFullWalletWithInvalidRequiredActions[] =
308 " \"required_action\":"
310 " \" setup_wallet\","
312 " \"UPGRADE_MIN_ADDRESS\","
313 " \"INVALID_form_field\","
314 " \" \\tGAIA_auth \\n\\r\","
315 " \"PASSIVE_GAIA_AUTH\","
320 const char kFullWalletMalformedBillingAddress[] =
322 " \"expiration_month\":12,"
323 " \"expiration_year\":2012,"
325 " \"rest\":\"rest\","
326 " \"billing_address\":"
328 " \"phone_number\":\"phone_number\","
329 " \"postal_address\":"
331 " \"recipient_name\":\"recipient_name\","
334 " \"address_line_1\","
335 " \"address_line_2\""
337 " \"locality_name\":\"locality_name\","
338 " \"administrative_area_name\":\"administrative_area_name\""
341 " \"shipping_address\":"
343 " \"id\":\"address_id\","
344 " \"phone_number\":\"ship_phone_number\","
345 " \"postal_address\":"
347 " \"recipient_name\":\"ship_recipient_name\","
350 " \"ship_address_line_1\","
351 " \"ship_address_line_2\""
353 " \"locality_name\":\"ship_locality_name\","
354 " \"administrative_area_name\":\"ship_admin_area_name\","
355 " \"postal_code_number\":\"ship_postal_code_number\","
356 " \"country_name_code\":\"ship_country_name_code\","
357 " \"language_code\":\"ship_language_code\""
360 " \"required_action\":"
365 } // anonymous namespace
370 class FullWalletTest : public testing::Test {
374 void SetUpDictionary(const std::string& json) {
375 scoped_ptr<base::Value> value(base::JSONReader::Read(json));
376 ASSERT_TRUE(value.get());
377 ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY));
378 dict.reset(static_cast<base::DictionaryValue*>(value.release()));
380 scoped_ptr<base::DictionaryValue> dict;
383 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationMonth) {
384 SetUpDictionary(kFullWalletMissingExpirationMonth);
385 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
388 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationYear) {
389 SetUpDictionary(kFullWalletMissingExpirationYear);
390 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
393 TEST_F(FullWalletTest, CreateFullWalletMissingIin) {
394 SetUpDictionary(kFullWalletMissingIin);
395 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
398 TEST_F(FullWalletTest, CreateFullWalletMissingRest) {
399 SetUpDictionary(kFullWalletMissingRest);
400 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
403 TEST_F(FullWalletTest, CreateFullWalletMissingBillingAddress) {
404 SetUpDictionary(kFullWalletMissingBillingAddress);
405 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
408 TEST_F(FullWalletTest, CreateFullWalletMalformedBillingAddress) {
409 SetUpDictionary(kFullWalletMalformedBillingAddress);
410 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
413 TEST_F(FullWalletTest, CreateFullWalletWithRequiredActions) {
414 SetUpDictionary(kFullWalletWithRequiredActions);
416 std::vector<RequiredAction> required_actions;
417 required_actions.push_back(CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS);
418 required_actions.push_back(UPDATE_EXPIRATION_DATE);
419 required_actions.push_back(VERIFY_CVV);
420 required_actions.push_back(REQUIRE_PHONE_NUMBER);
422 FullWallet full_wallet(-1,
426 scoped_ptr<Address>(),
427 scoped_ptr<Address>(),
429 EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict));
431 ASSERT_FALSE(required_actions.empty());
432 required_actions.pop_back();
433 FullWallet different_required_actions(-1,
437 scoped_ptr<Address>(),
438 scoped_ptr<Address>(),
440 EXPECT_NE(full_wallet, different_required_actions);
443 TEST_F(FullWalletTest, CreateFullWalletWithInvalidRequiredActions) {
444 SetUpDictionary(kFullWalletWithInvalidRequiredActions);
445 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
448 TEST_F(FullWalletTest, CreateFullWallet) {
449 SetUpDictionary(kFullWalletValidResponse);
450 std::vector<RequiredAction> required_actions;
451 FullWallet full_wallet(12,
456 GetTestNonDefaultShippingAddress(),
458 EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict));
461 TEST_F(FullWalletTest, RestLengthCorrectDecryptionTest) {
462 std::vector<RequiredAction> required_actions;
463 FullWallet full_wallet(12,
468 GetTestShippingAddress(),
470 std::vector<uint8> one_time_pad;
471 EXPECT_TRUE(base::HexStringToBytes("5F04A8704183", &one_time_pad));
472 full_wallet.set_one_time_pad(one_time_pad);
473 EXPECT_EQ(ASCIIToUTF16("5285121925598459"),
474 full_wallet.GetInfo("", AutofillType(CREDIT_CARD_NUMBER)));
475 EXPECT_EQ(ASCIIToUTF16("989"),
477 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE)));
480 TEST_F(FullWalletTest, RestLengthUnderDecryptionTest) {
481 std::vector<RequiredAction> required_actions;
482 FullWallet full_wallet(12,
487 GetTestShippingAddress(),
489 std::vector<uint8> one_time_pad;
490 EXPECT_TRUE(base::HexStringToBytes("063AD35324BF", &one_time_pad));
491 full_wallet.set_one_time_pad(one_time_pad);
492 EXPECT_EQ(ASCIIToUTF16("5285127106109719"),
493 full_wallet.GetInfo("", AutofillType(CREDIT_CARD_NUMBER)));
494 EXPECT_EQ(ASCIIToUTF16("385"),
496 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE)));
499 TEST_F(FullWalletTest, GetCreditCardInfo) {
500 std::vector<RequiredAction> required_actions;
501 FullWallet full_wallet(12,
506 GetTestShippingAddress(),
509 EXPECT_EQ(ASCIIToUTF16("15"),
511 "", AutofillType(CREDIT_CARD_EXP_2_DIGIT_YEAR)));
513 EXPECT_EQ(ASCIIToUTF16("12/15"),
515 "", AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR)));
517 EXPECT_EQ(ASCIIToUTF16("12/2015"),
519 "", AutofillType(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)));
521 std::vector<uint8> one_time_pad;
522 EXPECT_TRUE(base::HexStringToBytes("075DA779F98B", &one_time_pad));
523 full_wallet.set_one_time_pad(one_time_pad);
524 EXPECT_EQ(ASCIIToUTF16("MasterCard"),
525 full_wallet.GetInfo("", AutofillType(CREDIT_CARD_TYPE)));
528 TEST_F(FullWalletTest, CreateFullWalletFromClearTextData) {
529 scoped_ptr<FullWallet> full_wallet =
530 FullWallet::CreateFullWalletFromClearText(
532 "5555555555554444", "123",
533 GetTestAddress(), GetTestShippingAddress());
534 EXPECT_EQ(ASCIIToUTF16("5555555555554444"),
535 full_wallet->GetInfo("", AutofillType(CREDIT_CARD_NUMBER)));
536 EXPECT_EQ(ASCIIToUTF16("MasterCard"),
537 full_wallet->GetInfo("", AutofillType(CREDIT_CARD_TYPE)));
538 EXPECT_EQ(ASCIIToUTF16("123"),
539 full_wallet->GetInfo(
540 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE)));
541 EXPECT_EQ(ASCIIToUTF16("11/12"),
542 full_wallet->GetInfo(
543 "", AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR)));
544 EXPECT_TRUE(GetTestAddress()->EqualsIgnoreID(
545 *full_wallet->billing_address()));
546 EXPECT_TRUE(GetTestShippingAddress()->EqualsIgnoreID(
547 *full_wallet->shipping_address()));
550 } // namespace wallet
551 } // namespace autofill