Squid Web Cache master
Loading...
Searching...
No Matches
testURL.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10
11#include "anyp/Uri.h"
12#include "base/CharacterSet.h"
13#include "base/TextException.h"
14#include "compat/cppunit.h"
15#include "debug/Stream.h"
16#include "sbuf/Stream.h"
17#include "unitTestMain.h"
18
19#include <cppunit/TestAssert.h>
20#include <sstream>
21
22/*
23 * test the Anyp::Uri-related classes
24 */
25
40
42class MyTestProgram: public TestProgram
43{
44public:
45 /* TestProgram API */
46 void startup() override;
47};
48
49void
51{
52 Mem::Init();
54}
55
56/*
57 * we can construct a URL with a AnyP::UriScheme.
58 * This creates a URL for that scheme.
59 */
60void
62{
63 AnyP::UriScheme empty_scheme;
64 AnyP::Uri protoless_url(AnyP::PROTO_NONE);
65 CPPUNIT_ASSERT_EQUAL(empty_scheme, protoless_url.getScheme());
66
69 CPPUNIT_ASSERT_EQUAL(ftp_scheme, ftp_url.getScheme());
70}
71
72/*
73 * a default constructed URL has scheme "NONE".
74 * Also, we should be able to use new and delete on
75 * scheme instances.
76 */
77void
79{
80 AnyP::UriScheme aScheme;
81 AnyP::Uri aUrl;
82 CPPUNIT_ASSERT_EQUAL(aScheme, aUrl.getScheme());
83
84 auto *urlPointer = new AnyP::Uri;
85 CPPUNIT_ASSERT(urlPointer != nullptr);
86 delete urlPointer;
87}
88
89void
91{
92 const std::vector< std::pair<SBuf, SBuf> > basicTestCases = {
93 {SBuf(""), SBuf("")},
94 {SBuf("foo"), SBuf("foo")},
95 {SBuf("%"), SBuf("%25")},
96 {SBuf("%foo"), SBuf("%25foo")},
97 {SBuf("foo%"), SBuf("foo%25")},
98 {SBuf("fo%o"), SBuf("fo%25o")},
99 {SBuf("fo%%o"), SBuf("fo%25%25o")},
100 {SBuf("fo o"), SBuf("fo%20o")},
101 {SBuf("?1"), SBuf("%3F1")},
102 {SBuf("\377"), SBuf("%FF")},
103 {SBuf("fo\0o", 4), SBuf("fo%00o")},
104 };
105
106 for (const auto &testCase: basicTestCases) {
107 const auto decoded = AnyP::Uri::Decode(testCase.second);
108 CPPUNIT_ASSERT(decoded);
109 CPPUNIT_ASSERT_EQUAL(testCase.first, *decoded);
110 CPPUNIT_ASSERT_EQUAL(testCase.second, AnyP::Uri::Encode(testCase.first, CharacterSet::RFC3986_UNRESERVED()));
111 };
112
113 const auto invalidEncodings = {
114 SBuf("%"),
115 SBuf("%%"),
116 SBuf("%%%"),
117 SBuf("%0"),
118 SBuf("%1"),
119 SBuf("%1Z"),
120 SBuf("%1\000", 2),
121 SBuf("%1\377"),
122 SBuf("%\0002", 3),
123 SBuf("%\3772"),
124 };
125
126 for (const auto &invalidEncoding: invalidEncodings) {
127 // test various input positions of an invalid escape sequence
128 CPPUNIT_ASSERT(!AnyP::Uri::Decode(invalidEncoding));
129 CPPUNIT_ASSERT(!AnyP::Uri::Decode(ToSBuf("word", invalidEncoding)));
130 CPPUNIT_ASSERT(!AnyP::Uri::Decode(ToSBuf(invalidEncoding, "word")));
131 CPPUNIT_ASSERT(!AnyP::Uri::Decode(ToSBuf("word", invalidEncoding, "word")));
132 CPPUNIT_ASSERT_EQUAL(invalidEncoding, AnyP::Uri::DecodeOrDupe(invalidEncoding));
133 };
134}
135
136int
137main(int argc, char *argv[])
138{
139 return MyTestProgram().run(argc, argv);
140}
141
static void Init()
initializes down-cased protocol scheme names array
Definition UriScheme.cc:38
AnyP::UriScheme const & getScheme() const
Definition Uri.h:58
static SBuf DecodeOrDupe(const SBuf &input)
Definition Uri.cc:132
static std::optional< SBuf > Decode(const SBuf &)
Definition Uri.cc:105
static SBuf Encode(const SBuf &, const CharacterSet &expected)
Definition Uri.cc:76
static const CharacterSet & RFC3986_UNRESERVED()
allowed URI characters that do not have a reserved purpose, RFC 3986
customizes our test setup
void startup() override
Definition SBuf.h:94
implements test program's main() function while enabling customization
int run(int argc, char *argv[])
void testDefaultConstructor()
Definition testURL.cc:78
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testEncoding)
void testConstructScheme()
Definition testURL.cc:61
CPPUNIT_TEST(testDefaultConstructor)
CPPUNIT_TEST_SUITE(TestUri)
void testEncoding()
Definition testURL.cc:90
CPPUNIT_TEST(testConstructScheme)
int main()
@ PROTO_NONE
@ PROTO_FTP
void Init()
Definition old_api.cc:281
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition Stream.h:63
CPPUNIT_TEST_SUITE_REGISTRATION(TestUri)