Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / components / autofill / core / browser / credit_card_unittest.cc
blob5175befd245b91bdbe0a6569c3291cf82f536298
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.
5 #include "base/basictypes.h"
6 #include "base/guid.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/browser/autofill_test_utils.h"
9 #include "components/autofill/core/browser/autofill_type.h"
10 #include "components/autofill/core/browser/credit_card.h"
11 #include "components/autofill/core/browser/validation.h"
12 #include "components/autofill/core/common/form_field_data.h"
13 #include "grit/components_scaled_resources.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::ASCIIToUTF16;
17 using base::UTF8ToUTF16;
19 namespace autofill {
21 const CreditCard::RecordType LOCAL_CARD = CreditCard::LOCAL_CARD;
22 const CreditCard::RecordType MASKED_SERVER_CARD =
23 CreditCard::MASKED_SERVER_CARD;
24 const CreditCard::RecordType FULL_SERVER_CARD = CreditCard::FULL_SERVER_CARD;
26 namespace {
28 // From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
29 const char* const kValidNumbers[] = {
30 "378282246310005",
31 "3714 4963 5398 431",
32 "3787-3449-3671-000",
33 "5610591081018250",
34 "3056 9309 0259 04",
35 "3852-0000-0232-37",
36 "6011111111111117",
37 "6011 0009 9013 9424",
38 "3530-1113-3330-0000",
39 "3566002020360505",
40 "5555 5555 5555 4444",
41 "5105-1051-0510-5100",
42 "4111111111111111",
43 "4012 8888 8888 1881",
44 "4222-2222-2222-2",
45 "5019717010103742",
46 "6331101999990016",
48 // A UnionPay card that doesn't pass the Luhn checksum
49 "6200000000000000",
51 const char* const kInvalidNumbers[] = {
52 "4111 1111 112", /* too short */
53 "41111111111111111115", /* too long */
54 "4111-1111-1111-1110", /* wrong Luhn checksum */
55 "3056 9309 0259 04aa", /* non-digit characters */
58 } // namespace
60 // Tests credit card summary string generation. This test simulates a variety
61 // of different possible summary strings. Variations occur based on the
62 // existence of credit card number, month, and year fields.
63 TEST(CreditCardTest, PreviewSummaryAndTypeAndLastFourDigitsStrings) {
64 // Case 0: empty credit card.
65 CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com/");
66 base::string16 summary0 = credit_card0.Label();
67 EXPECT_EQ(base::string16(), summary0);
68 base::string16 obfuscated0 = credit_card0.TypeAndLastFourDigits();
69 EXPECT_EQ(ASCIIToUTF16("Card"), obfuscated0);
71 // Case 00: Empty credit card with empty strings.
72 CreditCard credit_card00(base::GenerateGUID(), "https://www.example.com/");
73 test::SetCreditCardInfo(&credit_card00,"John Dillinger", "", "", "");
74 base::string16 summary00 = credit_card00.Label();
75 EXPECT_EQ(base::string16(ASCIIToUTF16("John Dillinger")), summary00);
76 base::string16 obfuscated00 = credit_card00.TypeAndLastFourDigits();
77 EXPECT_EQ(ASCIIToUTF16("Card"), obfuscated00);
79 // Case 1: No credit card number.
80 CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com/");
81 test::SetCreditCardInfo(&credit_card1,"John Dillinger", "", "01", "2010");
82 base::string16 summary1 = credit_card1.Label();
83 EXPECT_EQ(base::string16(ASCIIToUTF16("John Dillinger")), summary1);
84 base::string16 obfuscated1 = credit_card1.TypeAndLastFourDigits();
85 EXPECT_EQ(ASCIIToUTF16("Card"), obfuscated1);
87 // Case 2: No month.
88 CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com/");
89 test::SetCreditCardInfo(
90 &credit_card2, "John Dillinger", "5105 1051 0510 5100", "", "2010");
91 base::string16 summary2 = credit_card2.Label();
92 EXPECT_EQ(UTF8ToUTF16(
93 "MasterCard\xC2\xA0\xE2\x8B\xAF"
94 "5100"),
95 summary2);
96 base::string16 obfuscated2 = credit_card2.TypeAndLastFourDigits();
97 EXPECT_EQ(UTF8ToUTF16(
98 "MasterCard\xC2\xA0\xE2\x8B\xAF"
99 "5100"),
100 obfuscated2);
102 // Case 3: No year.
103 CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com/");
104 test::SetCreditCardInfo(
105 &credit_card3, "John Dillinger", "5105 1051 0510 5100", "01", "");
106 base::string16 summary3 = credit_card3.Label();
107 EXPECT_EQ(UTF8ToUTF16(
108 "MasterCard\xC2\xA0\xE2\x8B\xAF"
109 "5100"),
110 summary3);
111 base::string16 obfuscated3 = credit_card3.TypeAndLastFourDigits();
112 EXPECT_EQ(UTF8ToUTF16(
113 "MasterCard\xC2\xA0\xE2\x8B\xAF"
114 "5100"),
115 obfuscated3);
117 // Case 4: Have everything.
118 CreditCard credit_card4(base::GenerateGUID(), "https://www.example.com/");
119 test::SetCreditCardInfo(
120 &credit_card4, "John Dillinger", "5105 1051 0510 5100", "01", "2010");
121 base::string16 summary4 = credit_card4.Label();
122 EXPECT_EQ(UTF8ToUTF16(
123 "MasterCard\xC2\xA0\xE2\x8B\xAF"
124 "5100, 01/2010"),
125 summary4);
126 base::string16 obfuscated4 = credit_card4.TypeAndLastFourDigits();
127 EXPECT_EQ(UTF8ToUTF16(
128 "MasterCard\xC2\xA0\xE2\x8B\xAF"
129 "5100"),
130 obfuscated4);
132 // Case 5: Very long credit card
133 CreditCard credit_card5(base::GenerateGUID(), "https://www.example.com/");
134 test::SetCreditCardInfo(
135 &credit_card5,
136 "John Dillinger",
137 "0123456789 0123456789 0123456789 5105 1051 0510 5100", "01", "2010");
138 base::string16 summary5 = credit_card5.Label();
139 EXPECT_EQ(UTF8ToUTF16(
140 "Card\xC2\xA0\xE2\x8B\xAF"
141 "5100, 01/2010"),
142 summary5);
143 base::string16 obfuscated5 = credit_card5.TypeAndLastFourDigits();
144 EXPECT_EQ(UTF8ToUTF16(
145 "Card\xC2\xA0\xE2\x8B\xAF"
146 "5100"),
147 obfuscated5);
150 TEST(CreditCardTest, AssignmentOperator) {
151 CreditCard a(base::GenerateGUID(), "some origin");
152 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010");
154 // Result of assignment should be logically equal to the original profile.
155 CreditCard b(base::GenerateGUID(), "some other origin");
156 b = a;
157 EXPECT_TRUE(a == b);
159 // Assignment to self should not change the profile value.
160 a = a;
161 EXPECT_TRUE(a == b);
164 TEST(CreditCardTest, Copy) {
165 CreditCard a(base::GenerateGUID(), "https://www.example.com");
166 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010");
168 // Clone should be logically equal to the original.
169 CreditCard b(a);
170 EXPECT_TRUE(a == b);
173 TEST(CreditCardTest, IsLocalDuplicateOfServerCard) {
174 struct {
175 CreditCard::RecordType first_card_record_type;
176 const char* first_card_name;
177 const char* first_card_number;
178 const char* first_card_exp_mo;
179 const char* first_card_exp_yr;
181 CreditCard::RecordType second_card_record_type;
182 const char* second_card_name;
183 const char* second_card_number;
184 const char* second_card_exp_mo;
185 const char* second_card_exp_yr;
186 const char* second_card_type;
188 bool is_local_duplicate;
189 } test_cases[] = {
190 { LOCAL_CARD, "", "", "", "",
191 LOCAL_CARD, "", "", "", "", nullptr, false },
192 { LOCAL_CARD, "", "", "", "",
193 FULL_SERVER_CARD, "", "", "", "", nullptr, true},
194 { FULL_SERVER_CARD, "", "", "", "",
195 FULL_SERVER_CARD, "", "", "", "", nullptr, false},
196 { LOCAL_CARD, "John Dillinger", "423456789012", "01", "2010",
197 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr,
198 true },
199 { LOCAL_CARD, "J Dillinger", "423456789012", "01", "2010",
200 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr,
201 false },
202 { LOCAL_CARD, "", "423456789012", "01", "2010",
203 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr,
204 true },
205 { LOCAL_CARD, "", "423456789012", "", "",
206 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr,
207 true },
208 { LOCAL_CARD, "", "423456789012", "", "",
209 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kVisaCard,
210 true },
211 { LOCAL_CARD, "", "423456789012", "", "",
212 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kMasterCard,
213 false },
214 { LOCAL_CARD, "John Dillinger", "4234-5678-9012", "01", "2010",
215 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr,
216 true },
219 for (size_t i = 0; i < arraysize(test_cases); ++i) {
220 CreditCard a(base::GenerateGUID(), std::string());
221 a.set_record_type(test_cases[i].first_card_record_type);
222 test::SetCreditCardInfo(&a,
223 test_cases[i].first_card_name,
224 test_cases[i].first_card_number,
225 test_cases[i].first_card_exp_mo,
226 test_cases[i].first_card_exp_yr);
228 CreditCard b(base::GenerateGUID(), std::string());
229 b.set_record_type(test_cases[i].second_card_record_type);
230 test::SetCreditCardInfo(&b,
231 test_cases[i].second_card_name,
232 test_cases[i].second_card_number,
233 test_cases[i].second_card_exp_mo,
234 test_cases[i].second_card_exp_yr);
236 if (test_cases[i].second_card_record_type == CreditCard::MASKED_SERVER_CARD)
237 b.SetTypeForMaskedCard(test_cases[i].second_card_type);
239 EXPECT_EQ(test_cases[i].is_local_duplicate,
240 a.IsLocalDuplicateOfServerCard(b)) << " when comparing cards "
241 << a.Label() << " and " << b.Label();
245 TEST(CreditCardTest, Compare) {
246 CreditCard a(base::GenerateGUID(), std::string());
247 CreditCard b(base::GenerateGUID(), std::string());
249 // Empty cards are the same.
250 EXPECT_EQ(0, a.Compare(b));
252 // GUIDs don't count.
253 a.set_guid(base::GenerateGUID());
254 b.set_guid(base::GenerateGUID());
255 EXPECT_EQ(0, a.Compare(b));
257 // Origins don't count.
258 a.set_origin("apple");
259 b.set_origin("banana");
260 EXPECT_EQ(0, a.Compare(b));
262 // Different values produce non-zero results.
263 test::SetCreditCardInfo(&a, "Jimmy", NULL, NULL, NULL);
264 test::SetCreditCardInfo(&b, "Ringo", NULL, NULL, NULL);
265 EXPECT_GT(0, a.Compare(b));
266 EXPECT_LT(0, b.Compare(a));
269 // This method is not compiled for iOS because these resources are not used and
270 // should not be shipped.
271 #if !defined(OS_IOS)
272 // Test we get the correct icon for each card type.
273 TEST(CreditCardTest, IconResourceId) {
274 EXPECT_EQ(IDR_AUTOFILL_CC_AMEX,
275 CreditCard::IconResourceId(kAmericanExpressCard));
276 EXPECT_EQ(IDR_AUTOFILL_CC_GENERIC,
277 CreditCard::IconResourceId(kDinersCard));
278 EXPECT_EQ(IDR_AUTOFILL_CC_DISCOVER,
279 CreditCard::IconResourceId(kDiscoverCard));
280 EXPECT_EQ(IDR_AUTOFILL_CC_GENERIC,
281 CreditCard::IconResourceId(kJCBCard));
282 EXPECT_EQ(IDR_AUTOFILL_CC_MASTERCARD,
283 CreditCard::IconResourceId(kMasterCard));
284 EXPECT_EQ(IDR_AUTOFILL_CC_VISA,
285 CreditCard::IconResourceId(kVisaCard));
287 #endif // #if !defined(OS_IOS)
289 TEST(CreditCardTest, UpdateFromImportedCard) {
290 CreditCard original_card(base::GenerateGUID(), "https://www.example.com");
291 test::SetCreditCardInfo(
292 &original_card, "John Dillinger", "123456789012", "09", "2017");
294 CreditCard a = original_card;
296 // The new card has a different name, expiration date, and origin.
297 CreditCard b = a;
298 b.set_guid(base::GenerateGUID());
299 b.set_origin("https://www.example.org");
300 b.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("J. Dillinger"));
301 b.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("08"));
302 b.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2019"));
304 EXPECT_TRUE(a.UpdateFromImportedCard(b, "en-US"));
305 EXPECT_EQ("https://www.example.org", a.origin());
306 EXPECT_EQ(ASCIIToUTF16("J. Dillinger"), a.GetRawInfo(CREDIT_CARD_NAME));
307 EXPECT_EQ(ASCIIToUTF16("08"), a.GetRawInfo(CREDIT_CARD_EXP_MONTH));
308 EXPECT_EQ(ASCIIToUTF16("2019"), a.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
310 // Try again, but with no name set for |b|.
311 a = original_card;
312 b.SetRawInfo(CREDIT_CARD_NAME, base::string16());
314 EXPECT_TRUE(a.UpdateFromImportedCard(b, "en-US"));
315 EXPECT_EQ("https://www.example.org", a.origin());
316 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), a.GetRawInfo(CREDIT_CARD_NAME));
317 EXPECT_EQ(ASCIIToUTF16("08"), a.GetRawInfo(CREDIT_CARD_EXP_MONTH));
318 EXPECT_EQ(ASCIIToUTF16("2019"), a.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
320 // Try again, but with only the original card having a verified origin.
321 // |a| should be unchanged.
322 a = original_card;
323 a.set_origin("Chrome settings");
324 b.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("J. Dillinger"));
326 EXPECT_TRUE(a.UpdateFromImportedCard(b, "en-US"));
327 EXPECT_EQ("Chrome settings", a.origin());
328 EXPECT_EQ(ASCIIToUTF16("John Dillinger"), a.GetRawInfo(CREDIT_CARD_NAME));
329 EXPECT_EQ(ASCIIToUTF16("09"), a.GetRawInfo(CREDIT_CARD_EXP_MONTH));
330 EXPECT_EQ(ASCIIToUTF16("2017"), a.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
332 // Try again, but with only the new card having a verified origin.
333 a = original_card;
334 b.set_origin("Chrome settings");
336 EXPECT_TRUE(a.UpdateFromImportedCard(b, "en-US"));
337 EXPECT_EQ("Chrome settings", a.origin());
338 EXPECT_EQ(ASCIIToUTF16("J. Dillinger"), a.GetRawInfo(CREDIT_CARD_NAME));
339 EXPECT_EQ(ASCIIToUTF16("08"), a.GetRawInfo(CREDIT_CARD_EXP_MONTH));
340 EXPECT_EQ(ASCIIToUTF16("2019"), a.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
342 // Try again, with both cards having a verified origin.
343 a = original_card;
344 a.set_origin("Chrome Autofill dialog");
345 b.set_origin("Chrome settings");
347 EXPECT_TRUE(a.UpdateFromImportedCard(b, "en-US"));
348 EXPECT_EQ("Chrome settings", a.origin());
349 EXPECT_EQ(ASCIIToUTF16("J. Dillinger"), a.GetRawInfo(CREDIT_CARD_NAME));
350 EXPECT_EQ(ASCIIToUTF16("08"), a.GetRawInfo(CREDIT_CARD_EXP_MONTH));
351 EXPECT_EQ(ASCIIToUTF16("2019"), a.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
353 // Try again, but with |b| having a different card number.
354 // |a| should be unchanged.
355 a = original_card;
356 b.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
358 EXPECT_FALSE(a.UpdateFromImportedCard(b, "en-US"));
359 EXPECT_EQ(original_card, a);
362 TEST(CreditCardTest, IsComplete) {
363 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
364 EXPECT_FALSE(card.IsComplete());
365 card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Wally T. Walrus"));
366 EXPECT_FALSE(card.IsComplete());
367 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("01"));
368 EXPECT_FALSE(card.IsComplete());
369 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2014"));
371 for (size_t i = 0; i < arraysize(kValidNumbers); ++i) {
372 SCOPED_TRACE(kValidNumbers[i]);
373 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kValidNumbers[i]));
374 EXPECT_TRUE(card.IsComplete());
376 for (size_t i = 0; i < arraysize(kInvalidNumbers); ++i) {
377 SCOPED_TRACE(kInvalidNumbers[i]);
378 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kInvalidNumbers[i]));
379 EXPECT_FALSE(card.IsComplete());
383 TEST(CreditCardTest, IsValid) {
384 CreditCard card;
385 // Invalid because expired
386 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1"));
387 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2010"));
388 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
389 EXPECT_FALSE(card.IsValid());
391 // Invalid because card number is not complete
392 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
393 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999"));
394 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111"));
395 EXPECT_FALSE(card.IsValid());
397 // Valid
398 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
399 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999"));
400 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
401 EXPECT_TRUE(card.IsValid());
404 TEST(CreditCardTest, InvalidMastercardNumber) {
405 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
407 test::SetCreditCardInfo(&card, "Baby Face Nelson",
408 "5200000000000004", "01", "2010");
409 EXPECT_EQ(kMasterCard, card.type());
410 EXPECT_FALSE(card.IsComplete());
413 // Verify that we preserve exactly what the user typed for credit card numbers.
414 TEST(CreditCardTest, SetRawInfoCreditCardNumber) {
415 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
417 test::SetCreditCardInfo(&card, "Bob Dylan",
418 "4321-5432-6543-xxxx", "07", "2013");
419 EXPECT_EQ(ASCIIToUTF16("4321-5432-6543-xxxx"),
420 card.GetRawInfo(CREDIT_CARD_NUMBER));
423 // Verify that we can handle both numeric and named months.
424 TEST(CreditCardTest, SetExpirationMonth) {
425 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
427 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("05"));
428 EXPECT_EQ(ASCIIToUTF16("05"), card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
429 EXPECT_EQ(5, card.expiration_month());
431 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("7"));
432 EXPECT_EQ(ASCIIToUTF16("07"), card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
433 EXPECT_EQ(7, card.expiration_month());
435 // This should fail, and preserve the previous value.
436 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("January"));
437 EXPECT_EQ(ASCIIToUTF16("07"), card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
438 EXPECT_EQ(7, card.expiration_month());
440 card.SetInfo(
441 AutofillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("January"), "en-US");
442 EXPECT_EQ(ASCIIToUTF16("01"), card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
443 EXPECT_EQ(1, card.expiration_month());
445 card.SetInfo(
446 AutofillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("Apr"), "en-US");
447 EXPECT_EQ(ASCIIToUTF16("04"), card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
448 EXPECT_EQ(4, card.expiration_month());
450 card.SetInfo(AutofillType(CREDIT_CARD_EXP_MONTH),
451 UTF8ToUTF16("FÉVRIER"), "fr-FR");
452 EXPECT_EQ(ASCIIToUTF16("02"), card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
453 EXPECT_EQ(2, card.expiration_month());
456 TEST(CreditCardTest, CreditCardType) {
457 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
459 // The card type cannot be set directly.
460 card.SetRawInfo(CREDIT_CARD_TYPE, ASCIIToUTF16("Visa"));
461 EXPECT_EQ(base::string16(), card.GetRawInfo(CREDIT_CARD_TYPE));
463 // Setting the number should implicitly set the type.
464 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111 1111 1111 1111"));
465 EXPECT_EQ(ASCIIToUTF16("Visa"), card.GetRawInfo(CREDIT_CARD_TYPE));
468 TEST(CreditCardTest, CreditCardVerificationCode) {
469 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
471 // The verification code cannot be set, as Chrome does not store this data.
472 card.SetRawInfo(CREDIT_CARD_VERIFICATION_CODE, ASCIIToUTF16("999"));
473 EXPECT_EQ(base::string16(), card.GetRawInfo(CREDIT_CARD_VERIFICATION_CODE));
477 TEST(CreditCardTest, GetCreditCardType) {
478 struct {
479 std::string card_number;
480 std::string type;
481 bool is_valid;
482 } test_cases[] = {
483 // The relevant sample numbers from
484 // http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
485 { "378282246310005", kAmericanExpressCard, true },
486 { "371449635398431", kAmericanExpressCard, true },
487 { "378734493671000", kAmericanExpressCard, true },
488 { "30569309025904", kDinersCard, true },
489 { "38520000023237", kDinersCard, true },
490 { "6011111111111117", kDiscoverCard, true },
491 { "6011000990139424", kDiscoverCard, true },
492 { "3530111333300000", kJCBCard, true },
493 { "3566002020360505", kJCBCard, true },
494 { "5555555555554444", kMasterCard, true },
495 { "5105105105105100", kMasterCard, true },
496 { "4111111111111111", kVisaCard, true },
497 { "4012888888881881", kVisaCard, true },
498 { "4222222222222", kVisaCard, true },
500 // The relevant sample numbers from
501 // http://auricsystems.com/support-center/sample-credit-card-numbers/
502 { "343434343434343", kAmericanExpressCard, true },
503 { "371144371144376", kAmericanExpressCard, true },
504 { "341134113411347", kAmericanExpressCard, true },
505 { "36438936438936", kDinersCard, true },
506 { "36110361103612", kDinersCard, true },
507 { "36111111111111", kDinersCard, true },
508 { "6011016011016011", kDiscoverCard, true },
509 { "6011000990139424", kDiscoverCard, true },
510 { "6011000000000004", kDiscoverCard, true },
511 { "6011000995500000", kDiscoverCard, true },
512 { "6500000000000002", kDiscoverCard, true },
513 { "3566002020360505", kJCBCard, true },
514 { "3528000000000007", kJCBCard, true },
515 { "5500005555555559", kMasterCard, true },
516 { "5555555555555557", kMasterCard, true },
517 { "5454545454545454", kMasterCard, true },
518 { "5555515555555551", kMasterCard, true },
519 { "5405222222222226", kMasterCard, true },
520 { "5478050000000007", kMasterCard, true },
521 { "5111005111051128", kMasterCard, true },
522 { "5112345112345114", kMasterCard, true },
523 { "5115915115915118", kMasterCard, true },
525 // A UnionPay card that doesn't pass the Luhn checksum
526 { "6200000000000000", kUnionPay, true },
528 // Empty string
529 { std::string(), kGenericCard, false },
531 // Non-numeric
532 { "garbage", kGenericCard, false },
533 { "4garbage", kVisaCard, false },
535 // Fails Luhn check.
536 { "4111111111111112", kVisaCard, false },
538 // Invalid length.
539 { "3434343434343434", kAmericanExpressCard, false },
540 { "411111111111116", kVisaCard, false },
542 // Issuer Identification Numbers (IINs) that Chrome recognizes.
543 { "4", kVisaCard, false },
544 { "34", kAmericanExpressCard, false },
545 { "37", kAmericanExpressCard, false },
546 { "300", kDinersCard, false },
547 { "301", kDinersCard, false },
548 { "302", kDinersCard, false },
549 { "303", kDinersCard, false },
550 { "304", kDinersCard, false },
551 { "305", kDinersCard, false },
552 { "3095", kDinersCard, false },
553 { "36", kDinersCard, false },
554 { "38", kDinersCard, false },
555 { "39", kDinersCard, false },
556 { "6011", kDiscoverCard, false },
557 { "644", kDiscoverCard, false },
558 { "645", kDiscoverCard, false },
559 { "646", kDiscoverCard, false },
560 { "647", kDiscoverCard, false },
561 { "648", kDiscoverCard, false },
562 { "649", kDiscoverCard, false },
563 { "65", kDiscoverCard, false },
564 { "3528", kJCBCard, false },
565 { "3531", kJCBCard, false },
566 { "3589", kJCBCard, false },
567 { "51", kMasterCard, false },
568 { "52", kMasterCard, false },
569 { "53", kMasterCard, false },
570 { "54", kMasterCard, false },
571 { "55", kMasterCard, false },
572 { "62", kUnionPay, false },
574 // Not enough data to determine an IIN uniquely.
575 { "3", kGenericCard, false },
576 { "30", kGenericCard, false },
577 { "309", kGenericCard, false },
578 { "35", kGenericCard, false },
579 { "5", kGenericCard, false },
580 { "6", kGenericCard, false },
581 { "60", kGenericCard, false },
582 { "601", kGenericCard, false },
583 { "64", kGenericCard, false },
585 // Unknown IINs.
586 { "0", kGenericCard, false },
587 { "1", kGenericCard, false },
588 { "2", kGenericCard, false },
589 { "306", kGenericCard, false },
590 { "307", kGenericCard, false },
591 { "308", kGenericCard, false },
592 { "3091", kGenericCard, false },
593 { "3094", kGenericCard, false },
594 { "3096", kGenericCard, false },
595 { "31", kGenericCard, false },
596 { "32", kGenericCard, false },
597 { "33", kGenericCard, false },
598 { "351", kGenericCard, false },
599 { "3527", kGenericCard, false },
600 { "359", kGenericCard, false },
601 { "50", kGenericCard, false },
602 { "56", kGenericCard, false },
603 { "57", kGenericCard, false },
604 { "58", kGenericCard, false },
605 { "59", kGenericCard, false },
606 { "600", kGenericCard, false },
607 { "602", kGenericCard, false },
608 { "603", kGenericCard, false },
609 { "604", kGenericCard, false },
610 { "605", kGenericCard, false },
611 { "606", kGenericCard, false },
612 { "607", kGenericCard, false },
613 { "608", kGenericCard, false },
614 { "609", kGenericCard, false },
615 { "61", kGenericCard, false },
616 { "63", kGenericCard, false },
617 { "640", kGenericCard, false },
618 { "641", kGenericCard, false },
619 { "642", kGenericCard, false },
620 { "643", kGenericCard, false },
621 { "66", kGenericCard, false },
622 { "67", kGenericCard, false },
623 { "68", kGenericCard, false },
624 { "69", kGenericCard, false },
625 { "7", kGenericCard, false },
626 { "8", kGenericCard, false },
627 { "9", kGenericCard, false },
629 // Oddball case: Unknown issuer, but valid Luhn check and plausible length.
630 { "7000700070007000", kGenericCard, true },
633 for (size_t i = 0; i < arraysize(test_cases); ++i) {
634 base::string16 card_number = ASCIIToUTF16(test_cases[i].card_number);
635 SCOPED_TRACE(card_number);
636 EXPECT_EQ(test_cases[i].type, CreditCard::GetCreditCardType(card_number));
637 EXPECT_EQ(test_cases[i].is_valid, IsValidCreditCardNumber(card_number));
641 TEST(CreditCardTest, LastFourDigits) {
642 CreditCard card(base::GenerateGUID(), "https://www.example.com/");
643 ASSERT_EQ(base::string16(), card.LastFourDigits());
645 test::SetCreditCardInfo(&card, "Baby Face Nelson",
646 "5212341234123489", "01", "2010");
647 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits());
649 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("3489"));
650 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits());
652 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("489"));
653 ASSERT_EQ(base::ASCIIToUTF16("489"), card.LastFourDigits());
656 TEST(CreditCardTest, CanBuildFromCardNumberAndExpirationDate) {
657 base::string16 card_number = base::ASCIIToUTF16("test");
658 int month = 1;
659 int year = 3000;
660 CreditCard card(card_number, month, year);
661 EXPECT_EQ(card_number, card.number());
662 EXPECT_EQ(month, card.expiration_month());
663 EXPECT_EQ(year, card.expiration_year());
666 } // namespace autofill