SocketTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 CakePHP(tm) v 1.2.0.4206
  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 (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\Error\SocketException
  108. * return void
  109. */
  110. public function testInvalidConnection($data) {
  111. $this->Socket->config = array_merge($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 (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 (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 (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 (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. $this->assertEquals(array(), $anotherSocket->config);
  213. }
  214. /**
  215. * testEncrypt
  216. *
  217. * @expectedException Cake\Error\SocketException
  218. * @return void
  219. */
  220. public function testEnableCryptoSocketExceptionNoSsl() {
  221. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  222. $configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
  223. // testing exception on no ssl socket server for ssl and tls methods
  224. $this->Socket = new Socket($configNoSslOrTls);
  225. $this->Socket->connect();
  226. $this->Socket->enableCrypto('sslv3', 'client');
  227. }
  228. /**
  229. * testEnableCryptoSocketExceptionNoTls
  230. *
  231. * @expectedException Cake\Error\SocketException
  232. * @return void
  233. */
  234. public function testEnableCryptoSocketExceptionNoTls() {
  235. $configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
  236. // testing exception on no ssl socket server for ssl and tls methods
  237. $this->Socket = new Socket($configNoSslOrTls);
  238. $this->Socket->connect();
  239. $this->Socket->enableCrypto('tls', 'client');
  240. }
  241. /**
  242. * _connectSocketToSslTls
  243. *
  244. * @return void
  245. */
  246. protected function _connectSocketToSslTls() {
  247. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  248. $configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
  249. $this->Socket = new Socket($configSslTls);
  250. try {
  251. $this->Socket->connect();
  252. } catch (Error\SocketException $e) {
  253. $this->markTestSkipped('Cannot test network, skipping.');
  254. }
  255. }
  256. /**
  257. * testEnableCryptoBadMode
  258. *
  259. * @expectedException \InvalidArgumentException
  260. * @return void
  261. */
  262. public function testEnableCryptoBadMode() {
  263. // testing wrong encryption mode
  264. $this->_connectSocketToSslTls();
  265. $this->Socket->enableCrypto('doesntExistMode', 'server');
  266. $this->Socket->disconnect();
  267. }
  268. /**
  269. * testEnableCrypto
  270. *
  271. * @return void
  272. */
  273. public function testEnableCrypto() {
  274. // testing on ssl server
  275. $this->_connectSocketToSslTls();
  276. $this->assertTrue($this->Socket->enableCrypto('sslv3', 'client'));
  277. $this->Socket->disconnect();
  278. // testing on tls server
  279. $this->_connectSocketToSslTls();
  280. $this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
  281. $this->Socket->disconnect();
  282. }
  283. /**
  284. * testEnableCryptoExceptionEnableTwice
  285. *
  286. * @expectedException Cake\Error\SocketException
  287. * @return void
  288. */
  289. public function testEnableCryptoExceptionEnableTwice() {
  290. // testing on tls server
  291. $this->_connectSocketToSslTls();
  292. $this->Socket->enableCrypto('tls', 'client');
  293. $this->Socket->enableCrypto('tls', 'client');
  294. }
  295. /**
  296. * testEnableCryptoExceptionDisableTwice
  297. *
  298. * @expectedException Cake\Error\SocketException
  299. * @return void
  300. */
  301. public function testEnableCryptoExceptionDisableTwice() {
  302. // testing on tls server
  303. $this->_connectSocketToSslTls();
  304. $this->Socket->enableCrypto('tls', 'client', false);
  305. }
  306. /**
  307. * testEnableCryptoEnableStatus
  308. *
  309. * @return void
  310. */
  311. public function testEnableCryptoEnableStatus() {
  312. // testing on tls server
  313. $this->_connectSocketToSslTls();
  314. $this->assertFalse($this->Socket->encrypted);
  315. $this->Socket->enableCrypto('tls', 'client', true);
  316. $this->assertTrue($this->Socket->encrypted);
  317. }
  318. /**
  319. * test getting the context for a socket.
  320. *
  321. * @return void
  322. */
  323. public function testGetContext() {
  324. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  325. $config = array(
  326. 'host' => 'smtp.gmail.com',
  327. 'port' => 465,
  328. 'timeout' => 5,
  329. 'context' => array(
  330. 'ssl' => array('capture_peer' => true)
  331. )
  332. );
  333. $this->Socket = new Socket($config);
  334. $this->Socket->connect();
  335. $result = $this->Socket->context();
  336. $this->assertEquals($config['context'], $result);
  337. }
  338. }