SocketTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network;
  16. use Cake\Network\Exception\SocketException;
  17. use Cake\Network\Socket;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * SocketTest class
  21. */
  22. class SocketTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->Socket = new Socket(['timeout' => 1]);
  33. }
  34. /**
  35. * tearDown method
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. parent::tearDown();
  42. unset($this->Socket);
  43. }
  44. /**
  45. * testConstruct method
  46. *
  47. * @return void
  48. */
  49. public function testConstruct()
  50. {
  51. $this->Socket = new Socket();
  52. $config = $this->Socket->config();
  53. $this->assertSame($config, [
  54. 'persistent' => false,
  55. 'host' => 'localhost',
  56. 'protocol' => 'tcp',
  57. 'port' => 80,
  58. 'timeout' => 30
  59. ]);
  60. $this->Socket->reset();
  61. $this->Socket->__construct(['host' => 'foo-bar']);
  62. $config['host'] = 'foo-bar';
  63. $this->assertSame($this->Socket->config(), $config);
  64. $this->Socket = new Socket(['host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp']);
  65. $config = $this->Socket->config();
  66. $config['host'] = 'www.cakephp.org';
  67. $config['port'] = 23;
  68. $config['protocol'] = 'udp';
  69. $this->assertSame($this->Socket->config(), $config);
  70. }
  71. /**
  72. * testSocketConnection method
  73. *
  74. * @return void
  75. */
  76. public function testSocketConnection()
  77. {
  78. $this->assertFalse($this->Socket->connected);
  79. $this->Socket->disconnect();
  80. $this->assertFalse($this->Socket->connected);
  81. try {
  82. $this->Socket->connect();
  83. $this->assertTrue($this->Socket->connected);
  84. $this->Socket->connect();
  85. $this->assertTrue($this->Socket->connected);
  86. $this->Socket->disconnect();
  87. $config = ['persistent' => true];
  88. $this->Socket = new Socket($config);
  89. $this->Socket->connect();
  90. $this->assertTrue($this->Socket->connected);
  91. } catch (SocketException $e) {
  92. $this->markTestSkipped('Cannot test network, skipping.');
  93. }
  94. }
  95. /**
  96. * data provider function for testInvalidConnection
  97. *
  98. * @return array
  99. */
  100. public static function invalidConnections()
  101. {
  102. return [
  103. [['host' => 'invalid.host', 'port' => 9999, 'timeout' => 1]],
  104. [['host' => '127.0.0.1', 'port' => '70000', 'timeout' => 1]]
  105. ];
  106. }
  107. /**
  108. * testInvalidConnection method
  109. *
  110. * @dataProvider invalidConnections
  111. * @expectedException \Cake\Network\Exception\SocketException
  112. * @return void
  113. */
  114. public function testInvalidConnection($data)
  115. {
  116. $this->Socket->config($data);
  117. $this->Socket->connect();
  118. }
  119. /**
  120. * testSocketHost method
  121. *
  122. * @return void
  123. */
  124. public function testSocketHost()
  125. {
  126. try {
  127. $this->Socket = new Socket();
  128. $this->Socket->connect();
  129. $this->assertEquals('127.0.0.1', $this->Socket->address());
  130. $this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
  131. $this->assertEquals(null, $this->Socket->lastError());
  132. $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
  133. $this->Socket = new Socket(['host' => '127.0.0.1']);
  134. $this->Socket->connect();
  135. $this->assertEquals('127.0.0.1', $this->Socket->address());
  136. $this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
  137. $this->assertEquals(null, $this->Socket->lastError());
  138. $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
  139. } catch (SocketException $e) {
  140. $this->markTestSkipped('Cannot test network, skipping.');
  141. }
  142. }
  143. /**
  144. * testSocketWriting method
  145. *
  146. * @return void
  147. */
  148. public function testSocketWriting()
  149. {
  150. try {
  151. $request = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  152. $this->assertTrue((bool)$this->Socket->write($request));
  153. } catch (SocketException $e) {
  154. $this->markTestSkipped('Cannot test network, skipping.');
  155. }
  156. }
  157. /**
  158. * testSocketReading method
  159. *
  160. * @return void
  161. */
  162. public function testSocketReading()
  163. {
  164. $this->Socket = new Socket(['timeout' => 5]);
  165. try {
  166. $this->Socket->connect();
  167. $this->assertEquals(null, $this->Socket->read(26));
  168. $config = ['host' => 'google.com', 'port' => 80, 'timeout' => 1];
  169. $this->Socket = new Socket($config);
  170. $this->assertTrue($this->Socket->connect());
  171. $this->assertEquals(null, $this->Socket->read(26));
  172. $this->assertEquals('2: ' . 'Connection timed out', $this->Socket->lastError());
  173. } catch (SocketException $e) {
  174. $this->markTestSkipped('Cannot test network, skipping.');
  175. }
  176. }
  177. /**
  178. * testTimeOutConnection method
  179. *
  180. * @return void
  181. */
  182. public function testTimeOutConnection()
  183. {
  184. $config = ['host' => '127.0.0.1', 'timeout' => 0.5];
  185. $this->Socket = new Socket($config);
  186. try {
  187. $this->assertTrue($this->Socket->connect());
  188. $config = ['host' => '127.0.0.1', 'timeout' => 0.00001];
  189. $this->Socket = new Socket($config);
  190. $this->assertFalse($this->Socket->read(1024 * 1024));
  191. $this->assertEquals('2: ' . 'Connection timed out', $this->Socket->lastError());
  192. } catch (SocketException $e) {
  193. $this->markTestSkipped('Cannot test network, skipping.');
  194. }
  195. }
  196. /**
  197. * testLastError method
  198. *
  199. * @return void
  200. */
  201. public function testLastError()
  202. {
  203. $this->Socket = new Socket();
  204. $this->Socket->setLastError(4, 'some error here');
  205. $this->assertEquals('4: some error here', $this->Socket->lastError());
  206. }
  207. /**
  208. * testReset method
  209. *
  210. * @return void
  211. */
  212. public function testReset()
  213. {
  214. $config = [
  215. 'persistent' => true,
  216. 'host' => '127.0.0.1',
  217. 'protocol' => 'udp',
  218. 'port' => 80,
  219. 'timeout' => 20
  220. ];
  221. $anotherSocket = new Socket($config);
  222. $anotherSocket->reset();
  223. $expected = [
  224. 'persistent' => false,
  225. 'host' => 'localhost',
  226. 'protocol' => 'tcp',
  227. 'port' => 80,
  228. 'timeout' => 30
  229. ];
  230. $this->assertEquals(
  231. $expected,
  232. $anotherSocket->config(),
  233. 'Reset should cause config to return the defaults defined in _defaultConfig'
  234. );
  235. }
  236. /**
  237. * testEncrypt
  238. *
  239. * @expectedException \Cake\Network\Exception\SocketException
  240. * @return void
  241. */
  242. public function testEnableCryptoSocketExceptionNoSsl()
  243. {
  244. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  245. $configNoSslOrTls = ['host' => 'localhost', 'port' => 80, 'timeout' => 0.1];
  246. // testing exception on no ssl socket server for ssl and tls methods
  247. $this->Socket = new Socket($configNoSslOrTls);
  248. $this->Socket->connect();
  249. $this->Socket->enableCrypto('sslv3', 'client');
  250. }
  251. /**
  252. * testEnableCryptoSocketExceptionNoTls
  253. *
  254. * @expectedException \Cake\Network\Exception\SocketException
  255. * @return void
  256. */
  257. public function testEnableCryptoSocketExceptionNoTls()
  258. {
  259. $configNoSslOrTls = ['host' => 'localhost', 'port' => 80, 'timeout' => 0.1];
  260. // testing exception on no ssl socket server for ssl and tls methods
  261. $this->Socket = new Socket($configNoSslOrTls);
  262. $this->Socket->connect();
  263. $this->Socket->enableCrypto('tls', 'client');
  264. }
  265. /**
  266. * Test that protocol in the host doesn't cause cert errors.
  267. *
  268. * @return void
  269. */
  270. public function testConnectProtocolInHost()
  271. {
  272. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  273. $configSslTls = ['host' => 'ssl://smtp.gmail.com', 'port' => 465, 'timeout' => 5];
  274. $socket = new Socket($configSslTls);
  275. try {
  276. $socket->connect();
  277. $this->assertEquals('smtp.gmail.com', $socket->config('host'));
  278. $this->assertEquals('ssl', $socket->config('protocol'));
  279. } catch (SocketException $e) {
  280. $this->markTestSkipped('Cannot test network, skipping.');
  281. }
  282. }
  283. /**
  284. * _connectSocketToSslTls
  285. *
  286. * @return void
  287. */
  288. protected function _connectSocketToSslTls()
  289. {
  290. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  291. $configSslTls = ['host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5];
  292. $this->Socket = new Socket($configSslTls);
  293. try {
  294. $this->Socket->connect();
  295. } catch (SocketException $e) {
  296. $this->markTestSkipped('Cannot test network, skipping.');
  297. }
  298. }
  299. /**
  300. * testEnableCryptoBadMode
  301. *
  302. * @expectedException \InvalidArgumentException
  303. * @return void
  304. */
  305. public function testEnableCryptoBadMode()
  306. {
  307. // testing wrong encryption mode
  308. $this->_connectSocketToSslTls();
  309. $this->Socket->enableCrypto('doesntExistMode', 'server');
  310. $this->Socket->disconnect();
  311. }
  312. /**
  313. * testEnableCrypto
  314. *
  315. * @return void
  316. */
  317. public function testEnableCrypto()
  318. {
  319. $this->skipIf(!function_exists('stream_socket_enable_crypto'), 'Broken on HHVM');
  320. $this->_connectSocketToSslTls();
  321. $this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
  322. $this->Socket->disconnect();
  323. }
  324. /**
  325. * testEnableCryptoExceptionEnableTwice
  326. *
  327. * @expectedException \Cake\Network\Exception\SocketException
  328. * @return void
  329. */
  330. public function testEnableCryptoExceptionEnableTwice()
  331. {
  332. $this->skipIf(!function_exists('stream_socket_enable_crypto'), 'Broken on HHVM');
  333. // testing on tls server
  334. $this->_connectSocketToSslTls();
  335. $this->Socket->enableCrypto('tls', 'client');
  336. $this->Socket->enableCrypto('tls', 'client');
  337. }
  338. /**
  339. * testEnableCryptoExceptionDisableTwice
  340. *
  341. * @expectedException \Cake\Network\Exception\SocketException
  342. * @return void
  343. */
  344. public function testEnableCryptoExceptionDisableTwice()
  345. {
  346. $this->skipIf(!function_exists('stream_socket_enable_crypto'), 'Broken on HHVM');
  347. // testing on tls server
  348. $this->_connectSocketToSslTls();
  349. $this->Socket->enableCrypto('tls', 'client', false);
  350. }
  351. /**
  352. * testEnableCryptoEnableStatus
  353. *
  354. * @return void
  355. */
  356. public function testEnableCryptoEnableStatus()
  357. {
  358. $this->skipIf(!function_exists('stream_socket_enable_crypto'), 'Broken on HHVM');
  359. // testing on tls server
  360. $this->_connectSocketToSslTls();
  361. $this->assertFalse($this->Socket->encrypted);
  362. $this->Socket->enableCrypto('tls', 'client', true);
  363. $this->assertTrue($this->Socket->encrypted);
  364. }
  365. /**
  366. * test getting the context for a socket.
  367. *
  368. * @return void
  369. */
  370. public function testGetContext()
  371. {
  372. $this->skipIf(
  373. !extension_loaded('openssl') || defined('HHVM_VERSION'),
  374. 'OpenSSL is not enabled cannot test SSL.'
  375. );
  376. $config = [
  377. 'host' => 'smtp.gmail.com',
  378. 'port' => 465,
  379. 'timeout' => 5,
  380. 'context' => [
  381. 'ssl' => ['capture_peer' => true]
  382. ]
  383. ];
  384. try {
  385. $this->Socket = new Socket($config);
  386. $this->Socket->connect();
  387. } catch (SocketException $e) {
  388. $this->markTestSkipped('No network, skipping test.');
  389. }
  390. $result = $this->Socket->context();
  391. $this->assertTrue($result['ssl']['capture_peer']);
  392. }
  393. /**
  394. * test configuring the context from the flat keys.
  395. *
  396. * @return void
  397. */
  398. public function testConfigContext()
  399. {
  400. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  401. $this->skipIf(!empty(getenv('http_proxy')) || !empty(getenv('https_proxy')), 'Proxy detected and cannot test SSL.');
  402. $config = [
  403. 'host' => 'smtp.gmail.com',
  404. 'port' => 465,
  405. 'timeout' => 5,
  406. 'ssl_verify_peer' => true,
  407. 'ssl_allow_self_signed' => false,
  408. 'ssl_verify_depth' => 5,
  409. 'ssl_verify_host' => true,
  410. ];
  411. $socket = new Socket($config);
  412. $socket->connect();
  413. $result = $socket->context();
  414. $this->assertTrue($result['ssl']['verify_peer']);
  415. $this->assertFalse($result['ssl']['allow_self_signed']);
  416. $this->assertEquals(5, $result['ssl']['verify_depth']);
  417. $this->assertEquals('smtp.gmail.com', $result['ssl']['CN_match']);
  418. $this->assertArrayNotHasKey('ssl_verify_peer', $socket->config());
  419. $this->assertArrayNotHasKey('ssl_allow_self_signed', $socket->config());
  420. $this->assertArrayNotHasKey('ssl_verify_host', $socket->config());
  421. $this->assertArrayNotHasKey('ssl_verify_depth', $socket->config());
  422. }
  423. }