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/strings/utf_string_conversions.h"
6 #include "base/time/time.h"
7 #include "components/autofill/core/browser/validation.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using base::ASCIIToUTF16
;
15 struct ExpirationDate
{
20 struct IntExpirationDate
{
25 // From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
26 const char* const kValidNumbers
[] = {
34 "6011 0009 9013 9424",
35 "3530-1113-3330-0000",
37 "5555 5555 5555 4444",
38 "5105-1051-0510-5100",
40 "4012 8888 8888 1881",
45 // A UnionPay card that doesn't pass the Luhn checksum
48 const char* const kInvalidNumbers
[] = {
49 "4111 1111 112", /* too short */
50 "41111111111111111115", /* too long */
51 "4111-1111-1111-1110", /* wrong Luhn checksum */
52 "3056 9309 0259 04aa", /* non-digit characters */
54 const char kCurrentDate
[]="1 May 2013";
55 const ExpirationDate kValidCreditCardExpirationDate
[] = {
56 { "2013", "5" }, // Valid month in current year.
57 { "2014", "1" }, // Any month in next year.
58 { "2014", " 1" }, // Whitespace in month.
59 { " 2014", "1" }, // Whitespace in year.
61 const IntExpirationDate kValidCreditCardIntExpirationDate
[] = {
62 { 2013, 5 }, // Valid month in current year.
63 { 2014, 1 }, // Any month in next year.
65 const ExpirationDate kInvalidCreditCardExpirationDate
[] = {
66 { "2013", "04" }, // Previous month in current year.
67 { "2012", "12" }, // Any month in previous year.
69 const IntExpirationDate kInvalidCreditCardIntExpirationDate
[] = {
70 { 2013, 4 }, // Previous month in current year.
71 { 2012, 12 }, // Any month in previous year.
73 const char* const kValidCreditCardSecurityCode
[] = {
74 "323", // 3-digit CSC.
75 "3234", // 4-digit CSC.
77 const char* const kInvalidCreditCardSecurityCode
[] = {
78 "32", // CSC too short.
79 "12345", // CSC too long.
80 "asd", // non-numeric CSC.
82 const char* const kValidEmailAddress
[] = {
85 "user@subdomain.example.com",
86 "user+postfix@example.com",
88 const char* const kInvalidEmailAddress
[] = {
94 const char kAmericanExpressCard
[] = "341111111111111";
95 const char kVisaCard
[] = "4111111111111111";
96 const char kAmericanExpressCVC
[] = "1234";
97 const char kVisaCVC
[] = "123";
100 TEST(AutofillValidation
, IsValidCreditCardNumber
) {
101 for (size_t i
= 0; i
< arraysize(kValidNumbers
); ++i
) {
102 SCOPED_TRACE(kValidNumbers
[i
]);
104 IsValidCreditCardNumber(ASCIIToUTF16(kValidNumbers
[i
])));
106 for (size_t i
= 0; i
< arraysize(kInvalidNumbers
); ++i
) {
107 SCOPED_TRACE(kInvalidNumbers
[i
]);
108 EXPECT_FALSE(IsValidCreditCardNumber(ASCIIToUTF16(kInvalidNumbers
[i
])));
112 TEST(AutofillValidation
, IsValidCreditCardExpirationDate
) {
114 ASSERT_TRUE(base::Time::FromString(kCurrentDate
, &now
));
116 for (size_t i
= 0; i
< arraysize(kValidCreditCardExpirationDate
); ++i
) {
117 const ExpirationDate
& data
= kValidCreditCardExpirationDate
[i
];
118 SCOPED_TRACE(data
.year
);
119 SCOPED_TRACE(data
.month
);
120 EXPECT_TRUE(IsValidCreditCardExpirationDate(ASCIIToUTF16(data
.year
),
121 ASCIIToUTF16(data
.month
),
124 for (size_t i
= 0; i
< arraysize(kInvalidCreditCardExpirationDate
); ++i
) {
125 const ExpirationDate
& data
= kInvalidCreditCardExpirationDate
[i
];
126 SCOPED_TRACE(data
.year
);
127 SCOPED_TRACE(data
.month
);
128 EXPECT_TRUE(!IsValidCreditCardExpirationDate(ASCIIToUTF16(data
.year
),
129 ASCIIToUTF16(data
.month
),
134 TEST(AutofillValidation
, IsValidCreditCardIntExpirationDate
) {
136 ASSERT_TRUE(base::Time::FromString(kCurrentDate
, &now
));
138 for (size_t i
= 0; i
< arraysize(kValidCreditCardIntExpirationDate
); ++i
) {
139 const IntExpirationDate
& data
= kValidCreditCardIntExpirationDate
[i
];
140 SCOPED_TRACE(data
.year
);
141 SCOPED_TRACE(data
.month
);
142 EXPECT_TRUE(IsValidCreditCardExpirationDate(data
.year
, data
.month
, now
));
144 for (size_t i
= 0; i
< arraysize(kInvalidCreditCardIntExpirationDate
); ++i
) {
145 const IntExpirationDate
& data
= kInvalidCreditCardIntExpirationDate
[i
];
146 SCOPED_TRACE(data
.year
);
147 SCOPED_TRACE(data
.month
);
148 EXPECT_TRUE(!IsValidCreditCardExpirationDate(data
.year
, data
.month
, now
));
151 TEST(AutofillValidation
, IsValidCreditCardSecurityCode
) {
152 for (size_t i
= 0; i
< arraysize(kValidCreditCardSecurityCode
); ++i
) {
153 SCOPED_TRACE(kValidCreditCardSecurityCode
[i
]);
154 EXPECT_TRUE(IsValidCreditCardSecurityCode(
155 ASCIIToUTF16(kValidCreditCardSecurityCode
[i
])));
157 for (size_t i
= 0; i
< arraysize(kInvalidCreditCardSecurityCode
); ++i
) {
158 SCOPED_TRACE(kInvalidCreditCardSecurityCode
[i
]);
159 EXPECT_FALSE(IsValidCreditCardSecurityCode(
160 ASCIIToUTF16(kInvalidCreditCardSecurityCode
[i
])));
164 TEST(AutofillValidation
, IsValidEmailAddress
) {
165 for (size_t i
= 0; i
< arraysize(kValidEmailAddress
); ++i
) {
166 SCOPED_TRACE(kValidEmailAddress
[i
]);
167 EXPECT_TRUE(IsValidEmailAddress(ASCIIToUTF16(kValidEmailAddress
[i
])));
169 for (size_t i
= 0; i
< arraysize(kInvalidEmailAddress
); ++i
) {
170 SCOPED_TRACE(kInvalidEmailAddress
[i
]);
171 EXPECT_FALSE(IsValidEmailAddress(ASCIIToUTF16(kInvalidEmailAddress
[i
])));
175 TEST(AutofillValidation
, IsValidCreditCardSecurityCodeWithNumber
) {
176 EXPECT_TRUE(IsValidCreditCardSecurityCode(
177 ASCIIToUTF16(kAmericanExpressCVC
), ASCIIToUTF16(kAmericanExpressCard
)));
178 EXPECT_TRUE(IsValidCreditCardSecurityCode(
179 ASCIIToUTF16(kVisaCVC
), ASCIIToUTF16(kVisaCard
)));
180 EXPECT_FALSE(IsValidCreditCardSecurityCode(
181 ASCIIToUTF16(kVisaCVC
), ASCIIToUTF16(kAmericanExpressCard
)));
182 EXPECT_FALSE(IsValidCreditCardSecurityCode(
183 ASCIIToUTF16(kAmericanExpressCVC
), ASCIIToUTF16(kVisaCard
)));
184 EXPECT_TRUE(IsValidCreditCardSecurityCode(
185 ASCIIToUTF16(kVisaCVC
), ASCIIToUTF16(kInvalidNumbers
[0])));
186 EXPECT_FALSE(IsValidCreditCardSecurityCode(
187 ASCIIToUTF16(kAmericanExpressCVC
), ASCIIToUTF16(kInvalidNumbers
[0])));
190 } // namespace autofill