Home | History | Annotate | Download | only in wallet
      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 namespace {
     19 
     20 const char kFullWalletValidResponse[] =
     21     "{"
     22     "  \"expiration_month\":12,"
     23     "  \"expiration_year\":3000,"
     24     "  \"iin\":\"iin\","
     25     "  \"rest\":\"rest\","
     26     "  \"billing_address\":"
     27     "  {"
     28     "    \"phone_number\":\"phone_number\","
     29     "    \"postal_address\":"
     30     "    {"
     31     "      \"recipient_name\":\"recipient_name\","
     32     "      \"address_line\":"
     33     "      ["
     34     "        \"address_line_1\","
     35     "        \"address_line_2\""
     36     "      ],"
     37     "      \"locality_name\":\"locality_name\","
     38     "      \"administrative_area_name\":\"admin_area_name\","
     39     "      \"postal_code_number\":\"postal_code_number\","
     40     "      \"country_name_code\":\"US\""
     41     "    }"
     42     "  },"
     43     "  \"shipping_address\":"
     44     "  {"
     45     "    \"id\":\"address_id\","
     46     "    \"phone_number\":\"ship_phone_number\","
     47     "    \"postal_address\":"
     48     "    {"
     49     "      \"recipient_name\":\"ship_recipient_name\","
     50     "      \"address_line\":"
     51     "      ["
     52     "        \"ship_address_line_1\","
     53     "        \"ship_address_line_2\""
     54     "      ],"
     55     "      \"locality_name\":\"ship_locality_name\","
     56     "      \"administrative_area_name\":\"ship_admin_area_name\","
     57     "      \"postal_code_number\":\"ship_postal_code_number\","
     58     "      \"country_name_code\":\"US\""
     59     "    }"
     60     "  },"
     61     "  \"required_action\":"
     62     "  ["
     63     "  ]"
     64     "}";
     65 
     66 const char kFullWalletMissingExpirationMonth[] =
     67     "{"
     68     "  \"expiration_year\":2012,"
     69     "  \"iin\":\"iin\","
     70     "  \"rest\":\"rest\","
     71     "  \"billing_address\":"
     72     "  {"
     73     "    \"id\":\"id\","
     74     "    \"phone_number\":\"phone_number\","
     75     "    \"postal_address\":"
     76     "    {"
     77     "      \"recipient_name\":\"recipient_name\","
     78     "      \"address_line\":"
     79     "      ["
     80     "        \"address_line_1\","
     81     "        \"address_line_2\""
     82     "      ],"
     83     "      \"locality_name\":\"locality_name\","
     84     "      \"administrative_area_name\":\"administrative_area_name\","
     85     "      \"postal_code_number\":\"postal_code_number\","
     86     "      \"country_name_code\":\"country_name_code\""
     87     "    }"
     88     "  },"
     89     "  \"shipping_address\":"
     90     "  {"
     91     "    \"id\":\"address_id\","
     92     "    \"phone_number\":\"ship_phone_number\","
     93     "    \"postal_address\":"
     94     "    {"
     95     "      \"recipient_name\":\"ship_recipient_name\","
     96     "      \"address_line\":"
     97     "      ["
     98     "        \"ship_address_line_1\","
     99     "        \"ship_address_line_2\""
    100     "      ],"
    101     "      \"locality_name\":\"ship_locality_name\","
    102     "      \"administrative_area_name\":\"ship_admin_area_name\","
    103     "      \"postal_code_number\":\"ship_postal_code_number\","
    104     "      \"country_name_code\":\"ship_country_name_code\""
    105     "    }"
    106     "  },"
    107     "  \"required_action\":"
    108     "  ["
    109     "  ]"
    110     "}";
    111 
    112 const char kFullWalletMissingExpirationYear[] =
    113     "{"
    114     "  \"expiration_month\":12,"
    115     "  \"iin\":\"iin\","
    116     "  \"rest\":\"rest\","
    117     "  \"billing_address\":"
    118     "  {"
    119     "    \"id\":\"id\","
    120     "    \"phone_number\":\"phone_number\","
    121     "    \"postal_address\":"
    122     "    {"
    123     "      \"recipient_name\":\"recipient_name\","
    124     "      \"address_line\":"
    125     "      ["
    126     "        \"address_line_1\","
    127     "        \"address_line_2\""
    128     "      ],"
    129     "      \"locality_name\":\"locality_name\","
    130     "      \"administrative_area_name\":\"administrative_area_name\","
    131     "      \"postal_code_number\":\"postal_code_number\","
    132     "      \"country_name_code\":\"country_name_code\""
    133     "    }"
    134     "  },"
    135     "  \"shipping_address\":"
    136     "  {"
    137     "    \"id\":\"address_id\","
    138     "    \"phone_number\":\"ship_phone_number\","
    139     "    \"postal_address\":"
    140     "    {"
    141     "      \"recipient_name\":\"ship_recipient_name\","
    142     "      \"address_line\":"
    143     "      ["
    144     "        \"ship_address_line_1\","
    145     "        \"ship_address_line_2\""
    146     "      ],"
    147     "      \"locality_name\":\"ship_locality_name\","
    148     "      \"administrative_area_name\":\"ship_admin_area_name\","
    149     "      \"postal_code_number\":\"ship_postal_code_number\","
    150     "      \"country_name_code\":\"ship_country_name_code\""
    151     "    }"
    152     "  },"
    153     "  \"required_action\":"
    154     "  ["
    155     "  ]"
    156     "}";
    157 
    158 const char kFullWalletMissingIin[] =
    159     "{"
    160     "  \"expiration_month\":12,"
    161     "  \"expiration_year\":2012,"
    162     "  \"rest\":\"rest\","
    163     "  \"billing_address\":"
    164     "  {"
    165     "    \"id\":\"id\","
    166     "    \"phone_number\":\"phone_number\","
    167     "    \"postal_address\":"
    168     "    {"
    169     "      \"recipient_name\":\"recipient_name\","
    170     "      \"address_line\":"
    171     "      ["
    172     "        \"address_line_1\","
    173     "        \"address_line_2\""
    174     "      ],"
    175     "      \"locality_name\":\"locality_name\","
    176     "      \"administrative_area_name\":\"administrative_area_name\","
    177     "      \"postal_code_number\":\"postal_code_number\","
    178     "      \"country_name_code\":\"country_name_code\""
    179     "    }"
    180     "  },"
    181     "  \"shipping_address\":"
    182     "  {"
    183     "    \"id\":\"address_id\","
    184     "    \"phone_number\":\"ship_phone_number\","
    185     "    \"postal_address\":"
    186     "    {"
    187     "      \"recipient_name\":\"ship_recipient_name\","
    188     "      \"address_line\":"
    189     "      ["
    190     "        \"ship_address_line_1\","
    191     "        \"ship_address_line_2\""
    192     "      ],"
    193     "      \"locality_name\":\"ship_locality_name\","
    194     "      \"administrative_area_name\":\"ship_admin_area_name\","
    195     "      \"postal_code_number\":\"ship_postal_code_number\","
    196     "      \"country_name_code\":\"ship_country_name_code\""
    197     "    }"
    198     "  },"
    199     "  \"required_action\":"
    200     "  ["
    201     "  ]"
    202     "}";
    203 
    204 const char kFullWalletMissingRest[] =
    205     "{"
    206     "  \"expiration_month\":12,"
    207     "  \"expiration_year\":2012,"
    208     "  \"iin\":\"iin\","
    209     "  \"billing_address\":"
    210     "  {"
    211     "    \"id\":\"id\","
    212     "    \"phone_number\":\"phone_number\","
    213     "    \"postal_address\":"
    214     "    {"
    215     "      \"recipient_name\":\"recipient_name\","
    216     "      \"address_line\":"
    217     "      ["
    218     "        \"address_line_1\","
    219     "        \"address_line_2\""
    220     "      ],"
    221     "      \"locality_name\":\"locality_name\","
    222     "      \"administrative_area_name\":\"administrative_area_name\","
    223     "      \"postal_code_number\":\"postal_code_number\","
    224     "      \"country_name_code\":\"country_name_code\""
    225     "    }"
    226     "  },"
    227     "  \"shipping_address\":"
    228     "  {"
    229     "    \"id\":\"address_id\","
    230     "    \"phone_number\":\"ship_phone_number\","
    231     "    \"postal_address\":"
    232     "    {"
    233     "      \"recipient_name\":\"ship_recipient_name\","
    234     "      \"address_line\":"
    235     "      ["
    236     "        \"ship_address_line_1\","
    237     "        \"ship_address_line_2\""
    238     "      ],"
    239     "      \"locality_name\":\"ship_locality_name\","
    240     "      \"administrative_area_name\":\"ship_admin_area_name\","
    241     "      \"postal_code_number\":\"ship_postal_code_number\","
    242     "      \"country_name_code\":\"ship_country_name_code\""
    243     "    }"
    244     "  },"
    245     "  \"required_action\":"
    246     "  ["
    247     "  ]"
    248     "}";
    249 
    250 const char kFullWalletMissingBillingAddress[] =
    251     "{"
    252     "  \"expiration_month\":12,"
    253     "  \"expiration_year\":2012,"
    254     "  \"iin\":\"iin\","
    255     "  \"rest\":\"rest\","
    256     "  \"shipping_address\":"
    257     "  {"
    258     "    \"id\":\"address_id\","
    259     "    \"phone_number\":\"ship_phone_number\","
    260     "    \"postal_address\":"
    261     "    {"
    262     "      \"recipient_name\":\"ship_recipient_name\","
    263     "      \"address_line\":"
    264     "      ["
    265     "        \"ship_address_line_1\","
    266     "        \"ship_address_line_2\""
    267     "      ],"
    268     "      \"locality_name\":\"ship_locality_name\","
    269     "      \"administrative_area_name\":\"ship_admin_area_name\","
    270     "      \"postal_code_number\":\"ship_postal_code_number\","
    271     "      \"country_name_code\":\"ship_country_name_code\""
    272     "    }"
    273     "  },"
    274     "  \"required_action\":"
    275     "  ["
    276     "  ]"
    277     "}";
    278 
    279 const char kFullWalletWithRequiredActions[] =
    280     "{"
    281     "  \"required_action\":"
    282     "  ["
    283     "    \"CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS\","
    284     "    \"update_EXPIRATION_date\","
    285     "    \"verify_CVV\","
    286     "    \" REQuIrE_PHONE_NumBER\t\n\r \""
    287     "  ]"
    288     "}";
    289 
    290 const char kFullWalletWithInvalidRequiredActions[] =
    291     "{"
    292     "  \"required_action\":"
    293     "  ["
    294     "    \"  setup_wallet\","
    295     "    \"AcCePt_ToS  \","
    296     "    \"UPGRADE_MIN_ADDRESS\","
    297     "    \"INVALID_form_field\","
    298     "    \"   \\tGAIA_auth   \\n\\r\","
    299     "    \"PASSIVE_GAIA_AUTH\","
    300     "    \"  \""
    301     "  ]"
    302     "}";
    303 
    304 const char kFullWalletMalformedBillingAddress[] =
    305     "{"
    306     "  \"expiration_month\":12,"
    307     "  \"expiration_year\":2012,"
    308     "  \"iin\":\"iin\","
    309     "  \"rest\":\"rest\","
    310     "  \"billing_address\":"
    311     "  {"
    312     "    \"phone_number\":\"phone_number\","
    313     "    \"postal_address\":"
    314     "    {"
    315     "      \"recipient_name\":\"recipient_name\","
    316     "      \"address_line\":"
    317     "      ["
    318     "        \"address_line_1\","
    319     "        \"address_line_2\""
    320     "      ],"
    321     "      \"locality_name\":\"locality_name\","
    322     "      \"administrative_area_name\":\"administrative_area_name\""
    323     "    }"
    324     "  },"
    325     "  \"shipping_address\":"
    326     "  {"
    327     "    \"id\":\"address_id\","
    328     "    \"phone_number\":\"ship_phone_number\","
    329     "    \"postal_address\":"
    330     "    {"
    331     "      \"recipient_name\":\"ship_recipient_name\","
    332     "      \"address_line\":"
    333     "      ["
    334     "        \"ship_address_line_1\","
    335     "        \"ship_address_line_2\""
    336     "      ],"
    337     "      \"locality_name\":\"ship_locality_name\","
    338     "      \"administrative_area_name\":\"ship_admin_area_name\","
    339     "      \"postal_code_number\":\"ship_postal_code_number\","
    340     "      \"country_name_code\":\"ship_country_name_code\""
    341     "    }"
    342     "  },"
    343     "  \"required_action\":"
    344     "  ["
    345     "  ]"
    346     "}";
    347 
    348 }  // anonymous namespace
    349 
    350 namespace autofill {
    351 namespace wallet {
    352 
    353 class FullWalletTest : public testing::Test {
    354  public:
    355   FullWalletTest() {}
    356  protected:
    357   void SetUpDictionary(const std::string& json) {
    358     scoped_ptr<Value> value(base::JSONReader::Read(json));
    359     ASSERT_TRUE(value.get());
    360     ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY));
    361     dict.reset(static_cast<DictionaryValue*>(value.release()));
    362   }
    363   scoped_ptr<DictionaryValue> dict;
    364 };
    365 
    366 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationMonth) {
    367   SetUpDictionary(kFullWalletMissingExpirationMonth);
    368   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    369 }
    370 
    371 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationYear) {
    372   SetUpDictionary(kFullWalletMissingExpirationYear);
    373   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    374 }
    375 
    376 TEST_F(FullWalletTest, CreateFullWalletMissingIin) {
    377   SetUpDictionary(kFullWalletMissingIin);
    378   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    379 }
    380 
    381 TEST_F(FullWalletTest, CreateFullWalletMissingRest) {
    382   SetUpDictionary(kFullWalletMissingRest);
    383   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    384 }
    385 
    386 TEST_F(FullWalletTest, CreateFullWalletMissingBillingAddress) {
    387   SetUpDictionary(kFullWalletMissingBillingAddress);
    388   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    389 }
    390 
    391 TEST_F(FullWalletTest, CreateFullWalletMalformedBillingAddress) {
    392   SetUpDictionary(kFullWalletMalformedBillingAddress);
    393   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    394 }
    395 
    396 TEST_F(FullWalletTest, CreateFullWalletWithRequiredActions) {
    397   SetUpDictionary(kFullWalletWithRequiredActions);
    398 
    399   std::vector<RequiredAction> required_actions;
    400   required_actions.push_back(CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS);
    401   required_actions.push_back(UPDATE_EXPIRATION_DATE);
    402   required_actions.push_back(VERIFY_CVV);
    403   required_actions.push_back(REQUIRE_PHONE_NUMBER);
    404 
    405   FullWallet full_wallet(-1,
    406                          -1,
    407                          std::string(),
    408                          std::string(),
    409                          scoped_ptr<Address>(),
    410                          scoped_ptr<Address>(),
    411                          required_actions);
    412   EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict));
    413 
    414   ASSERT_FALSE(required_actions.empty());
    415   required_actions.pop_back();
    416   FullWallet different_required_actions(-1,
    417                                         -1,
    418                                         std::string(),
    419                                         std::string(),
    420                                         scoped_ptr<Address>(),
    421                                         scoped_ptr<Address>(),
    422                                         required_actions);
    423   EXPECT_NE(full_wallet, different_required_actions);
    424 }
    425 
    426 TEST_F(FullWalletTest, CreateFullWalletWithInvalidRequiredActions) {
    427   SetUpDictionary(kFullWalletWithInvalidRequiredActions);
    428   EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get());
    429 }
    430 
    431 TEST_F(FullWalletTest, CreateFullWallet) {
    432   SetUpDictionary(kFullWalletValidResponse);
    433   std::vector<RequiredAction> required_actions;
    434   FullWallet full_wallet(12,
    435                          3000,
    436                          "iin",
    437                          "rest",
    438                          GetTestAddress(),
    439                          GetTestNonDefaultShippingAddress(),
    440                          required_actions);
    441   EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict));
    442 }
    443 
    444 TEST_F(FullWalletTest, RestLengthCorrectDecryptionTest) {
    445   std::vector<RequiredAction> required_actions;
    446   FullWallet full_wallet(12,
    447                          2012,
    448                          "528512",
    449                          "5ec4feecf9d6",
    450                          GetTestAddress(),
    451                          GetTestShippingAddress(),
    452                          required_actions);
    453   std::vector<uint8> one_time_pad;
    454   EXPECT_TRUE(base::HexStringToBytes("5F04A8704183", &one_time_pad));
    455   full_wallet.set_one_time_pad(one_time_pad);
    456   EXPECT_EQ(ASCIIToUTF16("5285121925598459"),
    457             full_wallet.GetInfo(AutofillType(CREDIT_CARD_NUMBER)));
    458   EXPECT_EQ(ASCIIToUTF16("989"),
    459             full_wallet.GetInfo(AutofillType(CREDIT_CARD_VERIFICATION_CODE)));
    460 }
    461 
    462 TEST_F(FullWalletTest, RestLengthUnderDecryptionTest) {
    463   std::vector<RequiredAction> required_actions;
    464   FullWallet full_wallet(12,
    465                          2012,
    466                          "528512",
    467                          "4c567667e6",
    468                          GetTestAddress(),
    469                          GetTestShippingAddress(),
    470                          required_actions);
    471   std::vector<uint8> one_time_pad;
    472   EXPECT_TRUE(base::HexStringToBytes("063AD35324BF", &one_time_pad));
    473   full_wallet.set_one_time_pad(one_time_pad);
    474   EXPECT_EQ(ASCIIToUTF16("5285127106109719"),
    475             full_wallet.GetInfo(AutofillType(CREDIT_CARD_NUMBER)));
    476   EXPECT_EQ(ASCIIToUTF16("385"),
    477             full_wallet.GetInfo(AutofillType(CREDIT_CARD_VERIFICATION_CODE)));
    478 }
    479 
    480 TEST_F(FullWalletTest, GetCreditCardInfo) {
    481   std::vector<RequiredAction> required_actions;
    482   FullWallet full_wallet(12,
    483                          2015,
    484                          "528512",
    485                          "1a068673eb0",
    486                          GetTestAddress(),
    487                          GetTestShippingAddress(),
    488                          required_actions);
    489 
    490   EXPECT_EQ(ASCIIToUTF16("15"),
    491             full_wallet.GetInfo(AutofillType(CREDIT_CARD_EXP_2_DIGIT_YEAR)));
    492 
    493   EXPECT_EQ(ASCIIToUTF16("12/15"),
    494             full_wallet.GetInfo(
    495                 AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR)));
    496 
    497   EXPECT_EQ(ASCIIToUTF16("12/2015"),
    498             full_wallet.GetInfo(
    499                 AutofillType(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)));
    500 
    501   std::vector<uint8> one_time_pad;
    502   EXPECT_TRUE(base::HexStringToBytes("075DA779F98B", &one_time_pad));
    503   full_wallet.set_one_time_pad(one_time_pad);
    504   EXPECT_EQ(ASCIIToUTF16("MasterCard"),
    505             full_wallet.GetInfo(AutofillType(CREDIT_CARD_TYPE)));
    506 }
    507 
    508 TEST_F(FullWalletTest, CreateFullWalletFromClearTextData) {
    509   scoped_ptr<FullWallet> full_wallet =
    510       FullWallet::CreateFullWalletFromClearText(
    511           11, 2012,
    512           "5555555555554444", "123",
    513           GetTestAddress(), GetTestShippingAddress());
    514   EXPECT_EQ(ASCIIToUTF16("5555555555554444"),
    515             full_wallet->GetInfo(AutofillType(CREDIT_CARD_NUMBER)));
    516   EXPECT_EQ(ASCIIToUTF16("MasterCard"),
    517             full_wallet->GetInfo(AutofillType(CREDIT_CARD_TYPE)));
    518   EXPECT_EQ(ASCIIToUTF16("123"),
    519             full_wallet->GetInfo(AutofillType(CREDIT_CARD_VERIFICATION_CODE)));
    520   EXPECT_EQ(ASCIIToUTF16("11/12"),
    521             full_wallet->GetInfo(
    522                 AutofillType(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR)));
    523   EXPECT_TRUE(GetTestAddress()->EqualsIgnoreID(
    524       *full_wallet->billing_address()));
    525   EXPECT_TRUE(GetTestShippingAddress()->EqualsIgnoreID(
    526       *full_wallet->shipping_address()));
    527 }
    528 
    529 }  // namespace wallet
    530 }  // namespace autofill
    531