Squid Web Cache master
Loading...
Searching...
No Matches
testACLMaxUserIP.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#if USE_AUTH
12
13#include "acl/Acl.h"
14#include "auth/AclMaxUserIp.h"
15#include "auth/UserRequest.h"
16#include "compat/cppunit.h"
17#include "ConfigParser.h"
18#include "SquidConfig.h"
19#include "unitTestMain.h"
20
21#include <stdexcept>
22
23/*
24 * demonstration test file, as new idioms are made they will
25 * be shown in the TestBoilerplate source.
26 */
27
28class TestACLMaxUserIP : public CPPUNIT_NS::TestFixture
29{
31 /* note the statement here and then the actual prototype below */
35
36protected:
37 void testDefaults();
38 void testParseLine();
39};
41
42/* globals required to resolve link issues */
44
45void
47{
48 ACLMaxUserIP anACL("max_user_ip");
49 /* 0 is not a valid maximum, so we start at 0 */
50 CPPUNIT_ASSERT_EQUAL(0,anACL.getMaximum());
51 /* and we have no option to turn strict OFF, so start ON. */
52 CPPUNIT_ASSERT_EQUAL(false, static_cast<bool>(anACL.beStrict));
53 /* an unparsed acl must not be valid - there is no sane default */
54 CPPUNIT_ASSERT_EQUAL(false,anACL.valid());
55}
56
59{
60public:
61 /* TestProgram API */
62 void startup() override;
63};
64
65void
67{
68 Acl::RegisterMaker("max_user_ip", [](Acl::TypeName name)->Acl::Node* { return new ACLMaxUserIP(name); });
69}
70
71void
73{
74 /* a config line to pass with a lead-in token to seed the parser. */
75 char * line = xstrdup("test max_user_ip -s 1");
76 /* seed the parser */
80 CPPUNIT_ASSERT(Config.namedAcls);
81 const auto anACL = Acl::Node::FindByName(SBuf("test"));
82 CPPUNIT_ASSERT(anACL);
83 ACLMaxUserIP *maxUserIpACL = dynamic_cast<ACLMaxUserIP *>(anACL);
84 CPPUNIT_ASSERT(maxUserIpACL);
85 if (maxUserIpACL) {
86 /* we want a maximum of one, and strict to be true */
87 CPPUNIT_ASSERT_EQUAL(1, maxUserIpACL->getMaximum());
88 CPPUNIT_ASSERT_EQUAL(true, static_cast<bool>(maxUserIpACL->beStrict));
89 /* the acl must be valid */
90 CPPUNIT_ASSERT_EQUAL(true, maxUserIpACL->valid());
91 }
93 xfree(line);
94}
95
96int
97main(int argc, char *argv[])
98{
99 return MyTestProgram().run(argc, argv);
100}
101
102#endif /* USE_AUTH */
103
class SquidConfig Config
static ConfigParser LegacyParser
Definition cache_cf.cc:267
bool valid() const override
int getMaximum() const
Acl::BooleanOptionValue beStrict
Enforce "one user, one device" policy?
static void ParseNamedAcl(ConfigParser &, NamedAcls *&)
parses acl directive parts that follow directive name (i.e. "acl")
Definition Acl.cc:229
static Acl::Node * FindByName(const SBuf &)
A configured ACL with a given name or nil.
Definition Acl.cc:159
static void SetCfgLine(char *line)
Set the configuration file line to parse.
customizes our test setup
void startup() override
Definition SBuf.h:94
Acl::NamedAcls * namedAcls
acl aclname acltype ...
CPPUNIT_TEST(testDefaults)
CPPUNIT_TEST_SUITE(TestACLMaxUserIP)
CPPUNIT_TEST(testParseLine)
implements test program's main() function while enabling customization
int run(int argc, char *argv[])
int main()
void RegisterMaker(TypeName typeName, Maker maker)
use the given Acl::Node Maker for all ACLs of the named type
Definition Acl.cc:92
const char * TypeName
the ACL type name known to admins
Definition Acl.h:24
void FreeNamedAcls(NamedAcls **)
delete the given list of "acl" directives
Definition Acl.cc:346
#define xfree
#define xstrdup
CPPUNIT_TEST_SUITE_REGISTRATION(TestACLMaxUserIP)
AnyP::PortCfgPointer HttpPortList
list of Squid http(s)_port configured