Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 // Result of assignment should be logically equal to the original profile. | 151 // Result of assignment should be logically equal to the original profile. |
| 152 CreditCard b(base::GenerateGUID(), "some other origin"); | 152 CreditCard b(base::GenerateGUID(), "some other origin"); |
| 153 b = a; | 153 b = a; |
| 154 EXPECT_TRUE(a == b); | 154 EXPECT_TRUE(a == b); |
| 155 | 155 |
| 156 // Assignment to self should not change the profile value. | 156 // Assignment to self should not change the profile value. |
| 157 a = a; | 157 a = a; |
| 158 EXPECT_TRUE(a == b); | 158 EXPECT_TRUE(a == b); |
| 159 } | 159 } |
| 160 | 160 |
| 161 TEST(CreditCardTest, SetExpirationYearFromString) { | 161 struct SetExpirationYearFromStringTestCase { |
| 162 static const struct { | 162 std::string expiration_year; |
| 163 std::string expiration_year; | 163 int expected_year; |
| 164 int expected_year; | 164 }; |
| 165 } kTestCases[] = { | |
| 166 // Valid values. | |
| 167 {"2040", 2040}, | |
| 168 {"45", 2045}, | |
| 169 {"045", 2045}, | |
| 170 {"9", 2009}, | |
| 171 | 165 |
| 172 // Unrecognized year values. | 166 class SetExpirationYearFromStringTest |
| 173 {"052045", 0}, | 167 : public testing::TestWithParam<SetExpirationYearFromStringTestCase> {}; |
| 174 {"123", 0}, | |
| 175 {"y2045", 0}, | |
| 176 }; | |
| 177 | 168 |
| 178 for (const auto& test_case : kTestCases) { | 169 TEST_P(SetExpirationYearFromStringTest, SetExpirationYearFromString) { |
| 179 CreditCard card(base::GenerateGUID(), "some origin"); | 170 auto test_case = GetParam(); |
| 180 card.SetExpirationYearFromString(ASCIIToUTF16(test_case.expiration_year)); | 171 CreditCard card(base::GenerateGUID(), "some origin"); |
| 172 card.SetExpirationYearFromString(ASCIIToUTF16(test_case.expiration_year)); | |
| 181 | 173 |
| 182 EXPECT_EQ(test_case.expected_year, card.expiration_year()) | 174 EXPECT_EQ(test_case.expected_year, card.expiration_year()) |
| 183 << test_case.expiration_year << " " << test_case.expected_year; | 175 << test_case.expiration_year << " " << test_case.expected_year; |
| 184 } | |
| 185 } | 176 } |
| 186 | 177 |
| 187 TEST(CreditCardTest, SetExpirationDateFromString) { | 178 INSTANTIATE_TEST_CASE_P(CreditCardTest, |
| 188 static const struct { | 179 SetExpirationYearFromStringTest, |
| 189 std::string expiration_date; | 180 testing::Values( |
| 190 int expected_month; | 181 // Valid values. |
| 191 int expected_year; | 182 SetExpirationYearFromStringTestCase{"2040", 2040}, |
| 192 } kTestCases[] = {{"10", 0, 0}, // Too small. | 183 SetExpirationYearFromStringTestCase{"45", 2045}, |
| 193 {"1020451", 0, 0}, // Too long. | 184 SetExpirationYearFromStringTestCase{"045", 2045}, |
| 185 SetExpirationYearFromStringTestCase{"9", 2009}, | |
| 194 | 186 |
| 195 // No separators. | 187 // Unrecognized year values. |
| 196 {"105", 0, 0}, // Too ambiguous. | 188 SetExpirationYearFromStringTestCase{"052045", 0}, |
| 197 {"0545", 5, 2045}, | 189 SetExpirationYearFromStringTestCase{"123", 0}, |
| 198 {"52045", 0, 0}, // Too ambiguous. | 190 SetExpirationYearFromStringTestCase{"y2045", 0})); |
| 199 {"052045", 5, 2045}, | |
| 200 | 191 |
| 201 // "/" separator. | 192 struct SetExpirationDateFromStringTestCase { |
| 202 {"05/45", 5, 2045}, | 193 std::string expiration_date; |
| 203 {"5/2045", 5, 2045}, | 194 int expected_month; |
| 204 {"05/2045", 5, 2045}, | 195 int expected_year; |
| 196 }; | |
| 205 | 197 |
| 206 // "-" separator. | 198 class SetExpirationDateFromStringTest |
| 207 {"05-45", 5, 2045}, | 199 : public testing::TestWithParam<SetExpirationDateFromStringTestCase> {}; |
| 208 {"5-2045", 5, 2045}, | |
| 209 {"05-2045", 5, 2045}, | |
| 210 | 200 |
| 211 // "|" separator. | 201 TEST_P(SetExpirationDateFromStringTest, SetExpirationDateFromString) { |
| 212 {"05|45", 5, 2045}, | 202 auto test_case = GetParam(); |
| 213 {"5|2045", 5, 2045}, | 203 CreditCard card(base::GenerateGUID(), "some origin"); |
| 214 {"05|2045", 5, 2045}, | 204 card.SetExpirationDateFromString(ASCIIToUTF16(test_case.expiration_date)); |
| 215 | 205 |
| 216 // Invalid values. | 206 EXPECT_EQ(test_case.expected_month, card.expiration_month()); |
| 217 {"13/2016", 0, 2016}, | 207 EXPECT_EQ(test_case.expected_year, card.expiration_year()); |
| 218 {"16/13", 0, 2013}, | 208 } |
| 219 {"May-2015", 0, 0}, | |
| 220 {"05-/2045", 0, 0}, | |
| 221 {"05_2045", 0, 0}}; | |
| 222 | 209 |
| 223 for (const auto& test_case : kTestCases) { | 210 INSTANTIATE_TEST_CASE_P( |
| 224 CreditCard card(base::GenerateGUID(), "some origin"); | 211 CreditCardTest, |
| 225 card.SetExpirationDateFromString(ASCIIToUTF16(test_case.expiration_date)); | 212 SetExpirationDateFromStringTest, |
| 213 testing::Values( | |
| 214 SetExpirationDateFromStringTestCase{"10", 0, 0}, // Too small. | |
| 215 SetExpirationDateFromStringTestCase{"1020451", 0, 0}, // Too long. | |
| 226 | 216 |
| 227 EXPECT_EQ(test_case.expected_month, card.expiration_month()); | 217 // No separators. |
| 228 EXPECT_EQ(test_case.expected_year, card.expiration_year()); | 218 SetExpirationDateFromStringTestCase{"105", 0, 0}, // Too ambiguous. |
| 229 } | 219 SetExpirationDateFromStringTestCase{"0545", 5, 2045}, |
| 230 } | 220 SetExpirationDateFromStringTestCase{"52045", 0, 0}, // Too ambiguous. |
| 221 SetExpirationDateFromStringTestCase{"052045", 5, 2045}, | |
| 222 | |
| 223 // "/" separator. | |
| 224 SetExpirationDateFromStringTestCase{"05/45", 5, 2045}, | |
| 225 SetExpirationDateFromStringTestCase{"5/2045", 5, 2045}, | |
| 226 SetExpirationDateFromStringTestCase{"05/2045", 5, 2045}, | |
| 227 | |
| 228 // "-" separator. | |
| 229 SetExpirationDateFromStringTestCase{"05-45", 5, 2045}, | |
| 230 SetExpirationDateFromStringTestCase{"5-2045", 5, 2045}, | |
| 231 SetExpirationDateFromStringTestCase{"05-2045", 5, 2045}, | |
| 232 | |
| 233 // "|" separator. | |
| 234 SetExpirationDateFromStringTestCase{"05|45", 5, 2045}, | |
| 235 SetExpirationDateFromStringTestCase{"5|2045", 5, 2045}, | |
| 236 SetExpirationDateFromStringTestCase{"05|2045", 5, 2045}, | |
| 237 | |
| 238 // Invalid values. | |
| 239 SetExpirationDateFromStringTestCase{"13/2016", 0, 2016}, | |
| 240 SetExpirationDateFromStringTestCase{"16/13", 0, 2013}, | |
| 241 SetExpirationDateFromStringTestCase{"May-2015", 0, 0}, | |
| 242 SetExpirationDateFromStringTestCase{"05-/2045", 0, 0}, | |
| 243 SetExpirationDateFromStringTestCase{"05_2045", 0, 0})); | |
| 231 | 244 |
| 232 TEST(CreditCardTest, Copy) { | 245 TEST(CreditCardTest, Copy) { |
| 233 CreditCard a(base::GenerateGUID(), "https://www.example.com"); | 246 CreditCard a(base::GenerateGUID(), "https://www.example.com"); |
| 234 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010"); | 247 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010"); |
| 235 | 248 |
| 236 // Clone should be logically equal to the original. | 249 // Clone should be logically equal to the original. |
| 237 CreditCard b(a); | 250 CreditCard b(a); |
| 238 EXPECT_TRUE(a == b); | 251 EXPECT_TRUE(a == b); |
| 239 } | 252 } |
| 240 | 253 |
| 241 TEST(CreditCardTest, IsLocalDuplicateOfServerCard) { | 254 struct IsLocalDuplicateOfServerCardTestCase { |
| 242 struct { | 255 CreditCard::RecordType first_card_record_type; |
| 243 CreditCard::RecordType first_card_record_type; | 256 const char* first_card_name; |
| 244 const char* first_card_name; | 257 const char* first_card_number; |
| 245 const char* first_card_number; | 258 const char* first_card_exp_mo; |
| 246 const char* first_card_exp_mo; | 259 const char* first_card_exp_yr; |
| 247 const char* first_card_exp_yr; | |
| 248 | 260 |
| 249 CreditCard::RecordType second_card_record_type; | 261 CreditCard::RecordType second_card_record_type; |
| 250 const char* second_card_name; | 262 const char* second_card_name; |
| 251 const char* second_card_number; | 263 const char* second_card_number; |
| 252 const char* second_card_exp_mo; | 264 const char* second_card_exp_mo; |
| 253 const char* second_card_exp_yr; | 265 const char* second_card_exp_yr; |
| 254 const char* second_card_type; | 266 const char* second_card_type; |
| 255 | 267 |
| 256 bool is_local_duplicate; | 268 bool is_local_duplicate; |
| 257 } test_cases[] = { | 269 }; |
| 258 { LOCAL_CARD, "", "", "", "", | |
| 259 LOCAL_CARD, "", "", "", "", nullptr, false }, | |
| 260 { LOCAL_CARD, "", "", "", "", | |
| 261 FULL_SERVER_CARD, "", "", "", "", nullptr, true}, | |
| 262 { FULL_SERVER_CARD, "", "", "", "", | |
| 263 FULL_SERVER_CARD, "", "", "", "", nullptr, false}, | |
| 264 { LOCAL_CARD, "John Dillinger", "423456789012", "01", "2010", | |
| 265 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, | |
| 266 true }, | |
| 267 { LOCAL_CARD, "J Dillinger", "423456789012", "01", "2010", | |
| 268 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, | |
| 269 false }, | |
| 270 { LOCAL_CARD, "", "423456789012", "01", "2010", | |
| 271 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, | |
| 272 true }, | |
| 273 { LOCAL_CARD, "", "423456789012", "", "", | |
| 274 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, | |
| 275 true }, | |
| 276 { LOCAL_CARD, "", "423456789012", "", "", | |
| 277 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kVisaCard, | |
| 278 true }, | |
| 279 { LOCAL_CARD, "", "423456789012", "", "", | |
| 280 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kMasterCard, | |
| 281 false }, | |
| 282 { LOCAL_CARD, "John Dillinger", "4234-5678-9012", "01", "2010", | |
| 283 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, | |
| 284 true }, | |
| 285 }; | |
| 286 | 270 |
| 287 for (const auto& test_case : test_cases) { | 271 class IsLocalDuplicateOfServerCardTest |
| 288 CreditCard a(base::GenerateGUID(), std::string()); | 272 : public testing::TestWithParam<IsLocalDuplicateOfServerCardTestCase> {}; |
| 289 a.set_record_type(test_case.first_card_record_type); | |
| 290 test::SetCreditCardInfo( | |
| 291 &a, test_case.first_card_name, test_case.first_card_number, | |
| 292 test_case.first_card_exp_mo, test_case.first_card_exp_yr); | |
| 293 | 273 |
| 294 CreditCard b(base::GenerateGUID(), std::string()); | 274 TEST_P(IsLocalDuplicateOfServerCardTest, IsLocalDuplicateOfServerCard) { |
| 295 b.set_record_type(test_case.second_card_record_type); | 275 auto test_case = GetParam(); |
| 296 test::SetCreditCardInfo( | 276 CreditCard a(base::GenerateGUID(), std::string()); |
| 297 &b, test_case.second_card_name, test_case.second_card_number, | 277 a.set_record_type(test_case.first_card_record_type); |
| 298 test_case.second_card_exp_mo, test_case.second_card_exp_yr); | 278 test::SetCreditCardInfo( |
| 279 &a, test_case.first_card_name, test_case.first_card_number, | |
| 280 test_case.first_card_exp_mo, test_case.first_card_exp_yr); | |
| 299 | 281 |
| 300 if (test_case.second_card_record_type == CreditCard::MASKED_SERVER_CARD) | 282 CreditCard b(base::GenerateGUID(), std::string()); |
| 301 b.SetTypeForMaskedCard(test_case.second_card_type); | 283 b.set_record_type(test_case.second_card_record_type); |
| 284 test::SetCreditCardInfo( | |
| 285 &b, test_case.second_card_name, test_case.second_card_number, | |
| 286 test_case.second_card_exp_mo, test_case.second_card_exp_yr); | |
| 302 | 287 |
| 303 EXPECT_EQ(test_case.is_local_duplicate, a.IsLocalDuplicateOfServerCard(b)) | 288 if (test_case.second_card_record_type == CreditCard::MASKED_SERVER_CARD) |
| 304 << " when comparing cards " << a.Label() << " and " << b.Label(); | 289 b.SetTypeForMaskedCard(test_case.second_card_type); |
| 305 } | 290 |
| 291 EXPECT_EQ(test_case.is_local_duplicate, a.IsLocalDuplicateOfServerCard(b)) | |
| 292 << " when comparing cards " << a.Label() << " and " << b.Label(); | |
| 306 } | 293 } |
| 307 | 294 |
| 295 INSTANTIATE_TEST_CASE_P( | |
| 296 CreditCardTest, | |
| 297 IsLocalDuplicateOfServerCardTest, | |
| 298 testing::Values( | |
| 299 IsLocalDuplicateOfServerCardTestCase{LOCAL_CARD, "", "", "", "", | |
| 300 LOCAL_CARD, "", "", "", "", | |
| 301 nullptr, false}, | |
| 302 IsLocalDuplicateOfServerCardTestCase{LOCAL_CARD, "", "", "", "", | |
| 303 FULL_SERVER_CARD, "", "", "", "", | |
| 304 nullptr, true}, | |
| 305 IsLocalDuplicateOfServerCardTestCase{FULL_SERVER_CARD, "", "", "", "", | |
| 306 FULL_SERVER_CARD, "", "", "", "", | |
| 307 nullptr, false}, | |
| 308 IsLocalDuplicateOfServerCardTestCase{ | |
| 309 LOCAL_CARD, "John Dillinger", "423456789012", "01", "2010", | |
| 310 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", | |
| 311 nullptr, true}, | |
| 312 IsLocalDuplicateOfServerCardTestCase{ | |
| 313 LOCAL_CARD, "J Dillinger", "423456789012", "01", "2010", | |
| 314 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", | |
| 315 nullptr, false}, | |
| 316 IsLocalDuplicateOfServerCardTestCase{ | |
| 317 LOCAL_CARD, "", "423456789012", "01", "2010", FULL_SERVER_CARD, | |
| 318 "John Dillinger", "423456789012", "01", "2010", nullptr, true}, | |
| 319 IsLocalDuplicateOfServerCardTestCase{ | |
| 320 LOCAL_CARD, "", "423456789012", "", "", FULL_SERVER_CARD, | |
| 321 "John Dillinger", "423456789012", "01", "2010", nullptr, true}, | |
| 322 IsLocalDuplicateOfServerCardTestCase{ | |
| 323 LOCAL_CARD, "", "423456789012", "", "", MASKED_SERVER_CARD, | |
| 324 "John Dillinger", "9012", "01", "2010", kVisaCard, true}, | |
| 325 IsLocalDuplicateOfServerCardTestCase{ | |
| 326 LOCAL_CARD, "", "423456789012", "", "", MASKED_SERVER_CARD, | |
| 327 "John Dillinger", "9012", "01", "2010", kMasterCard, false}, | |
| 328 IsLocalDuplicateOfServerCardTestCase{ | |
| 329 LOCAL_CARD, "John Dillinger", "4234-5678-9012", "01", "2010", | |
| 330 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", | |
| 331 nullptr, true})); | |
| 332 | |
| 308 TEST(CreditCardTest, HasSameNumberAs) { | 333 TEST(CreditCardTest, HasSameNumberAs) { |
| 309 CreditCard a(base::GenerateGUID(), std::string()); | 334 CreditCard a(base::GenerateGUID(), std::string()); |
| 310 CreditCard b(base::GenerateGUID(), std::string()); | 335 CreditCard b(base::GenerateGUID(), std::string()); |
| 311 | 336 |
| 312 // Empty cards have the same empty number. | 337 // Empty cards have the same empty number. |
| 313 EXPECT_TRUE(a.HasSameNumberAs(b)); | 338 EXPECT_TRUE(a.HasSameNumberAs(b)); |
| 314 EXPECT_TRUE(b.HasSameNumberAs(a)); | 339 EXPECT_TRUE(b.HasSameNumberAs(a)); |
| 315 | 340 |
| 316 // Same number. | 341 // Same number. |
| 317 a.set_record_type(CreditCard::LOCAL_CARD); | 342 a.set_record_type(CreditCard::LOCAL_CARD); |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 596 } | 621 } |
| 597 | 622 |
| 598 TEST(CreditCardTest, CreditCardVerificationCode) { | 623 TEST(CreditCardTest, CreditCardVerificationCode) { |
| 599 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); | 624 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); |
| 600 | 625 |
| 601 // The verification code cannot be set, as Chrome does not store this data. | 626 // The verification code cannot be set, as Chrome does not store this data. |
| 602 card.SetRawInfo(CREDIT_CARD_VERIFICATION_CODE, ASCIIToUTF16("999")); | 627 card.SetRawInfo(CREDIT_CARD_VERIFICATION_CODE, ASCIIToUTF16("999")); |
| 603 EXPECT_EQ(base::string16(), card.GetRawInfo(CREDIT_CARD_VERIFICATION_CODE)); | 628 EXPECT_EQ(base::string16(), card.GetRawInfo(CREDIT_CARD_VERIFICATION_CODE)); |
| 604 } | 629 } |
| 605 | 630 |
| 606 | 631 struct GetCreditCardTypeTestCase { |
| 607 TEST(CreditCardTest, GetCreditCardType) { | 632 std::string card_number; |
| 608 struct { | 633 std::string type; |
| 609 std::string card_number; | 634 bool is_valid; |
| 610 std::string type; | 635 }; |
| 611 bool is_valid; | 636 |
| 612 } test_cases[] = { | 637 // We are doing batches here because INSTANTIATE_TEST_CASE_P has a |
| 613 // The relevant sample numbers from | 638 // 50 upper limit. |
| 614 // http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_n umbers.htm | 639 class GetCreditCardTypeTestBatch1 |
| 615 { "378282246310005", kAmericanExpressCard, true }, | 640 : public testing::TestWithParam<GetCreditCardTypeTestCase> {}; |
| 616 { "371449635398431", kAmericanExpressCard, true }, | 641 |
| 617 { "378734493671000", kAmericanExpressCard, true }, | 642 TEST_P(GetCreditCardTypeTestBatch1, GetCreditCardType) { |
| 618 { "30569309025904", kDinersCard, true }, | 643 auto test_case = GetParam(); |
| 619 { "38520000023237", kDinersCard, true }, | 644 base::string16 card_number = ASCIIToUTF16(test_case.card_number); |
| 620 { "6011111111111117", kDiscoverCard, true }, | 645 SCOPED_TRACE(card_number); |
| 621 { "6011000990139424", kDiscoverCard, true }, | 646 EXPECT_EQ(test_case.type, CreditCard::GetCreditCardType(card_number)); |
| 622 { "3530111333300000", kJCBCard, true }, | 647 EXPECT_EQ(test_case.is_valid, IsValidCreditCardNumber(card_number)); |
| 623 { "3566002020360505", kJCBCard, true }, | 648 } |
| 624 { "5555555555554444", kMasterCard, true }, | 649 |
| 625 { "5105105105105100", kMasterCard, true }, | 650 INSTANTIATE_TEST_CASE_P( |
| 626 { "4111111111111111", kVisaCard, true }, | 651 CreditCardTest, |
| 627 { "4012888888881881", kVisaCard, true }, | 652 GetCreditCardTypeTestBatch1, |
| 628 { "4222222222222", kVisaCard, true }, | 653 testing::Values( |
| 629 | 654 // The relevant sample numbers from |
| 630 // The relevant sample numbers from | 655 // http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_ca rd_numbers.htm |
| 631 // https://www.auricsystems.com/sample-credit-card-numbers/ | 656 GetCreditCardTypeTestCase{"378282246310005", kAmericanExpressCard, |
| 632 { "343434343434343", kAmericanExpressCard, true }, | 657 true}, |
| 633 { "371144371144376", kAmericanExpressCard, true }, | 658 GetCreditCardTypeTestCase{"371449635398431", kAmericanExpressCard, |
| 634 { "341134113411347", kAmericanExpressCard, true }, | 659 true}, |
| 635 { "36438936438936", kDinersCard, true }, | 660 GetCreditCardTypeTestCase{"378734493671000", kAmericanExpressCard, |
| 636 { "36110361103612", kDinersCard, true }, | 661 true}, |
| 637 { "36111111111111", kDinersCard, true }, | 662 GetCreditCardTypeTestCase{"30569309025904", kDinersCard, true}, |
| 638 { "6011016011016011", kDiscoverCard, true }, | 663 GetCreditCardTypeTestCase{"38520000023237", kDinersCard, true}, |
| 639 { "6011000990139424", kDiscoverCard, true }, | 664 GetCreditCardTypeTestCase{"6011111111111117", kDiscoverCard, true}, |
| 640 { "6011000000000004", kDiscoverCard, true }, | 665 GetCreditCardTypeTestCase{"6011000990139424", kDiscoverCard, true}, |
| 641 { "6011000995500000", kDiscoverCard, true }, | 666 GetCreditCardTypeTestCase{"3530111333300000", kJCBCard, true}, |
| 642 { "6500000000000002", kDiscoverCard, true }, | 667 GetCreditCardTypeTestCase{"3566002020360505", kJCBCard, true}, |
| 643 { "3566002020360505", kJCBCard, true }, | 668 GetCreditCardTypeTestCase{"5555555555554444", kMasterCard, true}, |
| 644 { "3528000000000007", kJCBCard, true }, | 669 GetCreditCardTypeTestCase{"5105105105105100", kMasterCard, true}, |
| 645 { "5500005555555559", kMasterCard, true }, | 670 GetCreditCardTypeTestCase{"4111111111111111", kVisaCard, true}, |
| 646 { "5555555555555557", kMasterCard, true }, | 671 GetCreditCardTypeTestCase{"4012888888881881", kVisaCard, true}, |
| 647 { "5454545454545454", kMasterCard, true }, | 672 GetCreditCardTypeTestCase{"4222222222222", kVisaCard, true}, |
| 648 { "5555515555555551", kMasterCard, true }, | 673 |
| 649 { "5405222222222226", kMasterCard, true }, | 674 // The relevant sample numbers from |
| 650 { "5478050000000007", kMasterCard, true }, | 675 // https://www.auricsystems.com/sample-credit-card-numbers/ |
| 651 { "5111005111051128", kMasterCard, true }, | 676 GetCreditCardTypeTestCase{"343434343434343", kAmericanExpressCard, |
| 652 { "5112345112345114", kMasterCard, true }, | 677 true}, |
| 653 { "5115915115915118", kMasterCard, true }, | 678 GetCreditCardTypeTestCase{"371144371144376", kAmericanExpressCard, |
| 654 { "6247130048162403", kUnionPay, true }, | 679 true}, |
| 655 { "6247130048162403", kUnionPay, true }, | 680 GetCreditCardTypeTestCase{"341134113411347", kAmericanExpressCard, |
| 656 { "622384452162063648", kUnionPay, true }, | 681 true}, |
| 657 { "2204883716636153", kMirCard, true }, | 682 GetCreditCardTypeTestCase{"36438936438936", kDinersCard, true}, |
| 658 { "2200111234567898", kMirCard, true }, | 683 GetCreditCardTypeTestCase{"36110361103612", kDinersCard, true}, |
| 659 { "2200481349288130", kMirCard, true }, | 684 GetCreditCardTypeTestCase{"36111111111111", kDinersCard, true}, |
| 660 | 685 GetCreditCardTypeTestCase{"6011016011016011", kDiscoverCard, true}, |
| 661 // Empty string | 686 GetCreditCardTypeTestCase{"6011000990139424", kDiscoverCard, true}, |
| 662 { std::string(), kGenericCard, false }, | 687 GetCreditCardTypeTestCase{"6011000000000004", kDiscoverCard, true}, |
| 663 | 688 GetCreditCardTypeTestCase{"6011000995500000", kDiscoverCard, true}, |
| 664 // Non-numeric | 689 GetCreditCardTypeTestCase{"6500000000000002", kDiscoverCard, true}, |
| 665 { "garbage", kGenericCard, false }, | 690 GetCreditCardTypeTestCase{"3566002020360505", kJCBCard, true}, |
| 666 { "4garbage", kVisaCard, false }, | 691 GetCreditCardTypeTestCase{"3528000000000007", kJCBCard, true}, |
| 667 | 692 GetCreditCardTypeTestCase{"5500005555555559", kMasterCard, true}, |
| 668 // Fails Luhn check. | 693 GetCreditCardTypeTestCase{"5555555555555557", kMasterCard, true}, |
| 669 { "4111111111111112", kVisaCard, false }, | 694 GetCreditCardTypeTestCase{"5454545454545454", kMasterCard, true}, |
| 670 { "6247130048162413", kUnionPay, false }, | 695 GetCreditCardTypeTestCase{"5555515555555551", kMasterCard, true}, |
| 671 { "2204883716636154", kMirCard, false }, | 696 GetCreditCardTypeTestCase{"5405222222222226", kMasterCard, true}, |
| 672 | 697 GetCreditCardTypeTestCase{"5478050000000007", kMasterCard, true}, |
| 673 // Invalid length. | 698 GetCreditCardTypeTestCase{"5111005111051128", kMasterCard, true}, |
| 674 { "3434343434343434", kAmericanExpressCard, false }, | 699 GetCreditCardTypeTestCase{"5112345112345114", kMasterCard, true}, |
| 675 { "411111111111116", kVisaCard, false }, | 700 GetCreditCardTypeTestCase{"5115915115915118", kMasterCard, true}, |
| 676 { "220011123456783", kMirCard, false }, | 701 GetCreditCardTypeTestCase{"6247130048162403", kUnionPay, true}, |
| 677 | 702 GetCreditCardTypeTestCase{"6247130048162403", kUnionPay, true}, |
| 678 // Issuer Identification Numbers (IINs) that Chrome recognizes. | 703 GetCreditCardTypeTestCase{"622384452162063648", kUnionPay, true}, |
| 679 { "4", kVisaCard, false }, | 704 GetCreditCardTypeTestCase{"2204883716636153", kMirCard, true}, |
| 680 { "22", kMirCard, false }, | 705 GetCreditCardTypeTestCase{"2200111234567898", kMirCard, true}, |
| 681 { "34", kAmericanExpressCard, false }, | 706 GetCreditCardTypeTestCase{"2200481349288130", kMirCard, true}, |
| 682 { "37", kAmericanExpressCard, false }, | 707 |
| 683 { "300", kDinersCard, false }, | 708 // Empty string |
| 684 { "301", kDinersCard, false }, | 709 GetCreditCardTypeTestCase{std::string(), kGenericCard, false}, |
| 685 { "302", kDinersCard, false }, | 710 |
| 686 { "303", kDinersCard, false }, | 711 // Non-numeric |
| 687 { "304", kDinersCard, false }, | 712 GetCreditCardTypeTestCase{"garbage", kGenericCard, false}, |
| 688 { "305", kDinersCard, false }, | 713 GetCreditCardTypeTestCase{"4garbage", kVisaCard, false}, |
| 689 { "3095", kDinersCard, false }, | 714 |
| 690 { "36", kDinersCard, false }, | 715 // Fails Luhn check. |
| 691 { "38", kDinersCard, false }, | 716 GetCreditCardTypeTestCase{"4111111111111112", kVisaCard, false}, |
| 692 { "39", kDinersCard, false }, | 717 GetCreditCardTypeTestCase{"6247130048162413", kUnionPay, false}, |
| 693 { "6011", kDiscoverCard, false }, | 718 GetCreditCardTypeTestCase{"2204883716636154", kMirCard, false})); |
| 694 { "644", kDiscoverCard, false }, | 719 |
| 695 { "645", kDiscoverCard, false }, | 720 class GetCreditCardTypeTestBatch2 |
| 696 { "646", kDiscoverCard, false }, | 721 : public testing::TestWithParam<GetCreditCardTypeTestCase> {}; |
| 697 { "647", kDiscoverCard, false }, | 722 |
| 698 { "648", kDiscoverCard, false }, | 723 TEST_P(GetCreditCardTypeTestBatch2, GetCreditCardType) { |
| 699 { "649", kDiscoverCard, false }, | 724 auto test_case = GetParam(); |
| 700 { "65", kDiscoverCard, false }, | 725 base::string16 card_number = ASCIIToUTF16(test_case.card_number); |
| 701 { "3528", kJCBCard, false }, | 726 SCOPED_TRACE(card_number); |
| 702 { "3531", kJCBCard, false }, | 727 EXPECT_EQ(test_case.type, CreditCard::GetCreditCardType(card_number)); |
| 703 { "3589", kJCBCard, false }, | 728 EXPECT_EQ(test_case.is_valid, IsValidCreditCardNumber(card_number)); |
| 704 { "51", kMasterCard, false }, | 729 } |
| 705 { "52", kMasterCard, false }, | 730 |
| 706 { "53", kMasterCard, false }, | 731 INSTANTIATE_TEST_CASE_P( |
| 707 { "54", kMasterCard, false }, | 732 CreditCardTest, |
| 708 { "55", kMasterCard, false }, | 733 GetCreditCardTypeTestBatch2, |
| 709 { "62", kUnionPay, false }, | 734 testing::Values( |
| 710 | 735 // Invalid length. |
| 711 // Not enough data to determine an IIN uniquely. | 736 GetCreditCardTypeTestCase{"3434343434343434", kAmericanExpressCard, |
| 712 { "2", kGenericCard, false }, | 737 false}, |
| 713 { "3", kGenericCard, false }, | 738 GetCreditCardTypeTestCase{"411111111111116", kVisaCard, false}, |
| 714 { "30", kGenericCard, false }, | 739 GetCreditCardTypeTestCase{"220011123456783", kMirCard, false}, |
| 715 { "309", kGenericCard, false }, | 740 |
| 716 { "35", kGenericCard, false }, | 741 // Issuer Identification Numbers (IINs) that Chrome recognizes. |
| 717 { "5", kGenericCard, false }, | 742 GetCreditCardTypeTestCase{"4", kVisaCard, false}, |
| 718 { "6", kGenericCard, false }, | 743 GetCreditCardTypeTestCase{"22", kMirCard, false}, |
| 719 { "60", kGenericCard, false }, | 744 GetCreditCardTypeTestCase{"34", kAmericanExpressCard, false}, |
| 720 { "601", kGenericCard, false }, | 745 GetCreditCardTypeTestCase{"37", kAmericanExpressCard, false}, |
| 721 { "64", kGenericCard, false }, | 746 GetCreditCardTypeTestCase{"300", kDinersCard, false}, |
| 722 | 747 GetCreditCardTypeTestCase{"301", kDinersCard, false}, |
| 723 // Unknown IINs. | 748 GetCreditCardTypeTestCase{"302", kDinersCard, false}, |
| 724 { "0", kGenericCard, false }, | 749 GetCreditCardTypeTestCase{"303", kDinersCard, false}, |
| 725 { "1", kGenericCard, false }, | 750 GetCreditCardTypeTestCase{"304", kDinersCard, false}, |
| 726 { "306", kGenericCard, false }, | 751 GetCreditCardTypeTestCase{"305", kDinersCard, false}, |
| 727 { "307", kGenericCard, false }, | 752 GetCreditCardTypeTestCase{"3095", kDinersCard, false}, |
| 728 { "308", kGenericCard, false }, | 753 GetCreditCardTypeTestCase{"36", kDinersCard, false}, |
| 729 { "3091", kGenericCard, false }, | 754 GetCreditCardTypeTestCase{"38", kDinersCard, false}, |
| 730 { "3094", kGenericCard, false }, | 755 GetCreditCardTypeTestCase{"39", kDinersCard, false}, |
| 731 { "3096", kGenericCard, false }, | 756 GetCreditCardTypeTestCase{"6011", kDiscoverCard, false}, |
| 732 { "31", kGenericCard, false }, | 757 GetCreditCardTypeTestCase{"644", kDiscoverCard, false}, |
| 733 { "32", kGenericCard, false }, | 758 GetCreditCardTypeTestCase{"645", kDiscoverCard, false}, |
| 734 { "33", kGenericCard, false }, | 759 GetCreditCardTypeTestCase{"646", kDiscoverCard, false}, |
| 735 { "351", kGenericCard, false }, | 760 GetCreditCardTypeTestCase{"647", kDiscoverCard, false}, |
| 736 { "3527", kGenericCard, false }, | 761 GetCreditCardTypeTestCase{"648", kDiscoverCard, false}, |
| 737 { "359", kGenericCard, false }, | 762 GetCreditCardTypeTestCase{"649", kDiscoverCard, false}, |
| 738 { "50", kGenericCard, false }, | 763 GetCreditCardTypeTestCase{"65", kDiscoverCard, false}, |
| 739 { "56", kGenericCard, false }, | 764 GetCreditCardTypeTestCase{"3528", kJCBCard, false}, |
| 740 { "57", kGenericCard, false }, | 765 GetCreditCardTypeTestCase{"3531", kJCBCard, false}, |
| 741 { "58", kGenericCard, false }, | 766 GetCreditCardTypeTestCase{"3589", kJCBCard, false}, |
| 742 { "59", kGenericCard, false }, | 767 GetCreditCardTypeTestCase{"51", kMasterCard, false}, |
| 743 { "600", kGenericCard, false }, | 768 GetCreditCardTypeTestCase{"52", kMasterCard, false}, |
| 744 { "602", kGenericCard, false }, | 769 GetCreditCardTypeTestCase{"53", kMasterCard, false}, |
| 745 { "603", kGenericCard, false }, | 770 GetCreditCardTypeTestCase{"54", kMasterCard, false}, |
| 746 { "604", kGenericCard, false }, | 771 GetCreditCardTypeTestCase{"55", kMasterCard, false}, |
| 747 { "605", kGenericCard, false }, | 772 GetCreditCardTypeTestCase{"62", kUnionPay, false}, |
| 748 { "606", kGenericCard, false }, | 773 |
| 749 { "607", kGenericCard, false }, | 774 // Not enough data to determine an IIN uniquely. |
| 750 { "608", kGenericCard, false }, | 775 GetCreditCardTypeTestCase{"2", kGenericCard, false}, |
| 751 { "609", kGenericCard, false }, | 776 GetCreditCardTypeTestCase{"3", kGenericCard, false}, |
| 752 { "61", kGenericCard, false }, | 777 GetCreditCardTypeTestCase{"30", kGenericCard, false}, |
| 753 { "63", kGenericCard, false }, | 778 GetCreditCardTypeTestCase{"309", kGenericCard, false}, |
| 754 { "640", kGenericCard, false }, | 779 GetCreditCardTypeTestCase{"35", kGenericCard, false}, |
| 755 { "641", kGenericCard, false }, | 780 GetCreditCardTypeTestCase{"5", kGenericCard, false}, |
| 756 { "642", kGenericCard, false }, | 781 GetCreditCardTypeTestCase{"6", kGenericCard, false}, |
| 757 { "643", kGenericCard, false }, | 782 GetCreditCardTypeTestCase{"60", kGenericCard, false}, |
| 758 { "66", kGenericCard, false }, | 783 GetCreditCardTypeTestCase{"601", kGenericCard, false}, |
| 759 { "67", kGenericCard, false }, | 784 GetCreditCardTypeTestCase{"64", kGenericCard, false})); |
| 760 { "68", kGenericCard, false }, | 785 |
| 761 { "69", kGenericCard, false }, | 786 class GetCreditCardTypeTestBatch3 |
| 762 { "7", kGenericCard, false }, | 787 : public testing::TestWithParam<GetCreditCardTypeTestCase> {}; |
| 763 { "8", kGenericCard, false }, | 788 |
| 764 { "9", kGenericCard, false }, | 789 TEST_P(GetCreditCardTypeTestBatch3, GetCreditCardType) { |
| 765 | 790 auto test_case = GetParam(); |
| 766 // Oddball case: Unknown issuer, but valid Luhn check and plausible length. | 791 base::string16 card_number = ASCIIToUTF16(test_case.card_number); |
| 767 { "7000700070007000", kGenericCard, true }, | 792 SCOPED_TRACE(card_number); |
| 768 }; | 793 EXPECT_EQ(test_case.type, CreditCard::GetCreditCardType(card_number)); |
| 769 | 794 EXPECT_EQ(test_case.is_valid, IsValidCreditCardNumber(card_number)); |
| 770 for (const auto& test_case : test_cases) { | 795 } |
| 771 base::string16 card_number = ASCIIToUTF16(test_case.card_number); | 796 |
| 772 SCOPED_TRACE(card_number); | 797 INSTANTIATE_TEST_CASE_P( |
| 773 EXPECT_EQ(test_case.type, CreditCard::GetCreditCardType(card_number)); | 798 CreditCardTest, |
| 774 EXPECT_EQ(test_case.is_valid, IsValidCreditCardNumber(card_number)); | 799 GetCreditCardTypeTestBatch3, |
| 775 } | 800 testing::Values( |
| 776 } | 801 // Unknown IINs. |
| 802 GetCreditCardTypeTestCase{"0", kGenericCard, false}, | |
| 803 GetCreditCardTypeTestCase{"1", kGenericCard, false}, | |
| 804 GetCreditCardTypeTestCase{"306", kGenericCard, false}, | |
| 805 GetCreditCardTypeTestCase{"307", kGenericCard, false}, | |
| 806 GetCreditCardTypeTestCase{"308", kGenericCard, false}, | |
| 807 GetCreditCardTypeTestCase{"3091", kGenericCard, false}, | |
| 808 GetCreditCardTypeTestCase{"3094", kGenericCard, false}, | |
| 809 GetCreditCardTypeTestCase{"3096", kGenericCard, false}, | |
| 810 GetCreditCardTypeTestCase{"31", kGenericCard, false}, | |
| 811 GetCreditCardTypeTestCase{"32", kGenericCard, false}, | |
| 812 GetCreditCardTypeTestCase{"33", kGenericCard, false}, | |
| 813 GetCreditCardTypeTestCase{"351", kGenericCard, false}, | |
| 814 GetCreditCardTypeTestCase{"3527", kGenericCard, false}, | |
| 815 GetCreditCardTypeTestCase{"359", kGenericCard, false}, | |
| 816 GetCreditCardTypeTestCase{"50", kGenericCard, false}, | |
| 817 GetCreditCardTypeTestCase{"56", kGenericCard, false}, | |
| 818 GetCreditCardTypeTestCase{"57", kGenericCard, false}, | |
| 819 GetCreditCardTypeTestCase{"58", kGenericCard, false}, | |
| 820 GetCreditCardTypeTestCase{"59", kGenericCard, false}, | |
| 821 GetCreditCardTypeTestCase{"600", kGenericCard, false}, | |
| 822 GetCreditCardTypeTestCase{"602", kGenericCard, false}, | |
| 823 GetCreditCardTypeTestCase{"603", kGenericCard, false}, | |
| 824 GetCreditCardTypeTestCase{"604", kGenericCard, false}, | |
| 825 GetCreditCardTypeTestCase{"605", kGenericCard, false}, | |
| 826 GetCreditCardTypeTestCase{"606", kGenericCard, false}, | |
| 827 GetCreditCardTypeTestCase{"607", kGenericCard, false}, | |
| 828 GetCreditCardTypeTestCase{"608", kGenericCard, false}, | |
| 829 GetCreditCardTypeTestCase{"609", kGenericCard, false}, | |
| 830 GetCreditCardTypeTestCase{"61", kGenericCard, false}, | |
| 831 GetCreditCardTypeTestCase{"63", kGenericCard, false}, | |
| 832 GetCreditCardTypeTestCase{"640", kGenericCard, false}, | |
| 833 GetCreditCardTypeTestCase{"641", kGenericCard, false}, | |
| 834 GetCreditCardTypeTestCase{"642", kGenericCard, false}, | |
| 835 GetCreditCardTypeTestCase{"643", kGenericCard, false}, | |
| 836 GetCreditCardTypeTestCase{"66", kGenericCard, false}, | |
| 837 GetCreditCardTypeTestCase{"67", kGenericCard, false}, | |
| 838 GetCreditCardTypeTestCase{"68", kGenericCard, false}, | |
| 839 GetCreditCardTypeTestCase{"69", kGenericCard, false}, | |
| 840 GetCreditCardTypeTestCase{"7", kGenericCard, false}, | |
| 841 GetCreditCardTypeTestCase{"8", kGenericCard, false}, | |
| 842 GetCreditCardTypeTestCase{"9", kGenericCard, false}, | |
| 843 | |
| 844 // Oddball case: Unknown issuer, but valid Luhn check and plausible | |
| 845 // length. | |
| 846 GetCreditCardTypeTestCase{"7000700070007000", kGenericCard, true})); | |
| 777 | 847 |
| 778 TEST(CreditCardTest, LastFourDigits) { | 848 TEST(CreditCardTest, LastFourDigits) { |
| 779 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); | 849 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); |
| 780 ASSERT_EQ(base::string16(), card.LastFourDigits()); | 850 ASSERT_EQ(base::string16(), card.LastFourDigits()); |
| 781 | 851 |
| 782 test::SetCreditCardInfo(&card, "Baby Face Nelson", | 852 test::SetCreditCardInfo(&card, "Baby Face Nelson", |
| 783 "5212341234123489", "01", "2010"); | 853 "5212341234123489", "01", "2010"); |
| 784 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); | 854 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); |
| 785 | 855 |
| 786 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("3489")); | 856 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("3489")); |
| 787 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); | 857 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); |
| 788 | 858 |
| 789 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("489")); | 859 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("489")); |
| 790 ASSERT_EQ(base::ASCIIToUTF16("489"), card.LastFourDigits()); | 860 ASSERT_EQ(base::ASCIIToUTF16("489"), card.LastFourDigits()); |
| 791 } | 861 } |
| 792 | 862 |
| 793 // Verifies that a credit card should be updated. | 863 // Verifies that a credit card should be updated. |
| 794 TEST(CreditCardTest, ShouldUpdateExpiration) { | 864 struct ShouldUpdateExpirationTestCase { |
| 795 base::Time now = base::Time::Now(); | 865 bool should_update_expiration; |
| 796 | 866 int month; |
| 797 base::Time::Exploded last_year; | 867 int year; |
| 798 (now - base::TimeDelta::FromDays(365)).LocalExplode(&last_year); | 868 CreditCard::RecordType record_type; |
| 799 | 869 CreditCard::ServerStatus server_status; |
| 800 base::Time::Exploded last_month; | 870 }; |
| 801 (now - base::TimeDelta::FromDays(31)).LocalExplode(&last_month); | 871 |
| 802 | 872 class ShouldUpdateExpirationTest |
| 803 base::Time::Exploded current; | 873 : public testing::TestWithParam<ShouldUpdateExpirationTestCase> {}; |
| 804 now.LocalExplode(¤t); | 874 |
| 805 | 875 class TestingTimes { |
| 806 base::Time::Exploded next_month; | 876 public: |
| 807 (now + base::TimeDelta::FromDays(31)).LocalExplode(&next_month); | 877 TestingTimes() { |
| 808 | 878 now_ = base::Time::Now(); |
| 809 base::Time::Exploded next_year; | 879 (now_ - base::TimeDelta::FromDays(365)).LocalExplode(&last_year_); |
| 810 (now + base::TimeDelta::FromDays(365)).LocalExplode(&next_year); | 880 (now_ - base::TimeDelta::FromDays(31)).LocalExplode(&last_month_); |
| 811 | 881 now_.LocalExplode(¤t_); |
| 812 static const struct { | 882 (now_ + base::TimeDelta::FromDays(31)).LocalExplode(&next_month_); |
| 813 bool should_update_expiration; | 883 (now_ + base::TimeDelta::FromDays(365)).LocalExplode(&next_year_); |
| 814 int month; | |
| 815 int year; | |
| 816 CreditCard::RecordType record_type; | |
| 817 CreditCard::ServerStatus server_status; | |
| 818 } kTestCases[] = { | |
| 819 | |
| 820 // Cards that expired last year should always be updated. | |
| 821 {true, last_year.month, last_year.year, CreditCard::LOCAL_CARD}, | |
| 822 {true, last_year.month, last_year.year, CreditCard::FULL_SERVER_CARD, | |
| 823 CreditCard::OK}, | |
| 824 {true, last_year.month, last_year.year, CreditCard::MASKED_SERVER_CARD, | |
| 825 CreditCard::OK}, | |
| 826 {true, last_year.month, last_year.year, CreditCard::FULL_SERVER_CARD, | |
| 827 CreditCard::EXPIRED}, | |
| 828 {true, last_year.month, last_year.year, CreditCard::MASKED_SERVER_CARD, | |
| 829 CreditCard::EXPIRED}, | |
| 830 | |
| 831 // Cards that expired last month should always be updated. | |
| 832 {true, last_month.month, last_month.year, CreditCard::LOCAL_CARD}, | |
| 833 {true, last_month.month, last_month.year, CreditCard::FULL_SERVER_CARD, | |
| 834 CreditCard::OK}, | |
| 835 {true, last_month.month, last_month.year, CreditCard::MASKED_SERVER_CARD, | |
| 836 CreditCard::OK}, | |
| 837 {true, last_month.month, last_month.year, CreditCard::FULL_SERVER_CARD, | |
| 838 CreditCard::EXPIRED}, | |
| 839 {true, last_month.month, last_month.year, CreditCard::MASKED_SERVER_CARD, | |
| 840 CreditCard::EXPIRED}, | |
| 841 | |
| 842 // Cards that expire this month should be updated only if the server | |
| 843 // status is EXPIRED. | |
| 844 {false, current.month, current.year, CreditCard::LOCAL_CARD}, | |
| 845 {false, current.month, current.year, CreditCard::FULL_SERVER_CARD, | |
| 846 CreditCard::OK}, | |
| 847 {false, current.month, current.year, CreditCard::MASKED_SERVER_CARD, | |
| 848 CreditCard::OK}, | |
| 849 {true, current.month, current.year, CreditCard::FULL_SERVER_CARD, | |
| 850 CreditCard::EXPIRED}, | |
| 851 {true, current.month, current.year, CreditCard::MASKED_SERVER_CARD, | |
| 852 CreditCard::EXPIRED}, | |
| 853 | |
| 854 // Cards that expire next month should be updated only if the server | |
| 855 // status is EXPIRED. | |
| 856 {false, next_month.month, next_month.year, CreditCard::LOCAL_CARD}, | |
| 857 {false, next_month.month, next_month.year, CreditCard::MASKED_SERVER_CARD, | |
| 858 CreditCard::OK}, | |
| 859 {false, next_month.month, next_month.year, CreditCard::FULL_SERVER_CARD, | |
| 860 CreditCard::OK}, | |
| 861 {true, next_month.month, next_month.year, CreditCard::MASKED_SERVER_CARD, | |
| 862 CreditCard::EXPIRED}, | |
| 863 {true, next_month.month, next_month.year, CreditCard::FULL_SERVER_CARD, | |
| 864 CreditCard::EXPIRED}, | |
| 865 | |
| 866 // Cards that expire next year should be updated only if the server status | |
| 867 // is EXPIRED. | |
| 868 {false, next_year.month, next_year.year, CreditCard::LOCAL_CARD}, | |
| 869 {false, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, | |
| 870 CreditCard::OK}, | |
| 871 {false, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, | |
| 872 CreditCard::OK}, | |
| 873 {true, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, | |
| 874 CreditCard::EXPIRED}, | |
| 875 {true, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, | |
| 876 CreditCard::EXPIRED}, | |
| 877 }; | |
| 878 | |
| 879 for (const auto& test_case : kTestCases) { | |
| 880 CreditCard card; | |
| 881 card.SetExpirationMonth(test_case.month); | |
| 882 card.SetExpirationYear(test_case.year); | |
| 883 card.set_record_type(test_case.record_type); | |
| 884 if (card.record_type() != CreditCard::LOCAL_CARD) | |
| 885 card.SetServerStatus(test_case.server_status); | |
| 886 | |
| 887 EXPECT_EQ(test_case.should_update_expiration, | |
| 888 card.ShouldUpdateExpiration(now)); | |
| 889 } | 884 } |
| 885 | |
| 886 base::Time now_; | |
| 887 base::Time::Exploded last_year_; | |
| 888 base::Time::Exploded last_month_; | |
| 889 base::Time::Exploded current_; | |
| 890 base::Time::Exploded next_month_; | |
| 891 base::Time::Exploded next_year_; | |
| 892 }; | |
| 893 | |
| 894 TestingTimes testingTimes; | |
| 895 | |
| 896 TEST_P(ShouldUpdateExpirationTest, ShouldUpdateExpiration) { | |
| 897 auto test_case = GetParam(); | |
| 898 CreditCard card; | |
| 899 card.SetExpirationMonth(test_case.month); | |
| 900 card.SetExpirationYear(test_case.year); | |
| 901 card.set_record_type(test_case.record_type); | |
| 902 if (card.record_type() != CreditCard::LOCAL_CARD) | |
| 903 card.SetServerStatus(test_case.server_status); | |
| 904 | |
| 905 EXPECT_EQ(test_case.should_update_expiration, | |
| 906 card.ShouldUpdateExpiration(testingTimes.now_)); | |
| 890 } | 907 } |
| 891 | 908 |
| 909 INSTANTIATE_TEST_CASE_P( | |
| 910 CreditCardTest, | |
| 911 ShouldUpdateExpirationTest, | |
| 912 testing::Values( | |
| 913 // Cards that expired last year should always be updated. | |
| 914 ShouldUpdateExpirationTestCase{true, testingTimes.last_year_.month, | |
| 915 testingTimes.last_year_.year, | |
| 916 CreditCard::LOCAL_CARD}, | |
| 917 ShouldUpdateExpirationTestCase{ | |
| 918 true, testingTimes.last_year_.month, testingTimes.last_year_.year, | |
| 919 CreditCard::FULL_SERVER_CARD, CreditCard::OK}, | |
| 920 ShouldUpdateExpirationTestCase{ | |
| 921 true, testingTimes.last_year_.month, testingTimes.last_year_.year, | |
| 922 CreditCard::MASKED_SERVER_CARD, CreditCard::OK}, | |
| 923 ShouldUpdateExpirationTestCase{ | |
| 924 true, testingTimes.last_year_.month, testingTimes.last_year_.year, | |
| 925 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED}, | |
| 926 ShouldUpdateExpirationTestCase{ | |
| 927 true, testingTimes.last_year_.month, testingTimes.last_year_.year, | |
| 928 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED}, | |
| 929 | |
| 930 // Cards that expired last month should always be updated. | |
| 931 ShouldUpdateExpirationTestCase{true, testingTimes.last_month_.month, | |
| 932 testingTimes.last_month_.year, | |
| 933 CreditCard::LOCAL_CARD}, | |
| 934 ShouldUpdateExpirationTestCase{ | |
| 935 true, testingTimes.last_month_.month, testingTimes.last_month_.year, | |
| 936 CreditCard::FULL_SERVER_CARD, CreditCard::OK}, | |
| 937 ShouldUpdateExpirationTestCase{ | |
| 938 true, testingTimes.last_month_.month, testingTimes.last_month_.year, | |
| 939 CreditCard::MASKED_SERVER_CARD, CreditCard::OK}, | |
| 940 ShouldUpdateExpirationTestCase{ | |
| 941 true, testingTimes.last_month_.month, testingTimes.last_month_.year, | |
| 942 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED}, | |
| 943 ShouldUpdateExpirationTestCase{ | |
| 944 true, testingTimes.last_month_.month, testingTimes.last_month_.year, | |
| 945 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED}, | |
| 946 | |
| 947 // Cards that expire this month should be updated only if the server | |
| 948 // status is EXPIRED. | |
| 949 ShouldUpdateExpirationTestCase{false, testingTimes.current_.month, | |
| 950 testingTimes.current_.year, | |
| 951 CreditCard::LOCAL_CARD}, | |
| 952 ShouldUpdateExpirationTestCase{ | |
| 953 false, testingTimes.current_.month, testingTimes.current_.year, | |
| 954 CreditCard::FULL_SERVER_CARD, CreditCard::OK}, | |
| 955 ShouldUpdateExpirationTestCase{ | |
| 956 false, testingTimes.current_.month, testingTimes.current_.year, | |
| 957 CreditCard::MASKED_SERVER_CARD, CreditCard::OK}, | |
| 958 ShouldUpdateExpirationTestCase{ | |
| 959 true, testingTimes.current_.month, testingTimes.current_.year, | |
| 960 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED}, | |
| 961 ShouldUpdateExpirationTestCase{ | |
| 962 true, testingTimes.current_.month, testingTimes.current_.year, | |
| 963 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED}, | |
| 964 | |
| 965 // Cards that expire next month should be updated only if the server | |
| 966 // status is EXPIRED. | |
| 967 ShouldUpdateExpirationTestCase{false, testingTimes.next_month_.month, | |
| 968 testingTimes.next_month_.year, | |
| 969 CreditCard::LOCAL_CARD}, | |
| 970 ShouldUpdateExpirationTestCase{false, testingTimes.next_month_.month, | |
| 971 testingTimes.next_month_.year, | |
| 972 CreditCard::MASKED_SERVER_CARD, | |
| 973 CreditCard::OK}, | |
| 974 ShouldUpdateExpirationTestCase{false, testingTimes.next_month_.month, | |
| 975 testingTimes.next_month_.year, | |
| 976 CreditCard::FULL_SERVER_CARD, | |
| 977 CreditCard::OK}, | |
| 978 ShouldUpdateExpirationTestCase{ | |
| 979 true, testingTimes.next_month_.month, testingTimes.next_month_.year, | |
| 980 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED}, | |
| 981 ShouldUpdateExpirationTestCase{ | |
| 982 true, testingTimes.next_month_.month, testingTimes.next_month_.year, | |
| 983 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED}, | |
| 984 | |
| 985 // Cards that expire next year should be updated only if the server | |
| 986 // status is EXPIRED. | |
| 987 ShouldUpdateExpirationTestCase{false, testingTimes.next_year_.month, | |
| 988 testingTimes.next_year_.year, | |
| 989 CreditCard::LOCAL_CARD}, | |
| 990 ShouldUpdateExpirationTestCase{ | |
| 991 false, testingTimes.next_year_.month, testingTimes.next_year_.year, | |
| 992 CreditCard::MASKED_SERVER_CARD, CreditCard::OK}, | |
| 993 ShouldUpdateExpirationTestCase{ | |
| 994 false, testingTimes.next_year_.month, testingTimes.next_year_.year, | |
| 995 CreditCard::FULL_SERVER_CARD, CreditCard::OK}, | |
| 996 ShouldUpdateExpirationTestCase{ | |
| 997 true, testingTimes.next_year_.month, testingTimes.next_year_.year, | |
| 998 CreditCard::MASKED_SERVER_CARD, CreditCard::EXPIRED}, | |
| 999 ShouldUpdateExpirationTestCase{ | |
| 1000 true, testingTimes.next_year_.month, testingTimes.next_year_.year, | |
| 1001 CreditCard::FULL_SERVER_CARD, CreditCard::EXPIRED})); | |
| 1002 | |
| 1003 // TODO(wuandy): rewriting this test with INSTANTIATE_TEST_CASE_P seems to | |
|
sebsg
2017/03/14 19:15:24
Would you mind removing this from the file? Like s
wuandy
2017/03/14 19:27:44
Done.
| |
| 1004 // trigger a complaint on windows compilers. Comment it out and revert to | |
| 1005 // original test for now. | |
| 1006 // struct GetLastUsedDateForDisplayTestCase { | |
| 1007 // const char* show_expiration_date; | |
| 1008 // const std::string& app_locale; | |
| 1009 // base::string16 added_to_autofill_date; | |
| 1010 // base::string16 last_used_date; | |
| 1011 // base::string16 last_used_year_ago; | |
| 1012 //}; | |
| 1013 // | |
| 1014 // class GetLastUsedDateForDisplayTest | |
| 1015 // : public testing::TestWithParam<GetLastUsedDateForDisplayTestCase> {}; | |
| 1016 // | |
| 1017 // TEST_P(GetLastUsedDateForDisplayTest, GetLastUsedDateForDisplay) { | |
| 1018 // const base::Time::Exploded kTestDateTimeExploded = { | |
| 1019 // 2016, 12, 6, 10, // Sat, Dec 10, 2016 | |
| 1020 // 15, 42, 7, 0 // 15:42:07.000 | |
| 1021 // }; | |
| 1022 // base::Time kArbitraryTime; | |
| 1023 // EXPECT_TRUE( | |
| 1024 // base::Time::FromLocalExploded(kTestDateTimeExploded, &kArbitraryTime)); | |
| 1025 // | |
| 1026 // // Test for added to chrome/chromium. | |
| 1027 // CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com"); | |
| 1028 // credit_card0.set_use_count(1); | |
| 1029 // credit_card0.set_use_date(kArbitraryTime - base::TimeDelta::FromDays(1)); | |
| 1030 // test::SetCreditCardInfo(&credit_card0, "John Dillinger", | |
| 1031 // "423456789012" /* Visa */, "01", "2021"); | |
| 1032 // | |
| 1033 // // Test for last used date. | |
| 1034 // CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com"); | |
| 1035 // test::SetCreditCardInfo(&credit_card1, "Clyde Barrow", | |
| 1036 // "347666888555" /* American Express */, "04", | |
| 1037 // "2021"); | |
| 1038 // credit_card1.set_use_count(10); | |
| 1039 // credit_card1.set_use_date(kArbitraryTime - base::TimeDelta::FromDays(10)); | |
| 1040 // | |
| 1041 // // Test for last used more than one year ago. | |
| 1042 // CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com"); | |
| 1043 // credit_card2.set_use_count(5); | |
| 1044 // credit_card2.set_use_date(kArbitraryTime - base::TimeDelta::FromDays(366)); | |
| 1045 // test::SetCreditCardInfo(&credit_card2, "Bonnie Parker", | |
| 1046 // "518765432109" /* Mastercard */, "12", "2021"); | |
| 1047 // | |
| 1048 // variations::testing::VariationParamsManager variation_params_; | |
| 1049 // auto test_case = GetParam(); | |
| 1050 // variation_params_.SetVariationParamsWithFeatureAssociations( | |
| 1051 // kAutofillCreditCardLastUsedDateDisplay.name, | |
| 1052 // {{kAutofillCreditCardLastUsedDateShowExpirationDateKey, | |
| 1053 // test_case.show_expiration_date}}, | |
| 1054 // {kAutofillCreditCardLastUsedDateDisplay.name}); | |
| 1055 // | |
| 1056 // EXPECT_EQ(test_case.added_to_autofill_date, | |
| 1057 // credit_card0.GetLastUsedDateForDisplay(test_case.app_locale)); | |
| 1058 // EXPECT_EQ(test_case.last_used_date, | |
| 1059 // credit_card1.GetLastUsedDateForDisplay(test_case.app_locale)); | |
| 1060 // EXPECT_EQ(test_case.last_used_year_ago, | |
| 1061 // credit_card2.GetLastUsedDateForDisplay(test_case.app_locale)); | |
| 1062 // variation_params_.ClearAllVariationParams(); | |
| 1063 //} | |
| 1064 // | |
| 1065 // INSTANTIATE_TEST_CASE_P(CreditCardTest, | |
| 1066 // GetLastUsedDateForDisplayTest, | |
| 1067 // testing::Values( | |
| 1068 // // only show last used date. | |
| 1069 // GetLastUsedDateForDisplayTestCase{ | |
| 1070 // "false", "en_US", ASCIIToUTF16("Added Dec | |
| 1071 // 09"), ASCIIToUTF16("Last used Nov 30"), | |
| 1072 // ASCIIToUTF16("Last used over a year ago")}, | |
| 1073 // // show expiration date and last used date. | |
| 1074 // GetLastUsedDateForDisplayTestCase{ | |
| 1075 // "true", "en_US", ASCIIToUTF16("Exp: 01/21, added Dec 09"), | |
| 1076 // ASCIIToUTF16("Exp: 04/21, last used Nov 30"), | |
| 1077 // ASCIIToUTF16("Exp: 12/21, last used over a year ago")}));*/ | |
| 1078 | |
| 892 // Test that credit card last used date suggestion can be generated correctly | 1079 // Test that credit card last used date suggestion can be generated correctly |
| 893 // in different variations. | 1080 // in different variations. |
| 894 TEST(CreditCardTest, GetLastUsedDateForDisplay) { | 1081 TEST(CreditCardTest, GetLastUsedDateForDisplay) { |
| 895 const base::Time::Exploded kTestDateTimeExploded = { | 1082 const base::Time::Exploded kTestDateTimeExploded = { |
| 896 2016, 12, 6, 10, // Sat, Dec 10, 2016 | 1083 2016, 12, 6, 10, // Sat, Dec 10, 2016 |
| 897 15, 42, 7, 0 // 15:42:07.000 | 1084 15, 42, 7, 0 // 15:42:07.000 |
| 898 }; | 1085 }; |
| 899 base::Time kArbitraryTime; | 1086 base::Time kArbitraryTime; |
| 900 EXPECT_TRUE( | 1087 EXPECT_TRUE( |
| 901 base::Time::FromLocalExploded(kTestDateTimeExploded, &kArbitraryTime)); | 1088 base::Time::FromLocalExploded(kTestDateTimeExploded, &kArbitraryTime)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 951 credit_card0.GetLastUsedDateForDisplay(test_case.app_locale)); | 1138 credit_card0.GetLastUsedDateForDisplay(test_case.app_locale)); |
| 952 EXPECT_EQ(test_case.last_used_date, | 1139 EXPECT_EQ(test_case.last_used_date, |
| 953 credit_card1.GetLastUsedDateForDisplay(test_case.app_locale)); | 1140 credit_card1.GetLastUsedDateForDisplay(test_case.app_locale)); |
| 954 EXPECT_EQ(test_case.last_used_year_ago, | 1141 EXPECT_EQ(test_case.last_used_year_ago, |
| 955 credit_card2.GetLastUsedDateForDisplay(test_case.app_locale)); | 1142 credit_card2.GetLastUsedDateForDisplay(test_case.app_locale)); |
| 956 variation_params_.ClearAllVariationParams(); | 1143 variation_params_.ClearAllVariationParams(); |
| 957 } | 1144 } |
| 958 } | 1145 } |
| 959 | 1146 |
| 960 } // namespace autofill | 1147 } // namespace autofill |
| OLD | NEW |