forked from arvidn/libtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_string.cpp
550 lines (471 loc) · 20.4 KB
/
test_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
Copyright (c) 2012, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "libtorrent/aux_/escape_string.hpp"
#include "libtorrent/hex.hpp"
#include "libtorrent/string_util.hpp"
#include "libtorrent/aux_/string_ptr.hpp"
#include <iostream>
#include <cstring> // for strcmp
#include "libtorrent/aux_/escape_string.hpp" // for trim
using namespace lt;
TORRENT_TEST(maybe_url_encode)
{
// test maybe_url_encode
TEST_EQUAL(maybe_url_encode("http://test:[email protected]/abc<>abc"), "http://test:[email protected]/abc%3c%3eabc");
TEST_EQUAL(maybe_url_encode("http://abc.com/foo bar"), "http://abc.com/foo%20bar");
TEST_EQUAL(maybe_url_encode("http://abc.com:80/foo bar"), "http://abc.com:80/foo%20bar");
TEST_EQUAL(maybe_url_encode("http://abc.com:8080/foo bar"), "http://abc.com:8080/foo%20bar");
TEST_EQUAL(maybe_url_encode("abc"), "abc");
TEST_EQUAL(maybe_url_encode("http://abc.com/abc"), "http://abc.com/abc");
}
TORRENT_TEST(hex)
{
static char const str[] = "0123456789012345678901234567890123456789";
char bin[20];
TEST_CHECK(aux::from_hex({str, 40}, bin));
char hex[41];
aux::to_hex(bin, hex);
TEST_CHECK(strcmp(hex, str) == 0);
TEST_CHECK(aux::to_hex({"\x55\x73",2}) == "5573");
TEST_CHECK(aux::to_hex({"\xaB\xd0",2}) == "abd0");
static char const hex_chars[] = "0123456789abcdefABCDEF";
for (int i = 1; i < 255; ++i)
{
bool const hex = strchr(hex_chars, i) != nullptr;
char const c = i;
TEST_EQUAL(aux::is_hex(c), hex);
}
TEST_EQUAL(aux::hex_to_int('0'), 0);
TEST_EQUAL(aux::hex_to_int('7'), 7);
TEST_EQUAL(aux::hex_to_int('a'), 10);
TEST_EQUAL(aux::hex_to_int('f'), 15);
TEST_EQUAL(aux::hex_to_int('b'), 11);
TEST_EQUAL(aux::hex_to_int('t'), -1);
TEST_EQUAL(aux::hex_to_int('g'), -1);
}
TORRENT_TEST(is_space)
{
TEST_CHECK(!is_space('C'));
TEST_CHECK(!is_space('\b'));
TEST_CHECK(!is_space('8'));
TEST_CHECK(!is_space('='));
TEST_CHECK(is_space(' '));
TEST_CHECK(is_space('\t'));
TEST_CHECK(is_space('\n'));
TEST_CHECK(is_space('\r'));
TEST_CHECK(is_space('\f'));
TEST_CHECK(is_space('\v'));
}
TORRENT_TEST(to_lower)
{
TEST_CHECK(to_lower('C') == 'c');
TEST_CHECK(to_lower('c') == 'c');
TEST_CHECK(to_lower('-') == '-');
TEST_CHECK(to_lower('&') == '&');
}
TORRENT_TEST(string_equal_no_case)
{
TEST_CHECK(string_equal_no_case("foobar", "FoobAR"));
TEST_CHECK(string_equal_no_case("foobar", "foobar"));
TEST_CHECK(!string_equal_no_case("foobar", "foobar "));
TEST_CHECK(!string_equal_no_case("foobar", "F00"));
TEST_CHECK(!string_equal_no_case("foobar", "foo"));
TEST_CHECK(!string_equal_no_case("foo", "foobar"));
TEST_CHECK(string_begins_no_case("foobar", "FoobAR --"));
TEST_CHECK(string_begins_no_case("foo", "foobar"));
TEST_CHECK(!string_begins_no_case("foobar", "F00"));
TEST_CHECK(!string_begins_no_case("foobar", "foo"));
TEST_CHECK(string_ends_with("foobar", "bar"));
TEST_CHECK(string_ends_with("name.txt", ".txt"));
TEST_CHECK(string_ends_with("name.a.b", ".a.b"));
TEST_CHECK(!string_ends_with("-- FoobAR", "foobar"));
TEST_CHECK(!string_ends_with("foobar", "F00"));
TEST_CHECK(!string_ends_with("foobar", "foo"));
TEST_CHECK(!string_ends_with("foo", "foobar"));
}
TORRENT_TEST(to_string)
{
TEST_CHECK(to_string(345).data() == std::string("345"));
TEST_CHECK(to_string(-345).data() == std::string("-345"));
TEST_CHECK(to_string(std::numeric_limits<std::int64_t>::max()).data() == std::string("9223372036854775807"));
TEST_CHECK(to_string(std::numeric_limits<std::int64_t>::min()).data() == std::string("-9223372036854775808"));
TEST_CHECK(to_string(0).data() == std::string("0"));
TEST_CHECK(to_string(10).data() == std::string("10"));
TEST_CHECK(to_string(100).data() == std::string("100"));
TEST_CHECK(to_string(1000).data() == std::string("1000"));
TEST_CHECK(to_string(10000).data() == std::string("10000"));
TEST_CHECK(to_string(100000).data() == std::string("100000"));
TEST_CHECK(to_string(1000000).data() == std::string("1000000"));
TEST_CHECK(to_string(10000000).data() == std::string("10000000"));
TEST_CHECK(to_string(100000000).data() == std::string("100000000"));
TEST_CHECK(to_string(1000000000).data() == std::string("1000000000"));
TEST_CHECK(to_string(10000000000).data() == std::string("10000000000"));
TEST_CHECK(to_string(100000000000).data() == std::string("100000000000"));
TEST_CHECK(to_string(1000000000000).data() == std::string("1000000000000"));
TEST_CHECK(to_string(10000000000000).data() == std::string("10000000000000"));
TEST_CHECK(to_string(100000000000000).data() == std::string("100000000000000"));
TEST_CHECK(to_string(1000000000000000).data() == std::string("1000000000000000"));
TEST_CHECK(to_string(9).data() == std::string("9"));
TEST_CHECK(to_string(99).data() == std::string("99"));
TEST_CHECK(to_string(999).data() == std::string("999"));
TEST_CHECK(to_string(9999).data() == std::string("9999"));
TEST_CHECK(to_string(99999).data() == std::string("99999"));
TEST_CHECK(to_string(999999).data() == std::string("999999"));
TEST_CHECK(to_string(9999999).data() == std::string("9999999"));
TEST_CHECK(to_string(99999999).data() == std::string("99999999"));
TEST_CHECK(to_string(999999999).data() == std::string("999999999"));
TEST_CHECK(to_string(9999999999).data() == std::string("9999999999"));
TEST_CHECK(to_string(99999999999).data() == std::string("99999999999"));
TEST_CHECK(to_string(999999999999).data() == std::string("999999999999"));
TEST_CHECK(to_string(9999999999999).data() == std::string("9999999999999"));
TEST_CHECK(to_string(99999999999999).data() == std::string("99999999999999"));
TEST_CHECK(to_string(999999999999999).data() == std::string("999999999999999"));
TEST_CHECK(to_string(9999999999999999).data() == std::string("9999999999999999"));
TEST_CHECK(to_string(99999999999999999).data() == std::string("99999999999999999"));
TEST_CHECK(to_string(999999999999999999).data() == std::string("999999999999999999"));
TEST_CHECK(to_string(-10).data() == std::string("-10"));
TEST_CHECK(to_string(-100).data() == std::string("-100"));
TEST_CHECK(to_string(-1000).data() == std::string("-1000"));
TEST_CHECK(to_string(-10000).data() == std::string("-10000"));
TEST_CHECK(to_string(-100000).data() == std::string("-100000"));
TEST_CHECK(to_string(-1000000).data() == std::string("-1000000"));
TEST_CHECK(to_string(-10000000).data() == std::string("-10000000"));
TEST_CHECK(to_string(-100000000).data() == std::string("-100000000"));
TEST_CHECK(to_string(-1000000000).data() == std::string("-1000000000"));
TEST_CHECK(to_string(-10000000000).data() == std::string("-10000000000"));
TEST_CHECK(to_string(-100000000000).data() == std::string("-100000000000"));
TEST_CHECK(to_string(-1000000000000).data() == std::string("-1000000000000"));
TEST_CHECK(to_string(-10000000000000).data() == std::string("-10000000000000"));
TEST_CHECK(to_string(-100000000000000).data() == std::string("-100000000000000"));
TEST_CHECK(to_string(-1000000000000000).data() == std::string("-1000000000000000"));
TEST_CHECK(to_string(-9).data() == std::string("-9"));
TEST_CHECK(to_string(-99).data() == std::string("-99"));
TEST_CHECK(to_string(-999).data() == std::string("-999"));
TEST_CHECK(to_string(-9999).data() == std::string("-9999"));
TEST_CHECK(to_string(-99999).data() == std::string("-99999"));
TEST_CHECK(to_string(-999999).data() == std::string("-999999"));
TEST_CHECK(to_string(-9999999).data() == std::string("-9999999"));
TEST_CHECK(to_string(-99999999).data() == std::string("-99999999"));
TEST_CHECK(to_string(-999999999).data() == std::string("-999999999"));
TEST_CHECK(to_string(-9999999999).data() == std::string("-9999999999"));
TEST_CHECK(to_string(-99999999999).data() == std::string("-99999999999"));
TEST_CHECK(to_string(-999999999999).data() == std::string("-999999999999"));
TEST_CHECK(to_string(-9999999999999).data() == std::string("-9999999999999"));
TEST_CHECK(to_string(-99999999999999).data() == std::string("-99999999999999"));
TEST_CHECK(to_string(-999999999999999).data() == std::string("-999999999999999"));
TEST_CHECK(to_string(-9999999999999999).data() == std::string("-9999999999999999"));
TEST_CHECK(to_string(-99999999999999999).data() == std::string("-99999999999999999"));
TEST_CHECK(to_string(-999999999999999999).data() == std::string("-999999999999999999"));
}
TORRENT_TEST(base64)
{
// base64 test vectors from http://www.faqs.org/rfcs/rfc4648.html
TEST_CHECK(base64encode("") == "");
TEST_CHECK(base64encode("f") == "Zg==");
TEST_CHECK(base64encode("fo") == "Zm8=");
TEST_CHECK(base64encode("foo") == "Zm9v");
TEST_CHECK(base64encode("foob") == "Zm9vYg==");
TEST_CHECK(base64encode("fooba") == "Zm9vYmE=");
TEST_CHECK(base64encode("foobar") == "Zm9vYmFy");
}
#if TORRENT_USE_I2P
TORRENT_TEST(base32)
{
// base32 test vectors from http://www.faqs.org/rfcs/rfc4648.html
TEST_CHECK(base32encode("") == "");
TEST_CHECK(base32encode("f") == "MY======");
TEST_CHECK(base32encode("fo") == "MZXQ====");
TEST_CHECK(base32encode("foo") == "MZXW6===");
TEST_CHECK(base32encode("foob") == "MZXW6YQ=");
TEST_CHECK(base32encode("fooba") == "MZXW6YTB");
TEST_CHECK(base32encode("foobar") == "MZXW6YTBOI======");
// base32 for i2p
TEST_CHECK(base32encode("fo", string::no_padding) == "MZXQ");
TEST_CHECK(base32encode("foob", string::i2p) == "mzxw6yq");
TEST_CHECK(base32encode("foobar", string::lowercase) == "mzxw6ytboi======");
#endif // TORRENT_USE_I2P
TEST_CHECK(base32decode("") == "");
TEST_CHECK(base32decode("MY======") == "f");
TEST_CHECK(base32decode("MZXQ====") == "fo");
TEST_CHECK(base32decode("MZXW6===") == "foo");
TEST_CHECK(base32decode("MZXW6YQ=") == "foob");
TEST_CHECK(base32decode("MZXW6YTB") == "fooba");
TEST_CHECK(base32decode("MZXW6YTBOI======") == "foobar");
TEST_CHECK(base32decode("MY") == "f");
TEST_CHECK(base32decode("MZXW6YQ") == "foob");
TEST_CHECK(base32decode("MZXW6YTBOI") == "foobar");
TEST_CHECK(base32decode("mZXw6yTBO1======") == "foobar");
// make sure invalid encoding returns the empty string
TEST_CHECK(base32decode("mZXw6yTBO1{#&*()=") == "");
std::string test;
for (int i = 0; i < 255; ++i)
test += char(i);
TEST_CHECK(base32decode(base32encode(test)) == test);
}
TORRENT_TEST(escape_string)
{
// escape_string
char const* test_string = "!@#$%^&*()-_=+/,. %?";
TEST_EQUAL(escape_string(test_string)
, "!%40%23%24%25%5e%26*()-_%3d%2b%2f%2c.%20%25%3f");
// escape_path
TEST_EQUAL(escape_path(test_string)
, "!%40%23%24%25%5e%26*()-_%3d%2b/%2c.%20%25%3f");
error_code ec;
TEST_CHECK(unescape_string(escape_path(test_string), ec) == test_string);
TEST_CHECK(!ec);
if (ec) std::printf("%s\n", ec.message().c_str());
// need_encoding
char const* test_string2 = "!@$&()-_/,.%?";
TEST_CHECK(need_encoding(test_string, int(strlen(test_string))) == true);
TEST_CHECK(need_encoding(test_string2, int(strlen(test_string2))) == false);
TEST_CHECK(need_encoding("\n", 1) == true);
// maybe_url_encode
TEST_EQUAL(maybe_url_encode("http://bla.com/\n"), "http://bla.com/%0a");
TEST_EQUAL(maybe_url_encode("http://bla.com/foo%20bar"), "http://bla.com/foo%20bar");
TEST_EQUAL(maybe_url_encode("http://bla.com/foo%20bar?k=v&k2=v2")
, "http://bla.com/foo%20bar?k=v&k2=v2");
TEST_EQUAL(maybe_url_encode("?&"), "?&");
// unescape_string
TEST_CHECK(unescape_string(escape_string(test_string), ec)
== test_string);
std::cout << unescape_string(escape_string(test_string), ec) << std::endl;
// prematurely terminated string
unescape_string("%", ec);
TEST_CHECK(ec == error_code(errors::invalid_escaped_string));
unescape_string("%0", ec);
TEST_CHECK(ec == error_code(errors::invalid_escaped_string));
// invalid hex character
unescape_string("%GE", ec);
TEST_CHECK(ec == error_code(errors::invalid_escaped_string));
unescape_string("%eg", ec);
TEST_CHECK(ec == error_code(errors::invalid_escaped_string));
ec.clear();
TEST_CHECK(unescape_string("123+abc", ec) == "123 abc");
}
TORRENT_TEST(read_until)
{
char const* test_string1 = "abcdesdf sdgf";
char const* tmp1 = test_string1;
TEST_CHECK(read_until(tmp1, 'd', test_string1 + strlen(test_string1)) == "abc");
tmp1 = test_string1;
TEST_CHECK(read_until(tmp1, '[', test_string1 + strlen(test_string1))
== "abcdesdf sdgf");
}
TORRENT_TEST(url_has_argument)
{
TEST_CHECK(url_has_argument("http://127.0.0.1/test", "test") == "");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24", "bar") == "");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24", "foo") == "24");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24&bar=23", "foo") == "24");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24&bar=23", "bar") == "23");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24&bar=23&a=e", "bar") == "23");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24&bar=23&a=e", "a") == "e");
TEST_CHECK(url_has_argument("http://127.0.0.1/test?foo=24&bar=23&a=e", "b") == "");
}
TORRENT_TEST(path)
{
std::string path = "a\\b\\c";
convert_path_to_posix(path);
TEST_EQUAL(path, "a/b/c");
#ifndef TORRENT_NO_DEPRECATE
// resolve_file_url
#ifdef TORRENT_WINDOWS
std::string p = "c:/blah/foo/bar\\";
convert_path_to_windows(p);
TEST_EQUAL(p, "c:\\blah\\foo\\bar\\");
TEST_EQUAL(resolve_file_url("file:///c:/blah/foo/bar"), "c:\\blah\\foo\\bar");
TEST_EQUAL(resolve_file_url("file:///c:/b%3fah/foo/bar"), "c:\\b?ah\\foo\\bar");
TEST_EQUAL(resolve_file_url("file://\\c:\\b%3fah\\foo\\bar"), "c:\\b?ah\\foo\\bar");
#else
TEST_EQUAL(resolve_file_url("file:///c/blah/foo/bar"), "/c/blah/foo/bar");
TEST_EQUAL(resolve_file_url("file:///c/b%3fah/foo/bar"), "/c/b?ah/foo/bar");
#endif
#endif
}
void test_parse_interface(char const* input
, std::vector<listen_interface_t> expected
, std::string output)
{
std::printf("parse interface: %s\n", input);
auto const list = parse_listen_interfaces(input);
TEST_EQUAL(list.size(), expected.size());
if (list.size() == expected.size())
{
TEST_CHECK(std::equal(list.begin(), list.end(), expected.begin()
, [&](listen_interface_t const& lhs, listen_interface_t const& rhs)
{ return lhs.device == rhs.device && lhs.port == rhs.port && lhs.ssl == rhs.ssl; }));
}
TEST_EQUAL(print_listen_interfaces(list), output);
}
TORRENT_TEST(parse_list)
{
std::vector<std::string> list;
parse_comma_separated_string(" a,b, c, d ,e \t,foobar\n\r,[::1]", list);
TEST_EQUAL(list.size(), 7);
TEST_EQUAL(list[0], "a");
TEST_EQUAL(list[1], "b");
TEST_EQUAL(list[2], "c");
TEST_EQUAL(list[3], "d");
TEST_EQUAL(list[4], "e");
TEST_EQUAL(list[5], "foobar");
TEST_EQUAL(list[6], "[::1]");
#if TORRENT_USE_IPV6
test_parse_interface(" a:4,b:35, c : 1000s, d: 351 ,e \t:42,foobar:1337s\n\r,[2001::1]:6881"
, {{"a", 4, false}, {"b", 35, false}, {"c", 1000, true}, {"d", 351, false}
, {"e", 42, false}, {"foobar", 1337, true}, {"2001::1", 6881, false}}
, "a:4,b:35,c:1000s,d:351,e:42,foobar:1337s,[2001::1]:6881");
#else
test_parse_interface(" a:4,b:35, c : 1000s, d: 351 ,e \t:42,foobar:1337s\n\r,[2001::1]:6881"
, {{"a", 4, false}, {"b", 35, false}, {"c", 1000, true}, {"d", 351, false}
, {"e", 42, false}, {"foobar", 1337, true}}
, "a:4,b:35,c:1000s,d:351,e:42,foobar:1337s");
#endif
// IPv6 address
#if TORRENT_USE_IPV6
test_parse_interface("[2001:ffff::1]:6882s"
, {{"2001:ffff::1", 6882, true}}
, "[2001:ffff::1]:6882s");
#else
test_parse_interface("[2001:ffff::1]:6882s", {}, "");
#endif
// IPv4 address
test_parse_interface("127.0.0.1:6882"
, {{"127.0.0.1", 6882, false}}
, "127.0.0.1:6882");
// maximum padding
test_parse_interface(" nic\r\n:\t 12\r s "
, {{"nic", 12, true}}
, "nic:12s");
// negative tests
test_parse_interface("nic:99999999999999999999999", {}, "");
test_parse_interface("nic: -3", {}, "");
test_parse_interface("nic: ", {}, "");
test_parse_interface("nic :", {}, "");
test_parse_interface("nic ", {}, "");
test_parse_interface("nic s", {}, "");
// parse interface with port 0
test_parse_interface("127.0.0.1:0"
, {{"127.0.0.1", 0, false}}, "127.0.0.1:0");
}
TORRENT_TEST(split_string)
{
TEST_CHECK(split_string("a b"_sv, ' ') == std::make_pair("a"_sv, "b"_sv));
TEST_CHECK(split_string("\"a b\" c"_sv, ' ') == std::make_pair("\"a b\""_sv, "c"_sv));
TEST_CHECK(split_string("\"a b\"foobar c"_sv, ' ') == std::make_pair("\"a b\"foobar"_sv, "c"_sv));
TEST_CHECK(split_string("a\nb foobar"_sv, ' ') == std::make_pair("a\nb"_sv, "foobar"_sv));
TEST_CHECK(split_string("a b\"foo\"bar"_sv, '"') == std::make_pair("a b"_sv, "foo\"bar"_sv));
TEST_CHECK(split_string("a"_sv, ' ') == std::make_pair("a"_sv, ""_sv));
TEST_CHECK(split_string("\"a b"_sv, ' ') == std::make_pair("\"a b"_sv, ""_sv));
TEST_CHECK(split_string(""_sv, ' ') == std::make_pair(""_sv, ""_sv));
}
TORRENT_TEST(convert_from_native)
{
TEST_EQUAL(std::string("foobar"), convert_from_native(convert_to_native("foobar")));
TEST_EQUAL(std::string("foobar")
, convert_from_native(convert_to_native("foo"))
+ convert_from_native(convert_to_native("bar")));
TEST_EQUAL(convert_to_native("foobar")
, convert_to_native("foo") + convert_to_native("bar"));
}
TORRENT_TEST(trim)
{
TEST_EQUAL(trim(""), "");
TEST_EQUAL(trim("\t "), "");
TEST_EQUAL(trim(" a"), "a");
TEST_EQUAL(trim(" a "), "a");
TEST_EQUAL(trim("\t \na \t\r"), "a");
TEST_EQUAL(trim(" \t \ta"), "a");
TEST_EQUAL(trim("a "), "a");
TEST_EQUAL(trim("a \t"), "a");
TEST_EQUAL(trim("a \t\n \tb"), "a \t\n \tb");
}
#if TORRENT_USE_I2P
TORRENT_TEST(i2p_url)
{
TEST_CHECK(is_i2p_url("http://a.i2p/a"));
TEST_CHECK(!is_i2p_url("http://a.I2P/a"));
TEST_CHECK(!is_i2p_url("http://c.i3p"));
TEST_CHECK(!is_i2p_url("http://i2p/foo bar"));
}
#endif
TORRENT_TEST(string_hash_no_case)
{
string_hash_no_case hsh;
// make sure different strings yield different hashes
TEST_CHECK(hsh("ab") != hsh("ba"));
// make sure case is ignored
TEST_EQUAL(hsh("Ab"), hsh("ab"));
TEST_EQUAL(hsh("Ab"), hsh("aB"));
// make sure zeroes in strings are supported
TEST_CHECK(hsh(std::string("\0a", 2)) != hsh(std::string("\0b", 2)));
TEST_EQUAL(hsh(std::string("\0a", 2)), hsh(std::string("\0a", 2)));
}
TORRENT_TEST(string_eq_no_case)
{
string_eq_no_case cmp;
TEST_CHECK(cmp("ab", "ba") == false);
TEST_CHECK(cmp("", ""));
TEST_CHECK(cmp("abc", "abc"));
// make sure different lengths are correctly treated as different
TEST_CHECK(cmp("abc", "ab") == false);
// make sure case is ignored
TEST_CHECK(cmp("Ab", "ab"));
TEST_CHECK(cmp("Ab", "aB"));
// make sure zeros are supported
TEST_CHECK(cmp(std::string("\0a", 2), std::string("\0b", 2)) == false);
TEST_CHECK(cmp(std::string("\0a", 2), std::string("\0a", 2)));
}
TORRENT_TEST(string_ptr_zero_termination)
{
char str[] = {'f', 'o', 'o', 'b', 'a', 'r'};
aux::string_ptr p(string_view(str, sizeof(str)));
// make sure it's zero-terminated now
TEST_CHECK(strlen(*p) == 6);
TEST_CHECK((*p)[6] == '\0');
TEST_CHECK(*p == string_view("foobar"));
}
TORRENT_TEST(string_ptr_move_construct)
{
aux::string_ptr p1("test");
TEST_CHECK(*p1 == string_view("test"));
aux::string_ptr p2(std::move(p1));
TEST_CHECK(*p2 == string_view("test"));
// moved-from state is empty
TEST_CHECK(*p1 == nullptr);
}
TORRENT_TEST(string_ptr_move_assign)
{
aux::string_ptr p1("test");
TEST_CHECK(*p1 == string_view("test"));
aux::string_ptr p2("foobar");
p1 = std::move(p2);
TEST_CHECK(*p1 == string_view("foobar"));
// moved-from state is empty
TEST_CHECK(*p2 == nullptr);
}