SocketTest.php 9.7 KB

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