| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* SocketTest 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
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('CakeSocket', 'Network');
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* SocketTest class
|
|
|
23 |
*
|
|
|
24 |
* @package Cake.Test.Case.Network
|
|
|
25 |
*/
|
|
|
26 |
class CakeSocketTest extends CakeTestCase {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* setUp method
|
|
|
30 |
*
|
|
|
31 |
* @return void
|
|
|
32 |
*/
|
|
|
33 |
public function setUp() {
|
|
|
34 |
parent::setUp();
|
|
|
35 |
$this->Socket = new CakeSocket(array('timeout' => 1));
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* tearDown method
|
|
|
40 |
*
|
|
|
41 |
* @return void
|
|
|
42 |
*/
|
|
|
43 |
public function tearDown() {
|
|
|
44 |
parent::tearDown();
|
|
|
45 |
unset($this->Socket);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* testConstruct method
|
|
|
50 |
*
|
|
|
51 |
* @return void
|
|
|
52 |
*/
|
|
|
53 |
public function testConstruct() {
|
|
|
54 |
$this->Socket = new CakeSocket();
|
|
|
55 |
$config = $this->Socket->config;
|
|
|
56 |
$this->assertSame($config, array(
|
|
|
57 |
'persistent' => false,
|
|
|
58 |
'host' => 'localhost',
|
|
|
59 |
'protocol' => getprotobyname('tcp'),
|
|
|
60 |
'port' => 80,
|
|
|
61 |
'timeout' => 30
|
|
|
62 |
));
|
|
|
63 |
|
|
|
64 |
$this->Socket->reset();
|
|
|
65 |
$this->Socket->__construct(array('host' => 'foo-bar'));
|
|
|
66 |
$config['host'] = 'foo-bar';
|
|
|
67 |
$this->assertSame($this->Socket->config, $config);
|
|
|
68 |
|
|
|
69 |
$this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
|
|
|
70 |
$config = $this->Socket->config;
|
|
|
71 |
|
|
|
72 |
$config['host'] = 'www.cakephp.org';
|
|
|
73 |
$config['port'] = 23;
|
|
|
74 |
$config['protocol'] = 17;
|
|
|
75 |
|
|
|
76 |
$this->assertSame($this->Socket->config, $config);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* testSocketConnection method
|
|
|
81 |
*
|
|
|
82 |
* @return void
|
|
|
83 |
*/
|
|
|
84 |
public function testSocketConnection() {
|
|
|
85 |
$this->assertFalse($this->Socket->connected);
|
|
|
86 |
$this->Socket->disconnect();
|
|
|
87 |
$this->assertFalse($this->Socket->connected);
|
|
|
88 |
try {
|
|
|
89 |
$this->Socket->connect();
|
|
|
90 |
$this->assertTrue($this->Socket->connected);
|
|
|
91 |
$this->Socket->connect();
|
|
|
92 |
$this->assertTrue($this->Socket->connected);
|
|
|
93 |
|
|
|
94 |
$this->Socket->disconnect();
|
|
|
95 |
$config = array('persistent' => true);
|
|
|
96 |
$this->Socket = new CakeSocket($config);
|
|
|
97 |
$this->Socket->connect();
|
|
|
98 |
$this->assertTrue($this->Socket->connected);
|
|
|
99 |
} catch (SocketException $e) {
|
|
|
100 |
$this->markTestSkipped('Cannot test network, skipping.');
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* data provider function for testInvalidConnection
|
|
|
106 |
*
|
|
|
107 |
* @return array
|
|
|
108 |
*/
|
|
|
109 |
public static function invalidConnections() {
|
|
|
110 |
return array(
|
|
|
111 |
array(array('host' => 'invalid.host', 'port' => 9999, 'timeout' => 1)),
|
|
|
112 |
array(array('host' => '127.0.0.1', 'port' => '70000', 'timeout' => 1))
|
|
|
113 |
);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* testInvalidConnection method
|
|
|
118 |
*
|
|
|
119 |
* @dataProvider invalidConnections
|
|
|
120 |
* @expectedException SocketException
|
|
|
121 |
* return void
|
|
|
122 |
*/
|
|
|
123 |
public function testInvalidConnection($data) {
|
|
|
124 |
$this->Socket->config = array_merge($this->Socket->config, $data);
|
|
|
125 |
$this->Socket->connect();
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* testSocketHost method
|
|
|
130 |
*
|
|
|
131 |
* @return void
|
|
|
132 |
*/
|
|
|
133 |
public function testSocketHost() {
|
|
|
134 |
try {
|
|
|
135 |
$this->Socket = new CakeSocket();
|
|
|
136 |
$this->Socket->connect();
|
|
|
137 |
$this->assertEquals('127.0.0.1', $this->Socket->address());
|
|
|
138 |
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
|
|
|
139 |
$this->assertEquals(null, $this->Socket->lastError());
|
|
|
140 |
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
|
|
|
141 |
|
|
|
142 |
$this->Socket = new CakeSocket(array('host' => '127.0.0.1'));
|
|
|
143 |
$this->Socket->connect();
|
|
|
144 |
$this->assertEquals('127.0.0.1', $this->Socket->address());
|
|
|
145 |
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
|
|
|
146 |
$this->assertEquals(null, $this->Socket->lastError());
|
|
|
147 |
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
|
|
|
148 |
} catch (SocketException $e) {
|
|
|
149 |
$this->markTestSkipped('Cannot test network, skipping.');
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* testSocketWriting method
|
|
|
155 |
*
|
|
|
156 |
* @return void
|
|
|
157 |
*/
|
|
|
158 |
public function testSocketWriting() {
|
|
|
159 |
try {
|
|
|
160 |
$request = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
|
|
|
161 |
$this->assertTrue((bool)$this->Socket->write($request));
|
|
|
162 |
} catch (SocketException $e) {
|
|
|
163 |
$this->markTestSkipped('Cannot test network, skipping.');
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* testSocketReading method
|
|
|
169 |
*
|
|
|
170 |
* @return void
|
|
|
171 |
*/
|
|
|
172 |
public function testSocketReading() {
|
|
|
173 |
$this->Socket = new CakeSocket(array('timeout' => 5));
|
|
|
174 |
try {
|
|
|
175 |
$this->Socket->connect();
|
|
|
176 |
$this->assertEquals(null, $this->Socket->read(26));
|
|
|
177 |
|
|
|
178 |
$config = array('host' => 'google.com', 'port' => 80, 'timeout' => 1);
|
|
|
179 |
$this->Socket = new CakeSocket($config);
|
|
|
180 |
$this->assertTrue($this->Socket->connect());
|
|
|
181 |
$this->assertEquals(null, $this->Socket->read(26));
|
|
|
182 |
$this->assertEquals('2: ' . __d('cake_dev', 'Connection timed out'), $this->Socket->lastError());
|
|
|
183 |
} catch (SocketException $e) {
|
|
|
184 |
$this->markTestSkipped('Cannot test network, skipping.');
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* testTimeOutConnection method
|
|
|
190 |
*
|
|
|
191 |
* @return void
|
|
|
192 |
*/
|
|
|
193 |
public function testTimeOutConnection() {
|
|
|
194 |
$config = array('host' => '127.0.0.1', 'timeout' => 0.5);
|
|
|
195 |
$this->Socket = new CakeSocket($config);
|
|
|
196 |
try {
|
|
|
197 |
$this->assertTrue($this->Socket->connect());
|
|
|
198 |
|
|
|
199 |
$config = array('host' => '127.0.0.1', 'timeout' => 0.00001);
|
|
|
200 |
$this->Socket = new CakeSocket($config);
|
|
|
201 |
$this->assertFalse($this->Socket->read(1024 * 1024));
|
|
|
202 |
$this->assertEquals('2: ' . __d('cake_dev', 'Connection timed out'), $this->Socket->lastError());
|
|
|
203 |
} catch (SocketException $e) {
|
|
|
204 |
$this->markTestSkipped('Cannot test network, skipping.');
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* testLastError method
|
|
|
210 |
*
|
|
|
211 |
* @return void
|
|
|
212 |
*/
|
|
|
213 |
public function testLastError() {
|
|
|
214 |
$this->Socket = new CakeSocket();
|
|
|
215 |
$this->Socket->setLastError(4, 'some error here');
|
|
|
216 |
$this->assertEquals('4: some error here', $this->Socket->lastError());
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* testReset method
|
|
|
221 |
*
|
|
|
222 |
* @return void
|
|
|
223 |
*/
|
|
|
224 |
public function testReset() {
|
|
|
225 |
$config = array(
|
|
|
226 |
'persistent' => true,
|
|
|
227 |
'host' => '127.0.0.1',
|
|
|
228 |
'protocol' => 'udp',
|
|
|
229 |
'port' => 80,
|
|
|
230 |
'timeout' => 20
|
|
|
231 |
);
|
|
|
232 |
$anotherSocket = new CakeSocket($config);
|
|
|
233 |
$anotherSocket->reset();
|
|
|
234 |
$this->assertEquals(array(), $anotherSocket->config);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* testEncrypt
|
|
|
239 |
*
|
|
|
240 |
* @expectedException SocketException
|
|
|
241 |
* @return void
|
|
|
242 |
*/
|
|
|
243 |
public function testEnableCryptoSocketExceptionNoSsl() {
|
|
|
244 |
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
|
|
|
245 |
$configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
|
|
|
246 |
|
|
|
247 |
// testing exception on no ssl socket server for ssl and tls methods
|
|
|
248 |
$this->Socket = new CakeSocket($configNoSslOrTls);
|
|
|
249 |
$this->Socket->connect();
|
|
|
250 |
$this->Socket->enableCrypto('sslv3', 'client');
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* testEnableCryptoSocketExceptionNoTls
|
|
|
255 |
*
|
|
|
256 |
* @expectedException SocketException
|
|
|
257 |
* @return void
|
|
|
258 |
*/
|
|
|
259 |
public function testEnableCryptoSocketExceptionNoTls() {
|
|
|
260 |
$configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
|
|
|
261 |
|
|
|
262 |
// testing exception on no ssl socket server for ssl and tls methods
|
|
|
263 |
$this->Socket = new CakeSocket($configNoSslOrTls);
|
|
|
264 |
$this->Socket->connect();
|
|
|
265 |
$this->Socket->enableCrypto('tls', 'client');
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* _connectSocketToSslTls
|
|
|
270 |
*
|
|
|
271 |
* @return void
|
|
|
272 |
*/
|
|
|
273 |
protected function _connectSocketToSslTls() {
|
|
|
274 |
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
|
|
|
275 |
$configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
|
|
|
276 |
$this->Socket = new CakeSocket($configSslTls);
|
|
|
277 |
try {
|
|
|
278 |
$this->Socket->connect();
|
|
|
279 |
} catch (SocketException $e) {
|
|
|
280 |
$this->markTestSkipped('Cannot test network, skipping.');
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* testEnableCryptoBadMode
|
|
|
286 |
*
|
|
|
287 |
* @expectedException InvalidArgumentException
|
|
|
288 |
* @return void
|
|
|
289 |
*/
|
|
|
290 |
public function testEnableCryptoBadMode() {
|
|
|
291 |
// testing wrong encryption mode
|
|
|
292 |
$this->_connectSocketToSslTls();
|
|
|
293 |
$this->Socket->enableCrypto('doesntExistMode', 'server');
|
|
|
294 |
$this->Socket->disconnect();
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
/**
|
|
|
298 |
* testEnableCrypto
|
|
|
299 |
*
|
|
|
300 |
* @return void
|
|
|
301 |
*/
|
|
|
302 |
public function testEnableCrypto() {
|
|
|
303 |
// testing on ssl server
|
|
|
304 |
$this->_connectSocketToSslTls();
|
|
|
305 |
$this->assertTrue($this->Socket->enableCrypto('sslv3', 'client'));
|
|
|
306 |
$this->Socket->disconnect();
|
|
|
307 |
|
|
|
308 |
// testing on tls server
|
|
|
309 |
$this->_connectSocketToSslTls();
|
|
|
310 |
$this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
|
|
|
311 |
$this->Socket->disconnect();
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
/**
|
|
|
315 |
* testEnableCryptoExceptionEnableTwice
|
|
|
316 |
*
|
|
|
317 |
* @expectedException SocketException
|
|
|
318 |
* @return void
|
|
|
319 |
*/
|
|
|
320 |
public function testEnableCryptoExceptionEnableTwice() {
|
|
|
321 |
// testing on tls server
|
|
|
322 |
$this->_connectSocketToSslTls();
|
|
|
323 |
$this->Socket->enableCrypto('tls', 'client');
|
|
|
324 |
$this->Socket->enableCrypto('tls', 'client');
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
/**
|
|
|
328 |
* testEnableCryptoExceptionDisableTwice
|
|
|
329 |
*
|
|
|
330 |
* @expectedException SocketException
|
|
|
331 |
* @return void
|
|
|
332 |
*/
|
|
|
333 |
public function testEnableCryptoExceptionDisableTwice() {
|
|
|
334 |
// testing on tls server
|
|
|
335 |
$this->_connectSocketToSslTls();
|
|
|
336 |
$this->Socket->enableCrypto('tls', 'client', false);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* testEnableCryptoEnableStatus
|
|
|
341 |
*
|
|
|
342 |
* @return void
|
|
|
343 |
*/
|
|
|
344 |
public function testEnableCryptoEnableStatus() {
|
|
|
345 |
// testing on tls server
|
|
|
346 |
$this->_connectSocketToSslTls();
|
|
|
347 |
$this->assertFalse($this->Socket->encrypted);
|
|
|
348 |
$this->Socket->enableCrypto('tls', 'client', true);
|
|
|
349 |
$this->assertTrue($this->Socket->encrypted);
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
/**
|
|
|
353 |
* test getting the context for a socket.
|
|
|
354 |
*
|
|
|
355 |
* @return void
|
|
|
356 |
*/
|
|
|
357 |
public function testGetContext() {
|
|
|
358 |
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
|
|
|
359 |
$config = array(
|
|
|
360 |
'host' => 'smtp.gmail.com',
|
|
|
361 |
'port' => 465,
|
|
|
362 |
'timeout' => 5,
|
|
|
363 |
'context' => array(
|
|
|
364 |
'ssl' => array('capture_peer' => true)
|
|
|
365 |
)
|
|
|
366 |
);
|
|
|
367 |
$this->Socket = new CakeSocket($config);
|
|
|
368 |
$this->Socket->connect();
|
|
|
369 |
$result = $this->Socket->context();
|
|
|
370 |
$this->assertEquals($config['context'], $result);
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
}
|