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

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

Issue 2645813006: Download web payment manifests. (Closed)
Patch Set: Ted's comments Created 3 years, 9 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/payments/content/payments_validators.h"
6
7 #include <ostream> // NOLINT
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace payments {
11 namespace {
12
13 struct CurrencyCodeTestCase {
14 CurrencyCodeTestCase(const char* code, const char* system, bool expectedValid)
15 : code(code), system(system), expectedValid(expectedValid) {}
16 ~CurrencyCodeTestCase() {}
17
18 const char* code;
19 const char* system;
20 bool expectedValid;
21 };
22
23 class PaymentsCurrencyValidatorTest
24 : public testing::TestWithParam<CurrencyCodeTestCase> {};
25
26 const char* longString2048() {
27 static char longString[2049];
28 for (int i = 0; i < 2048; i++)
29 longString[i] = 'a';
30 longString[2048] = '\0';
31 return longString;
32 }
33
34 const char* longString2049() {
35 static char longString[2050];
36 for (int i = 0; i < 2049; i++)
37 longString[i] = 'a';
38 longString[2049] = '\0';
39 return longString;
40 }
41
42 TEST_P(PaymentsCurrencyValidatorTest, IsValidCurrencyCodeFormat) {
43 std::string errorMessage;
44 EXPECT_EQ(GetParam().expectedValid,
45 payments::PaymentsValidators::isValidCurrencyCodeFormat(
46 GetParam().code, GetParam().system, &errorMessage))
47 << errorMessage;
48 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
49
50 EXPECT_EQ(GetParam().expectedValid,
51 payments::PaymentsValidators::isValidCurrencyCodeFormat(
52 GetParam().code, GetParam().system, nullptr));
53 }
54
55 INSTANTIATE_TEST_CASE_P(
56 CurrencyCodes,
57 PaymentsCurrencyValidatorTest,
58 testing::Values(
59 // The most common identifiers are three-letter alphabetic codes as
60 // defined by [ISO4217] (for example, "USD" for US Dollars).
61 // |system| is a URL that indicates the currency system that the
62 // currency identifier belongs to. By default,
63 // the value is urn:iso:std:iso:4217 indicating that currency is defined
64 // by [[ISO4217]], however any string of at most 2048
65 // characters is considered valid in other currencySystem. Returns false
66 // if currency |code| is too long (greater than 2048).
67 CurrencyCodeTestCase("USD", "urn:iso:std:iso:4217", true),
68 CurrencyCodeTestCase("US1", "http://www.example.com", true),
69 CurrencyCodeTestCase("US1", "urn:iso:std:iso:4217", false),
70 CurrencyCodeTestCase("US", "http://www.example.com", true),
71 CurrencyCodeTestCase("US", "urn:iso:std:iso:4217", false),
72 CurrencyCodeTestCase("USDO", "http://www.example.com", true),
73 CurrencyCodeTestCase("USDO", "urn:iso:std:iso:4217", false),
74 CurrencyCodeTestCase("usd", "http://www.example.com", true),
75 CurrencyCodeTestCase("usd", "urn:iso:std:iso:4217", false),
76 CurrencyCodeTestCase("ANYSTRING", "http://www.example.com", true),
77 CurrencyCodeTestCase("ANYSTRING", "urn:iso:std:iso:4217", false),
78 CurrencyCodeTestCase("", "http://www.example.com", true),
79 CurrencyCodeTestCase("", "urn:iso:std:iso:4217", false),
80 CurrencyCodeTestCase(longString2048(), "http://www.example.com", true),
81 CurrencyCodeTestCase(longString2048(), "urn:iso:std:iso:4217", false),
82 CurrencyCodeTestCase(longString2049(),
83 "http://www.example.com",
84 false)));
85
86 struct TestCase {
87 TestCase(const char* input, bool expectedValid)
88 : input(input), expectedValid(expectedValid) {}
89 ~TestCase() {}
90
91 const char* input;
92 bool expectedValid;
93 };
94
95 std::ostream& operator<<(std::ostream& out, const TestCase& testCase) {
96 out << "'" << testCase.input << "' is expected to be "
97 << (testCase.expectedValid ? "valid" : "invalid");
98 return out;
99 }
100
101 class PaymentsAmountValidatorTest : public testing::TestWithParam<TestCase> {};
102
103 TEST_P(PaymentsAmountValidatorTest, IsValidAmountFormat) {
104 std::string errorMessage;
105 EXPECT_EQ(GetParam().expectedValid,
106 payments::PaymentsValidators::isValidAmountFormat(GetParam().input,
107 &errorMessage))
108 << errorMessage;
109 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
110
111 EXPECT_EQ(GetParam().expectedValid,
112 payments::PaymentsValidators::isValidAmountFormat(GetParam().input,
113 nullptr));
114 }
115
116 INSTANTIATE_TEST_CASE_P(
117 Amounts,
118 PaymentsAmountValidatorTest,
119 testing::Values(TestCase("0", true),
120 TestCase("-0", true),
121 TestCase("1", true),
122 TestCase("10", true),
123 TestCase("-3", true),
124 TestCase("10.99", true),
125 TestCase("-3.00", true),
126 TestCase("01234567890123456789.0123456789", true),
127 TestCase("01234567890123456789012345678.9", true),
128 TestCase("012345678901234567890123456789", true),
129 TestCase("-01234567890123456789.0123456789", true),
130 TestCase("-01234567890123456789012345678.9", true),
131 TestCase("-012345678901234567890123456789", true),
132 // Invalid amount formats
133 TestCase("", false),
134 TestCase("-", false),
135 TestCase("notdigits", false),
136 TestCase("ALSONOTDIGITS", false),
137 TestCase("10.", false),
138 TestCase(".99", false),
139 TestCase("-10.", false),
140 TestCase("-.99", false),
141 TestCase("10-", false),
142 TestCase("1-0", false),
143 TestCase("1.0.0", false),
144 TestCase("1/3", false)));
145
146 class PaymentsRegionValidatorTest : public testing::TestWithParam<TestCase> {};
147
148 TEST_P(PaymentsRegionValidatorTest, IsValidCountryCodeFormat) {
149 std::string errorMessage;
150 EXPECT_EQ(GetParam().expectedValid,
151 payments::PaymentsValidators::isValidCountryCodeFormat(
152 GetParam().input, &errorMessage))
153 << errorMessage;
154 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
155
156 EXPECT_EQ(GetParam().expectedValid,
157 payments::PaymentsValidators::isValidCountryCodeFormat(
158 GetParam().input, nullptr));
159 }
160
161 INSTANTIATE_TEST_CASE_P(CountryCodes,
162 PaymentsRegionValidatorTest,
163 testing::Values(TestCase("US", true),
164 // Invalid country code formats
165 TestCase("U1", false),
166 TestCase("U", false),
167 TestCase("us", false),
168 TestCase("USA", false),
169 TestCase("", false)));
170
171 class PaymentsLanguageValidatorTest : public testing::TestWithParam<TestCase> {
172 };
173
174 TEST_P(PaymentsLanguageValidatorTest, IsValidLanguageCodeFormat) {
175 std::string errorMessage;
176 EXPECT_EQ(GetParam().expectedValid,
177 payments::PaymentsValidators::isValidLanguageCodeFormat(
178 GetParam().input, &errorMessage))
179 << errorMessage;
180 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
181
182 EXPECT_EQ(GetParam().expectedValid,
183 payments::PaymentsValidators::isValidLanguageCodeFormat(
184 GetParam().input, nullptr));
185 }
186
187 INSTANTIATE_TEST_CASE_P(LanguageCodes,
188 PaymentsLanguageValidatorTest,
189 testing::Values(TestCase("", true),
190 TestCase("en", true),
191 TestCase("eng", true),
192 // Invalid language code formats
193 TestCase("e1", false),
194 TestCase("en1", false),
195 TestCase("e", false),
196 TestCase("engl", false),
197 TestCase("EN", false)));
198
199 class PaymentsScriptValidatorTest : public testing::TestWithParam<TestCase> {};
200
201 TEST_P(PaymentsScriptValidatorTest, IsValidScriptCodeFormat) {
202 std::string errorMessage;
203 EXPECT_EQ(GetParam().expectedValid,
204 payments::PaymentsValidators::isValidScriptCodeFormat(
205 GetParam().input, &errorMessage))
206 << errorMessage;
207 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
208
209 EXPECT_EQ(GetParam().expectedValid,
210 payments::PaymentsValidators::isValidScriptCodeFormat(
211 GetParam().input, nullptr));
212 }
213
214 INSTANTIATE_TEST_CASE_P(ScriptCodes,
215 PaymentsScriptValidatorTest,
216 testing::Values(TestCase("", true),
217 TestCase("Latn", true),
218 // Invalid script code formats
219 TestCase("Lat1", false),
220 TestCase("1lat", false),
221 TestCase("Latin", false),
222 TestCase("Lat", false),
223 TestCase("latn", false),
224 TestCase("LATN", false)));
225
226 struct ShippingAddressTestCase {
227 ShippingAddressTestCase(const char* countryCode,
228 const char* languageCode,
229 const char* scriptCode,
230 bool expectedValid)
231 : countryCode(countryCode),
232 languageCode(languageCode),
233 scriptCode(scriptCode),
234 expectedValid(expectedValid) {}
235 ~ShippingAddressTestCase() {}
236
237 const char* countryCode;
238 const char* languageCode;
239 const char* scriptCode;
240 bool expectedValid;
241 };
242
243 class PaymentsShippingAddressValidatorTest
244 : public testing::TestWithParam<ShippingAddressTestCase> {};
245
246 TEST_P(PaymentsShippingAddressValidatorTest, IsValidShippingAddress) {
247 payments::mojom::PaymentAddressPtr address =
248 payments::mojom::PaymentAddress::New();
249 address->country = GetParam().countryCode;
250 address->language_code = GetParam().languageCode;
251 address->script_code = GetParam().scriptCode;
252
253 std::string errorMessage;
254 EXPECT_EQ(GetParam().expectedValid,
255 payments::PaymentsValidators::isValidShippingAddress(address,
256 &errorMessage))
257 << errorMessage;
258 EXPECT_EQ(GetParam().expectedValid, errorMessage.empty()) << errorMessage;
259
260 EXPECT_EQ(
261 GetParam().expectedValid,
262 payments::PaymentsValidators::isValidShippingAddress(address, nullptr));
263 }
264
265 INSTANTIATE_TEST_CASE_P(
266 ShippingAddresses,
267 PaymentsShippingAddressValidatorTest,
268 testing::Values(
269 ShippingAddressTestCase("US", "en", "Latn", true),
270 ShippingAddressTestCase("US", "en", "", true),
271 ShippingAddressTestCase("US", "", "", true),
272 // Invalid shipping addresses
273 ShippingAddressTestCase("", "", "", false),
274 ShippingAddressTestCase("InvalidCountryCode", "", "", false),
275 ShippingAddressTestCase("US", "InvalidLanguageCode", "", false),
276 ShippingAddressTestCase("US", "en", "InvalidScriptCode", false),
277 ShippingAddressTestCase("US", "", "Latn", false)));
278
279 } // namespace
280 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698