~ubuntu-branches/ubuntu/lucid/gallery2/lucid

« back to all changes in this revision

Viewing changes to modules/httpauth/test/phpunit/HttpAuthHelperTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
GalleryCoreApi::requireOnce('modules/httpauth/classes/HttpAuthHelper.class');
22
 
 
23
 
/**
24
 
 * HTTP auth helper tests.
25
 
 * @package HttpAuth
26
 
 * @subpackage PHPUnit
27
 
 * @author Jack Bates <ms419@freezone.co.uk>
28
 
 * @version $Revision: 15979 $
29
 
 */
30
 
class HttpAuthHelperTest extends GalleryTestCase {
31
 
 
32
 
    function HttpAuthHelperTest($methodName) {
33
 
        $this->GalleryTestCase($methodName);
34
 
    }
35
 
 
36
 
    function setUp() {
37
 
        parent::setUp();
38
 
        global $gallery;
39
 
        $urlGenerator =& $gallery->getUrlGenerator();
40
 
 
41
 
        /* Mock activate the rewrite module */
42
 
        $moduleStatus = array('rewrite' => array('active' => true));
43
 
        GalleryDataCache::put('GalleryPluginHelper::fetchPluginList(module)', $moduleStatus);
44
 
 
45
 
        /* Mock preGallery rewrite parser.  Mock activate HTTP auth rewrite rules. */
46
 
        $this->_parserType =& HttpAuthHelperTestRewriteApi::_getParserType();
47
 
        $this->_parserType = 'preGallery';
48
 
        $this->_activeRules =& HttpAuthHelperTestRewriteApi::_fetchActiveRulesForModule('httpauth');
49
 
        $this->_activeRules = array('authorization');
50
 
        $this->_markFactoryForCleanup();
51
 
        $this->_registerFactoryImplementationForTest(
52
 
            'RewriteApi', 'HttpAuthHelperTestRewriteApi', 'HttpAuthHelperTestRewriteApi',
53
 
            'modules/httpauth/test/phpunit/HttpAuthHelperTest.class', 'test');
54
 
 
55
 
        /* Define HTTP auth works request */
56
 
        $this->_httpAuthWorksRequest = 'GET ' . $urlGenerator->generateUrl(
57
 
                array('view' => 'httpauth.HttpAuthWorks'),
58
 
                array('forceServerRelativeUrl' => true,
59
 
                      'htmlEntities' => false)) . " HTTP/1.0\r\n"
60
 
            . 'Host: ' . $urlGenerator->getHostName() . "\r\n"
61
 
            . 'Authorization: Basic ' . base64_encode('USERNAME:PASSWORD') . "\r\n"
62
 
            . "\r\n";
63
 
 
64
 
        /* Define success and failure responses */
65
 
        $this->_accessAuthorizationResponse = implode("\r\n", array(
66
 
                'HTTP/1.1 200 OK',
67
 
                'Date: Mon, 24 Nov 2003 05:40:03 GMT',
68
 
                'Server: Apache/1.3.28 (Unix)',
69
 
                'Content-Length: 26',
70
 
                'Connection: close',
71
 
                'Content-Type: text/html',
72
 
                '',
73
 
                ''))
74
 
                .  "Basic\nUSERNAME\nPASSWORD";
75
 
        $this->_missingAuthorizationResponse = implode("\r\n", array(
76
 
                'HTTP/1.1 200 OK',
77
 
                'Date: Mon, 24 Nov 2003 05:40:03 GMT',
78
 
                'Server: Apache/1.3.28 (Unix)',
79
 
                'Content-Length: 26',
80
 
                'Connection: close',
81
 
                'Content-Type: text/html',
82
 
                '',
83
 
                ''))
84
 
                . "\n\n\n\n";
85
 
 
86
 
        /* Use HttpAuthHelperTestPlatform */
87
 
        $this->_platform = new HttpAuthHelperTestPlatform();
88
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
89
 
            $this->_accessAuthorizationResponse;
90
 
        $gallery->setPlatform($this->_platform);
91
 
 
92
 
        $ret = $this->_markPluginParametersForCleanup('module', 'httpauth');
93
 
        if ($ret) {
94
 
            return $this->failWithStatus($ret);
95
 
        }
96
 
 
97
 
        /* Configure plugin */
98
 
        foreach (array('httpAuthPlugin' => true, 'regexAuthPlugin' => true,
99
 
                       'authtypePattern' => '//', 'usernameReplace' => '$2',
100
 
                       'usernamePattern' => '/^(.+\\\\)?([^\\\\@]+)(@.+)?$/',
101
 
                       'serverAuthPlugin' => false, 'useGlobally' => true,
102
 
                       'authName' => 'TEST_AUTH_NAME') as $key => $value) {
103
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', $key, $value);
104
 
            if ($ret) {
105
 
                print $ret->getAsHtml();
106
 
                return $this->failWithStatus($ret);
107
 
            }
108
 
        }
109
 
 
110
 
        /* Create test user */
111
 
        list ($ret, $this->_user) = $this->_createRandomUser();
112
 
        if ($ret) {
113
 
            print $ret->getAsHtml();
114
 
            return $this->failWithStatus($ret);
115
 
        }
116
 
        $this->_markForCleanup($this->_user);
117
 
 
118
 
        list ($ret, $this->_authInterface) =
119
 
                GalleryCoreApi::newFactoryInstance('HttpAuthInterface_1_0');
120
 
        if ($ret) {
121
 
            print $ret->getAsHtml();
122
 
            return $this->failWithStatus($ret);
123
 
        }
124
 
        $this->assert(isset($this->_authInterface), 'Could not instantiate auth interface.');
125
 
 
126
 
        /* Use HttpAuthModuleTestPhpVm */
127
 
        $this->_phpVm = new HttpAuthHelperTestPhpVm();
128
 
        $gallery->_phpVm =& $this->_phpVm;
129
 
 
130
 
        /* Clear response headers */
131
 
        $this->_headers =& GalleryUtilities::_getResponseHeaders();
132
 
        $this->_headers = array();
133
 
 
134
 
        $this->_saveSession = $gallery->_session;
135
 
 
136
 
        $_SERVER['AUTH_TYPE'] = $_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = null;
137
 
        GalleryUtilities::removeRequestVariable('authorization');
138
 
        $_SERVER['HTTP_AUTHORIZATION'] = null;
139
 
    }
140
 
 
141
 
