]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testCacheManager.cc
d62043803fb9bcbb407da223397274e609d162ba
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
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.
11 #include "CacheManager.h"
12 #include "compat/cppunit.h"
13 #include "mgr/Action.h"
14 #include "mgr/Registration.h"
16 #include "unitTestMain.h"
18 #include <cppunit/TestAssert.h>
20 * test the CacheManager implementation
23 class TestCacheManager
: public CPPUNIT_NS::TestFixture
25 CPPUNIT_TEST_SUITE(TestCacheManager
);
26 CPPUNIT_TEST(testCreate
);
27 CPPUNIT_TEST(testRegister
);
28 CPPUNIT_TEST(testParseUrl
);
29 CPPUNIT_TEST_SUITE_END();
37 CPPUNIT_TEST_SUITE_REGISTRATION( TestCacheManager
);
39 /// Provides test code access to CacheManager internal symbols
40 class CacheManagerInternals
: public CacheManager
43 /// checks CacheManager parsing of the given valid URL
44 void testValidUrl(const AnyP::Uri
&);
46 /// checks CacheManager parsing of the given invalid URL
47 /// \param problem a bad part of the URL or its description
48 void testInvalidUrl(const AnyP::Uri
&, const char *problem
);
52 CacheManagerInternals::testValidUrl(const AnyP::Uri
&url
)
54 CPPUNIT_ASSERT_NO_THROW(ParseUrl(url
));
58 CacheManagerInternals::testInvalidUrl(const AnyP::Uri
&url
, const char *const problem
)
60 CPPUNIT_ASSERT_THROW_MESSAGE(problem
, ParseUrl(url
), TextException
);
63 /// customizes our test setup
64 class MyTestProgram
: public TestProgram
68 void startup() override
;
72 MyTestProgram::startup()
75 AnyP::UriScheme::Init();
79 * Test creating a CacheManager
82 TestCacheManager::testCreate()
84 CacheManager::GetInstance(); //it's a singleton..
87 /* an action to register */
89 dummy_action(StoreEntry
* sentry
)
95 * registering an action makes it findable.
98 TestCacheManager::testRegister()
100 CacheManager
*manager
=CacheManager::GetInstance();
101 CPPUNIT_ASSERT(manager
!= nullptr);
103 Mgr::RegisterAction("sample", "my sample", &dummy_action
, Mgr::Protected::no
, Mgr::Atomic::no
, Mgr::Format::informal
);
104 Mgr::Action::Pointer action
= manager
->createNamedAction("sample");
105 CPPUNIT_ASSERT(action
!= nullptr);
107 const Mgr::ActionProfile::Pointer profile
= action
->command().profile
;
108 CPPUNIT_ASSERT(profile
!= nullptr);
109 CPPUNIT_ASSERT(profile
->creator
!= nullptr);
110 CPPUNIT_ASSERT_EQUAL(false, profile
->isPwReq
);
111 CPPUNIT_ASSERT_EQUAL(false, profile
->isAtomic
);
112 CPPUNIT_ASSERT_EQUAL(Mgr::Format::informal
, profile
->format
);
113 CPPUNIT_ASSERT_EQUAL(Mgr::Format::informal
, action
->format());
114 CPPUNIT_ASSERT_EQUAL(String("sample"), String(action
->name()));
116 StoreEntry
*sentry
=new StoreEntry();
117 sentry
->createMemObject();
118 sentry
->flags
=0x25; //arbitrary test value
119 action
->run(sentry
, false);
120 CPPUNIT_ASSERT_EQUAL(1,(int)sentry
->flags
);
124 TestCacheManager::testParseUrl()
126 auto *mgr
= static_cast<CacheManagerInternals
*>(CacheManager::GetInstance());
127 CPPUNIT_ASSERT(mgr
!= nullptr);
129 std::vector
<AnyP::ProtocolType
> validSchemes
= {
136 mgrUrl
.host("localhost");
139 const std::vector
<const char *> validActions
= {
144 const std::vector
<const char *> invalidActions
= {
145 "INVALID" // any unregistered name
148 const std::vector
<const char *> validParams
= {
161 "?foo=?_weird?~`:[]stuff&bar=okay&&&&&&",
163 "?intlist=1,2,3,4,5",
166 "?string=1,2,3,4,[0]",
167 "?intlist=1,2,3,4,5&string=1,2,3,4,y"
170 const std::vector
<const char *> invalidParams
= {
191 const std::vector
<const char *> validFragments
= {
199 const auto &prefix
= CacheManager::WellKnownUrlPathPrefix();
201 assert(prefix
.length());
202 const auto insufficientPrefix
= prefix
.substr(0, prefix
.length()-1);
204 for (const auto &scheme
: validSchemes
) {
205 mgrUrl
.setScheme(scheme
);
207 // Check that the parser rejects URLs that lack the full prefix prefix.
208 // These negative tests log "Squid BUG: assurance failed" ERRORs because
209 // they violate CacheManager::ParseUrl()'s ForSomeCacheManager()
211 for (const auto *action
: validActions
) {
212 for (const auto *param
: validParams
) {
213 for (const auto *frag
: validFragments
) {
215 bits
.append(insufficientPrefix
);
220 mgr
->testInvalidUrl(mgrUrl
, "insufficient prefix");
225 // Check that the parser accepts valid URLs.
226 for (const auto action
: validActions
) {
227 for (const auto param
: validParams
) {
228 for (const auto frag
: validFragments
) {
235 mgr
->testValidUrl(mgrUrl
);
240 // Check that the parser rejects URLs with invalid parameters.
241 for (const auto action
: validActions
) {
242 for (const auto invalidParam
: invalidParams
) {
243 for (const auto frag
: validFragments
) {
247 bits
.append(invalidParam
);
250 mgr
->testInvalidUrl(mgrUrl
, invalidParam
);
258 main(int argc
, char *argv
[])
260 return MyTestProgram().run(argc
, argv
);