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