Branch data Line data Source code
1 : : // Copyright (c) 2024-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or https://opensource.org/license/mit/.
4 : :
5 : : #include <common/url.h>
6 : :
7 : : #include <string>
8 : :
9 : : #include <boost/test/unit_test.hpp>
10 : :
11 : : BOOST_AUTO_TEST_SUITE(common_url_tests)
12 : :
13 : : // These test vectors were ported from test/regress.c in the libevent library
14 : : // which used to be a dependency of the UrlDecode function.
15 : :
16 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(encode_decode_test) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
17 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("Hello"), "Hello");
18 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("99"), "99");
19 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode(""), "");
20 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_"),
21 : : "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_");
22 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%20"), " ");
23 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%FF%F0%E0"), "\xff\xf0\xe0");
24 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%01%19"), "\x01\x19");
25 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt"),
26 : : "http://www.ietf.org/rfc/rfc3986.txt");
27 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("1%2B2%3D3"), "1+2=3");
28 : 1 : }
29 : :
30 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(decode_malformed_test) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
31 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%%xhello th+ere \xff"), "%%xhello th+ere \xff");
32 : :
33 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%"), "%");
34 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%%"), "%%");
35 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%%%"), "%%%");
36 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%%%%"), "%%%%");
37 : :
38 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("+"), "+");
39 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("++"), "++");
40 : :
41 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("?"), "?");
42 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("??"), "??");
43 : :
44 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%G1"), "%G1");
45 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%2"), "%2");
46 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%ZX"), "%ZX");
47 : :
48 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("valid%20string%G1"), "valid string%G1");
49 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%20invalid%ZX"), " invalid%ZX");
50 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%20%G1%ZX"), " %G1%ZX");
51 : :
52 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%1 "), "%1 ");
53 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("% 9"), "% 9");
54 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode(" %Z "), " %Z ");
55 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode(" % X"), " % X");
56 : :
57 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%%ffg"), "%\xffg");
58 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%fg"), "%fg");
59 : :
60 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%-1"), "%-1");
61 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%1-"), "%1-");
62 : 1 : }
63 : :
64 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(decode_lowercase_hex_test) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
65 [ + - ]: 1 : BOOST_CHECK_EQUAL(UrlDecode("%f0%a0%b0"), "\xf0\xa0\xb0");
66 : 1 : }
67 : :
68 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(decode_internal_nulls_test) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
69 : 1 : std::string result1{"\0\0x\0\0", 5};
70 [ + - + - : 1 : BOOST_CHECK_EQUAL(UrlDecode("%00%00x%00%00"), result1);
+ - ]
71 [ + - ]: 1 : std::string result2{"abc\0\0", 5};
72 [ + - + - : 1 : BOOST_CHECK_EQUAL(UrlDecode("abc%00%00"), result2);
+ - ]
73 : 1 : }
74 : :
75 : : BOOST_AUTO_TEST_SUITE_END()
|