testAuth.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 "auth/Config.h"
14#include "auth/Gadgets.h"
15#include "auth/UserRequest.h"
16#include "ConfigParser.h"
17#include "testAuth.h"
18#include "unitTestMain.h"
19
23#if HAVE_AUTH_MODULE_BASIC
24CPPUNIT_TEST_SUITE_REGISTRATION( TestAuthBasicUserRequest );
25#endif
26#if HAVE_AUTH_MODULE_DIGEST
27CPPUNIT_TEST_SUITE_REGISTRATION( TestAuthDigestUserRequest );
28#endif
29#if HAVE_AUTH_MODULE_NTLM
30CPPUNIT_TEST_SUITE_REGISTRATION( TestAuthNtlmUserRequest );
31#endif
32#if HAVE_AUTH_MODULE_NEGOTIATE
33CPPUNIT_TEST_SUITE_REGISTRATION( TestAuthNegotiateUserRequest );
34#endif
35
36/* Instantiate all auth framework types */
37void
39{}
40
41char const * stub_config="auth_param digest program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd\n"
42 "auth_param digest children 5\n"
43 "auth_param digest realm Squid proxy-caching web server\n"
44 "auth_param digest nonce_garbage_interval 5 minutes\n"
45 "auth_param digest nonce_max_duration 30 minutes\n"
46 "auth_param digest nonce_max_count 50\n";
47
48static
49char const *
50find_proxy_auth(char const *type)
51{
52 char const * proxy_auths[][2]= { {"basic","Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="},
53
54 {"digest", "Digest username=\"robertdig\", realm=\"Squid proxy-caching web server\", nonce=\"yy8rQXjEWwixXVBj\", uri=\"/images/bg8.gif\", response=\"f75a7d3edd48d93c681c75dc4fb58700\", qop=auth, nc=00000012, cnonce=\"e2216641961e228e\" "},
55 {"ntlm", "NTLM "},
56 {"negotiate", "Negotiate "}
57 };
58
59 for (unsigned count = 0; count < 4 ; ++count) {
60 if (strcasecmp(type, proxy_auths[count][0]) == 0)
61 return proxy_auths[count][1];
62 }
63
64 return nullptr;
65}
66
67static
69getConfig(char const *type_str)
70{
72 /* find a configuration for the scheme */
73 Auth::Config *scheme = Auth::Config::Find(type_str);
74
75 if (scheme == NULL) {
76 /* Create a configuration */
77 Auth::Scheme::Pointer theScheme = Auth::Scheme::Find(type_str);
78
79 if (theScheme == NULL) {
80 return nullptr;
81 //fatalf("Unknown authentication scheme '%s'.\n", type_str);
82 }
83
84 config.push_back(theScheme->createConfig());
85 scheme = config.back();
86 assert(scheme);
87 }
88
89 return scheme;
90}
91
92static
93void
94setup_scheme(Auth::Config *scheme, char const **params, unsigned param_count)
95{
97
98 for (unsigned position=0; position < param_count; ++position) {
99 char *param_str=xstrdup(params[position]);
100 strtok(param_str, w_space);
101 ConfigParser::SetCfgLine(strtok(nullptr, ""));
102 scheme->parse(scheme, config.size(), param_str);
103 }
104}
105
106static
107void
109{
110 static bool setup(false);
111
112 if (setup)
113 return;
114
115 Mem::Init();
116
118
119 char const *digest_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd",
120 "realm foo"
121 };
122
123 char const *basic_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd",
124 "realm foo"
125 };
126
127 char const *ntlm_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd"};
128
129 char const *negotiate_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd"};
130
131 struct _scheme_params {
132 char const *name;
133 char const **params;
134 unsigned paramlength;
135 }
136
137 params[]= { {"digest", digest_parms, 2},
138 {"basic", basic_parms, 2},
139 {"ntlm", ntlm_parms, 1},
140 {"negotiate", negotiate_parms, 1}
141 };
142
143 for (unsigned scheme=0; scheme < 4; ++scheme) {
144 Auth::Config *schemeConfig;
145 schemeConfig = getConfig(params[scheme].name);
146 if (schemeConfig != NULL)
147 setup_scheme(schemeConfig, params[scheme].params,
148 params[scheme].paramlength);
149 else
150 fprintf(stderr,"Skipping unknown authentication scheme '%s'.\n",
151 params[scheme].name);
152 }
153
154 authenticateInit(&config);
155
156 setup=true;
157}
158
159/* Auth::Config::CreateAuthUser works for all
160 * authentication types
161 */
162void
164{
165 Debug::Levels[29]=9;
167
168 for (Auth::Scheme::iterator i = Auth::Scheme::GetSchemes().begin(); i != Auth::Scheme::GetSchemes().end(); ++i) {
169 AuthUserRequest::Pointer authRequest = Auth::Config::CreateAuthUser(find_proxy_auth((*i)->type()));
170 CPPUNIT_ASSERT(authRequest != NULL);
171 }
172}
173
174#include <iostream>
175
176/* AuthUserRequest::scheme returns the correct scheme for all
177 * authentication types
178 */
179void
181{
182 Debug::Levels[29]=9;
184
185 for (Auth::Scheme::iterator i = Auth::Scheme::GetSchemes().begin(); i != Auth::Scheme::GetSchemes().end(); ++i) {
186 // create a user request
187 // check its scheme matches *i
188 AuthUserRequest::Pointer authRequest = Auth::Config::CreateAuthUser(find_proxy_auth((*i)->type()));
189 CPPUNIT_ASSERT_EQUAL(authRequest->scheme(), *i);
190 }
191}
192
193#if HAVE_AUTH_MODULE_BASIC
194#include "auth/basic/User.h"
196/* AuthBasicUserRequest::AuthBasicUserRequest works
197 */
198void
199TestAuthBasicUserRequest::construction()
200{
201 AuthBasicUserRequest();
202 AuthBasicUserRequest *temp=new AuthBasicUserRequest();
203 delete temp;
204}
205
206void
207TestAuthBasicUserRequest::username()
208{
209 AuthUserRequest::Pointer temp = new AuthBasicUserRequest();
210 Auth::Basic::User *basic_auth=new Auth::Basic::User(Auth::Config::Find("basic"));
211 basic_auth->username("John");
212 temp->user(basic_auth);
213 CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username()));
214}
215#endif /* HAVE_AUTH_MODULE_BASIC */
216
217#if HAVE_AUTH_MODULE_DIGEST
218#include "auth/digest/User.h"
220/* AuthDigestUserRequest::AuthDigestUserRequest works
221 */
222void
223TestAuthDigestUserRequest::construction()
224{
225 AuthDigestUserRequest();
226 AuthDigestUserRequest *temp=new AuthDigestUserRequest();
227 delete temp;
228}
229
230void
231TestAuthDigestUserRequest::username()
232{
233 AuthUserRequest::Pointer temp = new AuthDigestUserRequest();
234 Auth::Digest::User *duser=new Auth::Digest::User(Auth::Config::Find("digest"));
235 duser->username("John");
236 temp->user(duser);
237 CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username()));
238}
239#endif /* HAVE_AUTH_MODULE_DIGEST */
240
241#if HAVE_AUTH_MODULE_NTLM
242#include "auth/ntlm/User.h"
244/* AuthNTLMUserRequest::AuthNTLMUserRequest works
245 */
246void
247TestAuthNtlmUserRequest::construction()
248{
249 AuthNTLMUserRequest();
250 AuthNTLMUserRequest *temp=new AuthNTLMUserRequest();
251 delete temp;
252}
253
254void
255TestAuthNtlmUserRequest::username()
256{
257 AuthUserRequest::Pointer temp = new AuthNTLMUserRequest();
258 Auth::Ntlm::User *nuser=new Auth::Ntlm::User(Auth::Config::Find("ntlm"));
259 nuser->username("John");
260 temp->user(nuser);
261 CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username()));
262}
263#endif /* HAVE_AUTH_MODULE_NTLM */
264
265#if HAVE_AUTH_MODULE_NEGOTIATE
266#include "auth/negotiate/User.h"
268/* AuthNegotiateUserRequest::AuthNegotiateUserRequest works
269 */
270void
271TestAuthNegotiateUserRequest::construction()
272{
273 AuthNegotiateUserRequest();
274 AuthNegotiateUserRequest *temp=new AuthNegotiateUserRequest();
275 delete temp;
276}
277
278void
279TestAuthNegotiateUserRequest::username()
280{
281 AuthUserRequest::Pointer temp = new AuthNegotiateUserRequest();
282 Auth::Negotiate::User *nuser=new Auth::Negotiate::User(Auth::Config::Find("negotiate"));
283 nuser->username("John");
284 temp->user(nuser);
285 CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username()));
286}
287
288#endif /* HAVE_AUTH_MODULE_NEGOTIATE */
289
290int
291main(int argc, char *argv[])
292{
293 return TestProgram().run(argc, argv);
294}
295
296#endif /* USE_AUTH */
297
#define assert(EX)
Definition: assert.h:17
void authenticateInit(Auth::ConfigVector *config)
Definition: Gadgets.cc:70
static std::vector< Scheme::Pointer > & GetSchemes()
Definition: Scheme.cc:44
static Scheme::Pointer Find(const char *)
Definition: Scheme.cc:33
std::vector< Scheme::Pointer >::iterator iterator
Definition: Scheme.h:31
static void SetCfgLine(char *line)
Set the configuration file line to parse.
static int Levels[MAX_DEBUG_SECTIONS]
Definition: Stream.h:83
void create()
Definition: testAuth.cc:163
void instantiate()
Definition: testAuth.cc:38
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
#define w_space
std::vector< Auth::SchemeConfig * > ConfigVector
Definition: forward.h:24
Auth::Config TheConfig
Definition: Config.cc:15
void Init()
Definition: old_api.cc:425
#define xstrdup
int main(int argc, char *argv[])
Definition: testAuth.cc:291
static void fake_auth_setup()
Definition: testAuth.cc:108
CPPUNIT_TEST_SUITE_REGISTRATION(TestAuth)
static char const * find_proxy_auth(char const *type)
Definition: testAuth.cc:50
static void setup_scheme(Auth::Config *scheme, char const **params, unsigned param_count)
Definition: testAuth.cc:94
static Auth::Config * getConfig(char const *type_str)
Definition: testAuth.cc:69
char const * stub_config
Definition: testAuth.cc:41
#define NULL
Definition: types.h:145

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors