SocketTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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\Socket;
  17. use Cake\Network\Error\SocketException;
  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' => getprotobyname('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'] = 17;
  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 (\Cake\Network\Error\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\Error\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 (\Cake\Network\Error\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 (\Cake\Network\Error\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 (\Cake\Network\Error\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 (\Cake\Network\Error\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\Error\SocketException
  228. * @return void
  229. */
  230. public function testEnableCryptoSocketExceptionNoSsl() {
  231. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  232. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  233. $configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
  234. // testing exception on no ssl socket server for ssl and tls methods
  235. $this->Socket = new Socket($configNoSslOrTls);
  236. $this->Socket->connect();
  237. $this->Socket->enableCrypto('sslv3', 'client');
  238. }
  239. /**
  240. * testEnableCryptoSocketExceptionNoTls
  241. *
  242. * @expectedException \Cake\Network\Error\SocketException
  243. * @return void
  244. */
  245. public function testEnableCryptoSocketExceptionNoTls() {
  246. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  247. $configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
  248. // testing exception on no ssl socket server for ssl and tls methods
  249. $this->Socket = new Socket($configNoSslOrTls);
  250. $this->Socket->connect();
  251. $this->Socket->enableCrypto('tls', 'client');
  252. }
  253. /**
  254. * _connectSocketToSslTls
  255. *
  256. * @return void
  257. */
  258. protected function _connectSocketToSslTls() {
  259. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  260. $configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
  261. $this->Socket = new Socket($configSslTls);
  262. try {
  263. $this->Socket->connect();
  264. } catch (\Cake\Network\Error\SocketException $e) {
  265. $this->markTestSkipped('Cannot test network, skipping.');
  266. }
  267. }
  268. /**
  269. * testEnableCryptoBadMode
  270. *
  271. * @expectedException \InvalidArgumentException
  272. * @return void
  273. */
  274. public function testEnableCryptoBadMode() {
  275. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  276. // testing wrong encryption mode
  277. $this->_connectSocketToSslTls();
  278. $this->Socket->enableCrypto('doesntExistMode', 'server');
  279. $this->Socket->disconnect();
  280. }
  281. /**
  282. * testEnableCrypto
  283. *
  284. * @return void
  285. */
  286. public function testEnableCrypto() {
  287. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  288. // testing on ssl server
  289. $this->_connectSocketToSslTls();
  290. $this->assertTrue($this->Socket->enableCrypto('sslv3', 'client'));
  291. $this->Socket->disconnect();
  292. // testing on tls server
  293. $this->_connectSocketToSslTls();
  294. $this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
  295. $this->Socket->disconnect();
  296. }
  297. /**
  298. * testEnableCryptoExceptionEnableTwice
  299. *
  300. * @expectedException \Cake\Network\Error\SocketException
  301. * @return void
  302. */
  303. public function testEnableCryptoExceptionEnableTwice() {
  304. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  305. // testing on tls server
  306. $this->_connectSocketToSslTls();
  307. $this->Socket->enableCrypto('tls', 'client');
  308. $this->Socket->enableCrypto('tls', 'client');
  309. }
  310. /**
  311. * testEnableCryptoExceptionDisableTwice
  312. *
  313. * @expectedException \Cake\Network\Error\SocketException
  314. * @return void
  315. */
  316. public function testEnableCryptoExceptionDisableTwice() {
  317. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  318. // testing on tls server
  319. $this->_connectSocketToSslTls();
  320. $this->Socket->enableCrypto('tls', 'client', false);
  321. }
  322. /**
  323. * testEnableCryptoEnableStatus
  324. *
  325. * @return void
  326. */
  327. public function testEnableCryptoEnableStatus() {
  328. $this->assertFalse(defined('HHVM_VERSION'), 'Broken on HHVM');
  329. // testing on tls server
  330. $this->_connectSocketToSslTls();
  331. $this->assertFalse($this->Socket->encrypted);
  332. $this->Socket->enableCrypto('tls', 'client', true);
  333. $this->assertTrue($this->Socket->encrypted);
  334. }
  335. /**
  336. * test getting the context for a socket.
  337. *
  338. * @return void
  339. */
  340. public function testGetContext() {
  341. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  342. $config = array(
  343. 'host' => 'smtp.gmail.com',
  344. 'port' => 465,
  345. 'timeout' => 5,
  346. 'context' => array(
  347. 'ssl' => array('capture_peer' => true)
  348. )
  349. );
  350. try {
  351. $this->Socket = new Socket($config);
  352. $this->Socket->connect();
  353. } catch (SocketException $e) {
  354. $this->markTestSkipped('No network, skipping test.');
  355. }
  356. $result = $this->Socket->context();
  357. $this->assertEquals($config['context'], $result);
  358. }
  359. }