testURL.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 
26 class TestUri : public CPPUNIT_NS::TestFixture
27 {
33 
34 protected:
35  void testConstructScheme();
37  void testEncoding();
38 };
40 
42 class MyTestProgram: public TestProgram
43 {
44 public:
45  /* TestProgram API */
46  void startup() override;
47 };
48 
49 void
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  */
60 void
62 {
63  AnyP::UriScheme empty_scheme;
64  AnyP::Uri protoless_url(AnyP::PROTO_NONE);
65  CPPUNIT_ASSERT_EQUAL(empty_scheme, protoless_url.getScheme());
66 
67  AnyP::UriScheme ftp_scheme(AnyP::PROTO_FTP);
68  AnyP::Uri ftp_url(AnyP::PROTO_FTP);
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  */
77 void
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 
89 void
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  CPPUNIT_ASSERT_EQUAL(testCase.first, AnyP::Uri::Decode(testCase.second));
108  CPPUNIT_ASSERT_EQUAL(testCase.second, AnyP::Uri::Encode(testCase.first, CharacterSet::RFC3986_UNRESERVED()));
109  };
110 
111  const auto invalidEncodings = {
112  SBuf("%"),
113  SBuf("%%"),
114  SBuf("%%%"),
115  SBuf("%1"),
116  SBuf("%1Z"),
117  SBuf("%1\000", 2),
118  SBuf("%1\377"),
119  SBuf("%\0002", 3),
120  SBuf("%\3772"),
121  };
122 
123  for (const auto &invalidEncoding: invalidEncodings) {
124  // test various input positions of an invalid escape sequence
125  CPPUNIT_ASSERT_THROW(AnyP::Uri::Decode(invalidEncoding), TextException);
126  CPPUNIT_ASSERT_THROW(AnyP::Uri::Decode(ToSBuf("word", invalidEncoding)), TextException);
127  CPPUNIT_ASSERT_THROW(AnyP::Uri::Decode(ToSBuf(invalidEncoding, "word")), TextException);
128  CPPUNIT_ASSERT_THROW(AnyP::Uri::Decode(ToSBuf("word", invalidEncoding, "word")), TextException);
129  };
130 }
131 
132 int
133 main(int argc, char *argv[])
134 {
135  return MyTestProgram().run(argc, argv);
136 }
137 
CPPUNIT_TEST_SUITE_REGISTRATION(TestUri)
void testDefaultConstructor()
Definition: testURL.cc:78
Definition: Uri.h:31
@ PROTO_NONE
Definition: ProtocolType.h:24
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
Definition: SBuf.h:93
void testEncoding()
Definition: testURL.cc:90
void testConstructScheme()
Definition: testURL.cc:61
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testConstructScheme)
CPPUNIT_TEST_SUITE(TestUri)
static void Init()
initializes down-cased protocol scheme names array
Definition: UriScheme.cc:38
int main(int argc, char *argv[])
Definition: testURL.cc:133
const AnyP::UriScheme & getScheme() const
Definition: Uri.h:58
@ PROTO_FTP
Definition: ProtocolType.h:26
static SBuf Decode(const SBuf &)
%-decode the given buffer
Definition: Uri.cc:86
an std::runtime_error with thrower location info
Definition: TextException.h:20
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
static const CharacterSet & RFC3986_UNRESERVED()
allowed URI characters that do not have a reserved purpose, RFC 3986
customizes our test setup
static SBuf Encode(const SBuf &, const CharacterSet &expected)
Definition: Uri.cc:57
void startup() override

 

Introduction

Documentation

Support

Miscellaneous