Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * DigestAuthenticationTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Network.Http
15
 * @since         CakePHP(tm) v 2.0.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('HttpSocket', 'Network/Http');
20
App::uses('DigestAuthentication', 'Network/Http');
21
 
22
/**
23
 * Class DigestHttpSocket
24
 *
25
 * @package       Cake.Test.Case.Network.Http
26
 */
27
class DigestHttpSocket extends HttpSocket {
28
 
29
/**
30
 * nextHeader attribute
31
 *
32
 * @var string
33
 */
34
	public $nextHeader = '';
35
 
36
/**
37
 * request method
38
 *
39
 * @param mixed $request
40
 * @return void
41
 */
42
	public function request($request = array()) {
43
		if ($request === false) {
44
			if (isset($this->response['header']['WWW-Authenticate'])) {
45
				unset($this->response['header']['WWW-Authenticate']);
46
			}
47
			return;
48
		}
49
		$this->response['header']['WWW-Authenticate'] = $this->nextHeader;
50
	}
51
 
52
}
53
 
54
/**
55
 * DigestAuthenticationTest class
56
 *
57
 * @package       Cake.Test.Case.Network.Http
58
 */
59
class DigestAuthenticationTest extends CakeTestCase {
60
 
61
/**
62
 * Socket property
63
 *
64
 * @var mixed null
65
 */
66
	public $HttpSocket = null;
67
 
68
/**
69
 * This function sets up a HttpSocket instance we are going to use for testing
70
 *
71
 * @return void
72
 */
73
	public function setUp() {
74
		parent::setUp();
75
		$this->HttpSocket = new DigestHttpSocket();
76
		$this->HttpSocket->request['method'] = 'GET';
77
		$this->HttpSocket->request['uri']['path'] = '/';
78
	}
79
 
80
/**
81
 * We use this function to clean up after the test case was executed
82
 *
83
 * @return void
84
 */
85
	public function tearDown() {
86
		parent::tearDown();
87
		unset($this->HttpSocket);
88
	}
89
 
90
/**
91
 * testBasic method
92
 *
93
 * @return void
94
 */
95
	public function testBasic() {
96
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
97
		$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
98
 
99
		$auth = array('user' => 'admin', 'pass' => '1234');
100
		DigestAuthentication::authentication($this->HttpSocket, $auth);
101
		$this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
102
		$this->assertEquals('The batcave', $auth['realm']);
103
		$this->assertEquals('4cded326c6c51', $auth['nonce']);
104
	}
105
 
106
/**
107
 * testQop method
108
 *
109
 * @return void
110
 */
111
	public function testQop() {
112
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
113
		$auth = array('user' => 'admin', 'pass' => '1234');
114
		DigestAuthentication::authentication($this->HttpSocket, $auth);
115
		$expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
116
		$this->assertEquals($expected, $this->HttpSocket->request['header']['Authorization']);
117
		$this->assertFalse(isset($auth['qop']));
118
		$this->assertFalse(isset($auth['nc']));
119
 
120
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
121
		$auth = array('user' => 'admin', 'pass' => '1234');
122
		DigestAuthentication::authentication($this->HttpSocket, $auth);
123
		$expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
124
		$this->assertRegExp($expected, $this->HttpSocket->request['header']['Authorization']);
125
		$this->assertEquals('auth', $auth['qop']);
126
		$this->assertEquals(2, $auth['nc']);
127
	}
128
 
129
/**
130
 * testOpaque method
131
 *
132
 * @return void
133
 */
134
	public function testOpaque() {
135
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
136
		$auth = array('user' => 'admin', 'pass' => '1234');
137
		DigestAuthentication::authentication($this->HttpSocket, $auth);
138
		$this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
139
 
140
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
141
		$auth = array('user' => 'admin', 'pass' => '1234');
142
		DigestAuthentication::authentication($this->HttpSocket, $auth);
143
		$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
144
	}
145
 
146
/**
147
 * testMultipleRequest method
148
 *
149
 * @return void
150
 */
151
	public function testMultipleRequest() {
152
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
153
		$auth = array('user' => 'admin', 'pass' => '1234');
154
		DigestAuthentication::authentication($this->HttpSocket, $auth);
155
		$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
156
		$this->assertEquals(2, $auth['nc']);
157
 
158
		DigestAuthentication::authentication($this->HttpSocket, $auth);
159
		$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
160
		$this->assertEquals(3, $auth['nc']);
161
		$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
162
		$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
163
 
164
		$this->HttpSocket->nextHeader = '';
165
		DigestAuthentication::authentication($this->HttpSocket, $auth);
166
		$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
167
		$this->assertEquals(4, $auth['nc']);
168
		$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
169
		$responseB = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
170
		$this->assertNotEquals($response, $responseB);
171
	}
172
 
173
/**
174
 * testPathChanged method
175
 *
176
 * @return void
177
 */
178
	public function testPathChanged() {
179
		$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
180
		$this->HttpSocket->request['uri']['path'] = '/admin';
181
		$auth = array('user' => 'admin', 'pass' => '1234');
182
		DigestAuthentication::authentication($this->HttpSocket, $auth);
183
		$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
184
		$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
185
		$this->assertNotEquals('da7e2a46b471d77f70a9bb3698c8902b', $response);
186
	}
187
 
188
/**
189
 * testNoDigestResponse method
190
 *
191
 * @return void
192
 */
193
	public function testNoDigestResponse() {
194
		$this->HttpSocket->nextHeader = false;
195
		$this->HttpSocket->request['uri']['path'] = '/admin';
196
		$auth = array('user' => 'admin', 'pass' => '1234');
197
		DigestAuthentication::authentication($this->HttpSocket, $auth);
198
		$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
199
	}
200
 
201
}