xrpld
Loading...
Searching...
No Matches
tests/libxrpl/basics/StringUtilities.cpp
1#include <xrpl/basics/StringUtilities.h>
2
3#include <xrpl/basics/Slice.h>
4#include <xrpl/basics/ToString.h>
5
6#include <gtest/gtest.h>
7
8#include <array>
9#include <cstdint>
10#include <format>
11#include <optional>
12#include <string>
13#include <string_view>
14
15namespace xrpl {
16
17class StringUtilitiesTest : public ::testing::Test
18{
19public:
20 static void
21 testUnHexSuccess(std::string const& strIn, std::string const& strExpected)
22 {
23 auto rv = strUnHex(strIn);
24 EXPECT_TRUE(rv);
25
26 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
27 EXPECT_EQ(makeSlice(*rv), makeSlice(strExpected));
28 }
29
30 static void
32 {
33 auto const rv = strUnHex(strIn);
34 EXPECT_FALSE(rv);
35 }
36};
37
39{
40 testUnHexSuccess("526970706c6544", "RippleD");
41 testUnHexSuccess("A", "\n");
42 testUnHexSuccess("0A", "\n");
43 testUnHexSuccess("D0A", "\r\n");
44 testUnHexSuccess("0D0A", "\r\n");
45 testUnHexSuccess("200D0A", " \r\n");
46 testUnHexSuccess("282A2B2C2D2E2F29", "(*+,-./)");
47
48 // Check for things which contain some or only invalid characters
49 testUnHexFailure("123X");
50 testUnHexFailure("V");
51 testUnHexFailure("XRP");
52
53 testUnHexFailure(std::string(2, static_cast<char>(0xFF)));
54 testUnHexFailure(std::string(2, static_cast<char>(0x80)));
55 testUnHexFailure(std::string(1, static_cast<char>(0xFE)));
56 testUnHexFailure(std::string(1, static_cast<char>(0x80)));
57 testUnHexFailure(std::string("4") + static_cast<char>(0x80));
58}
59
60// Everything a URL is expected to parse into. The components a case does not
61// mention default to "absent", so each row lists only what it is about.
63{
67 // NOLINTBEGIN(readability-redundant-member-init)
73 // NOLINTEND(readability-redundant-member-init)
74};
75
76constexpr auto kParseUrlCases = std::to_array<ParseUrlCase>({
77 // RFC 3986:
78 // > In general, a URI that uses the generic syntax for authority
79 // with an empty path should be normalized to a path of "/".
80 // Do we want to normalize paths? Today an absent path stays absent.
81 {
82 .name = "scheme_only",
83 .url = "scheme://",
84 .scheme = "scheme",
85 },
86 {
87 .name = "empty_authority_with_root_path",
88 .url = "scheme:///",
89 .scheme = "scheme",
90 .path = "/",
91 },
92 {
93 .name = "lowercase_scheme",
94 .url = "lower://domain",
95 .scheme = "lower",
96 .domain = "domain",
97 },
98 {
99 .name = "uppercase_scheme_is_lowercased",
100 .url = "UPPER://domain:234/",
101 .scheme = "upper",
102 .domain = "domain",
103 .port = 234,
104 .path = "/",
105 },
106 {
107 .name = "mixed_case_scheme_is_lowercased",
108 .url = "Mixed://domain/path",
109 .scheme = "mixed",
110 .domain = "domain",
111 .path = "/path",
112 },
113 {
114 .name = "bracketed_ipv6_keeps_its_port",
115 .url = "scheme://[::1]:123/path",
116 .scheme = "scheme",
117 .domain = "::1",
118 .port = 123,
119 .path = "/path",
120 },
121 {
122 .name = "username_and_password_with_port",
123 .url = "scheme://user:pass@domain:123/abc:321",
124 .scheme = "scheme",
125 .username = "user",
126 .password = "pass",
127 .domain = "domain",
128 .port = 123,
129 .path = "/abc:321",
130 },
131 {
132 .name = "username_only_with_port",
133 .url = "scheme://user@domain:123/abc:321",
134 .scheme = "scheme",
135 .username = "user",
136 .domain = "domain",
137 .port = 123,
138 .path = "/abc:321",
139 },
140 {
141 .name = "password_only_with_port",
142 .url = "scheme://:pass@domain:123/abc:321",
143 .scheme = "scheme",
144 .password = "pass",
145 .domain = "domain",
146 .port = 123,
147 .path = "/abc:321",
148 },
149 {
150 .name = "no_credentials_with_port",
151 .url = "scheme://domain:123/abc:321",
152 .scheme = "scheme",
153 .domain = "domain",
154 .port = 123,
155 .path = "/abc:321",
156 },
157 {
158 .name = "username_and_password_without_port",
159 .url = "scheme://user:pass@domain/abc:321",
160 .scheme = "scheme",
161 .username = "user",
162 .password = "pass",
163 .domain = "domain",
164 .path = "/abc:321",
165 },
166 {
167 .name = "username_only_without_port",
168 .url = "scheme://user@domain/abc:321",
169 .scheme = "scheme",
170 .username = "user",
171 .domain = "domain",
172 .path = "/abc:321",
173 },
174 {
175 .name = "password_only_without_port",
176 .url = "scheme://:pass@domain/abc:321",
177 .scheme = "scheme",
178 .password = "pass",
179 .domain = "domain",
180 .path = "/abc:321",
181 },
182 {
183 .name = "no_credentials_without_port",
184 .url = "scheme://domain/abc:321",
185 .scheme = "scheme",
186 .domain = "domain",
187 .path = "/abc:321",
188 },
189 {
190 .name = "empty_authority_with_file_path",
191 .url = "scheme:///path/to/file",
192 .scheme = "scheme",
193 .path = "/path/to/file",
194 },
195 // The '@' separating credentials from the domain is the first one, so a
196 // later '@' belongs to the path.
197 {
198 .name = "at_sign_in_path_with_credentials",
199 .url = "scheme://user:pass@domain/path/with/an@sign",
200 .scheme = "scheme",
201 .username = "user",
202 .password = "pass",
203 .domain = "domain",
204 .path = "/path/with/an@sign",
205 },
206 {
207 .name = "at_sign_in_path_without_credentials",
208 .url = "scheme://domain/path/with/an@sign",
209 .scheme = "scheme",
210 .domain = "domain",
211 .path = "/path/with/an@sign",
212 },
213 // A port with no domain in front of it is not recognised as a port at all;
214 // the whole ":999" becomes the domain.
215 {
216 .name = "port_without_domain_is_taken_as_domain",
217 .url = "scheme://:999/",
218 .scheme = "scheme",
219 .domain = ":999",
220 .path = "/",
221 },
222 // An unbracketed IPv6 address is parsed as an address, not as host:port, so
223 // the trailing ":1234" is folded into the address itself.
224 {
225 .name = "unbracketed_ipv6_absorbs_the_trailing_port",
226 .url = "http://::1:1234/validators",
227 .scheme = "http",
228 .domain = "::0.1.18.52",
229 .path = "/validators",
230 },
231});
232
233class ParseUrlTest : public ::testing::TestWithParam<ParseUrlCase>
234{
235};
236
237TEST_P(ParseUrlTest, splits_url_into_components)
238{
239 auto const& testCase = GetParam();
240
241 ParsedUrl parsed;
242 ASSERT_TRUE(parseUrl(parsed, std::string{testCase.url}));
243
244 EXPECT_EQ(parsed.scheme, testCase.scheme);
245 EXPECT_EQ(parsed.username, testCase.username);
246 EXPECT_EQ(parsed.password, testCase.password);
247 EXPECT_EQ(parsed.domain, testCase.domain);
248 EXPECT_EQ(parsed.port, testCase.port);
249 EXPECT_EQ(parsed.path, testCase.path);
250}
251
253 StringUtilities,
255 ::testing::ValuesIn(kParseUrlCases),
256 [](::testing::TestParamInfo<ParseUrlCase> const& info) {
257 return std::string{info.param.name};
258 });
259
265
266constexpr auto kParseUrlFailureCases = std::to_array<ParseUrlFailureCase>({
267 {.name = "empty", .url = ""},
268 {.name = "no_scheme_separator", .url = "nonsense"},
269 {.name = "empty_scheme", .url = "://"},
270 {.name = "empty_scheme_with_path", .url = ":///"},
271 {.name = "port_one_above_the_16_bit_range", .url = "scheme://user:pass@domain:65536/abc:321"},
272 {.name = "port_far_above_the_16_bit_range", .url = "UPPER://domain:23498765/"},
273 {.name = "port_zero", .url = "UPPER://domain:0/"},
274 {.name = "port_with_a_plus_sign", .url = "UPPER://domain:+7/"},
275 {.name = "negative_port", .url = "UPPER://domain:-7234/"},
276 {.name = "port_of_punctuation", .url = "UPPER://domain:@#$56!/"},
277});
278
279class ParseUrlFailureTest : public ::testing::TestWithParam<ParseUrlFailureCase>
280{
281};
282
284{
285 ParsedUrl parsed;
286 EXPECT_FALSE(parseUrl(parsed, std::string{GetParam().url}));
287}
288
290 StringUtilities,
292 ::testing::ValuesIn(kParseUrlFailureCases),
293 [](::testing::TestParamInfo<ParseUrlFailureCase> const& info) {
294 return std::string{info.param.name};
295 });
296
297TEST_F(StringUtilitiesTest, parse_url_rejects_an_overlong_authority)
298{
299 ParsedUrl parsed;
300 EXPECT_FALSE(parseUrl(parsed, std::format("s://{}", std::string(8192, ':'))));
301}
302
304{
305 auto const result = to_string("hello");
306 EXPECT_EQ(result, "hello");
307}
308
310{
311 EXPECT_EQ(trimWhitespace(""), "");
312 EXPECT_EQ(trimWhitespace(" "), "");
313 EXPECT_EQ(trimWhitespace("abc"), "abc");
314 EXPECT_EQ(trimWhitespace(" abc"), "abc");
315 EXPECT_EQ(trimWhitespace("abc "), "abc");
316 EXPECT_EQ(trimWhitespace(" \t\n\v\f\r abc \t\n\v\f\r "), "abc");
317
318 // Interior whitespace is preserved.
319 EXPECT_EQ(trimWhitespace(" a b\tc "), "a b\tc");
320}
321
323{
324 EXPECT_EQ(toLower(""), "");
325 EXPECT_EQ(toLower("ABC"), "abc");
326 EXPECT_EQ(toLower("AbC123"), "abc123");
327 EXPECT_EQ(toLower("already lower"), "already lower");
328
329 // Only 'A'-'Z' are remapped. Neighbouring punctuation and digits, which a
330 // buggy range check could catch, must survive untouched.
331 EXPECT_EQ(toLower("@[`{_^"), "@[`{_^");
332}
333
334// Both helpers are documented as depending only on their input. Guard that by
335// checking the bytes just outside ASCII, which a locale-aware isspace/tolower
336// could classify differently.
337TEST_F(StringUtilitiesTest, trim_and_lower_ignore_locale)
338{
339 // 0xA0 is NO-BREAK SPACE in Latin-1 and is whitespace to some locales.
340 std::string const nbsp("\xA0", 1);
341 EXPECT_EQ(trimWhitespace(nbsp), nbsp);
342 EXPECT_EQ(trimWhitespace(std::format(" {} ", nbsp)), nbsp);
343
344 // 0xC0 is LATIN CAPITAL LETTER A WITH GRAVE in Latin-1.
345 std::string const agrave("\xC0", 1);
346 EXPECT_EQ(toLower(agrave), agrave);
347}
348
349} // namespace xrpl
static void testUnHexFailure(std::string const &strIn)
static void testUnHexSuccess(std::string const &strIn, std::string const &strExpected)
T format(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string toLower(std::string str)
Fold ASCII upper case letters to lower case.
INSTANTIATE_TEST_SUITE_P(StringUtilities, ParseUrlTest, ::testing::ValuesIn(kParseUrlCases), [](::testing::TestParamInfo< ParseUrlCase > const &info) { return std::string{info.param.name};})
TEST_P(ParseUrlTest, splits_url_into_components)
TEST_F(HardenedHashTest, user_types)
std::string trimWhitespace(std::string str)
Remove leading and trailing ASCII whitespace.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
bool parseUrl(ParsedUrl &pUrl, std::string const &strUrl)
std::optional< std::uint16_t > port
std::optional< std::uint16_t > port
std::string password
std::string username