    function tearDown() {
142
 
        global $gallery;
143
 
        GalleryDataCache::reset();
144
 
        $gallery->_session = $this->_saveSession;
145
 
 
146
 
        parent::tearDown();
147
 
 
148
 
        if (!empty($this->_resetFactory)) {    
149
 
            /* After the plugin parameters have been reset, reset the factory as well */
150
 
            list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'httpauth');
151
 
            if ($ret) {
152
 
                $this->failWithStatus($ret);
153
 
            } else {
154
 
                $ret = GalleryCoreApi::unregisterFactoryImplementationsByModuleId('httpauth');
155
 
                if ($ret) {
156
 
                    $this->failWithStatus($ret);
157
 
                }
158
 
                $ret = $module->performFactoryRegistrations();
159
 
                if ($ret) {
160
 
                    $this->failWithStatus($ret);
161
 
                }
162
 
            }
163
 
 
164
 
            $storage =& $gallery->getStorage();
165
 
            $ret = $storage->commitTransaction();
166
 
            if ($ret) {
167
 
                $this->failWithStatus($ret);
168
 
            }
169
 
        }
170
 
    }
171
 
 
172
 
    function testCheckConfiguration() {
173
 
        list ($ret, $code) = HttpAuthHelper::checkConfiguration();
174
 
        if ($ret) {
175
 
            return $this->failWithStatus($ret);
176
 
        }
177
 
 
178
 
        $this->assertEquals(0, $code, 'unexpected error code');
179
 
    }
