Bitcoin Core 28.99.0
P2P Digital Currency
common_url_tests.cpp
Go to the documentation of this file.
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
11BOOST_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
16BOOST_AUTO_TEST_CASE(encode_decode_test) {
17 BOOST_CHECK_EQUAL(UrlDecode("Hello"), "Hello");
18 BOOST_CHECK_EQUAL(UrlDecode("99"), "99");
20 BOOST_CHECK_EQUAL(UrlDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_"),
21 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_");
22 BOOST_CHECK_EQUAL(UrlDecode("%20"), " ");
23 BOOST_CHECK_EQUAL(UrlDecode("%FF%F0%E0"), "\xff\xf0\xe0");
24 BOOST_CHECK_EQUAL(UrlDecode("%01%19"), "\x01\x19");
25 BOOST_CHECK_EQUAL(UrlDecode("http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt"),
26 "http://www.ietf.org/rfc/rfc3986.txt");
27 BOOST_CHECK_EQUAL(UrlDecode("1%2B2%3D3"), "1+2=3");
28}
29
30BOOST_AUTO_TEST_CASE(decode_malformed_test) {
31 BOOST_CHECK_EQUAL(UrlDecode("%%xhello th+ere \xff"), "%%xhello th+ere \xff");
32
34 BOOST_CHECK_EQUAL(UrlDecode("%%"), "%%");
35 BOOST_CHECK_EQUAL(UrlDecode("%%%"), "%%%");
36 BOOST_CHECK_EQUAL(UrlDecode("%%%%"), "%%%%");
37
39 BOOST_CHECK_EQUAL(UrlDecode("++"), "++");
40
42 BOOST_CHECK_EQUAL(UrlDecode("??"), "??");
43
44 BOOST_CHECK_EQUAL(UrlDecode("%G1"), "%G1");
45 BOOST_CHECK_EQUAL(UrlDecode("%2"), "%2");
46 BOOST_CHECK_EQUAL(UrlDecode("%ZX"), "%ZX");
47
48 BOOST_CHECK_EQUAL(UrlDecode("valid%20string%G1"), "valid string%G1");
49 BOOST_CHECK_EQUAL(UrlDecode("%20invalid%ZX"), " invalid%ZX");
50 BOOST_CHECK_EQUAL(UrlDecode("%20%G1%ZX"), " %G1%ZX");
51
52 BOOST_CHECK_EQUAL(UrlDecode("%1 "), "%1 ");
53 BOOST_CHECK_EQUAL(UrlDecode("% 9"), "% 9");
54 BOOST_CHECK_EQUAL(UrlDecode(" %Z "), " %Z ");
55 BOOST_CHECK_EQUAL(UrlDecode(" % X"), " % X");
56
57 BOOST_CHECK_EQUAL(UrlDecode("%%ffg"), "%\xffg");
58 BOOST_CHECK_EQUAL(UrlDecode("%fg"), "%fg");
59
60 BOOST_CHECK_EQUAL(UrlDecode("%-1"), "%-1");
61 BOOST_CHECK_EQUAL(UrlDecode("%1-"), "%1-");
62}
63
64BOOST_AUTO_TEST_CASE(decode_lowercase_hex_test) {
65 BOOST_CHECK_EQUAL(UrlDecode("%f0%a0%b0"), "\xf0\xa0\xb0");
66}
67
68BOOST_AUTO_TEST_CASE(decode_internal_nulls_test) {
69 std::string result1{"\0\0x\0\0", 5};
70 BOOST_CHECK_EQUAL(UrlDecode("%00%00x%00%00"), result1);
71 std::string result2{"abc\0\0", 5};
72 BOOST_CHECK_EQUAL(UrlDecode("abc%00%00"), result2);
73}
74
BOOST_AUTO_TEST_CASE(encode_decode_test)
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
std::string UrlDecode(std::string_view url_encoded)
Definition: url.cpp:12