testAuth.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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
24 CPPUNIT_TEST_SUITE_REGISTRATION( testAuthBasicUserRequest );
25 #endif
26 #if HAVE_AUTH_MODULE_DIGEST
27 CPPUNIT_TEST_SUITE_REGISTRATION( testAuthDigestUserRequest );
28 #endif
29 #if HAVE_AUTH_MODULE_NTLM
30 CPPUNIT_TEST_SUITE_REGISTRATION( testAuthNTLMUserRequest );
31 #endif
32 #if HAVE_AUTH_MODULE_NEGOTIATE
33 CPPUNIT_TEST_SUITE_REGISTRATION( testAuthNegotiateUserRequest );
34 #endif
35 
36 /* Instantiate all auth framework types */
37 void
39 {}
40 
41 char 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 
48 static
49 char const *
50 find_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 NULL;
65 }
66 
67 static
69 getConfig(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 NULL;
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 
92 static
93 void
94 setup_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(NULL, ""));
102  scheme->parse(scheme, config.size(), param_str);
103  }
104 }
105 
106 static
107 void
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  */
162 void
164 {
165  Debug::Levels[29]=9;
166  fake_auth_setup();
167 
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  */
179 void
181 {
182  Debug::Levels[29]=9;
183  fake_auth_setup();
184 
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"
195 #include "auth/basic/UserRequest.h"
196 /* AuthBasicUserRequest::AuthBasicUserRequest works
197  */
198 void
199 testAuthBasicUserRequest::construction()
200 {
201  AuthBasicUserRequest();
202  AuthBasicUserRequest *temp=new AuthBasicUserRequest();
203  delete temp;
204 }
205 
206 void
207 testAuthBasicUserRequest::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"
219 #include "auth/digest/UserRequest.h"
220 /* AuthDigestUserRequest::AuthDigestUserRequest works
221  */
222 void
223 testAuthDigestUserRequest::construction()
224 {
225  AuthDigestUserRequest();
226  AuthDigestUserRequest *temp=new AuthDigestUserRequest();
227  delete temp;
228 }
229 
230 void
231 testAuthDigestUserRequest::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"
243 #include "auth/ntlm/UserRequest.h"
244 /* AuthNTLMUserRequest::AuthNTLMUserRequest works
245  */
246 void
247 testAuthNTLMUserRequest::construction()
248 {
249  AuthNTLMUserRequest();
250  AuthNTLMUserRequest *temp=new AuthNTLMUserRequest();
251  delete temp;
252 }
253 
254 void
255 testAuthNTLMUserRequest::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  */
270 void
271 testAuthNegotiateUserRequest::construction()
272 {
273  AuthNegotiateUserRequest();
274  AuthNegotiateUserRequest *temp=new AuthNegotiateUserRequest();
275  delete temp;
276 }
277 
278 void
279 testAuthNegotiateUserRequest::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 #endif /* USE_AUTH */
290 
#define assert(EX)
Definition: assert.h:17
std::vector< Scheme::Pointer >::iterator iterator
Definition: Scheme.h:31
static std::vector< Scheme::Pointer > & GetSchemes()
Definition: Scheme.cc:44
int type
Definition: errorpage.cc:79
CPPUNIT_TEST_SUITE_REGISTRATION(testAuth)
int i
Definition: membanger.c:49
#define xstrdup
std::vector< Auth::SchemeConfig * > ConfigVector
Definition: forward.h:23
#define w_space
void authenticateInit(Auth::ConfigVector *config)
Definition: Gadgets.cc:70
char const * stub_config
Definition: testAuth.cc:41
static void SetCfgLine(char *line)
Set the configuration file line to parse.
void create()
Definition: testAuth.cc:163
void Init()
Definition: old_api.cc:412
static Scheme::Pointer Find(const char *)
Definition: Scheme.cc:33
static char const * find_proxy_auth(char const *type)
Definition: testAuth.cc:50
Auth::Config TheConfig
Definition: stub_libauth.cc:21
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
static int Levels[MAX_DEBUG_SECTIONS]
Definition: Debug.h:81
void instantiate()
Definition: testAuth.cc:38
#define NULL
Definition: types.h:166
static void fake_auth_setup()
Definition: testAuth.cc:108

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors