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 ripple {
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(
124 parseUrl(pUrl, "scheme://user:pass@domain:123/abc:321"));
125 BEAST_EXPECT(pUrl.scheme == "scheme");
126 BEAST_EXPECT(pUrl.username == "user");
127 BEAST_EXPECT(pUrl.password == "pass");
128 BEAST_EXPECT(pUrl.domain == "domain");
129 BEAST_EXPECT(*pUrl.port == 123);
130 BEAST_EXPECT(pUrl.path == "/abc:321");
131 }
132
133 {
134 parsedURL pUrl;
135 BEAST_EXPECT(parseUrl(pUrl, "scheme://user@domain:123/abc:321"));
136 BEAST_EXPECT(pUrl.scheme == "scheme");
137 BEAST_EXPECT(pUrl.username == "user");
138 BEAST_EXPECT(pUrl.password.empty());
139 BEAST_EXPECT(pUrl.domain == "domain");
140 BEAST_EXPECT(*pUrl.port == 123);
141 BEAST_EXPECT(pUrl.path == "/abc:321");
142 }
143
144 {
145 parsedURL pUrl;
146 BEAST_EXPECT(parseUrl(pUrl, "scheme://:pass@domain:123/abc:321"));
147 BEAST_EXPECT(pUrl.scheme == "scheme");
148 BEAST_EXPECT(pUrl.username.empty());
149 BEAST_EXPECT(pUrl.password == "pass");
150 BEAST_EXPECT(pUrl.domain == "domain");
151 BEAST_EXPECT(*pUrl.port == 123);
152 BEAST_EXPECT(pUrl.path == "/abc:321");
153 }
154
155 {
156 parsedURL pUrl;
157 BEAST_EXPECT(parseUrl(pUrl, "scheme://domain:123/abc:321"));
158 BEAST_EXPECT(pUrl.scheme == "scheme");
159 BEAST_EXPECT(pUrl.username.empty());
160 BEAST_EXPECT(pUrl.password.empty());
161 BEAST_EXPECT(pUrl.domain == "domain");
162 BEAST_EXPECT(*pUrl.port == 123);
163 BEAST_EXPECT(pUrl.path == "/abc:321");
164 }
165
166 {
167 parsedURL pUrl;
168 BEAST_EXPECT(parseUrl(pUrl, "scheme://user:pass@domain/abc:321"));
169 BEAST_EXPECT(pUrl.scheme == "scheme");
170 BEAST_EXPECT(pUrl.username == "user");
171 BEAST_EXPECT(pUrl.password == "pass");
172 BEAST_EXPECT(pUrl.domain == "domain");
173 BEAST_EXPECT(!pUrl.port);
174 BEAST_EXPECT(pUrl.path == "/abc:321");
175 }
176
177 {
178 parsedURL pUrl;
179 BEAST_EXPECT(parseUrl(pUrl, "scheme://user@domain/abc:321"));
180 BEAST_EXPECT(pUrl.scheme == "scheme");
181 BEAST_EXPECT(pUrl.username == "user");
182 BEAST_EXPECT(pUrl.password.empty());
183 BEAST_EXPECT(pUrl.domain == "domain");
184 BEAST_EXPECT(!pUrl.port);
185 BEAST_EXPECT(pUrl.path == "/abc:321");
186 }
187
188 {
189 parsedURL pUrl;
190 BEAST_EXPECT(parseUrl(pUrl, "scheme://:pass@domain/abc:321"));
191 BEAST_EXPECT(pUrl.scheme == "scheme");
192 BEAST_EXPECT(pUrl.username.empty());
193 BEAST_EXPECT(pUrl.password == "pass");
194 BEAST_EXPECT(pUrl.domain == "domain");
195 BEAST_EXPECT(!pUrl.port);
196 BEAST_EXPECT(pUrl.path == "/abc:321");
197 }
198
199 {
200 parsedURL pUrl;
201 BEAST_EXPECT(parseUrl(pUrl, "scheme://domain/abc:321"));
202 BEAST_EXPECT(pUrl.scheme == "scheme");
203 BEAST_EXPECT(pUrl.username.empty());
204 BEAST_EXPECT(pUrl.password.empty());
205 BEAST_EXPECT(pUrl.domain == "domain");
206 BEAST_EXPECT(!pUrl.port);
207 BEAST_EXPECT(pUrl.path == "/abc:321");
208 }
209
210 {
211 parsedURL pUrl;
212 BEAST_EXPECT(parseUrl(pUrl, "scheme:///path/to/file"));
213 BEAST_EXPECT(pUrl.scheme == "scheme");
214 BEAST_EXPECT(pUrl.username.empty());
215 BEAST_EXPECT(pUrl.password.empty());
216 BEAST_EXPECT(pUrl.domain.empty());
217 BEAST_EXPECT(!pUrl.port);
218 BEAST_EXPECT(pUrl.path == "/path/to/file");
219 }
220
221 {
222 parsedURL pUrl;
223 BEAST_EXPECT(
224 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(
274 !parseUrl(pUrl, "scheme://user:pass@domain:65536/abc:321"));
275 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:23498765/"));
276 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:0/"));
277 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:+7/"));
278 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:-7234/"));
279 BEAST_EXPECT(!parseUrl(pUrl, "UPPER://domain:@#$56!/"));
280 }
281
282 {
283 std::string strUrl("s://" + std::string(8192, ':'));
284 parsedURL pUrl;
285 BEAST_EXPECT(!parseUrl(pUrl, strUrl));
286 }
287 }
288
289 void
291 {
292 testcase("toString");
293 auto result = to_string("hello");
294 BEAST_EXPECT(result == "hello");
295 }
296
297 void
298 run() override
299 {
300 testParseUrl();
301 testUnHex();
302 testToString();
303 }
304};
305
306BEAST_DEFINE_TESTSUITE(StringUtilities, basics, ripple);
307
308} // namespace ripple
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
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:6
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:225
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
std::optional< std::uint16_t > port