| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/payments/PaymentsValidators.h" | 5 #include "components/payments/payments_validators.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "wtf/text/WTFString.h" | |
| 9 #include <ostream> // NOLINT | 8 #include <ostream> // NOLINT |
| 10 | 9 |
| 11 namespace blink { | 10 namespace blink { |
| 12 namespace { | 11 namespace { |
| 13 | 12 |
| 14 struct CurrencyCodeTestCase { | 13 struct TestCase { |
| 15 CurrencyCodeTestCase(const char* code, const char* system, bool expectedValid) | 14 TestCase(const char* input, bool expectedValid) |
| 16 : code(code), system(system), expectedValid(expectedValid) {} | 15 : input(input), expectedValid(expectedValid) {} |
| 17 ~CurrencyCodeTestCase() {} | 16 ~TestCase() {} |
| 18 | 17 |
| 19 const char* code; | 18 const char* input; |
| 20 const char* system; | |
| 21 bool expectedValid; | 19 bool expectedValid; |
| 22 }; | 20 }; |
| 23 | 21 |
| 24 class PaymentsCurrencyValidatorTest | 22 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) { |
| 25 : public testing::TestWithParam<CurrencyCodeTestCase> {}; | 23 out << "'" << testCase.input << "' is expected to be " |
| 24 << (testCase.expectedValid ? "valid" : "invalid"); |
| 25 return out; |
| 26 } |
| 27 |
| 28 class PaymentsCurrencyValidatorTest : public testing::TestWithParam<TestCase> { |
| 29 }; |
| 26 | 30 |
| 27 const char* longString2048() { | 31 const char* longString2048() { |
| 28 static char longString[2049]; | 32 static char longString[2049]; |
| 29 for (int i = 0; i < 2048; i++) | 33 for (int i = 0; i < 2048; i++) |
| 30 longString[i] = 'a'; | 34 longString[i] = 'a'; |
| 31 longString[2048] = '\0'; | 35 longString[2048] = '\0'; |
| 32 return longString; | 36 return longString; |
| 33 } | 37 } |
| 34 | 38 |
| 35 const char* longString2049() { | 39 const char* longString2049() { |
| 36 static char longString[2050]; | 40 static char longString[2050]; |
| 37 for (int i = 0; i < 2049; i++) | 41 for (int i = 0; i < 2049; i++) |
| 38 longString[i] = 'a'; | 42 longString[i] = 'a'; |
| 39 longString[2049] = '\0'; | 43 longString[2049] = '\0'; |
| 40 return longString; | 44 return longString; |
| 41 } | 45 } |
| 42 | 46 |
| 43 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) { | 47 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) { |
| 44 String errorMessage; | 48 String errorMessage; |
| 45 EXPECT_EQ(GetParam().expectedValid, | 49 EXPECT_EQ(GetParam().expectedValid, |
| 46 PaymentsValidators::isValidCurrencyCodeFormat( | 50 PaymentsValidators::isValidCurrencyCodeFormat(GetParam().input, |
| 47 GetParam().code, GetParam().system, &errorMessage)) | 51 &errorMessage)) |
| 48 << errorMessage; | 52 << errorMessage; |
| 49 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; | 53 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; |
| 50 | 54 |
| 51 EXPECT_EQ(GetParam().expectedValid, | 55 EXPECT_EQ( |
| 52 PaymentsValidators::isValidCurrencyCodeFormat( | 56 GetParam().expectedValid, |
| 53 GetParam().code, GetParam().system, nullptr)); | 57 PaymentsValidators::isValidCurrencyCodeFormat(GetParam().input, nullptr)); |
| 54 } | 58 } |
| 55 | 59 |
| 56 INSTANTIATE_TEST_CASE_P( | 60 INSTANTIATE_TEST_CASE_P( |
| 57 CurrencyCodes, | 61 CurrencyCodes, |
| 58 PaymentsCurrencyValidatorTest, | 62 PaymentsCurrencyValidatorTest, |
| 59 testing::Values( | 63 testing::Values( |
| 60 // The most common identifiers are three-letter alphabetic codes as | 64 // Any string of at most 2048 characters can be a valid currency code |
| 61 // defined by [ISO4217] (for example, "USD" for US Dollars). | 65 TestCase("USD", true), |
| 62 // |system| is a URL that indicates the currency system that the | 66 TestCase("US1", true), |
| 63 // currency identifier belongs to. By default, | 67 TestCase("US", true), |
| 64 // the value is urn:iso:std:iso:4217 indicating that currency is defined | 68 TestCase("USDO", true), |
| 65 // by [[ISO4217]], however any string of at most 2048 | 69 TestCase("usd", true), |
| 66 // characters is considered valid in other currencySystem. Returns false | 70 TestCase("ANYSTRING", true), |
| 67 // if currency |code| is too long (greater than 2048). | 71 TestCase("", true), |
| 68 CurrencyCodeTestCase("USD", "urn:iso:std:iso:4217", true), | 72 TestCase(longString2048(), true), |
| 69 CurrencyCodeTestCase("US1", "http://www.example.com", true), | 73 TestCase(longString2049(), false))); |
| 70 CurrencyCodeTestCase("US1", "urn:iso:std:iso:4217", false), | |
| 71 CurrencyCodeTestCase("US", "http://www.example.com", true), | |
| 72 CurrencyCodeTestCase("US", "urn:iso:std:iso:4217", false), | |
| 73 CurrencyCodeTestCase("USDO", "http://www.example.com", true), | |
| 74 CurrencyCodeTestCase("USDO", "urn:iso:std:iso:4217", false), | |
| 75 CurrencyCodeTestCase("usd", "http://www.example.com", true), | |
| 76 CurrencyCodeTestCase("usd", "urn:iso:std:iso:4217", false), | |
| 77 CurrencyCodeTestCase("ANYSTRING", "http://www.example.com", true), | |
| 78 CurrencyCodeTestCase("ANYSTRING", "urn:iso:std:iso:4217", false), | |
| 79 CurrencyCodeTestCase("", "http://www.example.com", true), | |
| 80 CurrencyCodeTestCase("", "urn:iso:std:iso:4217", false), | |
| 81 CurrencyCodeTestCase(longString2048(), "http://www.example.com", true), | |
| 82 CurrencyCodeTestCase(longString2048(), "urn:iso:std:iso:4217", false), | |
| 83 CurrencyCodeTestCase(longString2049(), | |
| 84 "http://www.example.com", | |
| 85 false))); | |
| 86 | |
| 87 struct TestCase { | |
| 88 TestCase(const char* input, bool expectedValid) | |
| 89 : input(input), expectedValid(expectedValid) {} | |
| 90 ~TestCase() {} | |
| 91 | |
| 92 const char* input; | |
| 93 bool expectedValid; | |
| 94 }; | |
| 95 | |
| 96 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) { | |
| 97 out << "'" << testCase.input << "' is expected to be " | |
| 98 << (testCase.expectedValid ? "valid" : "invalid"); | |
| 99 return out; | |
| 100 } | |
| 101 | 74 |
| 102 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; | 75 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; |
| 103 | 76 |
| 104 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { | 77 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { |
| 105 String errorMessage; | 78 String errorMessage; |
| 106 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat( | 79 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat( |
| 107 GetParam().input, &errorMessage)) | 80 GetParam().input, &errorMessage)) |
| 108 << errorMessage; | 81 << errorMessage; |
| 109 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; | 82 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; |
| 110 | 83 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 ShippingAddressTestCase("US", "", "", true), | 240 ShippingAddressTestCase("US", "", "", true), |
| 268 // Invalid shipping addresses | 241 // Invalid shipping addresses |
| 269 ShippingAddressTestCase("", "", "", false), | 242 ShippingAddressTestCase("", "", "", false), |
| 270 ShippingAddressTestCase("InvalidCountryCode", "", "", false), | 243 ShippingAddressTestCase("InvalidCountryCode", "", "", false), |
| 271 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), | 244 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), |
| 272 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), | 245 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), |
| 273 ShippingAddressTestCase("US", "", "Latn", false))); | 246 ShippingAddressTestCase("US", "", "Latn", false))); |
| 274 | 247 |
| 275 } // namespace | 248 } // namespace |
| 276 } // namespace blink | 249 } // namespace blink |
| OLD | NEW |