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 "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" 17 18 using base::ASCIIToUTF16; 19 20 namespace { 21 22 const char kFullWalletValidResponse[] = 23 "{" 24 " \"expiration_month\":12," 25 " \"expiration_year\":3000," 26 " \"iin\":\"iin\"," 27 " \"rest\":\"rest\"," 28 " \"billing_address\":" 29 " {" 30 " \"phone_number\":\"phone_number\"," 31 " \"postal_address\":" 32 " {" 33 " \"recipient_name\":\"recipient_name\"," 34 " \"address_line\":" 35 " [" 36 " \"address_line_1\"," 37 " \"address_line_2\"" 38 " ]," 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\"" 46 " }" 47 " }," 48 " \"shipping_address\":" 49 " {" 50 " \"id\":\"address_id\"," 51 " \"phone_number\":\"ship_phone_number\"," 52 " \"postal_address\":" 53 " {" 54 " \"recipient_name\":\"ship_recipient_name\"," 55 " \"address_line\":" 56 " [" 57 " \"ship_address_line_1\"," 58 " \"ship_address_line_2\"" 59 " ]," 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\"" 67 " }" 68 " }," 69 " \"required_action\":" 70 " [" 71 " ]" 72 "}"; 73 74 const char kFullWalletMissingExpirationMonth[] = 75 "{" 76 " \"expiration_year\":2012," 77 " \"iin\":\"iin\"," 78 " \"rest\":\"rest\"," 79 " \"billing_address\":" 80 " {" 81 " \"id\":\"id\"," 82 " \"phone_number\":\"phone_number\"," 83 " \"postal_address\":" 84 " {" 85 " \"recipient_name\":\"recipient_name\"," 86 " \"address_line\":" 87 " [" 88 " \"address_line_1\"," 89 " \"address_line_2\"" 90 " ]," 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\"" 96 " }" 97 " }," 98 " \"shipping_address\":" 99 " {" 100 " \"id\":\"address_id\"," 101 " \"phone_number\":\"ship_phone_number\"," 102 " \"postal_address\":" 103 " {" 104 " \"recipient_name\":\"ship_recipient_name\"," 105 " \"address_line\":" 106 " [" 107 " \"ship_address_line_1\"," 108 " \"ship_address_line_2\"" 109 " ]," 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\"" 115 " }" 116 " }," 117 " \"required_action\":" 118 " [" 119 " ]" 120 "}"; 121 122 const char kFullWalletMissingExpirationYear[] = 123 "{" 124 " \"expiration_month\":12," 125 " \"iin\":\"iin\"," 126 " \"rest\":\"rest\"," 127 " \"billing_address\":" 128 " {" 129 " \"id\":\"id\"," 130 " \"phone_number\":\"phone_number\"," 131 " \"postal_address\":" 132 " {" 133 " \"recipient_name\":\"recipient_name\"," 134 " \"address_line\":" 135 " [" 136 " \"address_line_1\"," 137 " \"address_line_2\"" 138 " ]," 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\"" 144 " }" 145 " }," 146 " \"shipping_address\":" 147 " {" 148 " \"id\":\"address_id\"," 149 " \"phone_number\":\"ship_phone_number\"," 150 " \"postal_address\":" 151 " {" 152 " \"recipient_name\":\"ship_recipient_name\"," 153 " \"address_line\":" 154 " [" 155 " \"ship_address_line_1\"," 156 " \"ship_address_line_2\"" 157 " ]," 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\"" 163 " }" 164 " }," 165 " \"required_action\":" 166 " [" 167 " ]" 168 "}"; 169 170 const char kFullWalletMissingIin[] = 171 "{" 172 " \"expiration_month\":12," 173 " \"expiration_year\":2012," 174 " \"rest\":\"rest\"," 175 " \"billing_address\":" 176 " {" 177 " \"id\":\"id\"," 178 " \"phone_number\":\"phone_number\"," 179 " \"postal_address\":" 180 " {" 181 " \"recipient_name\":\"recipient_name\"," 182 " \"address_line\":" 183 " [" 184 " \"address_line_1\"," 185 " \"address_line_2\"" 186 " ]," 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\"" 192 " }" 193 " }," 194 " \"shipping_address\":" 195 " {" 196 " \"id\":\"address_id\"," 197 " \"phone_number\":\"ship_phone_number\"," 198 " \"postal_address\":" 199 " {" 200 " \"recipient_name\":\"ship_recipient_name\"," 201 " \"address_line\":" 202 " [" 203 " \"ship_address_line_1\"," 204 " \"ship_address_line_2\"" 205 " ]," 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\"" 211 " }" 212 " }," 213 " \"required_action\":" 214 " [" 215 " ]" 216 "}"; 217 218 const char kFullWalletMissingRest[] = 219 "{" 220 " \"expiration_month\":12," 221 " \"expiration_year\":2012," 222 " \"iin\":\"iin\"," 223 " \"billing_address\":" 224 " {" 225 " \"id\":\"id\"," 226 " \"phone_number\":\"phone_number\"," 227 " \"postal_address\":" 228 " {" 229 " \"recipient_name\":\"recipient_name\"," 230 " \"address_line\":" 231 " [" 232 " \"address_line_1\"," 233 " \"address_line_2\"" 234 " ]," 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\"" 240 " }" 241 " }," 242 " \"shipping_address\":" 243 " {" 244 " \"id\":\"address_id\"," 245 " \"phone_number\":\"ship_phone_number\"," 246 " \"postal_address\":" 247 " {" 248 " \"recipient_name\":\"ship_recipient_name\"," 249 " \"address_line\":" 250 " [" 251 " \"ship_address_line_1\"," 252 " \"ship_address_line_2\"" 253 " ]," 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\"" 259 " }" 260 " }," 261 " \"required_action\":" 262 " [" 263 " ]" 264 "}"; 265 266 const char kFullWalletMissingBillingAddress[] = 267 "{" 268 " \"expiration_month\":12," 269 " \"expiration_year\":2012," 270 " \"iin\":\"iin\"," 271 " \"rest\":\"rest\"," 272 " \"shipping_address\":" 273 " {" 274 " \"id\":\"address_id\"," 275 " \"phone_number\":\"ship_phone_number\"," 276 " \"postal_address\":" 277 " {" 278 " \"recipient_name\":\"ship_recipient_name\"," 279 " \"address_line\":" 280 " [" 281 " \"ship_address_line_1\"," 282 " \"ship_address_line_2\"" 283 " ]," 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\"" 289 " }" 290 " }," 291 " \"required_action\":" 292 " [" 293 " ]" 294 "}"; 295 296 const char kFullWalletWithRequiredActions[] = 297 "{" 298 " \"required_action\":" 299 " [" 300 " \"CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS\"," 301 " \"update_EXPIRATION_date\"," 302 " \"verify_CVV\"," 303 " \" REQuIrE_PHONE_NumBER\t\n\r \"" 304 " ]" 305 "}"; 306 307 const char kFullWalletWithInvalidRequiredActions[] = 308 "{" 309 " \"required_action\":" 310 " [" 311 " \" setup_wallet\"," 312 " \"AcCePt_ToS \"," 313 " \"UPGRADE_MIN_ADDRESS\"," 314 " \"INVALID_form_field\"," 315 " \" \\tGAIA_auth \\n\\r\"," 316 " \"PASSIVE_GAIA_AUTH\"," 317 " \" \"" 318 " ]" 319 "}"; 320 321 const char kFullWalletMalformedBillingAddress[] = 322 "{" 323 " \"expiration_month\":12," 324 " \"expiration_year\":2012," 325 " \"iin\":\"iin\"," 326 " \"rest\":\"rest\"," 327 " \"billing_address\":" 328 " {" 329 " \"phone_number\":\"phone_number\"," 330 " \"postal_address\":" 331 " {" 332 " \"recipient_name\":\"recipient_name\"," 333 " \"address_line\":" 334 " [" 335 " \"address_line_1\"," 336 " \"address_line_2\"" 337 " ]," 338 " \"locality_name\":\"locality_name\"," 339 " \"administrative_area_name\":\"administrative_area_name\"" 340 " }" 341 " }," 342 " \"shipping_address\":" 343 " {" 344 " \"id\":\"address_id\"," 345 " \"phone_number\":\"ship_phone_number\"," 346 " \"postal_address\":" 347 " {" 348 " \"recipient_name\":\"ship_recipient_name\"," 349 " \"address_line\":" 350 " [" 351 " \"ship_address_line_1\"," 352 " \"ship_address_line_2\"" 353 " ]," 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\"" 359 " }" 360 " }," 361 " \"required_action\":" 362 " [" 363 " ]" 364 "}"; 365 366 } // anonymous namespace 367 368 namespace autofill { 369 namespace wallet { 370 371 class FullWalletTest : public testing::Test { 372 public: 373 FullWalletTest() {} 374 protected: 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())); 380 } 381 scoped_ptr<base::DictionaryValue> dict; 382 }; 383 384 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationMonth) { 385 SetUpDictionary(kFullWalletMissingExpirationMonth); 386 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 387 } 388 389 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationYear) { 390 SetUpDictionary(kFullWalletMissingExpirationYear); 391 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 392 } 393 394 TEST_F(FullWalletTest, CreateFullWalletMissingIin) { 395 SetUpDictionary(kFullWalletMissingIin); 396 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 397 } 398 399 TEST_F(FullWalletTest, CreateFullWalletMissingRest) { 400 SetUpDictionary(kFullWalletMissingRest); 401 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 402 } 403 404 TEST_F(FullWalletTest, CreateFullWalletMissingBillingAddress) { 405 SetUpDictionary(kFullWalletMissingBillingAddress); 406 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 407 } 408 409 TEST_F(FullWalletTest, CreateFullWalletMalformedBillingAddress) { 410 SetUpDictionary(kFullWalletMalformedBillingAddress); 411 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 412 } 413 414 TEST_F(FullWalletTest, CreateFullWalletWithRequiredActions) { 415 SetUpDictionary(kFullWalletWithRequiredActions); 416 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); 422 423 FullWallet full_wallet(-1, 424 -1, 425 std::string(), 426 std::string(), 427 scoped_ptr<Address>(), 428 scoped_ptr<Address>(), 429 required_actions); 430 EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict)); 431 432 ASSERT_FALSE(required_actions.empty()); 433 required_actions.pop_back(); 434 FullWallet different_required_actions(-1, 435 -1, 436 std::string(), 437 std::string(), 438 scoped_ptr<Address>(), 439 scoped_ptr<Address>(), 440 required_actions); 441 EXPECT_NE(full_wallet, different_required_actions); 442 } 443 444 TEST_F(FullWalletTest, CreateFullWalletWithInvalidRequiredActions) { 445 SetUpDictionary(kFullWalletWithInvalidRequiredActions); 446 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); 447 } 448 449 TEST_F(FullWalletTest, CreateFullWallet) { 450 SetUpDictionary(kFullWalletValidResponse); 451 std::vector<RequiredAction> required_actions; 452 FullWallet full_wallet(12, 453 3000, 454 "iin", 455 "rest", 456 GetTestAddress(), 457 GetTestNonDefaultShippingAddress(), 458 required_actions); 459 EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict)); 460 } 461 462 TEST_F(FullWalletTest, RestLengthCorrectDecryptionTest) { 463 std::vector<RequiredAction> required_actions; 464 FullWallet full_wallet(12, 465 2012, 466 "528512", 467 "5ec4feecf9d6", 468 GetTestAddress(), 469 GetTestShippingAddress(), 470 required_actions); 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"), 477 full_wallet.GetInfo( 478 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE))); 479 } 480 481 TEST_F(FullWalletTest, RestLengthUnderDecryptionTest) { 482 std::vector<RequiredAction> required_actions; 483 FullWallet full_wallet(12, 484 2012, 485 "528512", 486 "4c567667e6", 487 GetTestAddress(), 488 GetTestShippingAddress(), 489 required_actions); 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"), 496 full_wallet.GetInfo( 497 "", AutofillType(CREDIT_CARD_VERIFICATION_CODE))); 498 } 499 500 TEST_F(FullWalletTest, GetCreditCardInfo) { 501 std::vector<RequiredAction> required_actions; 502 FullWallet full_wallet(12, 503 2015, 504 "528512", 505 "1a068673eb0", 506 GetTestAddress(), 507 GetTestShippingAddress(), 508 required_actions); 509 510 EXPECT_EQ(ASCIIToUTF16("15"), 511 full_wallet.GetInfo( 512 "", AutofillType(CREDIT_CARD_EXP_2_DIGIT_YEAR))); 513 514 EXPECT_EQ(ASCIIToUTF16("12/15"), 515 full_wallet.GetInfo( 516 "", AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR))); 517 518 EXPECT_EQ(ASCIIToUTF16("12/2015"), 519 full_wallet.GetInfo( 520 "", AutofillType(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR))); 521 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))); 527 } 528 529 TEST_F(FullWalletTest, CreateFullWalletFromClearTextData) { 530 scoped_ptr<FullWallet> full_wallet = 531 FullWallet::CreateFullWalletFromClearText( 532 11, 2012, 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())); 549 } 550 551 } // namespace wallet 552 } // namespace autofill 553