testACLMaxUserIP.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 #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 
28 class TestACLMaxUserIP : public CPPUNIT_NS::TestFixture
29 {
31  /* note the statement here and then the actual prototype below */
35 
36 protected:
37  void testDefaults();
38  void testParseLine();
39 };
41 
42 /* globals required to resolve link issues */
44 
45 void
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 {
60 public:
61  /* TestProgram API */
62  void startup() override;
63 };
64 
65 void
67 {
68  Acl::RegisterMaker("max_user_ip", [](Acl::TypeName name)->Acl::Node* { return new ACLMaxUserIP(name); });
69 }
70 
71 void
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 vaid */
90  CPPUNIT_ASSERT_EQUAL(true, maxUserIpACL->valid());
91  }
93  xfree(line);
94 }
95 
96 int
97 main(int argc, char *argv[])
98 {
99  return MyTestProgram().run(argc, argv);
100 }
101 
102 #endif /* USE_AUTH */
103 
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE(TestACLMaxUserIP)
static ConfigParser LegacyParser
Definition: cache_cf.cc:268
void RegisterMaker(TypeName typeName, Maker maker)
use the given Acl::Node Maker for all ACLs of the named type
Definition: Acl.cc:92
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
Definition: SBuf.h:93
#define xstrdup
void FreeNamedAcls(NamedAcls **)
delete the given list of "acl" directives
Definition: Acl.cc:346
static void ParseNamedAcl(ConfigParser &, NamedAcls *&)
parses acl directive parts that follow directive name (i.e. "acl")
Definition: Acl.cc:229
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
static Acl::Node * FindByName(const SBuf &)
A configured ACL with a given name or nil.
Definition: Acl.cc:159
bool valid() const override
Definition: AclMaxUserIp.cc:39
CPPUNIT_TEST(testDefaults)
Acl::BooleanOptionValue beStrict
Enforce "one user, one device" policy?
Definition: AclMaxUserIp.h:39
#define xfree
int getMaximum() const
Definition: AclMaxUserIp.h:33
Definition: Node.h:25
const char * TypeName
the ACL type name known to admins
Definition: Acl.h:24
Acl::NamedAcls * namedAcls
acl aclname acltype ...
Definition: SquidConfig.h:356
static void SetCfgLine(char *line)
Set the configuration file line to parse.
CPPUNIT_TEST_SUITE_REGISTRATION(TestACLMaxUserIP)
customizes our test setup
AnyP::PortCfgPointer HttpPortList
list of Squid http(s)_port configured
class SquidConfig Config
Definition: SquidConfig.cc:12
void startup() override

 

Introduction

Documentation

Support

Miscellaneous