180
 
 
181
 
    function testCheckConfigurationRewriteModuleDisabled() {
182
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
183
 
            $this->_missingAuthorizationResponse;
184
 
        $moduleStatus = array();
185
 
        GalleryDataCache::put('GalleryPluginHelper::fetchPluginList(module)', $moduleStatus);
186
 
 
187
 
        list ($ret, $code) = HttpAuthHelper::checkConfiguration();
188
 
        if ($ret) {
189
 
            return $this->failWithStatus($ret);
190
 
        }
191
 
 
192
 
        $this->assertEquals(HTTPAUTH_STATUS_MISSING_AUTHORIZATION
193
 
                            | HTTPAUTH_STATUS_REWRITE_MODULE_DISABLED, $code);
194
 
    }
195
 
 
196
 
    function testCheckConfigurationBadRewriteParser() {
197
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
198
 
            $this->_missingAuthorizationResponse;
199
 
        $this->_parserType = 'bogus';
200
 
 
201
 
        list ($ret, $code) = HttpAuthHelper::checkConfiguration();
202
 
        if ($ret) {
203
 
            return $this->failWithStatus($ret);
204
 
        }
205
 
 
206
 
        $this->assertEquals(HTTPAUTH_STATUS_MISSING_AUTHORIZATION
207
 
                            | HTTPAUTH_STATUS_BAD_REWRITE_PARSER, $code);
208
 
    }
209
 
 
210
 
    function testCheckConfigurationAuthorizationRuleDisabled() {
211
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
212
 
            $this->_missingAuthorizationResponse;
213
 
        $this->_activeRules = array();
214
 
 
215
 
        list ($ret, $code) = HttpAuthHelper::checkConfiguration();
216
 
        if ($ret) {
217
 
            return $this->failWithStatus($ret);
218
 
        }
219
 
 
220
 
        $this->assertEquals(HTTPAUTH_STATUS_MISSING_AUTHORIZATION
221
 
                            | HTTPAUTH_STATUS_AUTHORIZATION_RULE_DISABLED, $code);
222
 
    }
223
 
 
224
 
    function testCheckConfigurationErrorUnknown() {
225
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
226
 
            $this->_missingAuthorizationResponse;
227
 
 
228
 
        list ($ret, $code) = HttpAuthHelper::checkConfiguration();
229
 
        if ($ret) {
230
 
            return $this->failWithStatus($ret);
231
 
        }
232
 
 
233
 
        $this->assertEquals(HTTPAUTH_STATUS_MISSING_AUTHORIZATION
234
 
                            | HTTPAUTH_STATUS_ERROR_UNKNOWN, $code);
235
 
    }
236
 
 
237
 
    function testCheckConfigurationHttpAuthWorksIgnoresCauses() {
238
 
        $moduleStatus = array();
239
 
        GalleryDataCache::put('GalleryPluginHelper::fetchPluginList(module)', $moduleStatus);
240
 
        $this->_parserType = 'bogus';
241
 
        $this->_activeRules = array();
242
 
 
243
 
        list ($ret, $code) = HttpAuthHelper::checkConfiguration();
244
 
        if ($ret) {
245
 
            return $this->failWithStatus($ret);
246
 
        }
247
 
 
248
 
        $this->assert(!$code);
249
 
    }
250
 
 
251
 
    function testCheckHttpAuth() {
252
 
        list ($ret, $success) = HttpAuthHelper::checkHttpAuth();
253
 
        if ($ret) {
254
 
            return $this->failWithStatus($ret);
255
 
        }
256
 
 
257
 
        $this->assert($success);
258
 
    }
259
 
 
260
 
    function testCheckHttpAuthMissingAuthorization() {
261
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
262
 
            $this->_missingAuthorizationResponse;
263
 
 
264
 
        list ($ret, $success) = HttpAuthHelper::checkHttpAuth();
265
 
        if ($ret) {
266
 
            return $this->failWithStatus($ret);
267
 
        }
268
 
 
269
 
        $this->assert(!$success);
270
 
    }
