]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testCacheManager.cc
mkrelease: allow two digits for minor release numbers (#1837)
[thirdparty/squid.git] / src / tests / testCacheManager.cc
CommitLineData
4e0938ef 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
4e0938ef
AJ
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
582c2af2 9#include "squid.h"
26e65059 10#include "anyp/Uri.h"
62ee09ca 11#include "CacheManager.h"
ae022809 12#include "compat/cppunit.h"
602d9612 13#include "mgr/Action.h"
b707cdeb 14#include "Store.h"
7f861c77 15#include "unitTestMain.h"
62ee09ca 16
3d41e53a 17#include <cppunit/TestAssert.h>
ae022809
FC
18/*
19 * test the CacheManager implementation
20 */
21
22class TestCacheManager : public CPPUNIT_NS::TestFixture
23{
24 CPPUNIT_TEST_SUITE(TestCacheManager);
25 CPPUNIT_TEST(testCreate);
26 CPPUNIT_TEST(testRegister);
27 CPPUNIT_TEST(testParseUrl);
28 CPPUNIT_TEST_SUITE_END();
29
ae022809
FC
30protected:
31 void testCreate();
32 void testRegister();
33 void testParseUrl();
34};
62ee09ca 35
41e9c9f0 36CPPUNIT_TEST_SUITE_REGISTRATION( TestCacheManager );
62ee09ca 37
26e65059
AJ
38/// Provides test code access to CacheManager internal symbols
39class CacheManagerInternals : public CacheManager
40{
41public:
3c383cc3
AR
42 /// checks CacheManager parsing of the given valid URL
43 void testValidUrl(const AnyP::Uri &);
44
45 /// checks CacheManager parsing of the given invalid URL
46 /// \param problem a bad part of the URL or its description
47 void testInvalidUrl(const AnyP::Uri &, const char *problem);
26e65059
AJ
48};
49
3c383cc3
AR
50void
51CacheManagerInternals::testValidUrl(const AnyP::Uri &url)
52{
eb743f8b 53 CPPUNIT_ASSERT_NO_THROW(ParseUrl(url));
3c383cc3
AR
54}
55
56void
57CacheManagerInternals::testInvalidUrl(const AnyP::Uri &url, const char *const problem)
58{
eb743f8b 59 CPPUNIT_ASSERT_THROW_MESSAGE(problem, ParseUrl(url), TextException);
3c383cc3
AR
60}
61
53a5a6de
AR
62/// customizes our test setup
63class MyTestProgram: public TestProgram
64{
65public:
66 /* TestProgram API */
67 void startup() override;
68};
62ee09ca 69
53a5a6de
AR
70void
71MyTestProgram::startup()
62ee09ca 72{
16555581 73 Mem::Init();
26e65059 74 AnyP::UriScheme::Init();
16555581 75}
62ee09ca 76
77/*
78 * Test creating a CacheManager
79 */
80void
41e9c9f0 81TestCacheManager::testCreate()
62ee09ca 82{
b707cdeb 83 CacheManager::GetInstance(); //it's a singleton..
62ee09ca 84}
85
86/* an action to register */
87static void
88dummy_action(StoreEntry * sentry)
b707cdeb
FC
89{
90 sentry->flags=1;
91}
62ee09ca 92
93/*
94 * registering an action makes it findable.
95 */
96void
41e9c9f0 97TestCacheManager::testRegister()
62ee09ca 98{
b707cdeb 99 CacheManager *manager=CacheManager::GetInstance();
aee3523a 100 CPPUNIT_ASSERT(manager != nullptr);
b707cdeb 101
8822ebee
AR
102 manager->registerProfile("sample", "my sample", &dummy_action, false, false);
103 Mgr::Action::Pointer action = manager->createNamedAction("sample");
aee3523a 104 CPPUNIT_ASSERT(action != nullptr);
b707cdeb 105
8822ebee 106 const Mgr::ActionProfile::Pointer profile = action->command().profile;
aee3523a
AR
107 CPPUNIT_ASSERT(profile != nullptr);
108 CPPUNIT_ASSERT(profile->creator != nullptr);
8822ebee
AR
109 CPPUNIT_ASSERT_EQUAL(false, profile->isPwReq);
110 CPPUNIT_ASSERT_EQUAL(false, profile->isAtomic);
111 CPPUNIT_ASSERT_EQUAL(String("sample"), String(action->name()));
b707cdeb
FC
112
113 StoreEntry *sentry=new StoreEntry();
114 sentry->flags=0x25; //arbitrary test value
8822ebee 115 action->run(sentry, false);
b707cdeb 116 CPPUNIT_ASSERT_EQUAL(1,(int)sentry->flags);
62ee09ca 117}
f53969cc 118
26e65059 119void
41e9c9f0 120TestCacheManager::testParseUrl()
26e65059
AJ
121{
122 auto *mgr = static_cast<CacheManagerInternals *>(CacheManager::GetInstance());
123 CPPUNIT_ASSERT(mgr != nullptr);
124
125 std::vector<AnyP::ProtocolType> validSchemes = {
26e65059
AJ
126 AnyP::PROTO_HTTP,
127 AnyP::PROTO_HTTPS,
128 AnyP::PROTO_FTP
129 };
130
131 AnyP::Uri mgrUrl;
132 mgrUrl.host("localhost");
133 mgrUrl.port(3128);
134
26e65059
AJ
135 const std::vector<const char *> validActions = {
136 "",
137 "menu"
138 };
139
140 const std::vector<const char *> invalidActions = {
141 "INVALID" // any unregistered name
142 };
143
144 const std::vector<const char *> validParams = {
145 "",
146 "?",
147 "?&",
148 "?&&&&&&&&&&&&",
149 "?foo=bar",
150 "?0123456789=bar",
151 "?foo=bar&",
152 "?foo=bar&&&&",
153 "?&foo=bar",
154 "?&&&&foo=bar",
155 "?&foo=bar&",
156 "?&&&&foo=bar&&&&",
157 "?foo=?_weird?~`:[]stuff&bar=okay&&&&&&",
158 "?intlist=1",
159 "?intlist=1,2,3,4,5",
160 "?string=1a",
161 "?string=1,2,3,4,z",
162 "?string=1,2,3,4,[0]",
163 "?intlist=1,2,3,4,5&string=1,2,3,4,y"
164 };
165
166 const std::vector<const char *> invalidParams = {
167 "?/",
168 "?foo",
169 "?/foo",
170 "?foo/",
171 "?foo=",
172 "?foo=&",
173 "?=foo",
174 "? foo=bar",
175 "? &",
176 "?& ",
177 "?=&",
178 "?&=",
179 "? &&&",
180 "?& &&",
181 "?&& &",
182 "?=&&&",
183 "?&=&&",
184 "?&&=&"
185 };
186
187 const std::vector<const char *> validFragments = {
188 "",
189 "#",
190 "##",
191 "#?a=b",
192 "#fragment"
193 };
194
3c383cc3
AR
195 const auto &prefix = CacheManager::WellKnownUrlPathPrefix();
196
197 assert(prefix.length());
198 const auto insufficientPrefix = prefix.substr(0, prefix.length()-1);
199
26e65059
AJ
200 for (const auto &scheme : validSchemes) {
201 mgrUrl.setScheme(scheme);
202
3c383cc3
AR
203 // Check that the parser rejects URLs that lack the full prefix prefix.
204 // These negative tests log "Squid BUG: assurance failed" ERRORs because
205 // they violate CacheManager::ParseUrl()'s ForSomeCacheManager()
206 // precondition.
207 for (const auto *action : validActions) {
208 for (const auto *param : validParams) {
209 for (const auto *frag : validFragments) {
210 SBuf bits;
211 bits.append(insufficientPrefix);
212 bits.append(action);
213 bits.append(param);
214 bits.append(frag);
215 mgrUrl.path(bits);
216 mgr->testInvalidUrl(mgrUrl, "insufficient prefix");
26e65059
AJ
217 }
218 }
3c383cc3
AR
219 }
220
221 // Check that the parser accepts valid URLs.
222 for (const auto action: validActions) {
223 for (const auto param: validParams) {
224 for (const auto frag: validFragments) {
225 SBuf bits;
226 bits.append(prefix);
227 bits.append(action);
228 bits.append(param);
229 bits.append(frag);
230 mgrUrl.path(bits);
231 mgr->testValidUrl(mgrUrl);
232 }
233 }
234 }
26e65059 235
3c383cc3
AR
236 // Check that the parser rejects URLs with invalid parameters.
237 for (const auto action: validActions) {
238 for (const auto invalidParam: invalidParams) {
239 for (const auto frag: validFragments) {
240 SBuf bits;
241 bits.append(prefix);
242 bits.append(action);
243 bits.append(invalidParam);
244 bits.append(frag);
245 mgrUrl.path(bits);
246 mgr->testInvalidUrl(mgrUrl, invalidParam);
26e65059
AJ
247 }
248 }
249 }
250 }
251}
53521734 252
53a5a6de
AR
253int
254main(int argc, char *argv[])
255{
256 return MyTestProgram().run(argc, argv);
257}
258