This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/validation.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
namespace autofill {
namespace {
struct ExpirationDate {
const char* year;
const char* month;
};
struct IntExpirationDate {
const int year;
const int month;
};
const char* const kValidNumbers[] = {
"378282246310005",
"3714 4963 5398 431",
"3787-3449-3671-000",
"5610591081018250",
"3056 9309 0259 04",
"3852-0000-0232-37",
"6011111111111117",
"6011 0009 9013 9424",
"3530-1113-3330-0000",
"3566002020360505",
"5555 5555 5555 4444",
"5105-1051-0510-5100",
"4111111111111111",
"4012 8888 8888 1881",
"4222-2222-2222-2",
"5019717010103742",
"6331101999990016",
"6200000000000000",
};
const char* const kInvalidNumbers[] = {
"4111 1111 112",
"41111111111111111115",
"4111-1111-1111-1110",
"3056 9309 0259 04aa",
};
const char kCurrentDate[]="1 May 2013";
const ExpirationDate kValidCreditCardExpirationDate[] = {
{ "2013", "5" },
{ "2014", "1" },
{ "2014", " 1" },
{ " 2014", "1" },
};
const IntExpirationDate kValidCreditCardIntExpirationDate[] = {
{ 2013, 5 },
{ 2014, 1 },
};
const ExpirationDate kInvalidCreditCardExpirationDate[] = {
{ "2013", "04" },
{ "2012", "12" },
};
const IntExpirationDate kInvalidCreditCardIntExpirationDate[] = {
{ 2013, 4 },
{ 2012, 12 },
};
const char* const kValidCreditCardSecurityCode[] = {
"323",
"3234",
};
const char* const kInvalidCreditCardSecurityCode[] = {
"32",
"12345",
"asd",
};
const char* const kValidEmailAddress[] = {
"user@example",
"user@example.com",
"user@subdomain.example.com",
"user+postfix@example.com",
};
const char* const kInvalidEmailAddress[] = {
"user",
"foo.com",
"user@",
"user@=example.com"
};
const char kAmericanExpressCard[] = "341111111111111";
const char kVisaCard[] = "4111111111111111";
const char kAmericanExpressCVC[] = "1234";
const char kVisaCVC[] = "123";
}
TEST(AutofillValidation, IsValidCreditCardNumber) {
for (size_t i = 0; i < arraysize(kValidNumbers); ++i) {
SCOPED_TRACE(kValidNumbers[i]);
EXPECT_TRUE(
autofill::IsValidCreditCardNumber(ASCIIToUTF16(kValidNumbers[i])));
}
for (size_t i = 0; i < arraysize(kInvalidNumbers); ++i) {
SCOPED_TRACE(kInvalidNumbers[i]);
EXPECT_FALSE(
autofill::IsValidCreditCardNumber(ASCIIToUTF16(kInvalidNumbers[i])));
}
}
TEST(AutofillValidation, IsValidCreditCardExpirationDate)
{
base::Time now;
ASSERT_TRUE(base::Time::FromString(kCurrentDate, &now));
for (size_t i = 0; i < arraysize(kValidCreditCardExpirationDate); ++i) {
const ExpirationDate& data = kValidCreditCardExpirationDate[i];
SCOPED_TRACE(data.year);
SCOPED_TRACE(data.month);
EXPECT_TRUE(
autofill::IsValidCreditCardExpirationDate(ASCIIToUTF16(data.year),
ASCIIToUTF16(data.month),
now));
}
for (size_t i = 0; i < arraysize(kInvalidCreditCardExpirationDate); ++i) {
const ExpirationDate& data = kInvalidCreditCardExpirationDate[i];
SCOPED_TRACE(data.year);
SCOPED_TRACE(data.month);
EXPECT_TRUE(
!autofill::IsValidCreditCardExpirationDate(ASCIIToUTF16(data.year),
ASCIIToUTF16(data.month),
now));
}
}
TEST(AutofillValidation, IsValidCreditCardIntExpirationDate)
{
base::Time now;
ASSERT_TRUE(base::Time::FromString(kCurrentDate, &now));
for (size_t i = 0; i < arraysize(kValidCreditCardIntExpirationDate); ++i) {
const IntExpirationDate& data = kValidCreditCardIntExpirationDate[i];
SCOPED_TRACE(data.year);
SCOPED_TRACE(data.month);
EXPECT_TRUE(
autofill::IsValidCreditCardExpirationDate(data.year, data.month, now));
}
for (size_t i = 0; i < arraysize(kInvalidCreditCardIntExpirationDate); ++i) {
const IntExpirationDate& data = kInvalidCreditCardIntExpirationDate[i];
SCOPED_TRACE(data.year);
SCOPED_TRACE(data.month);
EXPECT_TRUE(
!autofill::IsValidCreditCardExpirationDate(data.year, data.month, now));
}
}
TEST(AutofillValidation, IsValidCreditCardSecurityCode) {
for (size_t i = 0; i < arraysize(kValidCreditCardSecurityCode); ++i) {
SCOPED_TRACE(kValidCreditCardSecurityCode[i]);
EXPECT_TRUE(
autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kValidCreditCardSecurityCode[i])));
}
for (size_t i = 0; i < arraysize(kInvalidCreditCardSecurityCode); ++i) {
SCOPED_TRACE(kInvalidCreditCardSecurityCode[i]);
EXPECT_FALSE(
autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kInvalidCreditCardSecurityCode[i])));
}
}
TEST(AutofillValidation, IsValidEmailAddress) {
for (size_t i = 0; i < arraysize(kValidEmailAddress); ++i) {
SCOPED_TRACE(kValidEmailAddress[i]);
EXPECT_TRUE(
autofill::IsValidEmailAddress(ASCIIToUTF16(kValidEmailAddress[i])));
}
for (size_t i = 0; i < arraysize(kInvalidEmailAddress); ++i) {
SCOPED_TRACE(kInvalidEmailAddress[i]);
EXPECT_FALSE(
autofill::IsValidEmailAddress(ASCIIToUTF16(kInvalidEmailAddress[i])));
}
}
TEST(AutofillValidation, IsValidCreditCardSecurityCodeWithNumber) {
EXPECT_TRUE(autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kAmericanExpressCVC), ASCIIToUTF16(kAmericanExpressCard)));
EXPECT_TRUE(autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kVisaCVC), ASCIIToUTF16(kVisaCard)));
EXPECT_FALSE(autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kVisaCVC), ASCIIToUTF16(kAmericanExpressCard)));
EXPECT_FALSE(autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kAmericanExpressCVC), ASCIIToUTF16(kVisaCard)));
EXPECT_TRUE(autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kVisaCVC), ASCIIToUTF16(kInvalidNumbers[0])));
EXPECT_FALSE(autofill::IsValidCreditCardSecurityCode(
ASCIIToUTF16(kAmericanExpressCVC), ASCIIToUTF16(kInvalidNumbers[0])));
}
}