271
 
 
272
 
    function testCheckHttpAuthPluginDisabled() {
273
 
        $this->_platform->_messages[$this->_httpAuthWorksRequest] =
274
 
            $this->_missingAuthorizationResponse;
275
 
        $ret = GalleryCoreApi::removePluginParameter('module', 'httpauth', 'httpAuthPlugin');
276
 
        if ($ret) {
277
 
            return $this->failWithStatus($ret);
278
 
        }
279
 
 
280
 
        list ($ret, $success) = HttpAuthHelper::checkHttpAuth();
281
 
        if ($ret) {
282
 
            return $this->failWithStatus($ret);
283
 
        }
284
 
 
285
 
        $this->assert($success);
286
 
    }
287
 
 
288
 
    function testGetHttpAuth() {
289
 
        $_SERVER['AUTH_TYPE'] = 'AUTHTYPE';
290
 
        $_SERVER['PHP_AUTH_USER'] = 'USERNAME';
291
 
        $_SERVER['PHP_AUTH_PW'] = 'PASSWORD';
292
 
 
293
 
        list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth();
294
 
        $this->assertEquals('AUTHTYPE', $authtype);
295
 
        $this->assertEquals('USERNAME', $username);
296
 
        $this->assertEquals('PASSWORD', $password);
297
 
    }
298
 
 
299
 
    function testGetHttpAuthFromRequestVariable() {
300
 
        GalleryUtilities::putRequestVariable(
301
 
            'authorization', 'AUTHTYPE ' . base64_encode('USERNAME:PASSWORD'));
302
 
 
303
 
        $_SERVER['HTTP_AUTHORIZATION'] = 'OTHER ' . base64_encode('NOUSER:NOPASS');
304
 
 
305
 
        list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth();
306
 
        $this->assertEquals('AUTHTYPE', $authtype);
307
 
        $this->assertEquals('USERNAME', $username);
308
 
        $this->assertEquals('PASSWORD', $password);
309
 
    }
310
 
 
311
 
    function testGetHttpAuthFromHttpAuthorizationHeader() {
312
 
        $_SERVER['HTTP_AUTHORIZATION'] = 'AUTHTYPE ' . base64_encode('USERNAME:PASSWORD');
313
 
 
314
 
        list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth();
315
 
        $this->assertEquals('AUTHTYPE', $authtype);
316
 
        $this->assertEquals('USERNAME', $username);
317
 
        $this->assertEquals('PASSWORD', $password);
318
 
    }
319
 
 
320
 
    function testGetHttpAuthWithoutAuthTypeDefaultsToBasic() {
321
 
        $_SERVER['PHP_AUTH_USER'] = 'USERNAME';
322
 
        $_SERVER['PHP_AUTH_PW'] = 'PASSWORD';
323
 
 
324
 
        list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth();
325
 
        $this->assertEquals('Basic', $authtype);
326
 
        $this->assertEquals('USERNAME', $username);
327
 
        $this->assertEquals('PASSWORD', $password);
328
 
    }
329
 
 
330
 
    function testGetHttpAuthWithoutAuthAndUserDoesNotDefault() {
331
 
        $_SERVER['PHP_AUTH_USER'] = '';
332
 
        $_SERVER['PHP_AUTH_PW'] = 'PASSWORD';
333
 
 
334
 
        list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth();
335
 
        $this->assertEquals(null, $authtype, 'auth type');
336
 
        $this->assertEquals('', $username, 'user name');
337
 
        $this->assertEquals('PASSWORD', $password, 'password');
338
 
    }
339
 
 
340
 
    function testGetUser() {
341
 
        list ($ret, $user) = HttpAuthHelper::getUser('Test', $this->_user->getUserName());
342
 
        if ($ret) {
343
 
            return $this->failWithStatus($ret);
344
 
        }
345
 
 
346
 
        $this->assertEquals($this->_user, $user);
347
 
    }
348
 
 
349
 
    function testGetUserInvalidUser() {
350
 
        list ($ret, $user) = HttpAuthHelper::getUser('Test', 'bogusUser-' . rand());
351
 
        if ($ret) {
352
 
            return $this->failWithStatus($ret);
353
 
        }
354
 
 
355
 
        $this->assertEquals(null, $user);
356
 
    }
