1 /*
2  * Copyright (C) 1996-2021 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 "ConfigParser.h"
17 #include "testACLMaxUserIP.h"
18 #include "unitTestMain.h"
19 
20 #include <stdexcept>
21 
22 CPPUNIT_TEST_SUITE_REGISTRATION( testACLMaxUserIP );
23 
24 void
testDefaults()25 testACLMaxUserIP::testDefaults()
26 {
27     ACLMaxUserIP anACL("max_user_ip");
28     /* 0 is not a valid maximum, so we start at 0 */
29     CPPUNIT_ASSERT_EQUAL(0,anACL.getMaximum());
30     /* and we have no option to turn strict OFF, so start ON. */
31     CPPUNIT_ASSERT_EQUAL(false, static_cast<bool>(anACL.beStrict));
32     /* an unparsed acl must not be valid - there is no sane default */
33     CPPUNIT_ASSERT_EQUAL(false,anACL.valid());
34 }
35 
36 void
setUp()37 testACLMaxUserIP::setUp()
38 {
39     CPPUNIT_NS::TestFixture::setUp();
40     Acl::RegisterMaker("max_user_ip", [](Acl::TypeName name)->ACL* { return new ACLMaxUserIP(name); });
41 }
42 
43 void
testParseLine()44 testACLMaxUserIP::testParseLine()
45 {
46     /* a config line to pass with a lead-in token to seed the parser. */
47     char * line = xstrdup("test max_user_ip -s 1");
48     /* seed the parser */
49     ConfigParser::SetCfgLine(line);
50     ACL *anACL = NULL;
51     ConfigParser LegacyParser;
52     ACL::ParseAclLine(LegacyParser, &anACL);
53     ACLMaxUserIP *maxUserIpACL = dynamic_cast<ACLMaxUserIP *>(anACL);
54     CPPUNIT_ASSERT(maxUserIpACL);
55     if (maxUserIpACL) {
56         /* we want a maximum of one, and strict to be true */
57         CPPUNIT_ASSERT_EQUAL(1, maxUserIpACL->getMaximum());
58         CPPUNIT_ASSERT_EQUAL(true, static_cast<bool>(maxUserIpACL->beStrict));
59         /* the acl must be vaid */
60         CPPUNIT_ASSERT_EQUAL(true, maxUserIpACL->valid());
61     }
62     delete anACL;
63     xfree(line);
64 }
65 
66 #endif /* USE_AUTH */
67 
68