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