357
 
 
358
 
    function testGetUserDisabledUser() {
359
 
        $ret = $this->_disableUserName($this->_user->getUserName());
360
 
        if ($ret) {
361
 
            return $this->failWithStatus($ret);
362
 
        }
363
 
 
364
 
        list ($ret, $user) = HttpAuthHelper::getUser('Test', $this->_user->getUserName());
365
 
        if ($ret) {
366
 
            return $this->failWithStatus($ret);
367
 
        }
368
 
 
369
 
        $this->assertEquals(null, $user);
370
 
    }
371
 
 
372
 
    function testGetUserAuthtypePattern() {
373
 
        $ret = GalleryCoreApi::setPluginParameter(
374
 
            'module', 'httpauth', 'regexAuthPlugin', true);
375
 
        if ($ret) {
376
 
            return $this->failWithStatus($ret);
377
 
        }
378
 
 
379
 
        $ret = GalleryCoreApi::setPluginParameter(
380
 
            'module', 'httpauth', 'authtypePattern', '/^Bogus$/');
381
 
        if ($ret) {
382
 
            return $this->failWithStatus($ret);
383
 
        }
384
 
 
385
 
        list ($ret, $user) = HttpAuthHelper::getUser('Test', $this->_user->getUserName());
386
 
        if ($ret) {
387
 
            return $this->failWithStatus($ret);
388
 
        }
389
 
 
390
 
        $this->assertEquals(null, $user);
391
 
    }
392
 
 
393
 
    function testGetUserUsernamePattern() {
394
 
        $ret = GalleryCoreApi::setPluginParameter(
395
 
            'module', 'httpauth', 'usernamePattern', '/^bogusUser-' . rand() . '$/');
396
 
        if ($ret) {
397
 
            return $this->failWithStatus($ret);
398
 
        }
399
 
 
400
 
        list ($ret, $user) = HttpAuthHelper::getUser('Test', $this->_user->getUserName());
401
 
        if ($ret) {
402
 
            return $this->failWithStatus($ret);
403
 
        }
404
 
 
405
 
        $this->assertEquals(null, $user);
406
 
    }
407
 
 
408
 
    function testGetUserUsernameReplace() {
409
 
        list ($ret, $user) =
410
 
            HttpAuthHelper::getUser('Test', $this->_user->getUserName() . '@REALM.TLD');
411
 
        if ($ret) {
412
 
            return $this->failWithStatus($ret);
413
 
        }
414
 
 
415
 
        $this->assertEquals($this->_user, $user);
416
 
    }
417
 
 
418
 
    function testGetUserIgnoresUseGloballyFlag() {
419
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', 'useGlobally', false);
420
 
        if ($ret) {
421
 
            return $this->failWithStatus($ret);
422
 
        }
423
 
 
424
 
        list ($ret, $user) = HttpAuthHelper::getUser('Test', $this->_user->getUserName());
425
 
        if ($ret) {
426
 
            return $this->failWithStatus($ret);
427
 
        }
428
 
        $this->assertEquals($this->_user, $user);
429
 
    }
430
 
 
431
 
    function testAddHttpAuthToUrl() {
432
 
        $url = 'http://www.example.com/gallery2/main.php?foo=bar&amp;view=baz#some';
433
 
        $this->assertEquals('http://FAKEUSER:FAKEPASS@www.example.com/gallery2/' .
434
 
                                'main.php?foo=bar&amp;view=baz#some',
435
 
                            HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'),
436
 
                            'URL with all components');
437
 
 
438
 
        $url = 'webdav://www.example.com/gallery2/main.php?foo=bar&view=baz#some';
439
 
        $this->assertEquals('webdav://FAKEUSER:FAKEPASS@www.example.com/gallery2/' .
440
 
                                'main.php?foo=bar&view=baz#some',
441
 
                            HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'),
442
 
                            'non-standard protocol, no HTML entities');
443
 
 
444
 
        $url = 'https://www.example.com/gallery2/main.php';
445
 
        $this->assertEquals('https://FAKEUSER:FAKEPASS@www.example.com/gallery2/main.php',
446
 
                            HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'),
447
 
                            'no query string, anchor');
448
 
 
449
 
        $url = 'http://OLDUSER:OLDPASS@example.com/gallery2/main.php?foo=bar&amp;view=baz#some';
450
 
        $this->assertEquals('http://FAKEUSER:FAKEPASS@example.com/gallery2/' .
451
 
                                'main.php?foo=bar&amp;view=baz#some',
452
 
                            HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'),
453
 
                            'URL with existing user:pass');     
454
 
    }
