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 autofill::IsValidCreditCardNumber(ASCIIToUTF16(kValidNumbers
[i
])));
106 for (size_t i
= 0; i
< arraysize(kInvalidNumbers
); ++i
) {
107 SCOPED_TRACE(kInvalidNumbers
[i
]);
109 autofill::IsValidCreditCardNumber(ASCIIToUTF16(kInvalidNumbers
[i
])));
113 TEST(AutofillValidation
, IsValidCreditCardExpirationDate
)
116 ASSERT_TRUE(base::Time::FromString(kCurrentDate
, &now
));
118 for (size_t i
= 0; i
< arraysize(kValidCreditCardExpirationDate
); ++i
) {
119 const ExpirationDate
& data
= kValidCreditCardExpirationDate
[i
];
120 SCOPED_TRACE(data
.year
);
121 SCOPED_TRACE(data
.month
);
123 autofill::IsValidCreditCardExpirationDate(ASCIIToUTF16(data
.year
),
124 ASCIIToUTF16(data
.month
),
127 for (size_t i
= 0; i
< arraysize(kInvalidCreditCardExpirationDate
); ++i
) {
128 const ExpirationDate
& data
= kInvalidCreditCardExpirationDate
[i
];
129 SCOPED_TRACE(data
.year
);
130 SCOPED_TRACE(data
.month
);
132 !autofill::IsValidCreditCardExpirationDate(ASCIIToUTF16(data
.year
),
133 ASCIIToUTF16(data
.month
),
138 TEST(AutofillValidation
, IsValidCreditCardIntExpirationDate
)
141 ASSERT_TRUE(base::Time::FromString(kCurrentDate
, &now
));
143 for (size_t i
= 0; i
< arraysize(kValidCreditCardIntExpirationDate
); ++i
) {
144 const IntExpirationDate
& data
= kValidCreditCardIntExpirationDate
[i
];
145 SCOPED_TRACE(data
.year
);
146 SCOPED_TRACE(data
.month
);
148 autofill::IsValidCreditCardExpirationDate(data
.year
, data
.month
, now
));
150 for (size_t i
= 0; i
< arraysize(kInvalidCreditCardIntExpirationDate
); ++i
) {
151 const IntExpirationDate
& data
= kInvalidCreditCardIntExpirationDate
[i
];
152 SCOPED_TRACE(data
.year
);
153 SCOPED_TRACE(data
.month
);
155 !autofill::IsValidCreditCardExpirationDate(data
.year
, data
.month
, now
));
158 TEST(AutofillValidation
, IsValidCreditCardSecurityCode
) {
159 for (size_t i
= 0; i
< arraysize(kValidCreditCardSecurityCode
); ++i
) {
160 SCOPED_TRACE(kValidCreditCardSecurityCode
[i
]);
162 autofill::IsValidCreditCardSecurityCode(
163 ASCIIToUTF16(kValidCreditCardSecurityCode
[i
])));
165 for (size_t i
= 0; i
< arraysize(kInvalidCreditCardSecurityCode
); ++i
) {
166 SCOPED_TRACE(kInvalidCreditCardSecurityCode
[i
]);
168 autofill::IsValidCreditCardSecurityCode(
169 ASCIIToUTF16(kInvalidCreditCardSecurityCode
[i
])));
173 TEST(AutofillValidation
, IsValidEmailAddress
) {
174 for (size_t i
= 0; i
< arraysize(kValidEmailAddress
); ++i
) {
175 SCOPED_TRACE(kValidEmailAddress
[i
]);
177 autofill::IsValidEmailAddress(ASCIIToUTF16(kValidEmailAddress
[i
])));
179 for (size_t i
= 0; i
< arraysize(kInvalidEmailAddress
); ++i
) {
180 SCOPED_TRACE(kInvalidEmailAddress
[i
]);
182 autofill::IsValidEmailAddress(ASCIIToUTF16(kInvalidEmailAddress
[i
])));
186 TEST(AutofillValidation
, IsValidCreditCardSecurityCodeWithNumber
) {
187 EXPECT_TRUE(autofill::IsValidCreditCardSecurityCode(
188 ASCIIToUTF16(kAmericanExpressCVC
), ASCIIToUTF16(kAmericanExpressCard
)));
189 EXPECT_TRUE(autofill::IsValidCreditCardSecurityCode(
190 ASCIIToUTF16(kVisaCVC
), ASCIIToUTF16(kVisaCard
)));
191 EXPECT_FALSE(autofill::IsValidCreditCardSecurityCode(
192 ASCIIToUTF16(kVisaCVC
), ASCIIToUTF16(kAmericanExpressCard
)));
193 EXPECT_FALSE(autofill::IsValidCreditCardSecurityCode(
194 ASCIIToUTF16(kAmericanExpressCVC
), ASCIIToUTF16(kVisaCard
)));
195 EXPECT_TRUE(autofill::IsValidCreditCardSecurityCode(
196 ASCIIToUTF16(kVisaCVC
), ASCIIToUTF16(kInvalidNumbers
[0])));
197 EXPECT_FALSE(autofill::IsValidCreditCardSecurityCode(
198 ASCIIToUTF16(kAmericanExpressCVC
), ASCIIToUTF16(kInvalidNumbers
[0])));
201 } // namespace autofill