SocketTest.php 10 KB

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