455
 
 
456
 
    function testStripHttpAuthFromUrl() {
457
 
        $url = 'http://OLDUSER:OLDPASS@example.com/gallery2/main.php?foo=bar&amp;view=baz#some';
458
 
        $this->assertEquals('http://example.com/gallery2/main.php?foo=bar&amp;view=baz#some',
459
 
                            HttpAuthHelper::stripHttpAuthFromUrl($url),
460
 
                            'URL with user:pass');
461
 
 
462
 
        $url = 'http://www.example.com/gallery2/main.php?foo=bar&amp;view=baz#some';
463
 
        $this->assertEquals('http://www.example.com/gallery2/main.php?foo=bar&amp;view=baz#some',
464
 
                            HttpAuthHelper::stripHttpAuthFromUrl($url),
465
 
                            'URL without user:pass');
466
 
 
467
 
        $url = 'webdav://OLDUSER:OLDPASS@example.com/gallery2/main.php?foo=bar&view=baz';
468
 
        $this->assertEquals('webdav://example.com/gallery2/main.php?foo=bar&view=baz',
469
 
                            HttpAuthHelper::stripHttpAuthFromUrl($url),
470
 
                            'URL without HTML entities, anchor, other protocol');
471
 
    }
472
 
 
473
 
    function testGetConfiguration() {
474
 
        list ($ret, $authPluginEnabled, $serverPluginEnabled, $usedGlobally) = 
475
 
                $this->_authInterface->getConfiguration();
476
 
        if ($ret) {
477
 
            return $this->failWithStatus($ret);
478
 
        }
479
 
 
480
 
        $this->assertEquals(array(true, false, true),
481
 
                            array($authPluginEnabled, $serverPluginEnabled, $usedGlobally));
482
 
    }
483
 
 
484
 
    function testSetConfiguration() {
485
 
        $this->_resetFactory = true;
486
 
        $ret = $this->_authInterface->setConfiguration(false, true, false);
487
 
        if ($ret) {
488
 
            return $this->failWithStatus($ret);
489
 
        }
490
 
 
491
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'httpauth');
492
 
        if ($ret) {
493
 
            return $this->failWithStatus($ret);
494
 
        }
495
 
 
496
 
        $this->assert(!(bool)$params['httpAuthPlugin'], 'wrong httpAuthPlugin value');
497
 
        $this->assert((bool)$params['serverAuthPlugin'], 'wrong serverAuthPlugin value');
498
 
        $this->assert(!(bool)$params['useGlobally'], 'wrong useGlobally value');
499
 
        list ($ret, $authPlugins) =
500
 
                GalleryCoreApi::getAllFactoryImplementationIds('GalleryAuthPlugin');
501
 
        if ($ret) {
502
 
            return $this->failWithStatus($ret);
503
 
        }
504
 
        $this->assert(!isset($authPlugins['HttpAuthPlugin']), 'HttpAuthPlugin still registerd');
505
 
        $this->assert(isset($authPlugins['ServerAuthPlugin']), 'ServerAuthPlugin not registered');
506
 
    }
507
 
 
508
 
    function testSetConfigurationDefaults() {
509
 
        $this->_resetFactory = true;
510
 
        $ret = $this->_authInterface->setConfiguration(true);
511
 
        if ($ret) {
512
 
            return $this->failWithStatus($ret);
513
 
        }
514
 
 
515
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'httpauth');
516
 
        if ($ret) {
517
 
            return $this->failWithStatus($ret);
518
 
        }
519
 
 
520
 
        $this->assert((bool)$params['httpAuthPlugin'], 'wrong httpAuthPlugin value');
521
 
        $this->assert(!(bool)$params['serverAuthPlugin'], 'wrong serverAuthPlugin value');
522
 
        $this->assert(!(bool)$params['useGlobally'], 'wrong useGlobally value');
