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 "modules/payments/PaymentsValidators.h" |
6 | 6 |
7 #include <ostream> // NOLINT | 7 #include <ostream> // NOLINT |
8 #include "platform/wtf/text/WTFString.h" | 8 #include "platform/wtf/text/WTFString.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 namespace { | 12 namespace { |
13 | 13 |
14 struct CurrencyCodeTestCase { | 14 struct CurrencyCodeTestCase { |
15 CurrencyCodeTestCase(const char* code, | 15 CurrencyCodeTestCase(const char* code, |
16 const char* system, | 16 const char* system, |
17 bool expected_valid) | 17 bool expected_valid) |
18 : code(code), system(system), expected_valid(expected_valid) {} | 18 : code(code), system(system), expected_valid(expected_valid) {} |
19 ~CurrencyCodeTestCase() {} | 19 ~CurrencyCodeTestCase() {} |
20 | 20 |
21 const char* code; | 21 const char* code; |
22 const char* system; | 22 const char* system; |
23 bool expected_valid; | 23 bool expected_valid; |
24 }; | 24 }; |
25 | 25 |
26 class PaymentsCurrencyValidatorTest | 26 class PaymentsCurrencyValidatorTest |
27 : public testing::TestWithParam<CurrencyCodeTestCase> {}; | 27 : public ::testing::TestWithParam<CurrencyCodeTestCase> {}; |
28 | 28 |
29 const char* LongString2048() { | 29 const char* LongString2048() { |
30 static char long_string[2049]; | 30 static char long_string[2049]; |
31 for (int i = 0; i < 2048; i++) | 31 for (int i = 0; i < 2048; i++) |
32 long_string[i] = 'a'; | 32 long_string[i] = 'a'; |
33 long_string[2048] = '\0'; | 33 long_string[2048] = '\0'; |
34 return long_string; | 34 return long_string; |
35 } | 35 } |
36 | 36 |
37 const char* LongString2049() { | 37 const char* LongString2049() { |
(...skipping 14 matching lines...) Expand all Loading... |
52 << error_message; | 52 << error_message; |
53 | 53 |
54 EXPECT_EQ(GetParam().expected_valid, | 54 EXPECT_EQ(GetParam().expected_valid, |
55 PaymentsValidators::IsValidCurrencyCodeFormat( | 55 PaymentsValidators::IsValidCurrencyCodeFormat( |
56 GetParam().code, GetParam().system, nullptr)); | 56 GetParam().code, GetParam().system, nullptr)); |
57 } | 57 } |
58 | 58 |
59 INSTANTIATE_TEST_CASE_P( | 59 INSTANTIATE_TEST_CASE_P( |
60 CurrencyCodes, | 60 CurrencyCodes, |
61 PaymentsCurrencyValidatorTest, | 61 PaymentsCurrencyValidatorTest, |
62 testing::Values( | 62 ::testing::Values( |
63 // The most common identifiers are three-letter alphabetic codes as | 63 // The most common identifiers are three-letter alphabetic codes as |
64 // defined by [ISO4217] (for example, "USD" for US Dollars). | 64 // defined by [ISO4217] (for example, "USD" for US Dollars). |
65 // |system| is a URL that indicates the currency system that the | 65 // |system| is a URL that indicates the currency system that the |
66 // currency identifier belongs to. By default, | 66 // currency identifier belongs to. By default, |
67 // the value is urn:iso:std:iso:4217 indicating that currency is defined | 67 // the value is urn:iso:std:iso:4217 indicating that currency is defined |
68 // by [[ISO4217]], however any string of at most 2048 | 68 // by [[ISO4217]], however any string of at most 2048 |
69 // characters is considered valid in other currencySystem. Returns false | 69 // characters is considered valid in other currencySystem. Returns false |
70 // if currency |code| is too long (greater than 2048). | 70 // if currency |code| is too long (greater than 2048). |
71 CurrencyCodeTestCase("USD", "urn:iso:std:iso:4217", true), | 71 CurrencyCodeTestCase("USD", "urn:iso:std:iso:4217", true), |
72 CurrencyCodeTestCase("US1", "http://www.example.com", true), | 72 CurrencyCodeTestCase("US1", "http://www.example.com", true), |
(...skipping 22 matching lines...) Expand all Loading... |
95 const char* input; | 95 const char* input; |
96 bool expected_valid; | 96 bool expected_valid; |
97 }; | 97 }; |
98 | 98 |
99 std::ostream& operator<<(std::ostream& out, const TestCase& test_case) { | 99 std::ostream& operator<<(std::ostream& out, const TestCase& test_case) { |
100 out << "'" << test_case.input << "' is expected to be " | 100 out << "'" << test_case.input << "' is expected to be " |
101 << (test_case.expected_valid ? "valid" : "invalid"); | 101 << (test_case.expected_valid ? "valid" : "invalid"); |
102 return out; | 102 return out; |
103 } | 103 } |
104 | 104 |
105 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; | 105 class PaymentsAmountValidatorTest : public ::testing::TestWithParam<TestCase> { |
| 106 }; |
106 | 107 |
107 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { | 108 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { |
108 String error_message; | 109 String error_message; |
109 EXPECT_EQ(GetParam().expected_valid, | 110 EXPECT_EQ(GetParam().expected_valid, |
110 PaymentsValidators::IsValidAmountFormat( | 111 PaymentsValidators::IsValidAmountFormat( |
111 GetParam().input, "test value", &error_message)) | 112 GetParam().input, "test value", &error_message)) |
112 << error_message; | 113 << error_message; |
113 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) | 114 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) |
114 << error_message; | 115 << error_message; |
115 | 116 |
116 EXPECT_EQ(GetParam().expected_valid, | 117 EXPECT_EQ(GetParam().expected_valid, |
117 PaymentsValidators::IsValidAmountFormat(GetParam().input, | 118 PaymentsValidators::IsValidAmountFormat(GetParam().input, |
118 "test value", nullptr)); | 119 "test value", nullptr)); |
119 } | 120 } |
120 | 121 |
121 INSTANTIATE_TEST_CASE_P( | 122 INSTANTIATE_TEST_CASE_P( |
122 Amounts, | 123 Amounts, |
123 PaymentsAmountValidatorTest, | 124 PaymentsAmountValidatorTest, |
124 testing::Values(TestCase("0", true), | 125 ::testing::Values(TestCase("0", true), |
125 TestCase("-0", true), | 126 TestCase("-0", true), |
126 TestCase("1", true), | 127 TestCase("1", true), |
127 TestCase("10", true), | 128 TestCase("10", true), |
128 TestCase("-3", true), | 129 TestCase("-3", true), |
129 TestCase("10.99", true), | 130 TestCase("10.99", true), |
130 TestCase("-3.00", true), | 131 TestCase("-3.00", true), |
131 TestCase("01234567890123456789.0123456789", true), | 132 TestCase("01234567890123456789.0123456789", true), |
132 TestCase("01234567890123456789012345678.9", true), | 133 TestCase("01234567890123456789012345678.9", true), |
133 TestCase("012345678901234567890123456789", true), | 134 TestCase("012345678901234567890123456789", true), |
134 TestCase("-01234567890123456789.0123456789", true), | 135 TestCase("-01234567890123456789.0123456789", true), |
135 TestCase("-01234567890123456789012345678.9", true), | 136 TestCase("-01234567890123456789012345678.9", true), |
136 TestCase("-012345678901234567890123456789", true), | 137 TestCase("-012345678901234567890123456789", true), |
137 // Invalid amount formats | 138 // Invalid amount formats |
138 TestCase("", false), | 139 TestCase("", false), |
139 TestCase("-", false), | 140 TestCase("-", false), |
140 TestCase("notdigits", false), | 141 TestCase("notdigits", false), |
141 TestCase("ALSONOTDIGITS", false), | 142 TestCase("ALSONOTDIGITS", false), |
142 TestCase("10.", false), | 143 TestCase("10.", false), |
143 TestCase(".99", false), | 144 TestCase(".99", false), |
144 TestCase("-10.", false), | 145 TestCase("-10.", false), |
145 TestCase("-.99", false), | 146 TestCase("-.99", false), |
146 TestCase("10-", false), | 147 TestCase("10-", false), |
147 TestCase("1-0", false), | 148 TestCase("1-0", false), |
148 TestCase("1.0.0", false), | 149 TestCase("1.0.0", false), |
149 TestCase("1/3", false))); | 150 TestCase("1/3", false))); |
150 | 151 |
151 class PaymentsRegionValidatorTest : public testing::TestWithParam<TestCase> {}; | 152 class PaymentsRegionValidatorTest : public ::testing::TestWithParam<TestCase> { |
| 153 }; |
152 | 154 |
153 TEST_P(PaymentsRegionValidatorTest, IsValidCountryCodeFormat) { | 155 TEST_P(PaymentsRegionValidatorTest, IsValidCountryCodeFormat) { |
154 String error_message; | 156 String error_message; |
155 EXPECT_EQ(GetParam().expected_valid, | 157 EXPECT_EQ(GetParam().expected_valid, |
156 PaymentsValidators::IsValidCountryCodeFormat(GetParam().input, | 158 PaymentsValidators::IsValidCountryCodeFormat(GetParam().input, |
157 &error_message)) | 159 &error_message)) |
158 << error_message; | 160 << error_message; |
159 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) | 161 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) |
160 << error_message; | 162 << error_message; |
161 | 163 |
162 EXPECT_EQ( | 164 EXPECT_EQ( |
163 GetParam().expected_valid, | 165 GetParam().expected_valid, |
164 PaymentsValidators::IsValidCountryCodeFormat(GetParam().input, nullptr)); | 166 PaymentsValidators::IsValidCountryCodeFormat(GetParam().input, nullptr)); |
165 } | 167 } |
166 | 168 |
167 INSTANTIATE_TEST_CASE_P(CountryCodes, | 169 INSTANTIATE_TEST_CASE_P(CountryCodes, |
168 PaymentsRegionValidatorTest, | 170 PaymentsRegionValidatorTest, |
169 testing::Values(TestCase("US", true), | 171 ::testing::Values(TestCase("US", true), |
170 // Invalid country code formats | 172 // Invalid country code formats |
171 TestCase("U1", false), | 173 TestCase("U1", false), |
172 TestCase("U", false), | 174 TestCase("U", false), |
173 TestCase("us", false), | 175 TestCase("us", false), |
174 TestCase("USA", false), | 176 TestCase("USA", false), |
175 TestCase("", false))); | 177 TestCase("", false))); |
176 | 178 |
177 class PaymentsLanguageValidatorTest : public testing::TestWithParam<TestCase> { | 179 class PaymentsLanguageValidatorTest |
178 }; | 180 : public ::testing::TestWithParam<TestCase> {}; |
179 | 181 |
180 TEST_P(PaymentsLanguageValidatorTest, IsValidLanguageCodeFormat) { | 182 TEST_P(PaymentsLanguageValidatorTest, IsValidLanguageCodeFormat) { |
181 String error_message; | 183 String error_message; |
182 EXPECT_EQ(GetParam().expected_valid, | 184 EXPECT_EQ(GetParam().expected_valid, |
183 PaymentsValidators::IsValidLanguageCodeFormat(GetParam().input, | 185 PaymentsValidators::IsValidLanguageCodeFormat(GetParam().input, |
184 &error_message)) | 186 &error_message)) |
185 << error_message; | 187 << error_message; |
186 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) | 188 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) |
187 << error_message; | 189 << error_message; |
188 | 190 |
189 EXPECT_EQ( | 191 EXPECT_EQ( |
190 GetParam().expected_valid, | 192 GetParam().expected_valid, |
191 PaymentsValidators::IsValidLanguageCodeFormat(GetParam().input, nullptr)); | 193 PaymentsValidators::IsValidLanguageCodeFormat(GetParam().input, nullptr)); |
192 } | 194 } |
193 | 195 |
194 INSTANTIATE_TEST_CASE_P(LanguageCodes, | 196 INSTANTIATE_TEST_CASE_P(LanguageCodes, |
195 PaymentsLanguageValidatorTest, | 197 PaymentsLanguageValidatorTest, |
196 testing::Values(TestCase("", true), | 198 ::testing::Values(TestCase("", true), |
197 TestCase("en", true), | 199 TestCase("en", true), |
198 TestCase("eng", true), | 200 TestCase("eng", true), |
199 // Invalid language code formats | 201 // Invalid language code formats |
200 TestCase("e1", false), | 202 TestCase("e1", false), |
201 TestCase("en1", false), | 203 TestCase("en1", false), |
202 TestCase("e", false), | 204 TestCase("e", false), |
203 TestCase("engl", false), | 205 TestCase("engl", false), |
204 TestCase("EN", false))); | 206 TestCase("EN", false))); |
205 | 207 |
206 class PaymentsScriptValidatorTest : public testing::TestWithParam<TestCase> {}; | 208 class PaymentsScriptValidatorTest : public ::testing::TestWithParam<TestCase> { |
| 209 }; |
207 | 210 |
208 TEST_P(PaymentsScriptValidatorTest, IsValidScriptCodeFormat) { | 211 TEST_P(PaymentsScriptValidatorTest, IsValidScriptCodeFormat) { |
209 String error_message; | 212 String error_message; |
210 EXPECT_EQ(GetParam().expected_valid, | 213 EXPECT_EQ(GetParam().expected_valid, |
211 PaymentsValidators::IsValidScriptCodeFormat(GetParam().input, | 214 PaymentsValidators::IsValidScriptCodeFormat(GetParam().input, |
212 &error_message)) | 215 &error_message)) |
213 << error_message; | 216 << error_message; |
214 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) | 217 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) |
215 << error_message; | 218 << error_message; |
216 | 219 |
217 EXPECT_EQ( | 220 EXPECT_EQ( |
218 GetParam().expected_valid, | 221 GetParam().expected_valid, |
219 PaymentsValidators::IsValidScriptCodeFormat(GetParam().input, nullptr)); | 222 PaymentsValidators::IsValidScriptCodeFormat(GetParam().input, nullptr)); |
220 } | 223 } |
221 | 224 |
222 INSTANTIATE_TEST_CASE_P(ScriptCodes, | 225 INSTANTIATE_TEST_CASE_P(ScriptCodes, |
223 PaymentsScriptValidatorTest, | 226 PaymentsScriptValidatorTest, |
224 testing::Values(TestCase("", true), | 227 ::testing::Values(TestCase("", true), |
225 TestCase("Latn", true), | 228 TestCase("Latn", true), |
226 // Invalid script code formats | 229 // Invalid script code formats |
227 TestCase("Lat1", false), | 230 TestCase("Lat1", false), |
228 TestCase("1lat", false), | 231 TestCase("1lat", false), |
229 TestCase("Latin", false), | 232 TestCase("Latin", false), |
230 TestCase("Lat", false), | 233 TestCase("Lat", false), |
231 TestCase("latn", false), | 234 TestCase("latn", false), |
232 TestCase("LATN", false))); | 235 TestCase("LATN", false))); |
233 | 236 |
234 struct ShippingAddressTestCase { | 237 struct ShippingAddressTestCase { |
235 ShippingAddressTestCase(const char* country_code, | 238 ShippingAddressTestCase(const char* country_code, |
236 const char* language_code, | 239 const char* language_code, |
237 const char* script_code, | 240 const char* script_code, |
238 bool expected_valid) | 241 bool expected_valid) |
239 : country_code(country_code), | 242 : country_code(country_code), |
240 language_code(language_code), | 243 language_code(language_code), |
241 script_code(script_code), | 244 script_code(script_code), |
242 expected_valid(expected_valid) {} | 245 expected_valid(expected_valid) {} |
243 ~ShippingAddressTestCase() {} | 246 ~ShippingAddressTestCase() {} |
244 | 247 |
245 const char* country_code; | 248 const char* country_code; |
246 const char* language_code; | 249 const char* language_code; |
247 const char* script_code; | 250 const char* script_code; |
248 bool expected_valid; | 251 bool expected_valid; |
249 }; | 252 }; |
250 | 253 |
251 class PaymentsShippingAddressValidatorTest | 254 class PaymentsShippingAddressValidatorTest |
252 : public testing::TestWithParam<ShippingAddressTestCase> {}; | 255 : public ::testing::TestWithParam<ShippingAddressTestCase> {}; |
253 | 256 |
254 TEST_P(PaymentsShippingAddressValidatorTest, IsValidShippingAddress) { | 257 TEST_P(PaymentsShippingAddressValidatorTest, IsValidShippingAddress) { |
255 payments::mojom::blink::PaymentAddressPtr address = | 258 payments::mojom::blink::PaymentAddressPtr address = |
256 payments::mojom::blink::PaymentAddress::New(); | 259 payments::mojom::blink::PaymentAddress::New(); |
257 address->country = GetParam().country_code; | 260 address->country = GetParam().country_code; |
258 address->language_code = GetParam().language_code; | 261 address->language_code = GetParam().language_code; |
259 address->script_code = GetParam().script_code; | 262 address->script_code = GetParam().script_code; |
260 | 263 |
261 String error_message; | 264 String error_message; |
262 EXPECT_EQ(GetParam().expected_valid, | 265 EXPECT_EQ(GetParam().expected_valid, |
263 PaymentsValidators::IsValidShippingAddress(address, &error_message)) | 266 PaymentsValidators::IsValidShippingAddress(address, &error_message)) |
264 << error_message; | 267 << error_message; |
265 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) | 268 EXPECT_EQ(GetParam().expected_valid, error_message.IsEmpty()) |
266 << error_message; | 269 << error_message; |
267 | 270 |
268 EXPECT_EQ(GetParam().expected_valid, | 271 EXPECT_EQ(GetParam().expected_valid, |
269 PaymentsValidators::IsValidShippingAddress(address, nullptr)); | 272 PaymentsValidators::IsValidShippingAddress(address, nullptr)); |
270 } | 273 } |
271 | 274 |
272 INSTANTIATE_TEST_CASE_P( | 275 INSTANTIATE_TEST_CASE_P( |
273 ShippingAddresses, | 276 ShippingAddresses, |
274 PaymentsShippingAddressValidatorTest, | 277 PaymentsShippingAddressValidatorTest, |
275 testing::Values( | 278 ::testing::Values( |
276 ShippingAddressTestCase("US", "en", "Latn", true), | 279 ShippingAddressTestCase("US", "en", "Latn", true), |
277 ShippingAddressTestCase("US", "en", "", true), | 280 ShippingAddressTestCase("US", "en", "", true), |
278 ShippingAddressTestCase("US", "", "", true), | 281 ShippingAddressTestCase("US", "", "", true), |
279 // Invalid shipping addresses | 282 // Invalid shipping addresses |
280 ShippingAddressTestCase("", "", "", false), | 283 ShippingAddressTestCase("", "", "", false), |
281 ShippingAddressTestCase("InvalidCountryCode", "", "", false), | 284 ShippingAddressTestCase("InvalidCountryCode", "", "", false), |
282 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), | 285 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), |
283 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), | 286 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), |
284 ShippingAddressTestCase("US", "", "Latn", false))); | 287 ShippingAddressTestCase("US", "", "Latn", false))); |
285 | 288 |
286 } // namespace | 289 } // namespace |
287 } // namespace blink | 290 } // namespace blink |
OLD | NEW |