SocketTest.php 10 KB

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