SocketTest.php 14 KB

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