Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / core / browser / validation_unittest.cc
blob92e951552d0faa5d5315eff6d48178f9228056c8
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;
12 namespace autofill {
13 namespace {
15 struct ExpirationDate {
16 const char* year;
17 const char* month;
20 struct IntExpirationDate {
21 const int year;
22 const int month;
25 // From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
26 const char* const kValidNumbers[] = {
27 "378282246310005",
28 "3714 4963 5398 431",
29 "3787-3449-3671-000",
30 "5610591081018250",
31 "3056 9309 0259 04",
32 "3852-0000-0232-37",
33 "6011111111111117",
34 "6011 0009 9013 9424",
35 "3530-1113-3330-0000",
36 "3566002020360505",
37 "5555 5555 5555 4444",
38 "5105-1051-0510-5100",
39 "4111111111111111",
40 "4012 8888 8888 1881",
41 "4222-2222-2222-2",
42 "5019717010103742",
43 "6331101999990016",
45 // A UnionPay card that doesn't pass the Luhn checksum
46 "6200000000000000",
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[] = {
83 "user@example",
84 "user@example.com",
85 "user@subdomain.example.com",
86 "user+postfix@example.com",
88 const char* const kInvalidEmailAddress[] = {
89 "user",
90 "foo.com",
91 "user@",
92 "user@=example.com"
94 const char kAmericanExpressCard[] = "341111111111111";
95 const char kVisaCard[] = "4111111111111111";
96 const char kAmericanExpressCVC[] = "1234";
97 const char kVisaCVC[] = "123";
98 } // namespace
100 TEST(AutofillValidation, IsValidCreditCardNumber) {
101 for (size_t i = 0; i < arraysize(kValidNumbers); ++i) {
102 SCOPED_TRACE(kValidNumbers[i]);
103 EXPECT_TRUE(
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) {
113 base::Time now;
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),
122 now));
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),
130 now));
134 TEST(AutofillValidation, IsValidCreditCardIntExpirationDate) {
135 base::Time now;
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