Skip to content

Commit 46bc6c2

Browse files
fjahrl0rinc
andcommitted
test: Add unit tests for urlDecode
Co-authored-by: Lőrinc <pap.lorinc@gmail.com>
1 parent 67c0d93 commit 46bc6c2

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ BITCOIN_TESTS =\
8585
test/checkqueue_tests.cpp \
8686
test/coins_tests.cpp \
8787
test/coinstatsindex_tests.cpp \
88+
test/common_url_tests.cpp \
8889
test/compilerbug_tests.cpp \
8990
test/compress_tests.cpp \
9091
test/crypto_tests.cpp \

src/test/common_url_tests.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
BOOST_AUTO_TEST_CASE(encode_decode_test) {
17+
BOOST_CHECK_EQUAL(urlDecode("Hello"), "Hello");
18+
BOOST_CHECK_EQUAL(urlDecode("99"), "99");
19+
BOOST_CHECK_EQUAL(urlDecode(""), "");
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+
30+
BOOST_AUTO_TEST_CASE(decode_malformed_test) {
31+
BOOST_CHECK_EQUAL(urlDecode("%%xhello th+ere \xff"), "%%xhello th+ere \xff");
32+
33+
BOOST_CHECK_EQUAL(urlDecode("%"), "%");
34+
BOOST_CHECK_EQUAL(urlDecode("%%"), "%%");
35+
BOOST_CHECK_EQUAL(urlDecode("%%%"), "%%%");
36+
BOOST_CHECK_EQUAL(urlDecode("%%%%"), "%%%%");
37+
38+
BOOST_CHECK_EQUAL(urlDecode("+"), "+");
39+
BOOST_CHECK_EQUAL(urlDecode("++"), "++");
40+
41+
BOOST_CHECK_EQUAL(urlDecode("?"), "?");
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("%-1"), "%-1");
58+
BOOST_CHECK_EQUAL(urlDecode("%1-"), "%1-");
59+
}
60+
61+
BOOST_AUTO_TEST_CASE(decode_lowercase_hex_test) {
62+
BOOST_CHECK_EQUAL(urlDecode("%f0%a0%b0"), "\xf0\xa0\xb0");
63+
}
64+
65+
BOOST_AUTO_TEST_CASE(decode_internal_nulls_test) {
66+
BOOST_CHECK_EQUAL(urlDecode("%00%00x%00%00"), "");
67+
BOOST_CHECK_EQUAL(urlDecode("abc%00%00"), "abc");
68+
}
69+
70+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)