Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / autofill / core / browser / validation_unittest.cc
blob82ca746b3e31610210e334548a1b3df52134dc48
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 autofill::IsValidCreditCardNumber(ASCIIToUTF16(kValidNumbers[i])));
106 for (size_t i = 0; i < arraysize(kInvalidNumbers); ++i) {
107 SCOPED_TRACE(kInvalidNumbers[i]);
108 EXPECT_FALSE(
109 autofill::IsValidCreditCardNumber(ASCIIToUTF16(kInvalidNumbers[i])));
113 TEST(AutofillValidation, IsValidCreditCardExpirationDate)
115 base::Time now;
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);
122 EXPECT_TRUE(
123 autofill::IsValidCreditCardExpirationDate(ASCIIToUTF16(data.year),
124 ASCIIToUTF16(data.month),
125 now));
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);
131 EXPECT_TRUE(
132 !autofill::IsValidCreditCardExpirationDate(ASCIIToUTF16(data.year),
133 ASCIIToUTF16(data.month),
134 now));
138 TEST(AutofillValidation, IsValidCreditCardIntExpirationDate)
140 base::Time now;
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);
147 EXPECT_TRUE(
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);
154 EXPECT_TRUE(
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]);
161 EXPECT_TRUE(
162 autofill::IsValidCreditCardSecurityCode(
163 ASCIIToUTF16(kValidCreditCardSecurityCode[i])));
165 for (size_t i = 0; i < arraysize(kInvalidCreditCardSecurityCode); ++i) {
166 SCOPED_TRACE(kInvalidCreditCardSecurityCode[i]);
167 EXPECT_FALSE(
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]);
176 EXPECT_TRUE(
177 autofill::IsValidEmailAddress(ASCIIToUTF16(kValidEmailAddress[i])));
179 for (size_t i = 0; i < arraysize(kInvalidEmailAddress); ++i) {
180 SCOPED_TRACE(kInvalidEmailAddress[i]);
181 EXPECT_FALSE(
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