SocketTest.php 13 KB

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