523
 
    }
524
 
 
525
 
    function testRequestAuthentication() {
526
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', 'useGlobally', false);
527
 
        if ($ret) {
528
 
            return $this->failWithStatus($ret);
529
 
        }
530
 
 
531
 
        $ret = $this->_authInterface->requestAuthentication();
532
 
        if ($ret) {
533
 
            return $this->failWithStatus($ret);
534
 
        }
535
 
 
536
 
        $this->assertEquals(array('status' => 'HTTP/1.0 401 Unauthorized',
537
 
                'www-authenticate' => "WWW-Authenticate: Basic realm='TEST_AUTH_NAME'"),
538
 
                            $this->_headers);
539
 
    }
540
 
 
541
 
    function testRequestAuthenticationDoNotIgnoreUseGloballyFlag() {
542
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', 'useGlobally', false);
543
 
        if ($ret) {
544
 
            return $this->failWithStatus($ret);
545
 
        }
546
 
 
547
 
        $ret = HttpAuthHelper::requestAuthentication(false);
548
 
        if ($ret) {
549
 
            return $this->failWithStatus($ret);
550
 
        }
551
 
 
552
 
        $this->assertEquals(array(), $this->_headers);
553
 
    }
554
 
 
555
 
    function testRequestAuthenticationIgnoredIfHttpAuthNotEnabled() {
556
 
        foreach (array('httpAuthPlugin' => false, 'serverAuthPlugin' => true) as $key => $value) {
557
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', $key, $value);
558
 
            if ($ret) {
559
 
                return $this->failWithStatus($ret);
560
 
            }
561
 
        }
562
 
 
563
 
        $ret = HttpAuthHelper::requestAuthentication();
564
 
        if ($ret) {
565
 
            return $this->failWithStatus($ret);
566
 
        }
567
 
 
568
 
        $this->assertEquals(array(), $this->_headers);
569
 
    }
570
 
 
571
 
    function testRequestAuthenticationAlsoSentWhenAlreadyLoggedIn() {
572
 
        $this->_becomeGuestUser();
573
 
 
574
 
        $ret = HttpAuthHelper::requestAuthentication();
575
 
        if ($ret) {
576
 
            return $this->failWithStatus($ret);
577
 
        }
578
 
 
579
 
        $this->assertEquals(array('status' => 'HTTP/1.0 401 Unauthorized',
580
 
                'www-authenticate' => "WWW-Authenticate: Basic realm='TEST_AUTH_NAME'"),
581
 
                            $this->_headers);
582
 
    }
583
 
 
584
 
    function testRegenerateSessionIfNecessary() {
585
 
        global $gallery;
586
 
        $gallery->_session = new HttpAuthHelperTestSession();
587
 
 
588
 
        $ret = HttpAuthHelper::regenerateSessionIfNecessary($this->_user);
589
 
        if ($ret) {
590
 
            return $this->failWithStatus($ret);
591
 
        }
592
 
 
593
 
        $this->assert($gallery->_session->regenerateWasCalled());
594
 
    }
595
 
 
596
 
    function testRegenerateSessionIfNecessarySameUserAsFromSession() {
597
 
        global $gallery;
598
 
        $gallery->_session = new HttpAuthHelperTestSession($this->_user->getId());
599
 
 
600
 
        $ret = HttpAuthHelper::regenerateSessionIfNecessary($this->_user);
601
 
        if ($ret) {
602
 
            return $this->failWithStatus($ret);
603
 
        }
604
 
 
605
 
        $this->assert(!$gallery->_session->regenerateWasCalled());
606
 
    }
607
 
 
608
 
    function testRegenerateSessionIfNecessaryOtherUserThanFromSession() {
609
 
        global $gallery;
610
 
        $gallery->_session = new HttpAuthHelperTestSession('OTHER_USER');
611
 
 
612
 
        $ret = HttpAuthHelper::regenerateSessionIfNecessary($this->_user);
613
 
        if ($ret) {
614
 
            return $this->failWithStatus($ret);
615
 
        }
616
 
 
617
 
        $this->assert($gallery->_session->regenerateWasCalled());
618
 
    }
