xrpld
Loading...
Searching...
No Matches
StringUtilities_test.cpp
1#include <xrpl/basics/Slice.h>
2#include <xrpl/basics/StringUtilities.h>
3#include <xrpl/basics/ToString.h>
4#include <xrpl/beast/unit_test/suite.h>
5
6#include <string>
7
8namespace xrpl {
9
11{
12public:
13 void
14 testUnHexSuccess(std::string const& strIn, std::string const& strExpected)
15 {
16 auto rv = strUnHex(strIn);
17 BEAST_EXPECT(rv);
18
19 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
20 BEAST_EXPECT(makeSlice(*rv) == makeSlice(strExpected));
21 }
22
23 void
25 {
26 auto rv = strUnHex(strIn);
27 BEAST_EXPECT(!rv);
28 }
29
30 void
32 {
33 testcase("strUnHex");
34
35 testUnHexSuccess("526970706c6544", "RippleD");
36 testUnHexSuccess("A", "\n");
37 testUnHexSuccess("0A", "\n");
38 testUnHexSuccess("D0A", "\r\n");
39 testUnHexSuccess("0D0A", "\r\n");
40 testUnHexSuccess("200D0A", " \r\n");
41 testUnHexSuccess("282A2B2C2D2E2F29", "(*+,-./)");
42
43 // Check for things which contain some or only invalid characters
44 testUnHexFailure("123X");
46 testUnHexFailure("XRP");
47 }
48
49 void
51 {
52 testcase("parseUrl");
53
54 // Expected passes.
55 {
56 ParsedUrl pUrl;
57 BEAST_EXPECT(parseUrl(pUrl, "scheme://"));
58 BEAST_EXPECT(pUrl.scheme == "scheme");
59 BEAST_EXPECT(pUrl.username.empty());
60 BEAST_EXPECT(pUrl.password.empty());
61 BEAST_EXPECT(pUrl.domain.empty());
62 BEAST_EXPECT(!pUrl.port);
63 // RFC 3986:
64 // > In general, a URI that uses the generic syntax for authority
65 // with an empty path should be normalized to a path of "/".
66 // Do we want to normalize paths?
67 BEAST_EXPECT(pUrl.path.empty());
68 }
69
70 {
71 ParsedUrl pUrl;
72 BEAST_EXPECT(parseUrl(pUrl, "scheme:///"));
73 BEAST_EXPECT(pUrl.scheme == "scheme");
74 BEAST_EXPECT(pUrl.username.empty());
75 BEAST_EXPECT(pUrl.password.empty());
76 BEAST_EXPECT(pUrl.domain.empty());
77 BEAST_EXPECT(!pUrl.port);
78 BEAST_EXPECT(pUrl.path == "/");
79 }
80
81 {
82 ParsedUrl pUrl;
83 BEAST_EXPECT(parseUrl(pUrl, "lower://domain"));
84 BEAST_EXPECT(pUrl.scheme == "lower");
85 BEAST_EXPECT(pUrl.username.empty());
86 BEAST_EXPECT(pUrl.password.empty());
87 BEAST_EXPECT(pUrl.domain == "domain");
88 BEAST_EXPECT(!pUrl.port);
89 BEAST_EXPECT(pUrl.path.empty());
90 }
91
92 {
93 ParsedUrl pUrl;
94 BEAST_EXPECT(parseUrl(pUrl, "UPPER://domain:234/"));
95 BEAST_EXPECT(pUrl.scheme == "upper");
96 BEAST_EXPECT(pUrl.username.empty());
97 BEAST_EXPECT(pUrl.password.empty());
98 BEAST_EXPECT(pUrl.domain == "domain");
99 BEAST_EXPECT(*pUrl.port == 234); // NOLINT(bugprone-unchecked-optional-access)
100 BEAST_EXPECT(pUrl.path == "/");
101 }
102
103 {
104 ParsedUrl pUrl;
105 BEAST_EXPECT(parseUrl(pUrl, "Mixed://domain/path"));
106 BEAST_EXPECT(pUrl.scheme == "mixed");
107 BEAST_EXPECT(pUrl.username.empty());
108 BEAST_EXPECT(pUrl.password.empty());
109 BEAST_EXPECT(pUrl.domain == "domain");
110 BEAST_EXPECT(!pUrl.port);
111 BEAST_EXPECT(pUrl.path == "/path");
112 }
113
114 {
115 ParsedUrl pUrl;
116 BEAST_EXPECT(parseUrl(pUrl, "scheme://[::1]:123/path"));
117 BEAST_EXPECT(pUrl.scheme == "scheme");
118 BEAST_EXPECT(pUrl.username.empty());
119 BEAST_EXPECT(pUrl.password.empty());
120 BEAST_EXPECT(pUrl.domain == "::1");
121 BEAST_EXPECT(*pUrl.port == 123); // NOLINT(bugprone-unchecked-optional-access)
122 BEAST_EXPECT(pUrl.path == "/path");
123 }
124
125 {
126 ParsedUrl pUrl;
127 BEAST_EXPECT(parseUrl(pUrl, "scheme://user:pass@domain:123/abc:321"));
128 BEAST_EXPECT(pUrl.scheme == "scheme");
129 BEAST_EXPECT(pUrl.username == "user");
130 BEAST_EXPECT(pUrl.password == "pass");
131 BEAST_EXPECT(pUrl.domain == "domain");
132 BEAST_EXPECT(*pUrl.port == 123); // NOLINT(bugprone-unchecked-optional-access)
133 BEAST_EXPECT(pUrl.path == "/abc:321");
134 }
135
136 {
137 ParsedUrl pUrl;
138 BEAST_EXPECT(parseUrl(pUrl, "scheme://user@domain:123/abc:321"));
139 BEAST_EXPECT(pUrl.scheme == "scheme");
140 BEAST_EXPECT(pUrl.username == "user");
141 BEAST_EXPECT(pUrl.password.empty());
142 BEAST_EXPECT(pUrl.domain == "domain");
143 BEAST_EXPECT(*pUrl.port == 123); // NOLINT(bugprone-unchecked-optional-access)
144 BEAST_EXPECT(pUrl.path == "/abc:321");
145 }
146
147 {
148 ParsedUrl pUrl;
149 BEAST_EXPECT(parseUrl(pUrl, "scheme://:pass@domain:123/abc:321"));
150 BEAST_EXPECT(pUrl.scheme == "scheme");
151 BEAST_EXPECT(pUrl.username.empty());
152 BEAST_EXPECT(pUrl.password == "pass");
153 BEAST_EXPECT(pUrl.domain == "domain");
154 BEAST_EXPECT(*pUrl.port == 123); // NOLINT(bugprone-unchecked-optional-access)
155 BEAST_EXPECT(pUrl.path == "/abc:321");
156 }
157
158 {
159 ParsedUrl pUrl;
160 BEAST_EXPECT(parseUrl(pUrl, "scheme://domain:123/abc:321"));
161 BEAST_EXPECT(pUrl.scheme == "scheme");
162 BEAST_EXPECT(pUrl.username.empty());
163 BEAST_EXPECT(pUrl.password.empty());
164 BEAST_EXPECT(pUrl.domain == "domain");
165 BEAST_EXPECT(*pUrl.port == 123); // NOLINT(bugprone-unchecked-optional-access)
166 BEAST_EXPECT(pUrl.path == "/abc:321");
167 }
168
169 {
170 ParsedUrl pUrl;
171 BEAST_EXPECT(parseUrl(pUrl, "scheme://user:pass@domain/abc:321"));
172 BEAST_EXPECT(pUrl.scheme == "scheme");
173 BEAST_EXPECT(pUrl.username == "user");
174 BEAST_EXPECT(pUrl.password == "pass");
175 BEAST_EXPECT(pUrl.domain == "domain");
176 BEAST_EXPECT(!pUrl.port);
177 BEAST_EXPECT(pUrl.path == "/abc:321");
178 }
179
180 {
181 ParsedUrl pUrl;
182 BEAST_EXPECT(parseUrl(pUrl, "scheme://user@domain/abc:321"));
183 BEAST_EXPECT(pUrl.scheme == "scheme");
184 BEAST_EXPECT(pUrl.username == "user");
185 BEAST_EXPECT(pUrl.password.empty());
186 BEAST_EXPECT(pUrl.domain == "domain");
187 BEAST_EXPECT(!pUrl.port);
188 BEAST_EXPECT(pUrl.path == "/abc:321");
189 }
190
191 {
192 ParsedUrl pUrl;
193 BEAST_EXPECT(parseUrl(pUrl, "scheme://:pass@domain/abc:321"));
194 BEAST_EXPECT(pUrl.scheme == "scheme");
195 BEAST_EXPECT(pUrl.username.empty());
196 BEAST_EXPECT(pUrl.password == "pass");
197 BEAST_EXPECT(pUrl.domain == "domain");
198 BEAST_EXPECT(!pUrl.port);
199 BEAST_EXPECT(pUrl.path == "/abc:321");
200 }
201
202 {
203 ParsedUrl pUrl;
204 BEAST_EXPECT(parseUrl(pUrl, "scheme://domain/abc:321"));
205 BEAST_EXPECT(pUrl.scheme == "scheme");
206 BEAST_EXPECT(pUrl.username.empty());
207 BEAST_EXPECT(pUrl.password.empty());
208 BEAST_EXPECT(pUrl.domain == "domain");
209 BEAST_EXPECT(!pUrl.port);
210 BEAST_EXPECT(pUrl.path == "/abc:321");
211 }
212
213 {
214 ParsedUrl pUrl;
215 BEAST_EXPECT(parseUrl(pUrl, "scheme:///path/to/file"));
216 BEAST_EXPECT(pUrl.scheme == "scheme");
217 BEAST_EXPECT(pUrl.username.empty());
218 BEAST_EXPECT(pUrl.password.empty());
219 BEAST_EXPECT(pUrl.domain.empty());
220 BEAST_EXPECT(!pUrl.port);
221 BEAST_EXPECT(pUrl.path == "/path/to/file");
222 }
223
224 {
225 ParsedUrl pUrl;
226 BEAST_EXPECT(parseUrl(pUrl, "scheme://user:pass@domain/path/with/an@sign"));
227 BEAST_EXPECT(pUrl.scheme == "scheme");
228 BEAST_EXPECT(pUrl.username == "user");
229 BEAST_EXPECT(pUrl.password == "pass");
230 BEAST_EXPECT(pUrl.domain == "domain");
231 BEAST_EXPECT(!pUrl.port);
232 BEAST_EXPECT(pUrl.path == "/path/with/an@sign");
233 }
234
235 {
236 ParsedUrl pUrl;
237 BEAST_EXPECT(parseUrl(pUrl, "scheme://domain/path/with/an@sign"));
238 BEAST_EXPECT(pUrl.scheme == "scheme");
239 BEAST_EXPECT(pUrl.username.empty());
240 BEAST_EXPECT(pUrl.password.empty());
241 BEAST_EXPECT(pUrl.domain == "domain");
242 BEAST_EXPECT(!pUrl.port);
243 BEAST_EXPECT(pUrl.path == "/path/with/an@sign");
244 }
245
246 {
247 ParsedUrl pUrl;
248 BEAST_EXPECT(parseUrl(pUrl, "scheme://:999/"));
249 BEAST_EXPECT(pUrl.scheme == "scheme");
250 BEAST_EXPECT(pUrl.username.empty());
251 BEAST_EXPECT(pUrl.password.empty());
252 BEAST_EXPECT(pUrl.domain == ":999");
253 BEAST_EXPECT(!pUrl.port);
254 BEAST_EXPECT(pUrl.path == "/");
255 }
256
257 {
258 ParsedUrl pUrl;
259 BEAST_EXPECT(parseUrl(pUrl, "http://::1:1234/validators"));
260 BEAST_EXPECT(pUrl.scheme == "http");
261 BEAST_EXPECT(pUrl.username.empty());
262 BEAST_EXPECT(pUrl.password.empty());
263 BEAST_EXPECT(pUrl.domain == "::0.1.18.52");
264 BEAST_EXPECT(!pUrl.port);
265 BEAST_EXPECT(pUrl.path == "/validators");
266 }
267
268 // Expected fails.
269 {
270 ParsedUrl pUrl;
271 BEAST_EXPECT(!parseUrl(pUrl, ""));
272 BEAST_EXPECT(!parseUrl(pUrl, "nonsense"));
273 BEAST_EXPECT(!parseUrl(pUrl, "://"));
274 BEAST_EXPECT(!parseUrl(pUrl, ":///"));
275 BEAST_EXPECT(!parseUrl(pUrl, "scheme://user:pass@domain:65536/abc:321"));
276 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:23498765/"));
277 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:0/"));
278 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:+7/"));
279 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:-7234/"));
280 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:@#$56!/"));
281 }
282
283 {
284 std::string const strUrl("s://" + std::string(8192, ':'));
285 ParsedUrl pUrl;
286 BEAST_EXPECT(!parseUrl(pUrl, strUrl));
287 }
288 }
289
290 void
292 {
293 testcase("toString");
294 auto result = to_string("hello");
295 BEAST_EXPECT(result == "hello");
296 }
297
298 void
299 run() override
300 {
301 testParseUrl();
302 testUnHex();
303 testToString();
304 }
305};
306
307BEAST_DEFINE_TESTSUITE(StringUtilities, basics, xrpl);
308
309} // namespace xrpl
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
void testUnHexSuccess(std::string const &strIn, std::string const &strExpected)
void testUnHexFailure(std::string const &strIn)
void run() override
Runs the suite.
T empty(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:215
bool parseUrl(ParsedUrl &pUrl, std::string const &strUrl)
std::optional< std::uint16_t > port
std::string password
std::string username