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