]>
Commit | Line | Data |
---|---|---|
ec94e362 | 1 | #define SQUID_UNIT_TEST 1 |
f5691f9c | 2 | |
f5691f9c | 3 | #include "squid.h" |
aa625860 | 4 | #include "testAuth.h" |
3ad63615 | 5 | #include "auth/Gadgets.h" |
2d2b0bb7 AR |
6 | #include "auth/UserRequest.h" |
7 | #include "auth/Scheme.h" | |
8 | #include "auth/Config.h" | |
f5691f9c | 9 | #include "Mem.h" |
10 | ||
11 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuth ); | |
12 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuthConfig ); | |
13 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuthUserRequest ); | |
a33a428a | 14 | #if HAVE_AUTH_MODULE_BASIC |
f5691f9c | 15 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuthBasicUserRequest ); |
0ea5c60c | 16 | #endif |
a33a428a | 17 | #if HAVE_AUTH_MODULE_DIGEST |
f5691f9c | 18 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuthDigestUserRequest ); |
0ea5c60c | 19 | #endif |
a33a428a | 20 | #if HAVE_AUTH_MODULE_NTLM |
f5691f9c | 21 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuthNTLMUserRequest ); |
0ea5c60c | 22 | #endif |
a33a428a | 23 | #if HAVE_AUTH_MODULE_NEGOTIATE |
f46fe759 | 24 | CPPUNIT_TEST_SUITE_REGISTRATION( testAuthNegotiateUserRequest ); |
0ea5c60c | 25 | #endif |
f5691f9c | 26 | |
27 | /* Instantiate all auth framework types */ | |
28 | void | |
29 | testAuth::instantiate() | |
30 | {} | |
31 | ||
32 | char const * stub_config="auth_param digest program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd\n" | |
33 | "auth_param digest children 5\n" | |
34 | "auth_param digest realm Squid proxy-caching web server\n" | |
35 | "auth_param digest nonce_garbage_interval 5 minutes\n" | |
36 | "auth_param digest nonce_max_duration 30 minutes\n" | |
37 | "auth_param digest nonce_max_count 50\n"; | |
38 | ||
39 | static | |
40 | char const * | |
41 | find_proxy_auth(char const *type) | |
42 | { | |
43 | char const * proxy_auths[][2]= { {"basic","Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="}, | |
44 | ||
26ac0430 AJ |
45 | {"digest", "Digest username=\"robertdig\", realm=\"Squid proxy-caching web server\", nonce=\"yy8rQXjEWwixXVBj\", uri=\"/images/bg8.gif\", response=\"f75a7d3edd48d93c681c75dc4fb58700\", qop=auth, nc=00000012, cnonce=\"e2216641961e228e\" "}, |
46 | {"ntlm", "NTLM "}, | |
47 | {"negotiate", "Negotiate "} | |
48 | }; | |
f5691f9c | 49 | |
f46fe759 | 50 | for (unsigned count = 0; count < 4 ; count++) { |
f5691f9c | 51 | if (strcasecmp(type, proxy_auths[count][0]) == 0) |
52 | return proxy_auths[count][1]; | |
53 | } | |
54 | ||
55 | return NULL; | |
56 | } | |
57 | ||
58 | static | |
59 | AuthConfig * | |
60 | getConfig(char const *type_str) | |
61 | { | |
62 | Vector<AuthConfig *> &config = Config.authConfiguration; | |
63 | /* find a configuration for the scheme */ | |
64 | AuthConfig *scheme = AuthConfig::Find (type_str); | |
65 | ||
66 | if (scheme == NULL) { | |
67 | /* Create a configuration */ | |
68 | AuthScheme *theScheme; | |
69 | ||
70 | if ((theScheme = AuthScheme::Find(type_str)) == NULL) { | |
0ea5c60c FC |
71 | return NULL; |
72 | //fatalf("Unknown authentication scheme '%s'.\n", type_str); | |
f5691f9c | 73 | } |
74 | ||
75 | config.push_back(theScheme->createConfig()); | |
76 | scheme = config.back(); | |
a33a428a | 77 | assert(scheme); |
f5691f9c | 78 | } |
79 | ||
80 | return scheme; | |
81 | } | |
82 | ||
83 | static | |
84 | void | |
85 | setup_scheme(AuthConfig *scheme, char const **params, unsigned param_count) | |
86 | { | |
87 | Vector<AuthConfig *> &config = Config.authConfiguration; | |
88 | ||
89 | for (unsigned position=0; position < param_count; position++) { | |
90 | char *param_str=xstrdup(params[position]); | |
91 | strtok(param_str, w_space); | |
92 | scheme->parse(scheme, config.size(), param_str); | |
93 | } | |
94 | } | |
95 | ||
96 | static | |
97 | void | |
98 | fake_auth_setup() | |
99 | { | |
100 | static bool setup(false); | |
101 | ||
102 | if (setup) | |
103 | return; | |
104 | ||
105 | Mem::Init(); | |
106 | ||
f5691f9c | 107 | Vector<AuthConfig *> &config = Config.authConfiguration; |
108 | ||
109 | char const *digest_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd", | |
110 | "realm foo" | |
111 | }; | |
112 | ||
113 | char const *basic_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd", | |
114 | "realm foo" | |
115 | }; | |
116 | ||
117 | char const *ntlm_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd"}; | |
118 | ||
f46fe759 | 119 | char const *negotiate_parms[]= {"program /home/robertc/install/squid/libexec/digest_pw_auth /home/robertc/install/squid/etc/digest.pwd"}; |
120 | ||
f5691f9c | 121 | struct _scheme_params { |
122 | char const *name; | |
123 | char const **params; | |
124 | unsigned paramlength; | |
125 | } | |
126 | ||
127 | params[]={ {"digest", digest_parms, 2}, | |
26ac0430 AJ |
128 | {"basic", basic_parms, 2}, |
129 | {"ntlm", ntlm_parms, 1}, | |
130 | {"negotiate", negotiate_parms, 1} | |
131 | }; | |
f5691f9c | 132 | |
0ea5c60c FC |
133 | for (unsigned scheme=0; scheme < 4; scheme++) { |
134 | AuthConfig *schemeConfig; | |
135 | schemeConfig = getConfig(params[scheme].name); | |
136 | if (schemeConfig != NULL) | |
9e008dda | 137 | setup_scheme(schemeConfig, params[scheme].params, |
af6a12ee | 138 | params[scheme].paramlength); |
9e008dda AJ |
139 | else |
140 | fprintf(stderr,"Skipping unknown authentication scheme '%s'.\n", | |
141 | params[scheme].name); | |
0ea5c60c | 142 | } |
f5691f9c | 143 | |
144 | authenticateInit(&config); | |
145 | ||
146 | setup=true; | |
147 | } | |
148 | ||
149 | /* AuthConfig::CreateAuthUser works for all | |
150 | * authentication types | |
151 | */ | |
152 | void | |
153 | testAuthConfig::create() | |
154 | { | |
155 | Debug::Levels[29]=9; | |
156 | fake_auth_setup(); | |
157 | ||
158 | for (AuthScheme::const_iterator i = AuthScheme::Schemes().begin(); i != AuthScheme::Schemes().end(); ++i) { | |
a33a428a | 159 | AuthUserRequest::Pointer authRequest = AuthConfig::CreateAuthUser(find_proxy_auth((*i)->type())); |
f5691f9c | 160 | CPPUNIT_ASSERT(authRequest != NULL); |
161 | } | |
162 | } | |
163 | ||
27e059d4 | 164 | #if HAVE_IOSTREAM |
f5691f9c | 165 | #include <iostream> |
27e059d4 | 166 | #endif |
f5691f9c | 167 | |
168 | /* AuthUserRequest::scheme returns the correct scheme for all | |
169 | * authentication types | |
170 | */ | |
171 | void | |
172 | testAuthUserRequest::scheme() | |
173 | { | |
174 | Debug::Levels[29]=9; | |
175 | fake_auth_setup(); | |
176 | ||
177 | for (AuthScheme::const_iterator i = AuthScheme::Schemes().begin(); i != AuthScheme::Schemes().end(); ++i) { | |
178 | // create a user request | |
179 | // check its scheme matches *i | |
a33a428a | 180 | AuthUserRequest::Pointer authRequest = AuthConfig::CreateAuthUser(find_proxy_auth((*i)->type())); |
f5691f9c | 181 | CPPUNIT_ASSERT_EQUAL(authRequest->scheme(), *i); |
182 | } | |
183 | } | |
184 | ||
a33a428a | 185 | #if HAVE_AUTH_MODULE_BASIC |
f5691f9c | 186 | #include "auth/basic/auth_basic.h" |
187 | /* AuthBasicUserRequest::AuthBasicUserRequest works | |
188 | */ | |
189 | void | |
190 | testAuthBasicUserRequest::construction() | |
191 | { | |
192 | AuthBasicUserRequest(); | |
193 | AuthBasicUserRequest *temp=new AuthBasicUserRequest(); | |
194 | delete temp; | |
195 | } | |
196 | ||
197 | void | |
198 | testAuthBasicUserRequest::username() | |
199 | { | |
a33a428a | 200 | AuthUserRequest::Pointer temp = new AuthBasicUserRequest(); |
f5691f9c | 201 | BasicUser *basic_auth=new BasicUser(AuthConfig::Find("basic")); |
202 | basic_auth->username("John"); | |
203 | temp->user(basic_auth); | |
204 | basic_auth->addRequest(temp); | |
205 | CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username())); | |
f5691f9c | 206 | } |
0ea5c60c | 207 | #endif /* HAVE_AUTH_MODULE_BASIC */ |
f5691f9c | 208 | |
a33a428a | 209 | #if HAVE_AUTH_MODULE_DIGEST |
f5691f9c | 210 | #include "auth/digest/auth_digest.h" |
211 | /* AuthDigestUserRequest::AuthDigestUserRequest works | |
212 | */ | |
213 | void | |
214 | testAuthDigestUserRequest::construction() | |
215 | { | |
216 | AuthDigestUserRequest(); | |
217 | AuthDigestUserRequest *temp=new AuthDigestUserRequest(); | |
218 | delete temp; | |
219 | } | |
220 | ||
221 | void | |
222 | testAuthDigestUserRequest::username() | |
223 | { | |
a33a428a AJ |
224 | AuthUserRequest::Pointer temp = new AuthDigestUserRequest(); |
225 | DigestUser *duser=new DigestUser(AuthConfig::Find("digest")); | |
226 | duser->username("John"); | |
227 | temp->user(duser); | |
228 | duser->addRequest(temp); | |
f5691f9c | 229 | CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username())); |
f5691f9c | 230 | } |
0ea5c60c | 231 | #endif /* HAVE_AUTH_MODULE_DIGEST */ |
f5691f9c | 232 | |
a33a428a | 233 | #if HAVE_AUTH_MODULE_NTLM |
f5691f9c | 234 | #include "auth/ntlm/auth_ntlm.h" |
235 | /* AuthNTLMUserRequest::AuthNTLMUserRequest works | |
236 | */ | |
237 | void | |
238 | testAuthNTLMUserRequest::construction() | |
239 | { | |
240 | AuthNTLMUserRequest(); | |
241 | AuthNTLMUserRequest *temp=new AuthNTLMUserRequest(); | |
242 | delete temp; | |
243 | } | |
244 | ||
245 | void | |
246 | testAuthNTLMUserRequest::username() | |
247 | { | |
a33a428a AJ |
248 | AuthUserRequest::Pointer temp = new AuthNTLMUserRequest(); |
249 | NTLMUser *nuser=new NTLMUser(AuthConfig::Find("ntlm")); | |
250 | nuser->username("John"); | |
251 | temp->user(nuser); | |
252 | nuser->addRequest(temp); | |
f5691f9c | 253 | CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username())); |
f5691f9c | 254 | } |
0ea5c60c | 255 | #endif /* HAVE_AUTH_MODULE_NTLM */ |
f46fe759 | 256 | |
a33a428a | 257 | #if HAVE_AUTH_MODULE_NEGOTIATE |
f46fe759 | 258 | #include "auth/negotiate/auth_negotiate.h" |
259 | /* AuthNegotiateUserRequest::AuthNegotiateUserRequest works | |
260 | */ | |
261 | void | |
262 | testAuthNegotiateUserRequest::construction() | |
263 | { | |
264 | AuthNegotiateUserRequest(); | |
265 | AuthNegotiateUserRequest *temp=new AuthNegotiateUserRequest(); | |
266 | delete temp; | |
267 | } | |
268 | ||
269 | void | |
270 | testAuthNegotiateUserRequest::username() | |
271 | { | |
a33a428a AJ |
272 | AuthUserRequest::Pointer temp = new AuthNegotiateUserRequest(); |
273 | NegotiateUser *nuser=new NegotiateUser(AuthConfig::Find("negotiate")); | |
274 | nuser->username("John"); | |
275 | temp->user(nuser); | |
276 | nuser->addRequest(temp); | |
f46fe759 | 277 | CPPUNIT_ASSERT_EQUAL(0, strcmp("John", temp->username())); |
f46fe759 | 278 | } |
0ea5c60c FC |
279 | |
280 | #endif /* HAVE_AUTH_MODULE_NEGOTIATE */ |