619
 
 
620
 
    function testRegenerateSessionIfNecessaryNoAuthenticatedUser() {
621
 
        global $gallery;
622
 
        $gallery->_session = new HttpAuthHelperTestSession($this->_user->getId());
623
 
 
624
 
        $ret = HttpAuthHelper::regenerateSessionIfNecessary(null);
625
 
        if ($ret) {
626
 
            return $this->failWithStatus($ret);
627
 
        }
628
 
 
629
 
        $this->assert(!$gallery->_session->regenerateWasCalled());
630
 
    }
631
 
}
632
 
 
633
 
class HttpAuthHelperTestRewriteApi {
634
 
    var $foo;
635
 
 
636
 
    function &_fetchActiveRulesForModule($moduleId) {
637
 
        static $activeRules;
638
 
        return $activeRules;
639
 
    }
640
 
 
641
 
    function &_getParserType() {
642
 
        static $parserType;
643
 
        return $parserType;
644
 
    }
645
 
 
646
 
    function isCompatibleWithApi($version) {
647
 
        return array(null, true);
648
 
    }
649
 
 
650
 
    function fetchActiveRulesForModule($moduleId) {
651
 
        return array(null, HttpAuthHelperTestRewriteApi::_fetchActiveRulesForModule($moduleId));
652
 
    }
653
 
 
654
 
    function getParserType() {
655
 
        return HttpAuthHelperTestRewriteApi::_getParserType();
656
 
    }
657
 
}
658
 
 
659
 
class HttpAuthHelperTestPlatform extends GalleryPlatform {
660
 
    var $_messages;
661
 
    var $_buffer;
662
 
 
663
 
    function fsockopen($target, $port, &$errno, &$errstr, $timeout) {
664
 
        return 'test';
665
 
    }
666
 
 
667
 
    function feof($handle) {
668
 
        return empty($this->_buffer);
669
 
    }
670
 
 
671
 
    function fgets($handle, $length) {
672
 
        if (empty($this->_buffer)) {
673
 
            return null;
674
 
        }
675
 
 
676
 
        if (strpos($this->_buffer, "\n") < $length) {
677
 
            $length = strpos($this->_buffer, "\n") + 1;
678
 
        }
679
 
 
680
 
        return $this->fread($handle, $length);
681
 
    }
682
 
 
683
 
    function fread($handle, $length) {
684
 
        if (empty($this->_buffer)) {
685
 
            return null;
686
 
        }
687
 
 
688
 
        $buffer = substr($this->_buffer, 0, $length);
689
 
        $this->_buffer = substr($this->_buffer, $length);
690
 
        return $buffer;
691
 
    }
692
 
 
693
 
    function fwrite($handle, $string, $length=0) {
694
 
        if (isset($this->_messages[$string])) {
695
 
            $this->_buffer = $this->_messages[$string];
696
 
            return strlen($string);
697
 
        } else {
698
 
            print "unexpected fwrite: $string ";
699
 
        }
700
 
    }
701
 
 
702
 
    function fflush($handle) {
703
 
        return true;
704
 
    }
705
 
 
706
 
    function fclose($handle) {
707
 
        return true;
708
 
    }
709
 
}
710
 
 
711
 
class HttpAuthHelperTestPhpVm extends GalleryPhpVm {
712
 
    function header($header, $replace=null) {
713
 
        /* Avoid modifying actual header information */
714
 
    }
715
 
}
716
 
 
717
 
class HttpAuthHelperTestSession {
718
 
    function HttpAuthHelperTestSession($userId=null) {
719
 
        $this->_userId = $userId;
720
 
        $this->_regenerateWasCalled = false;
721
 
    }
722
 
 
723
 
    function getUserId() {
724
 
        return $this->_userId;
725
 
    }
726
 
 
727
 
    function regenerate() {
728
 
        $this->_regenerateWasCalled = true;
729
 
    }
730
 
 
731
 
    function regenerateWasCalled() {
732
 
        return $this->_regenerateWasCalled;
733
 
    }
734
 
}
735
 
?>