CakeSocketTest.php 9.0 KB

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