testCacheManager.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#include "anyp/Uri.h"
11#include "CacheManager.h"
12#include "compat/cppunit.h"
13#include "mgr/Action.h"
14#include "Store.h"
15#include "unitTestMain.h"
16
17#include <cppunit/TestAssert.h>
18/*
19 * test the CacheManager implementation
20 */
21
22class TestCacheManager : public CPPUNIT_NS::TestFixture
23{
29
30protected:
31 void testCreate();
32 void testRegister();
33 void testParseUrl();
34};
35
37
40{
41public:
43 void testValidUrl(const AnyP::Uri &);
44
47 void testInvalidUrl(const AnyP::Uri &, const char *problem);
48};
49
50void
52{
53 try {
54 (void)ParseUrl(url);
55 } catch (...) {
56 std::cerr << "\nFAIL: " << url <<
57 Debug::Extra << "error: " << CurrentException << "\n";
58 CPPUNIT_FAIL("rejected a valid URL");
59 }
60}
61
62void
63CacheManagerInternals::testInvalidUrl(const AnyP::Uri &url, const char *const problem)
64{
65 try {
66 (void)ParseUrl(url);
67 std::cerr << "\nFAIL: " << url <<
68 Debug::Extra << "error: should be rejected due to '" << problem << "'\n";
69 } catch (const TextException &) {
70 return; // success -- the parser signaled bad input
71 }
72 CPPUNIT_FAIL("failed to reject an invalid URL");
73}
74
76class MyTestProgram: public TestProgram
77{
78public:
79 /* TestProgram API */
80 void startup() override;
81};
82
83void
85{
86 Mem::Init();
88}
89
90/*
91 * Test creating a CacheManager
92 */
93void
95{
96 CacheManager::GetInstance(); //it's a singleton..
97}
98
99/* an action to register */
100static void
102{
103 sentry->flags=1;
104}
105
106/*
107 * registering an action makes it findable.
108 */
109void
111{
113 CPPUNIT_ASSERT(manager != nullptr);
114
115 manager->registerProfile("sample", "my sample", &dummy_action, false, false);
116 Mgr::Action::Pointer action = manager->createNamedAction("sample");
117 CPPUNIT_ASSERT(action != nullptr);
118
119 const Mgr::ActionProfile::Pointer profile = action->command().profile;
120 CPPUNIT_ASSERT(profile != nullptr);
121 CPPUNIT_ASSERT(profile->creator != nullptr);
122 CPPUNIT_ASSERT_EQUAL(false, profile->isPwReq);
123 CPPUNIT_ASSERT_EQUAL(false, profile->isAtomic);
124 CPPUNIT_ASSERT_EQUAL(String("sample"), String(action->name()));
125
126 StoreEntry *sentry=new StoreEntry();
127 sentry->flags=0x25; //arbitrary test value
128 action->run(sentry, false);
129 CPPUNIT_ASSERT_EQUAL(1,(int)sentry->flags);
130}
131
132void
134{
135 auto *mgr = static_cast<CacheManagerInternals *>(CacheManager::GetInstance());
136 CPPUNIT_ASSERT(mgr != nullptr);
137
138 std::vector<AnyP::ProtocolType> validSchemes = {
142 };
143
144 AnyP::Uri mgrUrl;
145 mgrUrl.host("localhost");
146 mgrUrl.port(3128);
147
148 const std::vector<const char *> validActions = {
149 "",
150 "menu"
151 };
152
153 const std::vector<const char *> invalidActions = {
154 "INVALID" // any unregistered name
155 };
156
157 const std::vector<const char *> validParams = {
158 "",
159 "?",
160 "?&",
161 "?&&&&&&&&&&&&",
162 "?foo=bar",
163 "?0123456789=bar",
164 "?foo=bar&",
165 "?foo=bar&&&&",
166 "?&foo=bar",
167 "?&&&&foo=bar",
168 "?&foo=bar&",
169 "?&&&&foo=bar&&&&",
170 "?foo=?_weird?~`:[]stuff&bar=okay&&&&&&",
171 "?intlist=1",
172 "?intlist=1,2,3,4,5",
173 "?string=1a",
174 "?string=1,2,3,4,z",
175 "?string=1,2,3,4,[0]",
176 "?intlist=1,2,3,4,5&string=1,2,3,4,y"
177 };
178
179 const std::vector<const char *> invalidParams = {
180 "?/",
181 "?foo",
182 "?/foo",
183 "?foo/",
184 "?foo=",
185 "?foo=&",
186 "?=foo",
187 "? foo=bar",
188 "? &",
189 "?& ",
190 "?=&",
191 "?&=",
192 "? &&&",
193 "?& &&",
194 "?&& &",
195 "?=&&&",
196 "?&=&&",
197 "?&&=&"
198 };
199
200 const std::vector<const char *> validFragments = {
201 "",
202 "#",
203 "##",
204 "#?a=b",
205 "#fragment"
206 };
207
208 const auto &prefix = CacheManager::WellKnownUrlPathPrefix();
209
210 assert(prefix.length());
211 const auto insufficientPrefix = prefix.substr(0, prefix.length()-1);
212
213 for (const auto &scheme : validSchemes) {
214 mgrUrl.setScheme(scheme);
215
216 // Check that the parser rejects URLs that lack the full prefix prefix.
217 // These negative tests log "Squid BUG: assurance failed" ERRORs because
218 // they violate CacheManager::ParseUrl()'s ForSomeCacheManager()
219 // precondition.
220 for (const auto *action : validActions) {
221 for (const auto *param : validParams) {
222 for (const auto *frag : validFragments) {
223 SBuf bits;
224 bits.append(insufficientPrefix);
225 bits.append(action);
226 bits.append(param);
227 bits.append(frag);
228 mgrUrl.path(bits);
229 mgr->testInvalidUrl(mgrUrl, "insufficient prefix");
230 }
231 }
232 }
233
234 // Check that the parser accepts valid URLs.
235 for (const auto action: validActions) {
236 for (const auto param: validParams) {
237 for (const auto frag: validFragments) {
238 SBuf bits;
239 bits.append(prefix);
240 bits.append(action);
241 bits.append(param);
242 bits.append(frag);
243 mgrUrl.path(bits);
244 mgr->testValidUrl(mgrUrl);
245 }
246 }
247 }
248
249 // Check that the parser rejects URLs with invalid parameters.
250 for (const auto action: validActions) {
251 for (const auto invalidParam: invalidParams) {
252 for (const auto frag: validFragments) {
253 SBuf bits;
254 bits.append(prefix);
255 bits.append(action);
256 bits.append(invalidParam);
257 bits.append(frag);
258 mgrUrl.path(bits);
259 mgr->testInvalidUrl(mgrUrl, invalidParam);
260 }
261 }
262 }
263 }
264}
265
266int
267main(int argc, char *argv[])
268{
269 return MyTestProgram().run(argc, argv);
270}
271
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
#define assert(EX)
Definition: assert.h:17
static void Init()
initializes down-cased protocol scheme names array
Definition: UriScheme.cc:38
Definition: Uri.h:31
void setScheme(const AnyP::ProtocolType &p, const char *str)
convert the URL scheme to that given
Definition: Uri.h:70
void path(const char *p)
Definition: Uri.h:101
void port(const Port p)
reset authority port subcomponent
Definition: Uri.h:95
void host(const char *src)
Definition: Uri.cc:100
Provides test code access to CacheManager internal symbols.
void testValidUrl(const AnyP::Uri &)
checks CacheManager parsing of the given valid URL
void testInvalidUrl(const AnyP::Uri &, const char *problem)
static CacheManager * GetInstance()
static const SBuf & WellKnownUrlPathPrefix()
initial URL path characters that identify cache manager requests
Mgr::Action::Pointer createNamedAction(const char *actionName)
void registerProfile(char const *action, char const *desc, OBJH *handler, int pw_req_flag, int atomic)
Mgr::CommandPointer ParseUrl(const AnyP::Uri &)
static std::ostream & Extra(std::ostream &)
Definition: debug.cc:1313
customizes our test setup
void startup() override
Definition: SBuf.h:94
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
uint16_t flags
Definition: Store.h:232
CPPUNIT_TEST(testParseUrl)
CPPUNIT_TEST(testRegister)
CPPUNIT_TEST_SUITE(TestCacheManager)
CPPUNIT_TEST(testCreate)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
an std::runtime_error with thrower location info
Definition: TextException.h:21
@ PROTO_HTTPS
Definition: ProtocolType.h:27
@ PROTO_HTTP
Definition: ProtocolType.h:25
@ PROTO_FTP
Definition: ProtocolType.h:26
void Init()
Definition: old_api.cc:425
static bool action(int fd, size_t metasize, const char *fn, const char *url, const SquidMetaList &meta)
Definition: purge.cc:315
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestCacheManager)
static void dummy_action(StoreEntry *sentry)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors