summaryrefslogtreecommitdiff
path: root/tests/UnitTests/simpletest/test/authentication_test.php
blob: 3a12a2ff7bfd4cf8078e6ac6e37386f79b56b686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
    // $Id: authentication_test.php,v 1.8 2004/09/24 22:55:10 lastcraft Exp $
    require_once(dirname(__FILE__) . '/../authentication.php');
    require_once(dirname(__FILE__) . '/../http.php');
    Mock::generate('SimpleHttpRequest');

    class TestOfRealm extends UnitTestCase {
        
        function testWithinSameUrl() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/hello.html'));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/hello.html')));
        }
        
        function testInsideWithLongerUrl() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/'));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/hello.html')));
        }
        
        function testBelowRootIsOutside() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/'));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/more/hello.html')));
        }
        
        function testOldNetscapeDefinitionIsOutside() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/'));
            $this->assertFalse($realm->isWithin(
                    new SimpleUrl('http://www.here.com/pathmore/hello.html')));
        }
        
        function testDifferentPageNameStillInside() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/hello.html'));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/goodbye.html')));
        }
        
        function testNewUrlInSameDirectoryDoesNotChangeRealm() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/hello.html'));
            $realm->stretch(new SimpleUrl('http://www.here.com/path/goodbye.html'));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/index.html')));
            $this->assertFalse($realm->isWithin(
                    new SimpleUrl('http://www.here.com/index.html')));
        }
        
        function testNewUrlMakesRealmTheCommonPath() {
            $realm = &new SimpleRealm(
                    'Basic',
                    new SimpleUrl('http://www.here.com/path/here/hello.html'));
            $realm->stretch(new SimpleUrl('http://www.here.com/path/there/goodbye.html'));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/here/index.html')));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/there/index.html')));
            $this->assertTrue($realm->isWithin(
                    new SimpleUrl('http://www.here.com/path/index.html')));
            $this->assertFalse($realm->isWithin(
                    new SimpleUrl('http://www.here.com/index.html')));
            $this->assertFalse($realm->isWithin(
                    new SimpleUrl('http://www.here.com/paths/index.html')));
            $this->assertFalse($realm->isWithin(
                    new SimpleUrl('http://www.here.com/pathindex.html')));
        }
    }

    class TestOfAuthenticator extends UnitTestCase {
        
        function testNoRealms() {
            $request = &new MockSimpleHttpRequest($this);
            $request->expectNever('addHeaderLine');
            $authenticator = &new SimpleAuthenticator();
            $authenticator->addHeaders($request, new SimpleUrl('http://here.com/'));
            $request->tally();
        }
        
        function &createSingleRealm() {
            $authenticator = &new SimpleAuthenticator();
            $authenticator->addRealm(
                    new SimpleUrl('http://www.here.com/path/hello.html'),
                    'Basic',
                    'Sanctuary');
            $authenticator->setIdentityForRealm('www.here.com', 'Sanctuary', 'test', 'secret');
            return $authenticator;
        }
        
        function testOutsideRealm() {
            $request = &new MockSimpleHttpRequest($this);
            $request->expectNever('addHeaderLine');
            $authenticator = &$this->createSingleRealm();
            $authenticator->addHeaders(
                    $request,
                    new SimpleUrl('http://www.here.com/hello.html'));
            $request->tally();
        }
        
        function testWithinRealm() {
            $request = &new MockSimpleHttpRequest($this);
            $request->expectOnce('addHeaderLine');
            $authenticator = &$this->createSingleRealm();
            $authenticator->addHeaders(
                    $request,
                    new SimpleUrl('http://www.here.com/path/more/hello.html'));
            $request->tally();
        }
        
        function testRestartingClearsRealm() {
            $request = &new MockSimpleHttpRequest($this);
            $request->expectNever('addHeaderLine');
            $authenticator = &$this->createSingleRealm();
            $authenticator->restartSession();
            $authenticator->addHeaders(
                    $request,
                    new SimpleUrl('http://www.here.com/hello.html'));
            $request->tally();
        }
        
        function testDifferentHostIsOutsideRealm() {
            $request = &new MockSimpleHttpRequest($this);
            $request->expectNever('addHeaderLine');
            $authenticator = &$this->createSingleRealm();
            $authenticator->addHeaders(
                    $request,
                    new SimpleUrl('http://here.com/path/hello.html'));
            $request->tally();
        }
    }
?>