OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/guid.h" | 6 #include "base/guid.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "components/autofill/core/browser/autofill_common_test.h" | 8 #include "components/autofill/core/browser/autofill_common_test.h" |
9 #include "components/autofill/core/browser/credit_card.h" | 9 #include "components/autofill/core/browser/credit_card.h" |
| 10 #include "components/autofill/core/browser/validation.h" |
10 #include "components/autofill/core/common/form_field_data.h" | 11 #include "components/autofill/core/common/form_field_data.h" |
11 #include "grit/webkit_resources.h" | 12 #include "grit/webkit_resources.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace autofill { | 15 namespace autofill { |
15 namespace { | 16 namespace { |
16 | 17 |
17 // From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card
_numbers.htm | 18 // From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card
_numbers.htm |
18 const char* const kValidNumbers[] = { | 19 const char* const kValidNumbers[] = { |
19 "378282246310005", | 20 "378282246310005", |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kInvalidNumbers[i])); | 265 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kInvalidNumbers[i])); |
265 EXPECT_FALSE(card.IsComplete()); | 266 EXPECT_FALSE(card.IsComplete()); |
266 } | 267 } |
267 } | 268 } |
268 | 269 |
269 TEST(CreditCardTest, InvalidMastercardNumber) { | 270 TEST(CreditCardTest, InvalidMastercardNumber) { |
270 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); | 271 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); |
271 | 272 |
272 test::SetCreditCardInfo(&card, "Baby Face Nelson", | 273 test::SetCreditCardInfo(&card, "Baby Face Nelson", |
273 "5200000000000004", "01", "2010"); | 274 "5200000000000004", "01", "2010"); |
274 EXPECT_EQ("genericCC", card.type()); | 275 EXPECT_EQ(kMasterCard, card.type()); |
| 276 EXPECT_FALSE(card.IsComplete()); |
275 } | 277 } |
276 | 278 |
277 // Verify that we preserve exactly what the user typed for credit card numbers. | 279 // Verify that we preserve exactly what the user typed for credit card numbers. |
278 TEST(CreditCardTest, SetRawInfoCreditCardNumber) { | 280 TEST(CreditCardTest, SetRawInfoCreditCardNumber) { |
279 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); | 281 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); |
280 | 282 |
281 test::SetCreditCardInfo(&card, "Bob Dylan", | 283 test::SetCreditCardInfo(&card, "Bob Dylan", |
282 "4321-5432-6543-xxxx", "07", "2013"); | 284 "4321-5432-6543-xxxx", "07", "2013"); |
283 EXPECT_EQ(ASCIIToUTF16("4321-5432-6543-xxxx"), | 285 EXPECT_EQ(ASCIIToUTF16("4321-5432-6543-xxxx"), |
284 card.GetRawInfo(CREDIT_CARD_NUMBER)); | 286 card.GetRawInfo(CREDIT_CARD_NUMBER)); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 { | 483 { |
482 // Case insensitivity: | 484 // Case insensitivity: |
483 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/"); | 485 CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/"); |
484 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, | 486 credit_card.SetRawInfo(CREDIT_CARD_NUMBER, |
485 ASCIIToUTF16("6011111111111117")); | 487 ASCIIToUTF16("6011111111111117")); |
486 credit_card.FillSelectControl(CREDIT_CARD_TYPE, "en-US", &field); | 488 credit_card.FillSelectControl(CREDIT_CARD_TYPE, "en-US", &field); |
487 EXPECT_EQ(ASCIIToUTF16("discover"), field.value); | 489 EXPECT_EQ(ASCIIToUTF16("discover"), field.value); |
488 } | 490 } |
489 } | 491 } |
490 | 492 |
| 493 TEST(CreditCardTest, GetCreditCardType) { |
| 494 struct { |
| 495 std::string card_number; |
| 496 std::string type; |
| 497 bool is_valid; |
| 498 } test_cases[] = { |
| 499 // The relevant sample numbers from |
| 500 // http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_n
umbers.htm |
| 501 { "378282246310005", kAmericanExpressCard, true }, |
| 502 { "371449635398431", kAmericanExpressCard, true }, |
| 503 { "378734493671000", kAmericanExpressCard, true }, |
| 504 { "30569309025904", kDinersCard, true }, |
| 505 { "38520000023237", kDinersCard, true }, |
| 506 { "6011111111111117", kDiscoverCard, true }, |
| 507 { "6011000990139424", kDiscoverCard, true }, |
| 508 { "3530111333300000", kJCBCard, true }, |
| 509 { "3566002020360505", kJCBCard, true }, |
| 510 { "5555555555554444", kMasterCard, true }, |
| 511 { "5105105105105100", kMasterCard, true }, |
| 512 { "4111111111111111", kVisaCard, true }, |
| 513 { "4012888888881881", kVisaCard, true }, |
| 514 { "4222222222222", kVisaCard, true }, |
| 515 |
| 516 // The relevant sample numbers from |
| 517 // http://auricsystems.com/support-center/sample-credit-card-numbers/ |
| 518 { "343434343434343", kAmericanExpressCard, true }, |
| 519 { "371144371144376", kAmericanExpressCard, true }, |
| 520 { "341134113411347", kAmericanExpressCard, true }, |
| 521 { "36438936438936", kDinersCard, true }, |
| 522 { "36110361103612", kDinersCard, true }, |
| 523 { "36111111111111", kDinersCard, true }, |
| 524 { "6011016011016011", kDiscoverCard, true }, |
| 525 { "6011000990139424", kDiscoverCard, true }, |
| 526 { "6011000000000004", kDiscoverCard, true }, |
| 527 { "6011000995500000", kDiscoverCard, true }, |
| 528 { "6500000000000002", kDiscoverCard, true }, |
| 529 { "3566002020360505", kJCBCard, true }, |
| 530 { "3528000000000007", kJCBCard, true }, |
| 531 { "5500005555555559", kMasterCard, true }, |
| 532 { "5555555555555557", kMasterCard, true }, |
| 533 { "5454545454545454", kMasterCard, true }, |
| 534 { "5555515555555551", kMasterCard, true }, |
| 535 { "5405222222222226", kMasterCard, true }, |
| 536 { "5478050000000007", kMasterCard, true }, |
| 537 { "5111005111051128", kMasterCard, true }, |
| 538 { "5112345112345114", kMasterCard, true }, |
| 539 { "5115915115915118", kMasterCard, true }, |
| 540 |
| 541 // Empty string |
| 542 { std::string(), kGenericCard, false }, |
| 543 |
| 544 // Non-numeric |
| 545 { "garbage", kGenericCard, false }, |
| 546 { "4garbage", kVisaCard, false }, |
| 547 |
| 548 // Fails Luhn check. |
| 549 { "4111111111111112", kVisaCard, false }, |
| 550 |
| 551 // Invalid length. |
| 552 { "3434343434343434", kAmericanExpressCard, false }, |
| 553 { "411111111111116", kVisaCard, false }, |
| 554 |
| 555 // Issuer Identification Numbers (IINs) that Chrome recognizes. |
| 556 { "4", kVisaCard, false }, |
| 557 { "34", kAmericanExpressCard, false }, |
| 558 { "37", kAmericanExpressCard, false }, |
| 559 { "300", kDinersCard, false }, |
| 560 { "301", kDinersCard, false }, |
| 561 { "302", kDinersCard, false }, |
| 562 { "303", kDinersCard, false }, |
| 563 { "304", kDinersCard, false }, |
| 564 { "305", kDinersCard, false }, |
| 565 { "3095", kDinersCard, false }, |
| 566 { "36", kDinersCard, false }, |
| 567 { "38", kDinersCard, false }, |
| 568 { "39", kDinersCard, false }, |
| 569 { "6011", kDiscoverCard, false }, |
| 570 { "644", kDiscoverCard, false }, |
| 571 { "645", kDiscoverCard, false }, |
| 572 { "646", kDiscoverCard, false }, |
| 573 { "647", kDiscoverCard, false }, |
| 574 { "648", kDiscoverCard, false }, |
| 575 { "649", kDiscoverCard, false }, |
| 576 { "65", kDiscoverCard, false }, |
| 577 { "3528", kJCBCard, false }, |
| 578 { "3531", kJCBCard, false }, |
| 579 { "3589", kJCBCard, false }, |
| 580 { "51", kMasterCard, false }, |
| 581 { "52", kMasterCard, false }, |
| 582 { "53", kMasterCard, false }, |
| 583 { "54", kMasterCard, false }, |
| 584 { "55", kMasterCard, false }, |
| 585 |
| 586 // Not enough data to determine an IIN uniquely. |
| 587 { "3", kGenericCard, false }, |
| 588 { "30", kGenericCard, false }, |
| 589 { "309", kGenericCard, false }, |
| 590 { "35", kGenericCard, false }, |
| 591 { "5", kGenericCard, false }, |
| 592 { "6", kGenericCard, false }, |
| 593 { "60", kGenericCard, false }, |
| 594 { "601", kGenericCard, false }, |
| 595 { "64", kGenericCard, false }, |
| 596 |
| 597 // Unknown IINs. |
| 598 { "0", kGenericCard, false }, |
| 599 { "1", kGenericCard, false }, |
| 600 { "2", kGenericCard, false }, |
| 601 { "306", kGenericCard, false }, |
| 602 { "307", kGenericCard, false }, |
| 603 { "308", kGenericCard, false }, |
| 604 { "3091", kGenericCard, false }, |
| 605 { "3094", kGenericCard, false }, |
| 606 { "3096", kGenericCard, false }, |
| 607 { "31", kGenericCard, false }, |
| 608 { "32", kGenericCard, false }, |
| 609 { "33", kGenericCard, false }, |
| 610 { "351", kGenericCard, false }, |
| 611 { "3527", kGenericCard, false }, |
| 612 { "359", kGenericCard, false }, |
| 613 { "50", kGenericCard, false }, |
| 614 { "56", kGenericCard, false }, |
| 615 { "57", kGenericCard, false }, |
| 616 { "58", kGenericCard, false }, |
| 617 { "59", kGenericCard, false }, |
| 618 { "600", kGenericCard, false }, |
| 619 { "602", kGenericCard, false }, |
| 620 { "603", kGenericCard, false }, |
| 621 { "604", kGenericCard, false }, |
| 622 { "605", kGenericCard, false }, |
| 623 { "606", kGenericCard, false }, |
| 624 { "607", kGenericCard, false }, |
| 625 { "608", kGenericCard, false }, |
| 626 { "609", kGenericCard, false }, |
| 627 { "61", kGenericCard, false }, |
| 628 { "62", kGenericCard, false }, |
| 629 { "63", kGenericCard, false }, |
| 630 { "640", kGenericCard, false }, |
| 631 { "641", kGenericCard, false }, |
| 632 { "642", kGenericCard, false }, |
| 633 { "643", kGenericCard, false }, |
| 634 { "66", kGenericCard, false }, |
| 635 { "67", kGenericCard, false }, |
| 636 { "68", kGenericCard, false }, |
| 637 { "69", kGenericCard, false }, |
| 638 { "7", kGenericCard, false }, |
| 639 { "8", kGenericCard, false }, |
| 640 { "9", kGenericCard, false }, |
| 641 |
| 642 // Oddball case: Unknown issuer, but valid Luhn check and plausible length. |
| 643 { "7000700070007000", kGenericCard, true }, |
| 644 }; |
| 645 |
| 646 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 647 base::string16 card_number = ASCIIToUTF16(test_cases[i].card_number); |
| 648 SCOPED_TRACE(card_number); |
| 649 EXPECT_EQ(test_cases[i].type, CreditCard::GetCreditCardType(card_number)); |
| 650 EXPECT_EQ(test_cases[i].is_valid, IsValidCreditCardNumber(card_number)); |
| 651 } |
| 652 } |
| 653 |
491 } // namespace autofill | 654 } // namespace autofill |
OLD | NEW |