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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 {"45", 2045}, | 175 {"45", 2045}, |
176 {"045", 2045}, | 176 {"045", 2045}, |
177 {"9", 2009}, | 177 {"9", 2009}, |
178 | 178 |
179 // Unrecognized year values. | 179 // Unrecognized year values. |
180 {"052045", 0}, | 180 {"052045", 0}, |
181 {"123", 0}, | 181 {"123", 0}, |
182 {"y2045", 0}, | 182 {"y2045", 0}, |
183 }; | 183 }; |
184 | 184 |
185 for (const auto& test_case : kTestCases) { | 185 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
186 CreditCard card(base::GenerateGUID(), "some origin"); | 186 CreditCard card(base::GenerateGUID(), "some origin"); |
187 card.SetExpirationYearFromString(ASCIIToUTF16(test_case.expiration_year)); | 187 card.SetExpirationYearFromString( |
| 188 ASCIIToUTF16(kTestCases[i].expiration_year)); |
188 | 189 |
189 EXPECT_EQ(test_case.expected_year, card.expiration_year()) | 190 EXPECT_EQ(kTestCases[i].expected_year, card.expiration_year()) |
190 << test_case.expiration_year << " " << test_case.expected_year; | 191 << kTestCases[i].expiration_year << " " << kTestCases[i].expected_year; |
191 } | 192 } |
192 } | 193 } |
193 | 194 |
194 TEST(CreditCardTest, SetExpirationDateFromString) { | 195 TEST(CreditCardTest, SetExpirationDateFromString) { |
195 static const struct { | 196 static const struct { |
196 std::string expiration_date; | 197 std::string expiration_date; |
197 int expected_month; | 198 int expected_month; |
198 int expected_year; | 199 int expected_year; |
199 } kTestCases[] = {{"10", 0, 0}, // Too small. | 200 } kTestCases[] = {{"10", 0, 0}, // Too small. |
200 {"1020451", 0, 0}, // Too long. | 201 {"1020451", 0, 0}, // Too long. |
(...skipping 19 matching lines...) Expand all Loading... |
220 {"5|2045", 5, 2045}, | 221 {"5|2045", 5, 2045}, |
221 {"05|2045", 5, 2045}, | 222 {"05|2045", 5, 2045}, |
222 | 223 |
223 // Invalid values. | 224 // Invalid values. |
224 {"13/2016", 0, 2016}, | 225 {"13/2016", 0, 2016}, |
225 {"16/13", 0, 2013}, | 226 {"16/13", 0, 2013}, |
226 {"May-2015", 0, 0}, | 227 {"May-2015", 0, 0}, |
227 {"05-/2045", 0, 0}, | 228 {"05-/2045", 0, 0}, |
228 {"05_2045", 0, 0}}; | 229 {"05_2045", 0, 0}}; |
229 | 230 |
230 for (const auto& test_case : kTestCases) { | 231 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
231 CreditCard card(base::GenerateGUID(), "some origin"); | 232 CreditCard card(base::GenerateGUID(), "some origin"); |
232 card.SetExpirationDateFromString(ASCIIToUTF16(test_case.expiration_date)); | 233 card.SetExpirationDateFromString( |
| 234 ASCIIToUTF16(kTestCases[i].expiration_date)); |
233 | 235 |
234 EXPECT_EQ(test_case.expected_month, card.expiration_month()); | 236 EXPECT_EQ(kTestCases[i].expected_month, card.expiration_month()); |
235 EXPECT_EQ(test_case.expected_year, card.expiration_year()); | 237 EXPECT_EQ(kTestCases[i].expected_year, card.expiration_year()); |
236 } | 238 } |
237 } | 239 } |
238 | 240 |
239 TEST(CreditCardTest, Copy) { | 241 TEST(CreditCardTest, Copy) { |
240 CreditCard a(base::GenerateGUID(), "https://www.example.com"); | 242 CreditCard a(base::GenerateGUID(), "https://www.example.com"); |
241 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010"); | 243 test::SetCreditCardInfo(&a, "John Dillinger", "123456789012", "01", "2010"); |
242 | 244 |
243 // Clone should be logically equal to the original. | 245 // Clone should be logically equal to the original. |
244 CreditCard b(a); | 246 CreditCard b(a); |
245 EXPECT_TRUE(a == b); | 247 EXPECT_TRUE(a == b); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kVisaCard, | 286 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kVisaCard, |
285 true }, | 287 true }, |
286 { LOCAL_CARD, "", "423456789012", "", "", | 288 { LOCAL_CARD, "", "423456789012", "", "", |
287 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kMasterCard, | 289 MASKED_SERVER_CARD, "John Dillinger", "9012", "01", "2010", kMasterCard, |
288 false }, | 290 false }, |
289 { LOCAL_CARD, "John Dillinger", "4234-5678-9012", "01", "2010", | 291 { LOCAL_CARD, "John Dillinger", "4234-5678-9012", "01", "2010", |
290 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, | 292 FULL_SERVER_CARD, "John Dillinger", "423456789012", "01", "2010", nullptr, |
291 true }, | 293 true }, |
292 }; | 294 }; |
293 | 295 |
294 for (const auto& test_case : test_cases) { | 296 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
295 CreditCard a(base::GenerateGUID(), std::string()); | 297 CreditCard a(base::GenerateGUID(), std::string()); |
296 a.set_record_type(test_case.first_card_record_type); | 298 a.set_record_type(test_cases[i].first_card_record_type); |
297 test::SetCreditCardInfo( | 299 test::SetCreditCardInfo(&a, |
298 &a, test_case.first_card_name, test_case.first_card_number, | 300 test_cases[i].first_card_name, |
299 test_case.first_card_exp_mo, test_case.first_card_exp_yr); | 301 test_cases[i].first_card_number, |
| 302 test_cases[i].first_card_exp_mo, |
| 303 test_cases[i].first_card_exp_yr); |
300 | 304 |
301 CreditCard b(base::GenerateGUID(), std::string()); | 305 CreditCard b(base::GenerateGUID(), std::string()); |
302 b.set_record_type(test_case.second_card_record_type); | 306 b.set_record_type(test_cases[i].second_card_record_type); |
303 test::SetCreditCardInfo( | 307 test::SetCreditCardInfo(&b, |
304 &b, test_case.second_card_name, test_case.second_card_number, | 308 test_cases[i].second_card_name, |
305 test_case.second_card_exp_mo, test_case.second_card_exp_yr); | 309 test_cases[i].second_card_number, |
| 310 test_cases[i].second_card_exp_mo, |
| 311 test_cases[i].second_card_exp_yr); |
306 | 312 |
307 if (test_case.second_card_record_type == CreditCard::MASKED_SERVER_CARD) | 313 if (test_cases[i].second_card_record_type == CreditCard::MASKED_SERVER_CARD) |
308 b.SetTypeForMaskedCard(test_case.second_card_type); | 314 b.SetTypeForMaskedCard(test_cases[i].second_card_type); |
309 | 315 |
310 EXPECT_EQ(test_case.is_local_duplicate, a.IsLocalDuplicateOfServerCard(b)) | 316 EXPECT_EQ(test_cases[i].is_local_duplicate, |
311 << " when comparing cards " << a.Label() << " and " << b.Label(); | 317 a.IsLocalDuplicateOfServerCard(b)) << " when comparing cards " |
| 318 << a.Label() << " and " << b.Label(); |
312 } | 319 } |
313 } | 320 } |
314 | 321 |
315 TEST(CreditCardTest, HasSameNumberAs) { | 322 TEST(CreditCardTest, HasSameNumberAs) { |
316 CreditCard a(base::GenerateGUID(), std::string()); | 323 CreditCard a(base::GenerateGUID(), std::string()); |
317 CreditCard b(base::GenerateGUID(), std::string()); | 324 CreditCard b(base::GenerateGUID(), std::string()); |
318 | 325 |
319 // Empty cards have the same empty number. | 326 // Empty cards have the same empty number. |
320 EXPECT_TRUE(a.HasSameNumberAs(b)); | 327 EXPECT_TRUE(a.HasSameNumberAs(b)); |
321 EXPECT_TRUE(b.HasSameNumberAs(a)); | 328 EXPECT_TRUE(b.HasSameNumberAs(a)); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 base::IntToString16(now_exploded.year - 1)); | 533 base::IntToString16(now_exploded.year - 1)); |
527 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111")); | 534 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111")); |
528 EXPECT_FALSE(card.IsValid()); | 535 EXPECT_FALSE(card.IsValid()); |
529 | 536 |
530 // Invalid because card number is not complete | 537 // Invalid because card number is not complete |
531 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12")); | 538 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12")); |
532 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2999")); | 539 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2999")); |
533 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111")); | 540 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111")); |
534 EXPECT_FALSE(card.IsValid()); | 541 EXPECT_FALSE(card.IsValid()); |
535 | 542 |
536 for (const char* valid_number : kValidNumbers) { | 543 for (size_t i = 0; i < arraysize(kValidNumbers); ++i) { |
537 SCOPED_TRACE(valid_number); | 544 SCOPED_TRACE(kValidNumbers[i]); |
538 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(valid_number)); | 545 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kValidNumbers[i])); |
539 EXPECT_TRUE(card.IsValid()); | 546 EXPECT_TRUE(card.IsValid()); |
540 } | 547 } |
541 for (const char* invalid_number : kInvalidNumbers) { | 548 for (size_t i = 0; i < arraysize(kInvalidNumbers); ++i) { |
542 SCOPED_TRACE(invalid_number); | 549 SCOPED_TRACE(kInvalidNumbers[i]); |
543 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(invalid_number)); | 550 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16(kInvalidNumbers[i])); |
544 EXPECT_FALSE(card.IsValid()); | 551 EXPECT_FALSE(card.IsValid()); |
545 } | 552 } |
546 } | 553 } |
547 | 554 |
548 // Verify that we preserve exactly what the user typed for credit card numbers. | 555 // Verify that we preserve exactly what the user typed for credit card numbers. |
549 TEST(CreditCardTest, SetRawInfoCreditCardNumber) { | 556 TEST(CreditCardTest, SetRawInfoCreditCardNumber) { |
550 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); | 557 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); |
551 | 558 |
552 test::SetCreditCardInfo(&card, "Bob Dylan", | 559 test::SetCreditCardInfo(&card, "Bob Dylan", |
553 "4321-5432-6543-xxxx", "07", "2013"); | 560 "4321-5432-6543-xxxx", "07", "2013"); |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 { "68", kGenericCard, false }, | 766 { "68", kGenericCard, false }, |
760 { "69", kGenericCard, false }, | 767 { "69", kGenericCard, false }, |
761 { "7", kGenericCard, false }, | 768 { "7", kGenericCard, false }, |
762 { "8", kGenericCard, false }, | 769 { "8", kGenericCard, false }, |
763 { "9", kGenericCard, false }, | 770 { "9", kGenericCard, false }, |
764 | 771 |
765 // Oddball case: Unknown issuer, but valid Luhn check and plausible length. | 772 // Oddball case: Unknown issuer, but valid Luhn check and plausible length. |
766 { "7000700070007000", kGenericCard, true }, | 773 { "7000700070007000", kGenericCard, true }, |
767 }; | 774 }; |
768 | 775 |
769 for (const auto& test_case : test_cases) { | 776 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
770 base::string16 card_number = ASCIIToUTF16(test_case.card_number); | 777 base::string16 card_number = ASCIIToUTF16(test_cases[i].card_number); |
771 SCOPED_TRACE(card_number); | 778 SCOPED_TRACE(card_number); |
772 EXPECT_EQ(test_case.type, CreditCard::GetCreditCardType(card_number)); | 779 EXPECT_EQ(test_cases[i].type, CreditCard::GetCreditCardType(card_number)); |
773 EXPECT_EQ(test_case.is_valid, IsValidCreditCardNumber(card_number)); | 780 EXPECT_EQ(test_cases[i].is_valid, IsValidCreditCardNumber(card_number)); |
774 } | 781 } |
775 } | 782 } |
776 | 783 |
777 TEST(CreditCardTest, LastFourDigits) { | 784 TEST(CreditCardTest, LastFourDigits) { |
778 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); | 785 CreditCard card(base::GenerateGUID(), "https://www.example.com/"); |
779 ASSERT_EQ(base::string16(), card.LastFourDigits()); | 786 ASSERT_EQ(base::string16(), card.LastFourDigits()); |
780 | 787 |
781 test::SetCreditCardInfo(&card, "Baby Face Nelson", | 788 test::SetCreditCardInfo(&card, "Baby Face Nelson", |
782 "5212341234123489", "01", "2010"); | 789 "5212341234123489", "01", "2010"); |
783 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); | 790 ASSERT_EQ(base::ASCIIToUTF16("3489"), card.LastFourDigits()); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 {false, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, | 875 {false, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, |
869 CreditCard::OK}, | 876 CreditCard::OK}, |
870 {false, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, | 877 {false, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, |
871 CreditCard::OK}, | 878 CreditCard::OK}, |
872 {true, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, | 879 {true, next_year.month, next_year.year, CreditCard::MASKED_SERVER_CARD, |
873 CreditCard::EXPIRED}, | 880 CreditCard::EXPIRED}, |
874 {true, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, | 881 {true, next_year.month, next_year.year, CreditCard::FULL_SERVER_CARD, |
875 CreditCard::EXPIRED}, | 882 CreditCard::EXPIRED}, |
876 }; | 883 }; |
877 | 884 |
878 for (const auto& test_case : kTestCases) { | 885 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
879 CreditCard card; | 886 CreditCard card; |
880 card.SetExpirationMonth(test_case.month); | 887 card.SetExpirationMonth(kTestCases[i].month); |
881 card.SetExpirationYear(test_case.year); | 888 card.SetExpirationYear(kTestCases[i].year); |
882 card.set_record_type(test_case.record_type); | 889 card.set_record_type(kTestCases[i].record_type); |
883 if (card.record_type() != CreditCard::LOCAL_CARD) | 890 if (card.record_type() != CreditCard::LOCAL_CARD) |
884 card.SetServerStatus(test_case.server_status); | 891 card.SetServerStatus(kTestCases[i].server_status); |
885 | 892 |
886 EXPECT_EQ(test_case.should_update_expiration, | 893 EXPECT_EQ(kTestCases[i].should_update_expiration, |
887 card.ShouldUpdateExpiration(now)); | 894 card.ShouldUpdateExpiration(now)); |
888 } | 895 } |
889 } | 896 } |
890 | 897 |
891 } // namespace autofill | 898 } // namespace autofill |
OLD | NEW |