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/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "components/autofill/content/browser/wallet/full_wallet.h"
12 #include "components/autofill/content/browser/wallet/required_action.h"
13 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
14 #include "components/autofill/core/browser/autofill_type.h"
15 #include "components/autofill/core/browser/field_types.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::ASCIIToUTF16
;
22 const char kFullWalletValidResponse
[] =
24 " \"expiration_month\":12,"
25 " \"expiration_year\":3000,"
28 " \"billing_address\":"
30 " \"phone_number\":\"phone_number\","
31 " \"postal_address\":"
33 " \"recipient_name\":\"recipient_name\","
36 " \"address_line_1\","
39 " \"locality_name\":\"locality_name\","
40 " \"dependent_locality_name\":\"dependent_locality_name\","
41 " \"administrative_area_name\":\"admin_area_name\","
42 " \"postal_code_number\":\"postal_code_number\","
43 " \"sorting_code\":\"sorting_code\","
44 " \"country_name_code\":\"US\","
45 " \"language_code\":\"language_code\""
48 " \"shipping_address\":"
50 " \"id\":\"address_id\","
51 " \"phone_number\":\"ship_phone_number\","
52 " \"postal_address\":"
54 " \"recipient_name\":\"ship_recipient_name\","
57 " \"ship_address_line_1\","
58 " \"ship_address_line_2\""
60 " \"locality_name\":\"ship_locality_name\","
61 " \"dependent_locality_name\":\"ship_dependent_locality_name\","
62 " \"administrative_area_name\":\"ship_admin_area_name\","
63 " \"postal_code_number\":\"ship_postal_code_number\","
64 " \"sorting_code\":\"ship_sorting_code\","
65 " \"country_name_code\":\"US\","
66 " \"language_code\":\"ship_language_code\""
69 " \"required_action\":"
74 const char kFullWalletMissingExpirationMonth
[] =
76 " \"expiration_year\":2012,"
79 " \"billing_address\":"
82 " \"phone_number\":\"phone_number\","
83 " \"postal_address\":"
85 " \"recipient_name\":\"recipient_name\","
88 " \"address_line_1\","
91 " \"locality_name\":\"locality_name\","
92 " \"administrative_area_name\":\"administrative_area_name\","
93 " \"postal_code_number\":\"postal_code_number\","
94 " \"country_name_code\":\"country_name_code\","
95 " \"language_code\":\"language_code\""
98 " \"shipping_address\":"
100 " \"id\":\"address_id\","
101 " \"phone_number\":\"ship_phone_number\","
102 " \"postal_address\":"
104 " \"recipient_name\":\"ship_recipient_name\","
107 " \"ship_address_line_1\","
108 " \"ship_address_line_2\""
110 " \"locality_name\":\"ship_locality_name\","
111 " \"administrative_area_name\":\"ship_admin_area_name\","
112 " \"postal_code_number\":\"ship_postal_code_number\","
113 " \"country_name_code\":\"ship_country_name_code\","
114 " \"language_code\":\"ship_language_code\""
117 " \"required_action\":"
122 const char kFullWalletMissingExpirationYear
[] =
124 " \"expiration_month\":12,"
126 " \"rest\":\"rest\","
127 " \"billing_address\":"
130 " \"phone_number\":\"phone_number\","
131 " \"postal_address\":"
133 " \"recipient_name\":\"recipient_name\","
136 " \"address_line_1\","
137 " \"address_line_2\""
139 " \"locality_name\":\"locality_name\","
140 " \"administrative_area_name\":\"administrative_area_name\","
141 " \"postal_code_number\":\"postal_code_number\","
142 " \"country_name_code\":\"country_name_code\","
143 " \"language_code\":\"language_code\""
146 " \"shipping_address\":"
148 " \"id\":\"address_id\","
149 " \"phone_number\":\"ship_phone_number\","
150 " \"postal_address\":"
152 " \"recipient_name\":\"ship_recipient_name\","
155 " \"ship_address_line_1\","
156 " \"ship_address_line_2\""
158 " \"locality_name\":\"ship_locality_name\","
159 " \"administrative_area_name\":\"ship_admin_area_name\","
160 " \"postal_code_number\":\"ship_postal_code_number\","
161 " \"country_name_code\":\"ship_country_name_code\","
162 " \"language_code\":\"ship_language_code\""
165 " \"required_action\":"
170 const char kFullWalletMissingIin
[] =
172 " \"expiration_month\":12,"
173 " \"expiration_year\":2012,"
174 " \"rest\":\"rest\","
175 " \"billing_address\":"
178 " \"phone_number\":\"phone_number\","
179 " \"postal_address\":"
181 " \"recipient_name\":\"recipient_name\","
184 " \"address_line_1\","
185 " \"address_line_2\""
187 " \"locality_name\":\"locality_name\","
188 " \"administrative_area_name\":\"administrative_area_name\","
189 " \"postal_code_number\":\"postal_code_number\","
190 " \"country_name_code\":\"country_name_code\","
191 " \"language_code\":\"language_code\""
194 " \"shipping_address\":"
196 " \"id\":\"address_id\","
197 " \"phone_number\":\"ship_phone_number\","
198 " \"postal_address\":"
200 " \"recipient_name\":\"ship_recipient_name\","
203 " \"ship_address_line_1\","
204 " \"ship_address_line_2\""
206 " \"locality_name\":\"ship_locality_name\","
207 " \"administrative_area_name\":\"ship_admin_area_name\","
208 " \"postal_code_number\":\"ship_postal_code_number\","
209 " \"country_name_code\":\"ship_country_name_code\","
210 " \"language_code\":\"language_code\""
213 " \"required_action\":"
218 const char kFullWalletMissingRest
[] =
220 " \"expiration_month\":12,"
221 " \"expiration_year\":2012,"
223 " \"billing_address\":"
226 " \"phone_number\":\"phone_number\","
227 " \"postal_address\":"
229 " \"recipient_name\":\"recipient_name\","
232 " \"address_line_1\","
233 " \"address_line_2\""
235 " \"locality_name\":\"locality_name\","
236 " \"administrative_area_name\":\"administrative_area_name\","
237 " \"postal_code_number\":\"postal_code_number\","
238 " \"country_name_code\":\"country_name_code\","
239 " \"language_code\":\"language_code\""
242 " \"shipping_address\":"
244 " \"id\":\"address_id\","
245 " \"phone_number\":\"ship_phone_number\","
246 " \"postal_address\":"
248 " \"recipient_name\":\"ship_recipient_name\","
251 " \"ship_address_line_1\","
252 " \"ship_address_line_2\""
254 " \"locality_name\":\"ship_locality_name\","
255 " \"administrative_area_name\":\"ship_admin_area_name\","
256 " \"postal_code_number\":\"ship_postal_code_number\","
257 " \"country_name_code\":\"ship_country_name_code\","
258 " \"language_code\":\"ship_language_code\""
261 " \"required_action\":"
266 const char kFullWalletMissingBillingAddress
[] =
268 " \"expiration_month\":12,"
269 " \"expiration_year\":2012,"
271 " \"rest\":\"rest\","
272 " \"shipping_address\":"
274 " \"id\":\"address_id\","
275 " \"phone_number\":\"ship_phone_number\","
276 " \"postal_address\":"
278 " \"recipient_name\":\"ship_recipient_name\","
281 " \"ship_address_line_1\","
282 " \"ship_address_line_2\""
284 " \"locality_name\":\"ship_locality_name\","
285 " \"administrative_area_name\":\"ship_admin_area_name\","
286 " \"postal_code_number\":\"ship_postal_code_number\","
287 " \"country_name_code\":\"ship_country_name_code\","
288 " \"language_code\":\"ship_language_code\""
291 " \"required_action\":"
296 const char kFullWalletWithRequiredActions
[] =
298 " \"required_action\":"
300 " \"CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS\","
301 " \"update_EXPIRATION_date\","
303 " \" REQuIrE_PHONE_NumBER\t\n\r \""
307 const char kFullWalletWithInvalidRequiredActions
[] =
309 " \"required_action\":"
311 " \" setup_wallet\","
313 " \"UPGRADE_MIN_ADDRESS\","
314 " \"INVALID_form_field\","
315 " \" \\tGAIA_auth \\n\\r\","
316 " \"PASSIVE_GAIA_AUTH\","
321 const char kFullWalletMalformedBillingAddress
[] =
323 " \"expiration_month\":12,"
324 " \"expiration_year\":2012,"
326 " \"rest\":\"rest\","
327 " \"billing_address\":"
329 " \"phone_number\":\"phone_number\","
330 " \"postal_address\":"
332 " \"recipient_name\":\"recipient_name\","
335 " \"address_line_1\","
336 " \"address_line_2\""
338 " \"locality_name\":\"locality_name\","
339 " \"administrative_area_name\":\"administrative_area_name\""
342 " \"shipping_address\":"
344 " \"id\":\"address_id\","
345 " \"phone_number\":\"ship_phone_number\","
346 " \"postal_address\":"
348 " \"recipient_name\":\"ship_recipient_name\","
351 " \"ship_address_line_1\","
352 " \"ship_address_line_2\""
354 " \"locality_name\":\"ship_locality_name\","
355 " \"administrative_area_name\":\"ship_admin_area_name\","
356 " \"postal_code_number\":\"ship_postal_code_number\","
357 " \"country_name_code\":\"ship_country_name_code\","
358 " \"language_code\":\"ship_language_code\""
361 " \"required_action\":"
366 } // anonymous namespace
371 class FullWalletTest
: public testing::Test
{
375 void SetUpDictionary(const std::string
& json
) {
376 scoped_ptr
<base::Value
> value(base::JSONReader::Read(json
));
377 ASSERT_TRUE(value
.get());
378 ASSERT_TRUE(value
->IsType(base::Value::TYPE_DICTIONARY
));
379 dict
.reset(static_cast<base::DictionaryValue
*>(value
.release()));
381 scoped_ptr
<base::DictionaryValue
> dict
;
384 TEST_F(FullWalletTest
, CreateFullWalletMissingExpirationMonth
) {
385 SetUpDictionary(kFullWalletMissingExpirationMonth
);
386 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
389 TEST_F(FullWalletTest
, CreateFullWalletMissingExpirationYear
) {
390 SetUpDictionary(kFullWalletMissingExpirationYear
);
391 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
394 TEST_F(FullWalletTest
, CreateFullWalletMissingIin
) {
395 SetUpDictionary(kFullWalletMissingIin
);
396 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
399 TEST_F(FullWalletTest
, CreateFullWalletMissingRest
) {
400 SetUpDictionary(kFullWalletMissingRest
);
401 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
404 TEST_F(FullWalletTest
, CreateFullWalletMissingBillingAddress
) {
405 SetUpDictionary(kFullWalletMissingBillingAddress
);
406 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
409 TEST_F(FullWalletTest
, CreateFullWalletMalformedBillingAddress
) {
410 SetUpDictionary(kFullWalletMalformedBillingAddress
);
411 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
414 TEST_F(FullWalletTest
, CreateFullWalletWithRequiredActions
) {
415 SetUpDictionary(kFullWalletWithRequiredActions
);
417 std::vector
<RequiredAction
> required_actions
;
418 required_actions
.push_back(CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS
);
419 required_actions
.push_back(UPDATE_EXPIRATION_DATE
);
420 required_actions
.push_back(VERIFY_CVV
);
421 required_actions
.push_back(REQUIRE_PHONE_NUMBER
);
423 FullWallet
full_wallet(-1,
427 scoped_ptr
<Address
>(),
428 scoped_ptr
<Address
>(),
430 EXPECT_EQ(full_wallet
, *FullWallet::CreateFullWallet(*dict
));
432 ASSERT_FALSE(required_actions
.empty());
433 required_actions
.pop_back();
434 FullWallet
different_required_actions(-1,
438 scoped_ptr
<Address
>(),
439 scoped_ptr
<Address
>(),
441 EXPECT_NE(full_wallet
, different_required_actions
);
444 TEST_F(FullWalletTest
, CreateFullWalletWithInvalidRequiredActions
) {
445 SetUpDictionary(kFullWalletWithInvalidRequiredActions
);
446 EXPECT_EQ(NULL
, FullWallet::CreateFullWallet(*dict
).get());
449 TEST_F(FullWalletTest
, CreateFullWallet
) {
450 SetUpDictionary(kFullWalletValidResponse
);
451 std::vector
<RequiredAction
> required_actions
;
452 FullWallet
full_wallet(12,
457 GetTestNonDefaultShippingAddress(),
459 EXPECT_EQ(full_wallet
, *FullWallet::CreateFullWallet(*dict
));
462 TEST_F(FullWalletTest
, RestLengthCorrectDecryptionTest
) {
463 std::vector
<RequiredAction
> required_actions
;
464 FullWallet
full_wallet(12,
469 GetTestShippingAddress(),
471 std::vector
<uint8
> one_time_pad
;
472 EXPECT_TRUE(base::HexStringToBytes("5F04A8704183", &one_time_pad
));
473 full_wallet
.set_one_time_pad(one_time_pad
);
474 EXPECT_EQ(ASCIIToUTF16("5285121925598459"),
475 full_wallet
.GetInfo("", AutofillType(CREDIT_CARD_NUMBER
)));
476 EXPECT_EQ(ASCIIToUTF16("989"),
478 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE
)));
481 TEST_F(FullWalletTest
, RestLengthUnderDecryptionTest
) {
482 std::vector
<RequiredAction
> required_actions
;
483 FullWallet
full_wallet(12,
488 GetTestShippingAddress(),
490 std::vector
<uint8
> one_time_pad
;
491 EXPECT_TRUE(base::HexStringToBytes("063AD35324BF", &one_time_pad
));
492 full_wallet
.set_one_time_pad(one_time_pad
);
493 EXPECT_EQ(ASCIIToUTF16("5285127106109719"),
494 full_wallet
.GetInfo("", AutofillType(CREDIT_CARD_NUMBER
)));
495 EXPECT_EQ(ASCIIToUTF16("385"),
497 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE
)));
500 TEST_F(FullWalletTest
, GetCreditCardInfo
) {
501 std::vector
<RequiredAction
> required_actions
;
502 FullWallet
full_wallet(12,
507 GetTestShippingAddress(),
510 EXPECT_EQ(ASCIIToUTF16("15"),
512 "", AutofillType(CREDIT_CARD_EXP_2_DIGIT_YEAR
)));
514 EXPECT_EQ(ASCIIToUTF16("12/15"),
516 "", AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
)));
518 EXPECT_EQ(ASCIIToUTF16("12/2015"),
520 "", AutofillType(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
)));
522 std::vector
<uint8
> one_time_pad
;
523 EXPECT_TRUE(base::HexStringToBytes("075DA779F98B", &one_time_pad
));
524 full_wallet
.set_one_time_pad(one_time_pad
);
525 EXPECT_EQ(ASCIIToUTF16("MasterCard"),
526 full_wallet
.GetInfo("", AutofillType(CREDIT_CARD_TYPE
)));
529 TEST_F(FullWalletTest
, CreateFullWalletFromClearTextData
) {
530 scoped_ptr
<FullWallet
> full_wallet
=
531 FullWallet::CreateFullWalletFromClearText(
533 "5555555555554444", "123",
534 GetTestAddress(), GetTestShippingAddress());
535 EXPECT_EQ(ASCIIToUTF16("5555555555554444"),
536 full_wallet
->GetInfo("", AutofillType(CREDIT_CARD_NUMBER
)));
537 EXPECT_EQ(ASCIIToUTF16("MasterCard"),
538 full_wallet
->GetInfo("", AutofillType(CREDIT_CARD_TYPE
)));
539 EXPECT_EQ(ASCIIToUTF16("123"),
540 full_wallet
->GetInfo(
541 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE
)));
542 EXPECT_EQ(ASCIIToUTF16("11/12"),
543 full_wallet
->GetInfo(
544 "", AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
)));
545 EXPECT_TRUE(GetTestAddress()->EqualsIgnoreID(
546 *full_wallet
->billing_address()));
547 EXPECT_TRUE(GetTestShippingAddress()->EqualsIgnoreID(
548 *full_wallet
->shipping_address()));
551 } // namespace wallet
552 } // namespace autofill