SessionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. $this->assertEmpty($session->id());
  242. }
  243. /**
  244. * testCheckingSavedEmpty method
  245. *
  246. * @return void
  247. */
  248. public function testCheckingSavedEmpty() {
  249. $session = new Session();
  250. $session->write('SessionTestCase', 0);
  251. $this->assertTrue($session->check('SessionTestCase'));
  252. $session->write('SessionTestCase', '0');
  253. $this->assertTrue($session->check('SessionTestCase'));
  254. $session->write('SessionTestCase', false);
  255. $this->assertTrue($session->check('SessionTestCase'));
  256. $session->write('SessionTestCase', null);
  257. $this->assertFalse($session->check('SessionTestCase'));
  258. }
  259. /**
  260. * testCheckKeyWithSpaces method
  261. *
  262. * @return void
  263. */
  264. public function testCheckKeyWithSpaces() {
  265. $session = new Session();
  266. $session->write('Session Test', "test");
  267. $this->assertTrue($session->check('Session Test'));
  268. $session->delete('Session Test');
  269. $session->write('Session Test.Test Case', "test");
  270. $this->assertTrue($session->check('Session Test.Test Case'));
  271. }
  272. /**
  273. * testCheckEmpty
  274. *
  275. * @return void
  276. */
  277. public function testCheckEmpty() {
  278. $session = new Session();
  279. $this->assertFalse($session->check());
  280. }
  281. /**
  282. * test key exploitation
  283. *
  284. * @return void
  285. */
  286. public function testKeyExploit() {
  287. $session = new Session();
  288. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  289. $session->write($key, 'haxored');
  290. $result = $session->read($key);
  291. $this->assertNull($result);
  292. }
  293. /**
  294. * testReadingSavedEmpty method
  295. *
  296. * @return void
  297. */
  298. public function testReadingSavedEmpty() {
  299. $session = new Session();
  300. $session->write('SessionTestCase', 0);
  301. $this->assertEquals(0, $session->read('SessionTestCase'));
  302. $session->write('SessionTestCase', '0');
  303. $this->assertEquals('0', $session->read('SessionTestCase'));
  304. $this->assertFalse($session->read('SessionTestCase') === 0);
  305. $session->write('SessionTestCase', false);
  306. $this->assertFalse($session->read('SessionTestCase'));
  307. $session->write('SessionTestCase', null);
  308. $this->assertEquals(null, $session->read('SessionTestCase'));
  309. }
  310. /**
  311. * test using a handler from app/Model/Datasource/Session.
  312. *
  313. * @return void
  314. */
  315. public function testUsingAppLibsHandler() {
  316. \Cake\Core\Configure::write('App.namespace', 'TestApp');
  317. $config = [
  318. 'defaults' => 'cake',
  319. 'handler' => array(
  320. 'engine' => 'TestAppLibSession'
  321. )
  322. ];
  323. $session = Session::create($config);
  324. $this->assertInstanceOf('TestApp\Network\Session\TestAppLibSession', $session->engine());
  325. $this->assertEquals('user', ini_get('session.save_handler'));
  326. }
  327. /**
  328. * test using a handler from a plugin.
  329. *
  330. * @return void
  331. */
  332. public function testUsingPluginHandler() {
  333. \Cake\Core\Configure::write('App.namespace', 'TestApp');
  334. \Cake\Core\Plugin::load('TestPlugin');
  335. $config = [
  336. 'defaults' => 'cake',
  337. 'handler' => array(
  338. 'engine' => 'TestPlugin.TestPluginSession'
  339. )
  340. ];
  341. $session = Session::create($config);
  342. $this->assertInstanceOf('TestPlugin\Network\Session\TestPluginSession', $session->engine());
  343. $this->assertEquals('user', ini_get('session.save_handler'));
  344. }
  345. /**
  346. * testSessionTimeout method
  347. *
  348. * @return void
  349. */
  350. public function testSessionTimeout() {
  351. Configure::write('debug', true);
  352. Configure::write('Session.defaults', 'cake');
  353. Configure::write('Session.autoRegenerate', false);
  354. $timeoutSeconds = Configure::read('Session.timeout') * 60;
  355. TestCakeSession::destroy();
  356. TestCakeSession::write('Test', 'some value');
  357. $this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
  358. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  359. $this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
  360. $this->assertWithinMargin(time(), Session::$time, 1);
  361. $this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  362. Configure::write('Session.harden', true);
  363. TestCakeSession::destroy();
  364. TestCakeSession::write('Test', 'some value');
  365. $this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
  366. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  367. $this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
  368. $this->assertWithinMargin(time(), Session::$time, 1);
  369. $this->assertWithinMargin(Session::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  370. }
  371. /**
  372. * Test that cookieTimeout matches timeout when unspecified.
  373. *
  374. * @return void
  375. */
  376. public function testCookieTimeoutFallback() {
  377. $_SESSION = null;
  378. Configure::write('Session', array(
  379. 'defaults' => 'cake',
  380. 'timeout' => 400,
  381. ));
  382. TestCakeSession::start();
  383. $this->assertEquals(400, Configure::read('Session.cookieTimeout'));
  384. $this->assertEquals(400, Configure::read('Session.timeout'));
  385. $this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
  386. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  387. $_SESSION = null;
  388. Configure::write('Session', array(
  389. 'defaults' => 'cake',
  390. 'timeout' => 400,
  391. 'cookieTimeout' => 600
  392. ));
  393. TestCakeSession::start();
  394. $this->assertEquals(600, Configure::read('Session.cookieTimeout'));
  395. $this->assertEquals(400, Configure::read('Session.timeout'));
  396. }
  397. /**
  398. * Proves that invalid sessions will be destroyed and re-created
  399. * if invalid
  400. *
  401. * @return void
  402. */
  403. public function testInvalidSessionRenew() {
  404. TestCakeSession::start();
  405. $this->assertNotEmpty($_SESSION['Config']);
  406. $data = $_SESSION;
  407. session_write_close();
  408. $_SESSION = null;
  409. TestCakeSession::start();
  410. $this->assertEquals($data, $_SESSION);
  411. TestCakeSession::write('Foo', 'Bar');
  412. session_write_close();
  413. $_SESSION = null;
  414. TestCakeSession::userAgent('bogus!');
  415. TestCakeSession::start();
  416. $this->assertNotEquals($data, $_SESSION);
  417. }
  418. }