/
digest_authentication.test.php
195 lines (174 loc) · 6.62 KB
/
digest_authentication.test.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* DigestAuthenticationTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake.tests.cases.libs.http
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('HttpSocket', 'Network/Http');
App::uses('DigestAuthentication', 'Network/Http');
class DigestHttpSocket extends HttpSocket {
/**
* nextHeader attribute
*
* @var string
*/
public $nextHeader = '';
/**
* request method
*
* @param mixed $request
* @return void
*/
public function request($request) {
if ($request === false) {
if (isset($this->response['header']['WWW-Authenticate'])) {
unset($this->response['header']['WWW-Authenticate']);
}
return;
}
$this->response['header']['WWW-Authenticate'] = $this->nextHeader;
}
}
/**
* DigestAuthenticationTest class
*
* @package cake.tests.cases.libs.http
*/
class DigestAuthenticationTest extends CakeTestCase {
/**
* Socket property
*
* @var mixed null
*/
public $HttpSocket = null;
/**
* This function sets up a HttpSocket instance we are going to use for testing
*
* @return void
*/
public function setUp() {
$this->HttpSocket = new DigestHttpSocket();
$this->HttpSocket->request['method'] = 'GET';
$this->HttpSocket->request['uri']['path'] = '/';
}
/**
* We use this function to clean up after the test case was executed
*
* @return void
*/
function tearDown() {
unset($this->HttpSocket);
}
/**
* testBasic method
*
* @return void
*/
public function testBasic() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
$this->assertEqual($auth['realm'], 'The batcave');
$this->assertEqual($auth['nonce'], '4cded326c6c51');
}
/**
* testQop method
*
* @return void
*/
public function testQop() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
$this->assertEqual($expected, $this->HttpSocket->request['header']['Authorization']);
$this->assertFalse(isset($auth['qop']));
$this->assertFalse(isset($auth['nc']));
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
$this->assertPattern($expected, $this->HttpSocket->request['header']['Authorization']);
$this->assertEqual($auth['qop'], 'auth');
$this->assertEqual($auth['nc'], 2);
}
/**
* testOpaque method
*
* @return void
*/
public function testOpaque() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
}
/**
* testMultipleRequest method
*
* @return void
*/
public function testMultipleRequest() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
$this->assertEqual($auth['nc'], 2);
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
$this->assertEqual($auth['nc'], 3);
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
$this->HttpSocket->nextHeader = '';
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
$this->assertEqual($auth['nc'], 4);
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
$response2 = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
$this->assertNotEqual($response, $response2);
}
/**
* testPathChanged method
*
* @return void
*/
public function testPathChanged() {
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
$this->HttpSocket->request['uri']['path'] = '/admin';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
$this->assertNotEqual($response, 'da7e2a46b471d77f70a9bb3698c8902b');
}
/**
* testNoDigestResponse method
*
* @return void
*/
public function testNoDigestResponse() {
$this->HttpSocket->nextHeader = false;
$this->HttpSocket->request['uri']['path'] = '/admin';
$auth = array('user' => 'admin', 'pass' => '1234');
DigestAuthentication::authentication($this->HttpSocket, $auth);
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
}
}