Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: components/payments/payments_validators_test.cc

Issue 2373103002: [Web Payments] Common Payments validation (Closed)
Patch Set: Fixed validator 2 Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 CurrencyCodeTestCase {
15 CurrencyCodeTestCase(const char* code, const char* system, bool expectedValid) 14 CurrencyCodeTestCase(const char* code, const char* system, bool expectedValid)
16 : code(code), system(system), expectedValid(expectedValid) {} 15 : code(code), system(system), expectedValid(expectedValid) {}
17 ~CurrencyCodeTestCase() {} 16 ~CurrencyCodeTestCase() {}
18 17
(...skipping 15 matching lines...) Expand all
34 33
35 const char* longString2049() { 34 const char* longString2049() {
36 static char longString[2050]; 35 static char longString[2050];
37 for (int i = 0; i < 2049; i++) 36 for (int i = 0; i < 2049; i++)
38 longString[i] = 'a'; 37 longString[i] = 'a';
39 longString[2049] = '\0'; 38 longString[2049] = '\0';
40 return longString; 39 return longString;
41 } 40 }
42 41
43 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) { 42 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) {
44 String errorMessage; 43 std::string errorMessage;
45 EXPECT_EQ(GetParam().expectedValid, 44 EXPECT_EQ(GetParam().expectedValid,
46 PaymentsValidators::isValidCurrencyCodeFormat( 45 payments::PaymentsValidators::isValidCurrencyCodeFormat(
47 GetParam().code, GetParam().system, &errorMessage)) 46 GetParam().code, GetParam().system, &errorMessage))
48 << errorMessage; 47 << errorMessage;
49 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 48 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
50 49
51 EXPECT_EQ(GetParam().expectedValid, 50 EXPECT_EQ(GetParam().expectedValid,
52 PaymentsValidators::isValidCurrencyCodeFormat( 51 payments::PaymentsValidators::isValidCurrencyCodeFormat(
53 GetParam().code, GetParam().system, nullptr)); 52 GetParam().code, GetParam().system, nullptr));
54 } 53 }
55 54
56 INSTANTIATE_TEST_CASE_P( 55 INSTANTIATE_TEST_CASE_P(
57 CurrencyCodes, 56 CurrencyCodes,
58 PaymentsCurrencyValidatorTest, 57 PaymentsCurrencyValidatorTest,
59 testing::Values( 58 testing::Values(
60 // The most common identifiers are three-letter alphabetic codes as 59 // The most common identifiers are three-letter alphabetic codes as
61 // defined by [ISO4217] (for example, "USD" for US Dollars). 60 // defined by [ISO4217] (for example, "USD" for US Dollars).
62 // |system| is a URL that indicates the currency system that the 61 // |system| is a URL that indicates the currency system that the
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 94
96 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) { 95 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) {
97 out << "'" << testCase.input << "' is expected to be " 96 out << "'" << testCase.input << "' is expected to be "
98 << (testCase.expectedValid ? "valid" : "invalid"); 97 << (testCase.expectedValid ? "valid" : "invalid");
99 return out; 98 return out;
100 } 99 }
101 100
102 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {}; 101 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {};
103 102
104 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) { 103 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) {
105 String errorMessage; 104 std::string errorMessage;
106 EXPECT_EQ(GetParam().expectedValid, PaymentsValidators::isValidAmountFormat( 105 EXPECT_EQ(GetParam().expectedValid,
107 GetParam().input, &errorMessage)) 106 payments::PaymentsValidators::isValidAmountFormat(GetParam().input,
107 &errorMessage))
108 << errorMessage; 108 << errorMessage;
109 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 109 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
110 110
111 EXPECT_EQ(GetParam().expectedValid, 111 EXPECT_EQ(GetParam().expectedValid,
112 PaymentsValidators::isValidAmountFormat(GetParam().input, nullptr)); 112 payments::PaymentsValidators::isValidAmountFormat(GetParam().input,
113 nullptr));
113 } 114 }
114 115
115 INSTANTIATE_TEST_CASE_P( 116 INSTANTIATE_TEST_CASE_P(
116 Amounts, 117 Amounts,
117 PaymentsAmountValidatorTest, 118 PaymentsAmountValidatorTest,
118 testing::Values(TestCase("0", true), 119 testing::Values(TestCase("0", true),
119 TestCase("-0", true), 120 TestCase("-0", true),
120 TestCase("1", true), 121 TestCase("1", true),
121 TestCase("10", true), 122 TestCase("10", true),
122 TestCase("-3", true), 123 TestCase("-3", true),
(...skipping 15 matching lines...) Expand all
138 TestCase("-10.", false), 139 TestCase("-10.", false),
139 TestCase("-.99", false), 140 TestCase("-.99", false),
140 TestCase("10-", false), 141 TestCase("10-", false),
141 TestCase("1-0", false), 142 TestCase("1-0", false),
142 TestCase("1.0.0", false), 143 TestCase("1.0.0", false),
143 TestCase("1/3", false))); 144 TestCase("1/3", false)));
144 145
145 class PaymentsRegionValidatorTest : public testing::TestWithParam<TestCase> {}; 146 class PaymentsRegionValidatorTest : public testing::TestWithParam<TestCase> {};
146 147
147 TEST_P(PaymentsRegionValidatorTest, IsValidCountryCodeFormat) { 148 TEST_P(PaymentsRegionValidatorTest, IsValidCountryCodeFormat) {
148 String errorMessage; 149 std::string errorMessage;
149 EXPECT_EQ(GetParam().expectedValid, 150 EXPECT_EQ(GetParam().expectedValid,
150 PaymentsValidators::isValidCountryCodeFormat(GetParam().input, 151 payments::PaymentsValidators::isValidCountryCodeFormat(
151 &errorMessage)) 152 GetParam().input, &errorMessage))
152 << errorMessage; 153 << errorMessage;
153 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 154 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
154 155
155 EXPECT_EQ( 156 EXPECT_EQ(GetParam().expectedValid,
156 GetParam().expectedValid, 157 payments::PaymentsValidators::isValidCountryCodeFormat(
157 PaymentsValidators::isValidCountryCodeFormat(GetParam().input, nullptr)); 158 GetParam().input, nullptr));
158 } 159 }
159 160
160 INSTANTIATE_TEST_CASE_P(CountryCodes, 161 INSTANTIATE_TEST_CASE_P(CountryCodes,
161 PaymentsRegionValidatorTest, 162 PaymentsRegionValidatorTest,
162 testing::Values(TestCase("US", true), 163 testing::Values(TestCase("US", true),
163 // Invalid country code formats 164 // Invalid country code formats
164 TestCase("U1", false), 165 TestCase("U1", false),
165 TestCase("U", false), 166 TestCase("U", false),
166 TestCase("us", false), 167 TestCase("us", false),
167 TestCase("USA", false), 168 TestCase("USA", false),
168 TestCase("", false))); 169 TestCase("", false)));
169 170
170 class PaymentsLanguageValidatorTest : public testing::TestWithParam<TestCase> { 171 class PaymentsLanguageValidatorTest : public testing::TestWithParam<TestCase> {
171 }; 172 };
172 173
173 TEST_P(PaymentsLanguageValidatorTest, IsValidLanguageCodeFormat) { 174 TEST_P(PaymentsLanguageValidatorTest, IsValidLanguageCodeFormat) {
174 String errorMessage; 175 std::string errorMessage;
175 EXPECT_EQ(GetParam().expectedValid, 176 EXPECT_EQ(GetParam().expectedValid,
176 PaymentsValidators::isValidLanguageCodeFormat(GetParam().input, 177 payments::PaymentsValidators::isValidLanguageCodeFormat(
177 &errorMessage)) 178 GetParam().input, &errorMessage))
178 << errorMessage; 179 << errorMessage;
179 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 180 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
180 181
181 EXPECT_EQ( 182 EXPECT_EQ(GetParam().expectedValid,
182 GetParam().expectedValid, 183 payments::PaymentsValidators::isValidLanguageCodeFormat(
183 PaymentsValidators::isValidLanguageCodeFormat(GetParam().input, nullptr)); 184 GetParam().input, nullptr));
184 } 185 }
185 186
186 INSTANTIATE_TEST_CASE_P(LanguageCodes, 187 INSTANTIATE_TEST_CASE_P(LanguageCodes,
187 PaymentsLanguageValidatorTest, 188 PaymentsLanguageValidatorTest,
188 testing::Values(TestCase("", true), 189 testing::Values(TestCase("", true),
189 TestCase("en", true), 190 TestCase("en", true),
190 TestCase("eng", true), 191 TestCase("eng", true),
191 // Invalid language code formats 192 // Invalid language code formats
192 TestCase("e1", false), 193 TestCase("e1", false),
193 TestCase("en1", false), 194 TestCase("en1", false),
194 TestCase("e", false), 195 TestCase("e", false),
195 TestCase("engl", false), 196 TestCase("engl", false),
196 TestCase("EN", false))); 197 TestCase("EN", false)));
197 198
198 class PaymentsScriptValidatorTest : public testing::TestWithParam<TestCase> {}; 199 class PaymentsScriptValidatorTest : public testing::TestWithParam<TestCase> {};
199 200
200 TEST_P(PaymentsScriptValidatorTest, IsValidScriptCodeFormat) { 201 TEST_P(PaymentsScriptValidatorTest, IsValidScriptCodeFormat) {
201 String errorMessage; 202 std::string errorMessage;
202 EXPECT_EQ(GetParam().expectedValid, 203 EXPECT_EQ(GetParam().expectedValid,
203 PaymentsValidators::isValidScriptCodeFormat(GetParam().input, 204 payments::PaymentsValidators::isValidScriptCodeFormat(
204 &errorMessage)) 205 GetParam().input, &errorMessage))
205 << errorMessage; 206 << errorMessage;
206 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 207 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
207 208
208 EXPECT_EQ( 209 EXPECT_EQ(GetParam().expectedValid,
209 GetParam().expectedValid, 210 payments::PaymentsValidators::isValidScriptCodeFormat(
210 PaymentsValidators::isValidScriptCodeFormat(GetParam().input, nullptr)); 211 GetParam().input, nullptr));
211 } 212 }
212 213
213 INSTANTIATE_TEST_CASE_P(ScriptCodes, 214 INSTANTIATE_TEST_CASE_P(ScriptCodes,
214 PaymentsScriptValidatorTest, 215 PaymentsScriptValidatorTest,
215 testing::Values(TestCase("", true), 216 testing::Values(TestCase("", true),
216 TestCase("Latn", true), 217 TestCase("Latn", true),
217 // Invalid script code formats 218 // Invalid script code formats
218 TestCase("Lat1", false), 219 TestCase("Lat1", false),
219 TestCase("1lat", false), 220 TestCase("1lat", false),
220 TestCase("Latin", false), 221 TestCase("Latin", false),
(...skipping 15 matching lines...) Expand all
236 const char* countryCode; 237 const char* countryCode;
237 const char* languageCode; 238 const char* languageCode;
238 const char* scriptCode; 239 const char* scriptCode;
239 bool expectedValid; 240 bool expectedValid;
240 }; 241 };
241 242
242 class PaymentsShippingAddressValidatorTest 243 class PaymentsShippingAddressValidatorTest
243 : public testing::TestWithParam<ShippingAddressTestCase> {}; 244 : public testing::TestWithParam<ShippingAddressTestCase> {};
244 245
245 TEST_P(PaymentsShippingAddressValidatorTest, IsValidShippingAddress) { 246 TEST_P(PaymentsShippingAddressValidatorTest, IsValidShippingAddress) {
246 mojom::blink::PaymentAddressPtr address = mojom::blink::PaymentAddress::New(); 247 blink::mojom::PaymentAddressPtr address = blink::mojom::PaymentAddress::New();
247 address->country = GetParam().countryCode; 248 address->country = GetParam().countryCode;
248 address->language_code = GetParam().languageCode; 249 address->language_code = GetParam().languageCode;
249 address->script_code = GetParam().scriptCode; 250 address->script_code = GetParam().scriptCode;
250 251
251 String errorMessage; 252 std::string errorMessage;
252 EXPECT_EQ(GetParam().expectedValid, 253 EXPECT_EQ(GetParam().expectedValid,
253 PaymentsValidators::isValidShippingAddress(address, &errorMessage)) 254 payments::PaymentsValidators::isValidShippingAddress(address,
255 &errorMessage))
254 << errorMessage; 256 << errorMessage;
255 EXPECT_EQ(GetParam().expectedValid, errorMessage.isEmpty()) << errorMessage; 257 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
256 258
257 EXPECT_EQ(GetParam().expectedValid, 259 EXPECT_EQ(
258 PaymentsValidators::isValidShippingAddress(address, nullptr)); 260 GetParam().expectedValid,
261 payments::PaymentsValidators::isValidShippingAddress(address, nullptr));
259 } 262 }
260 263
261 INSTANTIATE_TEST_CASE_P( 264 INSTANTIATE_TEST_CASE_P(
262 ShippingAddresses, 265 ShippingAddresses,
263 PaymentsShippingAddressValidatorTest, 266 PaymentsShippingAddressValidatorTest,
264 testing::Values( 267 testing::Values(
265 ShippingAddressTestCase("US", "en", "Latn", true), 268 ShippingAddressTestCase("US", "en", "Latn", true),
266 ShippingAddressTestCase("US", "en", "", true), 269 ShippingAddressTestCase("US", "en", "", true),
267 ShippingAddressTestCase("US", "", "", true), 270 ShippingAddressTestCase("US", "", "", true),
268 // Invalid shipping addresses 271 // Invalid shipping addresses
269 ShippingAddressTestCase("", "", "", false), 272 ShippingAddressTestCase("", "", "", false),
270 ShippingAddressTestCase("InvalidCountryCode", "", "", false), 273 ShippingAddressTestCase("InvalidCountryCode", "", "", false),
271 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false), 274 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false),
272 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false), 275 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false),
273 ShippingAddressTestCase("US", "", "Latn", false))); 276 ShippingAddressTestCase("US", "", "Latn", false)));
274 277
275 } // namespace 278 } // namespace
276 } // namespace blink 279 } // namespace blink
OLDNEW
« no previous file with comments | « components/payments/payments_validators.cc ('k') | third_party/WebKit/Source/modules/payments/PaymentRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698