PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/Network/CakeSocketTest.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 373 lines | 197 code | 35 blank | 141 comment | 0 complexity | d710439579207be69cf8ee3f23272a0d MD5 | raw file
  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. * @package Cake.Test.Case.Network
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('CakeSocket', 'Network');
  19. /**
  20. * SocketTest class
  21. *
  22. * @package Cake.Test.Case.Network
  23. */
  24. class CakeSocketTest extends CakeTestCase {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->Socket = new CakeSocket(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 CakeSocket();
  50. $config = $this->Socket->config;
  51. $this->assertSame($config, array(
  52. 'persistent' => false,
  53. 'host' => 'localhost',
  54. 'protocol' => '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 CakeSocket(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'] = 'udp';
  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 CakeSocket($config);
  86. $this->Socket->connect();
  87. $this->assertTrue($this->Socket->connected);
  88. } catch (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 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 CakeSocket();
  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 CakeSocket(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 (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 CakeSocket(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 CakeSocket($config);
  162. $this->assertTrue($this->Socket->connect());
  163. $this->assertEquals(null, $this->Socket->read(26));
  164. $this->assertEquals('2: ' . __d('cake_dev', 'Connection timed out'), $this->Socket->lastError());
  165. } catch (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 CakeSocket($config);
  177. try {
  178. $this->assertTrue($this->Socket->connect());
  179. $config = array('host' => '127.0.0.1', 'timeout' => 0.00001);
  180. $this->Socket = new CakeSocket($config);
  181. $this->assertFalse($this->Socket->read(1024 * 1024));
  182. $this->assertEquals('2: ' . __d('cake_dev', 'Connection timed out'), $this->Socket->lastError());
  183. } catch (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 CakeSocket();
  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 CakeSocket($config);
  211. $anotherSocket->reset();
  212. $this->assertEquals(array(), $anotherSocket->config);
  213. }
  214. /**
  215. * testEncrypt
  216. *
  217. * @expectedException 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 CakeSocket($configNoSslOrTls);
  225. $this->Socket->connect();
  226. $this->Socket->enableCrypto('sslv3', 'client');
  227. }
  228. /**
  229. * testEnableCryptoSocketExceptionNoTls
  230. *
  231. * @expectedException 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 CakeSocket($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 CakeSocket($configSslTls);
  250. try {
  251. $this->Socket->connect();
  252. } catch (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 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 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 CakeSocket($config);
  334. $this->Socket->connect();
  335. $result = $this->Socket->context();
  336. $this->assertEquals($config['context'], $result);
  337. }
  338. }