Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1762)

Unified Diff: chrome/browser/autofill/validation_unittest.cc

Issue 12213077: [Autofill] Credit Card validation for rAc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/autofill/validation_unittest.cc
diff --git a/chrome/browser/autofill/validation_unittest.cc b/chrome/browser/autofill/validation_unittest.cc
index bb94a0fcc07796f9befdeed660c5e2eea9ccba6d..077582c904b1bf030463d0652805fc7bf45bb600 100644
--- a/chrome/browser/autofill/validation_unittest.cc
+++ b/chrome/browser/autofill/validation_unittest.cc
@@ -2,12 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/validation.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
+// Test data for expiration date validation.
+struct DateTestData {
+ const char* const value; // Expiration date.
+ int digits; // Number of expected digits for expiration year.
+};
+
// From https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
const char* const kValidNumbers[] = {
"378282246310005",
@@ -34,7 +41,78 @@ const char* const kInvalidNumbers[] = {
"4111-1111-1111-1110", /* wrong Luhn checksum */
"3056 9309 0259 04aa", /* non-digit characters */
};
-
+const char kCurrentDate[]="1 May 2013";
+DateTestData kValidCCYears[] = {
+ { "13", 2 }, // Current year.
+ { "2013", 4 }, // Current year.
+ { "23", 2 }, // Future year.
+ { "2023", 4 }, // Future year.
+};
+DateTestData kInvalidCCYears[] = {
+ { "12", 2 }, // Past year.
+ { "2012", 4 }, // Past year.
+ { "1812", 4 }, // Past year.
+ { "rr", 2 }, // Invalid characters.
+ { "2r", 2 }, // Invalid characters.
+ { "rrrr", 4 }, // Invalid characters.
+ { "2rrr", 4 }, // Invalid characters.
+ { "3", 2 }, // string too short.
+ { "333", 4 }, // string too short.
+ { "333", 2 }, // string too long.
+ { "33333", 4 }, // string too long.
+};
+DateTestData kValidCCDates[] = {
+ { "05/13", 2 }, // Current month & year.
+ { "05/2013", 4 }, // Current month & year.
+ { "13-05", 2 }, // Current month & year.
+ { "2013-05", 4 }, // Current month & year.
+ { "01/14", 2 }, // Next month & year.
+ { "01/2014", 4 }, // Next month & year.
+ { "14-01", 2 }, // Next month & year.
+ { "2014-01", 4 }, // Next month & year.
+};
+DateTestData kInvalidCCDates[] = {
+ { "04/13", 2 }, // Past month.
+ { "04/2013", 4 }, // Past month.
+ { "13-04", 2 }, // Past month.
+ { "2013-04", 4 }, // Past month.
+ { "12/12", 2 }, // Past month.
+ { "12/2012", 4 }, // Past year.
+ { "12-12", 2 }, // Past year.
+ { "2012-12", 4 }, // Past year.
+ { "1/14", 2 }, // Month too short.
+ { "1/2014", 4 }, // Month too short.
+ { "14-1", 2 }, // Month too short.
+ { "2014-1", 4 }, // Month too short.
+ { "111/14", 2 }, // Month too long.
+ { "111/2014", 4 }, // Month too long.
+ { "14-111", 2 }, // Month too long.
+ { "2014-111", 4 }, // Month too long.
+ { "12/133", 2 }, // Year too long.
+ { "12/20133", 4 }, // Year too long.
+ { "133-12", 2 }, // Year too long.
+ { "20133-12", 4 }, // Year too long.
+ { "12/1a", 2 }, // Invalid character (month).
+ { "12/20a3", 4 }, // Invalid character (month).
+ { "13-1a", 2 }, // Invalid character (month).
+ { "2013-1a", 4 }, // Invalid character (month).
+ { "12/a3", 2 }, // Invalid character (year).
+ { "12/2a13", 4 }, // Invalid character (year).
+ { "a3-12", 2 }, // Invalid character (year).
+ { "2a13-12", 4 }, // Invalid character (year).
+ { "12+12", 2 }, // Invalid separator.
+ { "12+2012", 4 }, // Invalid separator.
+ { "2012+12", 4 }, // Invalid separator.
+};
+const char* const kValidCSC[] = {
+ "323", // 3-digit CSC.
+ "3234", // 4-digit CSC.
+};
+const char* const kInvalidCSC[] = {
+ "32", // CSC too short.
+ "12345", // CSC too long.
+ "asd", // non-numeric CSC.
+};
} // namespace
TEST(AutofillValidation, IsValidCreditCardNumber) {
@@ -49,3 +127,59 @@ TEST(AutofillValidation, IsValidCreditCardNumber) {
autofill::IsValidCreditCardNumber(ASCIIToUTF16(kInvalidNumbers[i])));
}
}
+
+TEST(AutofillValidation, IsValidCreditCardExpirationYear)
+{
+ base::Time now;
+ ASSERT_TRUE(base::Time::FromString(kCurrentDate, &now));
+
+ for (size_t i = 0; i < arraysize(kValidCCYears); ++i) {
+ const DateTestData data = kValidCCYears[i];
+ SCOPED_TRACE(data.value);
+ EXPECT_TRUE(
+ autofill::IsValidCreditCardExpirationYear(
+ ASCIIToUTF16(data.value), data.digits, now));
+ }
+ for (size_t i = 0; i < arraysize(kInvalidCCYears); ++i) {
+ const DateTestData data = kInvalidCCYears[i];
+ SCOPED_TRACE(data.value);
+ EXPECT_FALSE(
+ autofill::IsValidCreditCardExpirationYear(
+ ASCIIToUTF16(data.value), data.digits, now));
+ }
+}
+
+TEST(AutofillValidation, IsValidCreditCardExpirationDate)
+{
+ base::Time now;
+ ASSERT_TRUE(base::Time::FromString(kCurrentDate, &now));
+
+ for (size_t i = 0; i < arraysize(kValidCCDates); ++i) {
+ const DateTestData data = kValidCCDates[i];
+ SCOPED_TRACE(data.value);
+ EXPECT_TRUE(
+ autofill::IsValidCreditCardExpirationDate(
+ ASCIIToUTF16(data.value), data.digits, now));
+ }
+ for (size_t i = 0; i < arraysize(kInvalidCCDates); ++i) {
+ const DateTestData data = kInvalidCCDates[i];
+ SCOPED_TRACE(data.value);
+ EXPECT_FALSE(
+ autofill::IsValidCreditCardExpirationDate(
+ ASCIIToUTF16(data.value), data.digits, now));
+ }
+}
+
+TEST(AutofillValidation, IsValidCreditCardCSC) {
+ for (size_t i = 0; i < arraysize(kValidCSC); ++i) {
+ SCOPED_TRACE(kValidCSC[i]);
+ EXPECT_TRUE(
+ autofill::IsValidCreditCardCSC(ASCIIToUTF16(kValidCSC[i])));
+ }
+ for (size_t i = 0; i < arraysize(kInvalidCSC); ++i) {
+ SCOPED_TRACE(kInvalidCSC[i]);
+ EXPECT_FALSE(
+ autofill::IsValidCreditCardCSC(ASCIIToUTF16(kInvalidCSC[i])));
+ }
+}
+

Powered by Google App Engine
This is Rietveld 408576698