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