SessionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * SessionTest 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 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Network;
  18. use Cake\Cache\Cache;
  19. use Cake\Core\App;
  20. use Cake\Core\Plugin;
  21. use Cake\Network\Session;
  22. use Cake\Network\Session\CacheSession;
  23. use Cake\Network\Session\DatabaseSession;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * Class TestCacheSession
  27. *
  28. */
  29. class TestCacheSession extends CacheSession {
  30. protected function _writeSession() {
  31. return true;
  32. }
  33. }
  34. /**
  35. * Class TestDatabaseSession
  36. *
  37. */
  38. class TestDatabaseSession extends DatabaseSession {
  39. protected function _writeSession() {
  40. return true;
  41. }
  42. }
  43. /**
  44. * SessionTest class
  45. *
  46. */
  47. class SessionTest extends TestCase {
  48. protected static $_gcDivisor;
  49. /**
  50. * Fixtures used in the SessionTest
  51. *
  52. * @var array
  53. */
  54. public $fixtures = array('core.session', 'core.cake_session');
  55. /**
  56. * setup before class.
  57. *
  58. * @return void
  59. */
  60. public static function setupBeforeClass() {
  61. // Make sure garbage colector will be called
  62. static::$_gcDivisor = ini_get('session.gc_divisor');
  63. ini_set('session.gc_divisor', '1');
  64. }
  65. /**
  66. * teardown after class
  67. *
  68. * @return void
  69. */
  70. public static function teardownAfterClass() {
  71. // Revert to the default setting
  72. ini_set('session.gc_divisor', static::$_gcDivisor);
  73. }
  74. /**
  75. * setUp method
  76. *
  77. * @return void
  78. */
  79. public function setUp() {
  80. parent::setUp();
  81. }
  82. /**
  83. * tearDown method
  84. *
  85. * @return void
  86. */
  87. public function tearDown() {
  88. unset($_SESSION);
  89. parent::tearDown();
  90. }
  91. /**
  92. * test setting ini properties with Session configuration.
  93. *
  94. * @return void
  95. */
  96. public function testSessionConfigIniSetting() {
  97. $_SESSION = null;
  98. $config = array(
  99. 'cookie' => 'test',
  100. 'checkAgent' => false,
  101. 'timeout' => 86400,
  102. 'ini' => array(
  103. 'session.referer_check' => 'example.com',
  104. 'session.use_trans_sid' => false
  105. )
  106. );
  107. $session = Session::create($config);
  108. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  109. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  110. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  111. }
  112. /**
  113. * testCheck method
  114. *
  115. * @return void
  116. */
  117. public function testCheck() {
  118. $session = new Session();
  119. $session->write('SessionTestCase', 'value');
  120. $this->assertTrue($session->check('SessionTestCase'));
  121. $this->assertFalse($session->check('NotExistingSessionTestCase'));
  122. $this->assertFalse($session->check('Crazy.foo'));
  123. $session->write('Crazy.foo', ['bar' => 'baz']);
  124. $this->assertTrue($session->check('Crazy.foo'));
  125. $this->assertTrue($session->check('Crazy.foo.bar'));
  126. }
  127. /**
  128. * testSimpleRead method
  129. *
  130. * @return void
  131. */
  132. public function testSimpleRead() {
  133. $session = new Session();
  134. $session->write('testing', '1,2,3');
  135. $result = $session->read('testing');
  136. $this->assertEquals('1,2,3', $result);
  137. $session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
  138. $result = $session->read('testing.1');
  139. $this->assertEquals('one', $result);
  140. $result = $session->read('testing');
  141. $this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);
  142. $result = $session->read();
  143. $this->assertTrue(isset($result['testing']));
  144. $session->write('This.is.a.deep.array.my.friend', 'value');
  145. $result = $session->read('This.is.a.deep.array');
  146. $this->assertEquals(['my' => ['friend' => 'value']], $result);
  147. }
  148. /**
  149. * testReadEmpty
  150. *
  151. * @return void
  152. */
  153. public function testReadEmpty() {
  154. $session = new Session();
  155. $this->assertNull($session->read(''));
  156. }
  157. /**
  158. * test writing a hash of values/
  159. *
  160. * @return void
  161. */
  162. public function testWriteArray() {
  163. $session = new Session();
  164. $session->write([
  165. 'one' => 1,
  166. 'two' => 2,
  167. 'three' => ['something'],
  168. 'null' => null
  169. ]);
  170. $this->assertEquals(1, $session->read('one'));
  171. $this->assertEquals(['something'], $session->read('three'));
  172. $this->assertEquals(null, $session->read('null'));
  173. }
  174. /**
  175. * Test overwriting a string value as if it were an array.
  176. *
  177. * @return void
  178. */
  179. public function testWriteOverwriteStringValue() {
  180. $session = new Session();
  181. $session->write('Some.string', 'value');
  182. $this->assertEquals('value', $session->read('Some.string'));
  183. $session->write('Some.string.array', ['values']);
  184. $this->assertEquals(['values'], $session->read('Some.string.array'));
  185. }
  186. /**
  187. * testId method
  188. *
  189. * @return void
  190. */
  191. public function testId() {
  192. $session = new Session();
  193. $result = $session->id();
  194. $expected = session_id();
  195. $this->assertEquals($expected, $result);
  196. $session->id('MySessionId');
  197. $this->assertEquals('MySessionId', $session->id());
  198. $this->assertEquals('MySessionId', session_id());
  199. $session->id('');
  200. $this->assertEquals('', session_id());
  201. }
  202. /**
  203. * testStarted method
  204. *
  205. * @return void
  206. */
  207. public function testStarted() {
  208. $session = new Session();
  209. $this->assertFalse($session->started());
  210. $this->assertTrue($session->start());
  211. $this->assertTrue($session->started());
  212. }
  213. /**
  214. * testDel method
  215. *
  216. * @return void
  217. */
  218. public function testDelete() {
  219. $session = new Session();
  220. $session->write('Delete.me', 'Clearing out');
  221. $session->delete('Delete.me');
  222. $this->assertFalse($session->check('Delete.me'));
  223. $this->assertTrue($session->check('Delete'));
  224. $session->write('Clearing.sale', 'everything must go');
  225. $session->delete('Clearing');
  226. $this->assertFalse($session->check('Clearing.sale'));
  227. $this->assertFalse($session->check('Clearing'));
  228. }
  229. /**
  230. * testDestroy method
  231. *
  232. * @return void
  233. */
  234. public function testDestroy() {
  235. $session = new Session();
  236. $session->start();
  237. $session->write('bulletProof', 'invincible');
  238. $session->id('foo');
  239. $session->destroy();
  240. $this->assertFalse($session->check('bulletProof'));
  241. }
  242. /**
  243. * testCheckingSavedEmpty method
  244. *
  245. * @return void
  246. */
  247. public function testCheckingSavedEmpty() {
  248. $session = new Session();
  249. $session->write('SessionTestCase', 0);
  250. $this->assertTrue($session->check('SessionTestCase'));
  251. $session->write('SessionTestCase', '0');
  252. $this->assertTrue($session->check('SessionTestCase'));
  253. $session->write('SessionTestCase', false);
  254. $this->assertTrue($session->check('SessionTestCase'));
  255. $session->write('SessionTestCase', null);
  256. $this->assertFalse($session->check('SessionTestCase'));
  257. }
  258. /**
  259. * testCheckKeyWithSpaces method
  260. *
  261. * @return void
  262. */
  263. public function testCheckKeyWithSpaces() {
  264. $session = new Session();
  265. $session->write('Session Test', "test");
  266. $this->assertTrue($session->check('Session Test'));
  267. $session->delete('Session Test');
  268. $session->write('Session Test.Test Case', "test");
  269. $this->assertTrue($session->check('Session Test.Test Case'));
  270. }
  271. /**
  272. * testCheckEmpty
  273. *
  274. * @return void
  275. */
  276. public function testCheckEmpty() {
  277. $session = new Session();
  278. $this->assertFalse($session->check());
  279. }
  280. /**
  281. * test key exploitation
  282. *
  283. * @return void
  284. */
  285. public function testKeyExploit() {
  286. $session = new Session();
  287. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  288. $session->write($key, 'haxored');
  289. $result = $session->read($key);
  290. $this->assertNull($result);
  291. }
  292. /**
  293. * testReadingSavedEmpty method
  294. *
  295. * @return void
  296. */
  297. public function testReadingSavedEmpty() {
  298. $session = new Session();
  299. $session->write('SessionTestCase', 0);
  300. $this->assertEquals(0, $session->read('SessionTestCase'));
  301. $session->write('SessionTestCase', '0');
  302. $this->assertEquals('0', $session->read('SessionTestCase'));
  303. $this->assertFalse($session->read('SessionTestCase') === 0);
  304. $session->write('SessionTestCase', false);
  305. $this->assertFalse($session->read('SessionTestCase'));
  306. $session->write('SessionTestCase', null);
  307. $this->assertEquals(null, $session->read('SessionTestCase'));
  308. }
  309. /**
  310. * test using a handler from app/Model/Datasource/Session.
  311. *
  312. * @return void
  313. */
  314. public function testUsingAppLibsHandler() {
  315. \Cake\Core\Configure::write('App.namespace', 'TestApp');
  316. $config = [
  317. 'defaults' => 'cake',
  318. 'handler' => array(
  319. 'engine' => 'TestAppLibSession',
  320. 'these' => 'are',
  321. 'a few' => 'options'
  322. )
  323. ];
  324. $session = Session::create($config);
  325. $this->assertInstanceOf('TestApp\Network\Session\TestAppLibSession', $session->engine());
  326. $this->assertEquals('user', ini_get('session.save_handler'));
  327. $this->assertEquals(['these' => 'are', 'a few' => 'options'], $session->engine()->options);
  328. }
  329. /**
  330. * test using a handler from a plugin.
  331. *
  332. * @return void
  333. */
  334. public function testUsingPluginHandler() {
  335. \Cake\Core\Configure::write('App.namespace', 'TestApp');
  336. \Cake\Core\Plugin::load('TestPlugin');
  337. $config = [
  338. 'defaults' => 'cake',
  339. 'handler' => array(
  340. 'engine' => 'TestPlugin.TestPluginSession'
  341. )
  342. ];
  343. $session = Session::create($config);
  344. $this->assertInstanceOf('TestPlugin\Network\Session\TestPluginSession', $session->engine());
  345. $this->assertEquals('user', ini_get('session.save_handler'));
  346. }
  347. /**
  348. * Tests that it is possible to pass an already made instance as the session engine
  349. *
  350. * @return void
  351. */
  352. public function testEngineWithPreMadeInstance() {
  353. \Cake\Core\Configure::write('App.namespace', 'TestApp');
  354. $engine = new \TestApp\Network\Session\TestAppLibSession;
  355. $session = new Session(['handler' => ['engine' => $engine]]);
  356. $this->assertSame($engine, $session->engine());
  357. $session = new Session();
  358. $session->engine($engine);
  359. $this->assertSame($engine, $session->engine());
  360. }
  361. /**
  362. * Test that cookieTimeout matches timeout when unspecified.
  363. *
  364. * @return void
  365. */
  366. public function testCookieTimeoutFallback() {
  367. $config = [
  368. 'defaults' => 'cake',
  369. 'timeout' => 400,
  370. ];
  371. $session = new Session($config);
  372. $this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
  373. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  374. }
  375. }