SocketTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Network;
  17. use Cake\Network\Exception\SocketException;
  18. use Cake\Network\Socket;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * SocketTest class
  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->getConfig();
  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->getConfig(), $config);
  65. $this->Socket = new Socket(['host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp']);
  66. $config = $this->Socket->getConfig();
  67. $config['host'] = 'www.cakephp.org';
  68. $config['port'] = 23;
  69. $config['protocol'] = 'udp';
  70. $this->assertSame($this->Socket->getConfig(), $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. * @return void
  113. */
  114. public function testInvalidConnection($data)
  115. {
  116. $this->expectException(\Cake\Network\Exception\SocketException::class);
  117. $this->Socket->setConfig($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->assertNull($this->Socket->lastError());
  133. $this->assertContains('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->assertNull($this->Socket->lastError());
  139. $this->assertContains('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->assertNull($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->assertNull($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' => 1];
  186. $this->Socket = new Socket($config);
  187. try {
  188. $this->assertTrue($this->Socket->connect());
  189. $config = ['host' => '127.0.0.1', 'timeout' => 1];
  190. $this->Socket = new Socket($config);
  191. $this->assertNull($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->getConfig(),
  234. 'Reset should cause config to return the defaults defined in _defaultConfig'
  235. );
  236. }
  237. /**
  238. * testEncrypt
  239. *
  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' => 1];
  246. // testing exception on no ssl socket server for ssl and tls methods
  247. $this->Socket = new Socket($configNoSslOrTls);
  248. try {
  249. $this->Socket->connect();
  250. } catch (SocketException $e) {
  251. $this->markTestSkipped('Cannot test network, skipping.');
  252. }
  253. $e = null;
  254. try {
  255. $this->Socket->enableCrypto('tlsv10', 'client');
  256. } catch (SocketException $e) {
  257. }
  258. $this->assertNotNull($e);
  259. $this->assertInstanceOf('Exception', $e->getPrevious());
  260. }
  261. /**
  262. * testEnableCryptoSocketExceptionNoTls
  263. *
  264. * @return void
  265. */
  266. public function testEnableCryptoSocketExceptionNoTls()
  267. {
  268. $configNoSslOrTls = ['host' => 'localhost', 'port' => 80, 'timeout' => 1];
  269. // testing exception on no ssl socket server for ssl and tls methods
  270. $this->Socket = new Socket($configNoSslOrTls);
  271. try {
  272. $this->Socket->connect();
  273. } catch (SocketException $e) {
  274. $this->markTestSkipped('Cannot test network, skipping.');
  275. }
  276. $e = null;
  277. try {
  278. $this->Socket->enableCrypto('tls', 'client');
  279. } catch (SocketException $e) {
  280. }
  281. $this->assertNotNull($e);
  282. $this->assertInstanceOf('Exception', $e->getPrevious());
  283. }
  284. /**
  285. * Test that protocol in the host doesn't cause cert errors.
  286. *
  287. * @return void
  288. */
  289. public function testConnectProtocolInHost()
  290. {
  291. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  292. $configSslTls = ['host' => 'ssl://smtp.gmail.com', 'port' => 465, 'timeout' => 5];
  293. $socket = new Socket($configSslTls);
  294. try {
  295. $socket->connect();
  296. $this->assertEquals('smtp.gmail.com', $socket->getConfig('host'));
  297. $this->assertEquals('ssl', $socket->getConfig('protocol'));
  298. } catch (SocketException $e) {
  299. $this->markTestSkipped('Cannot test network, skipping.');
  300. }
  301. }
  302. /**
  303. * _connectSocketToSslTls
  304. *
  305. * @return void
  306. */
  307. protected function _connectSocketToSslTls()
  308. {
  309. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  310. $configSslTls = ['host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5];
  311. $this->Socket = new Socket($configSslTls);
  312. try {
  313. $this->Socket->connect();
  314. } catch (SocketException $e) {
  315. $this->markTestSkipped('Cannot test network, skipping.');
  316. }
  317. }
  318. /**
  319. * testEnableCryptoBadMode
  320. *
  321. * @return void
  322. */
  323. public function testEnableCryptoBadMode()
  324. {
  325. $this->expectException(\InvalidArgumentException::class);
  326. // testing wrong encryption mode
  327. $this->_connectSocketToSslTls();
  328. $this->Socket->enableCrypto('doesntExistMode', 'server');
  329. $this->Socket->disconnect();
  330. }
  331. /**
  332. * testEnableCrypto
  333. *
  334. * @return void
  335. */
  336. public function testEnableCrypto()
  337. {
  338. $this->_connectSocketToSslTls();
  339. $this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
  340. $this->Socket->disconnect();
  341. }
  342. /**
  343. * testEnableCryptoExceptionEnableTwice
  344. *
  345. * @return void
  346. */
  347. public function testEnableCryptoExceptionEnableTwice()
  348. {
  349. $this->expectException(\Cake\Network\Exception\SocketException::class);
  350. // testing on tls server
  351. $this->_connectSocketToSslTls();
  352. $this->Socket->enableCrypto('tls', 'client');
  353. $this->Socket->enableCrypto('tls', 'client');
  354. }
  355. /**
  356. * testEnableCryptoExceptionDisableTwice
  357. *
  358. * @return void
  359. */
  360. public function testEnableCryptoExceptionDisableTwice()
  361. {
  362. $this->expectException(\Cake\Network\Exception\SocketException::class);
  363. $this->_connectSocketToSslTls();
  364. $this->Socket->enableCrypto('tls', 'client', false);
  365. }
  366. /**
  367. * testEnableCryptoEnableStatus
  368. *
  369. * @return void
  370. */
  371. public function testEnableCryptoEnableTls12()
  372. {
  373. $this->_connectSocketToSslTls();
  374. $this->assertFalse($this->Socket->encrypted);
  375. $this->Socket->enableCrypto('tlsv12', 'client', true);
  376. $this->assertTrue($this->Socket->encrypted);
  377. }
  378. /**
  379. * testEnableCryptoEnableStatus
  380. *
  381. * @return void
  382. */
  383. public function testEnableCryptoEnableStatus()
  384. {
  385. $this->_connectSocketToSslTls();
  386. $this->assertFalse($this->Socket->encrypted);
  387. $this->Socket->enableCrypto('tls', 'client', true);
  388. $this->assertTrue($this->Socket->encrypted);
  389. }
  390. /**
  391. * test getting the context for a socket.
  392. *
  393. * @return void
  394. */
  395. public function testGetContext()
  396. {
  397. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  398. $config = [
  399. 'host' => 'smtp.gmail.com',
  400. 'port' => 465,
  401. 'timeout' => 5,
  402. 'context' => [
  403. 'ssl' => ['capture_peer' => true],
  404. ],
  405. ];
  406. try {
  407. $this->Socket = new Socket($config);
  408. $this->Socket->connect();
  409. } catch (SocketException $e) {
  410. $this->markTestSkipped('No network, skipping test.');
  411. }
  412. $result = $this->Socket->context();
  413. $this->assertTrue($result['ssl']['capture_peer']);
  414. }
  415. /**
  416. * test configuring the context from the flat keys.
  417. *
  418. * @return void
  419. */
  420. public function testConfigContext()
  421. {
  422. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  423. $this->skipIf(!empty(getenv('http_proxy')) || !empty(getenv('https_proxy')), 'Proxy detected and cannot test SSL.');
  424. $config = [
  425. 'host' => 'smtp.gmail.com',
  426. 'port' => 465,
  427. 'timeout' => 5,
  428. 'ssl_verify_peer' => true,
  429. 'ssl_allow_self_signed' => false,
  430. 'ssl_verify_depth' => 5,
  431. 'ssl_verify_host' => true,
  432. ];
  433. $socket = new Socket($config);
  434. $socket->connect();
  435. $result = $socket->context();
  436. $this->assertTrue($result['ssl']['verify_peer']);
  437. $this->assertFalse($result['ssl']['allow_self_signed']);
  438. $this->assertEquals(5, $result['ssl']['verify_depth']);
  439. $this->assertEquals('smtp.gmail.com', $result['ssl']['CN_match']);
  440. $this->assertArrayNotHasKey('ssl_verify_peer', $socket->getConfig());
  441. $this->assertArrayNotHasKey('ssl_allow_self_signed', $socket->getConfig());
  442. $this->assertArrayNotHasKey('ssl_verify_host', $socket->getConfig());
  443. $this->assertArrayNotHasKey('ssl_verify_depth', $socket->getConfig());
  444. }
  445. }