統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Network / CakeSocketTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (11.398 KB)

1 635eef61 spyder1211
<?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'                => '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'] = 'udp';
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
 * Test that protocol in the host doesn't cause cert errors.
270
 *
271
 * @return void
272
 */
273
        public function testConnectProtocolInHost() {
274
                $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
275
                $configSslTls = array('host' => 'ssl://smtp.gmail.com', 'port' => 465, 'timeout' => 5);
276
                $socket = new CakeSocket($configSslTls);
277
                try {
278
                        $socket->connect();
279
                        $this->assertEquals('smtp.gmail.com', $socket->config['host']);
280
                        $this->assertEquals('ssl', $socket->config['protocol']);
281
                } catch (SocketException $e) {
282
                        $this->markTestSkipped('Cannot test network, skipping.');
283
                }
284
        }
285
286
/**
287
 * _connectSocketToSslTls
288
 *
289
 * @return void
290
 */
291
        protected function _connectSocketToSslTls() {
292
                $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
293
                $configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
294
                $this->Socket = new CakeSocket($configSslTls);
295
                try {
296
                        $this->Socket->connect();
297
                } catch (SocketException $e) {
298
                        $this->markTestSkipped('Cannot test network, skipping.');
299
                }
300
        }
301
302
/**
303
 * testEnableCryptoBadMode
304
 *
305
 * @expectedException InvalidArgumentException
306
 * @return void
307
 */
308
        public function testEnableCryptoBadMode() {
309
                // testing wrong encryption mode
310
                $this->_connectSocketToSslTls();
311
                $this->Socket->enableCrypto('doesntExistMode', 'server');
312
                $this->Socket->disconnect();
313
        }
314
315
/**
316
 * testEnableCrypto
317
 *
318
 * @return void
319
 */
320
        public function testEnableCrypto() {
321
                // testing on ssl server
322
                $this->_connectSocketToSslTls();
323
                $this->assertTrue($this->Socket->enableCrypto('sslv3', 'client'));
324
                $this->Socket->disconnect();
325
326
                // testing on tls server
327
                $this->_connectSocketToSslTls();
328
                $this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
329
                $this->Socket->disconnect();
330
        }
331
332
/**
333
 * testEnableCryptoExceptionEnableTwice
334
 *
335
 * @expectedException SocketException
336
 * @return void
337
 */
338
        public function testEnableCryptoExceptionEnableTwice() {
339
                // testing on tls server
340
                $this->_connectSocketToSslTls();
341
                $this->Socket->enableCrypto('tls', 'client');
342
                $this->Socket->enableCrypto('tls', 'client');
343
        }
344
345
/**
346
 * testEnableCryptoExceptionDisableTwice
347
 *
348
 * @expectedException SocketException
349
 * @return void
350
 */
351
        public function testEnableCryptoExceptionDisableTwice() {
352
                // testing on tls server
353
                $this->_connectSocketToSslTls();
354
                $this->Socket->enableCrypto('tls', 'client', false);
355
        }
356
357
/**
358
 * testEnableCryptoEnableStatus
359
 *
360
 * @return void
361
 */
362
        public function testEnableCryptoEnableStatus() {
363
                // testing on tls server
364
                $this->_connectSocketToSslTls();
365
                $this->assertFalse($this->Socket->encrypted);
366
                $this->Socket->enableCrypto('tls', 'client', true);
367
                $this->assertTrue($this->Socket->encrypted);
368
        }
369
370
/**
371
 * test getting the context for a socket.
372
 *
373
 * @return void
374
 */
375
        public function testGetContext() {
376
                $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
377
                $config = array(
378
                        'host' => 'smtp.gmail.com',
379
                        'port' => 465,
380
                        'timeout' => 5,
381
                        'context' => array(
382
                                'ssl' => array('capture_peer' => true)
383
                        )
384
                );
385
                $this->Socket = new CakeSocket($config);
386
                $this->Socket->connect();
387
                $result = $this->Socket->context();
388
                $this->assertSame($config['context']['ssl']['capture_peer'], $result['ssl']['capture_peer']);
389
        }
390
391
/**
392
 * test configuring the context from the flat keys.
393
 *
394
 * @return void
395
 */
396
        public function testConfigContext() {
397
                $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
398
                $config = array(
399
                        'host' => 'smtp.gmail.com',
400
                        'port' => 465,
401
                        'timeout' => 5,
402
                        'ssl_verify_peer' => true,
403
                        'ssl_allow_self_signed' => false,
404
                        'ssl_verify_depth' => 5,
405
                        'ssl_verify_host' => true,
406
                );
407
                $this->Socket = new CakeSocket($config);
408
409
                $this->Socket->connect();
410
                $result = $this->Socket->context();
411
412
                $this->assertTrue($result['ssl']['verify_peer']);
413
                $this->assertFalse($result['ssl']['allow_self_signed']);
414
                $this->assertEquals(5, $result['ssl']['verify_depth']);
415
                $this->assertEquals('smtp.gmail.com', $result['ssl']['CN_match']);
416
                $this->assertArrayNotHasKey('ssl_verify_peer', $this->Socket->config);
417
                $this->assertArrayNotHasKey('ssl_allow_self_signed', $this->Socket->config);
418
                $this->assertArrayNotHasKey('ssl_verify_host', $this->Socket->config);
419
                $this->assertArrayNotHasKey('ssl_verify_depth', $this->Socket->config);
420
        }
421
}