1 // Copyright (c) 2011 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/basictypes.h" 6 #include "base/utf_string_conversions.h" 7 #include "chrome/browser/autofill/autofill_common_test.h" 8 #include "chrome/browser/autofill/credit_card.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 // Tests credit card summary string generation. This test simulates a variety 12 // of different possible summary strings. Variations occur based on the 13 // existence of credit card number, month, and year fields. 14 TEST(CreditCardTest, PreviewSummaryAndObfuscatedNumberStrings) { 15 // Case 0: empty credit card. 16 CreditCard credit_card0; 17 string16 summary0 = credit_card0.Label(); 18 EXPECT_EQ(string16(), summary0); 19 string16 obfuscated0 = credit_card0.ObfuscatedNumber(); 20 EXPECT_EQ(string16(), obfuscated0); 21 22 // Case 00: Empty credit card with empty strings. 23 CreditCard credit_card00; 24 autofill_test::SetCreditCardInfo(&credit_card00, 25 "John Dillinger", "", "", ""); 26 string16 summary00 = credit_card00.Label(); 27 EXPECT_EQ(string16(ASCIIToUTF16("John Dillinger")), summary00); 28 string16 obfuscated00 = credit_card00.ObfuscatedNumber(); 29 EXPECT_EQ(string16(), obfuscated00); 30 31 // Case 1: No credit card number. 32 CreditCard credit_card1; 33 autofill_test::SetCreditCardInfo(&credit_card1, 34 "John Dillinger", "", "01", "2010"); 35 string16 summary1 = credit_card1.Label(); 36 EXPECT_EQ(string16(ASCIIToUTF16("John Dillinger")), summary1); 37 string16 obfuscated1 = credit_card1.ObfuscatedNumber(); 38 EXPECT_EQ(string16(), obfuscated1); 39 40 // Case 2: No month. 41 CreditCard credit_card2; 42 autofill_test::SetCreditCardInfo(&credit_card2, 43 "John Dillinger", "5105 1051 0510 5100", "", "2010"); 44 string16 summary2 = credit_card2.Label(); 45 EXPECT_EQ(ASCIIToUTF16("************5100"), summary2); 46 string16 obfuscated2 = credit_card2.ObfuscatedNumber(); 47 EXPECT_EQ(ASCIIToUTF16("************5100"), obfuscated2); 48 49 // Case 3: No year. 50 CreditCard credit_card3; 51 autofill_test::SetCreditCardInfo(&credit_card3, 52 "John Dillinger", "5105 1051 0510 5100", "01", ""); 53 string16 summary3 = credit_card3.Label(); 54 EXPECT_EQ(ASCIIToUTF16("************5100"), summary3); 55 string16 obfuscated3 = credit_card3.ObfuscatedNumber(); 56 EXPECT_EQ(ASCIIToUTF16("************5100"), obfuscated3); 57 58 // Case 4: Have everything. 59 CreditCard credit_card4; 60 autofill_test::SetCreditCardInfo(&credit_card4, 61 "John Dillinger", "5105 1051 0510 5100", "01", "2010"); 62 string16 summary4 = credit_card4.Label(); 63 EXPECT_EQ(ASCIIToUTF16("************5100, Exp: 01/2010"), summary4); 64 string16 obfuscated4 = credit_card4.ObfuscatedNumber(); 65 EXPECT_EQ(ASCIIToUTF16("************5100"), obfuscated4); 66 } 67 68 TEST(CreditCardTest, AssignmentOperator) { 69 CreditCard a, b; 70 71 // Result of assignment should be logically equal to the original profile. 72 autofill_test::SetCreditCardInfo(&a, "John Dillinger", 73 "123456789012", "01", "2010"); 74 b = a; 75 EXPECT_TRUE(a == b); 76 77 // Assignment to self should not change the profile value. 78 a = a; 79 EXPECT_TRUE(a == b); 80 } 81 82 TEST(CreditCardTest, IsValidCreditCardNumber) { 83 // From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm 84 const string16 valid_numbers[] = { 85 ASCIIToUTF16("378282246310005"), 86 ASCIIToUTF16("3714 4963 5398 431"), 87 ASCIIToUTF16("3787-3449-3671-000"), 88 ASCIIToUTF16("5610591081018250"), 89 ASCIIToUTF16("3056 9309 0259 04"), 90 ASCIIToUTF16("3852-0000-0232-37"), 91 ASCIIToUTF16("6011111111111117"), 92 ASCIIToUTF16("6011 0009 9013 9424"), 93 ASCIIToUTF16("3530-1113-3330-0000"), 94 ASCIIToUTF16("3566002020360505"), 95 ASCIIToUTF16("5555 5555 5555 4444"), 96 ASCIIToUTF16("5105-1051-0510-5100"), 97 ASCIIToUTF16("4111111111111111"), 98 ASCIIToUTF16("4012 8888 8888 1881"), 99 ASCIIToUTF16("4222-2222-2222-2"), 100 ASCIIToUTF16("5019717010103742"), 101 ASCIIToUTF16("6331101999990016"), 102 }; 103 const string16 invalid_numbers[] = { 104 ASCIIToUTF16("4111 1111 112"), /* too short */ 105 ASCIIToUTF16("41111111111111111115"), /* too long */ 106 ASCIIToUTF16("4111-1111-1111-1110"), /* wrong Luhn checksum */ 107 ASCIIToUTF16("3056 9309 0259 04aa"), /* non-digit characters */ 108 }; 109 110 for (size_t i = 0; i < arraysize(valid_numbers); ++i) { 111 SCOPED_TRACE(valid_numbers[i]); 112 EXPECT_TRUE(CreditCard::IsValidCreditCardNumber(valid_numbers[i])); 113 } 114 for (size_t i = 0; i < arraysize(invalid_numbers); ++i) { 115 SCOPED_TRACE(invalid_numbers[i]); 116 EXPECT_FALSE(CreditCard::IsValidCreditCardNumber(invalid_numbers[i])); 117 } 118 } 119 120 TEST(CreditCardTest, InvalidMastercardNumber) { 121 CreditCard card; 122 123 autofill_test::SetCreditCardInfo(&card, "Baby Face Nelson", 124 "5200000000000004", "01", "2010"); 125 EXPECT_EQ("genericCC", card.type()); 126 } 127 128 // Verify that we preserve exactly what the user typed for credit card numbers. 129 TEST(CreditCardTest, SetInfoCreditCardNumber) { 130 CreditCard card; 131 132 autofill_test::SetCreditCardInfo(&card, "Bob Dylan", 133 "4321-5432-6543-xxxx", "07", "2013"); 134 EXPECT_EQ(ASCIIToUTF16("4321-5432-6543-xxxx"), 135 card.GetInfo(CREDIT_CARD_NUMBER)); 136 } 137