SessionTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. $result = $session->id();
  198. $this->assertEquals('MySessionId', $result);
  199. $this->assertEquals('MySessionId', session_id());
  200. $session->id('');
  201. $this->assertEquals('', session_id());
  202. }
  203. /**
  204. * testStarted method
  205. *
  206. * @return void
  207. */
  208. public function testStarted() {
  209. unset($_SESSION);
  210. $_SESSION = null;
  211. $this->assertFalse(TestCakeSession::started());
  212. $this->assertTrue(TestCakeSession::start());
  213. $this->assertTrue(TestCakeSession::started());
  214. }
  215. /**
  216. * testDel method
  217. *
  218. * @return void
  219. */
  220. public function testDelete() {
  221. $this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
  222. $this->assertTrue(TestCakeSession::delete('Delete.me'));
  223. $this->assertFalse(TestCakeSession::check('Delete.me'));
  224. $this->assertTrue(TestCakeSession::check('Delete'));
  225. $this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
  226. $this->assertTrue(TestCakeSession::delete('Clearing'));
  227. $this->assertFalse(TestCakeSession::check('Clearing.sale'));
  228. $this->assertFalse(TestCakeSession::check('Clearing'));
  229. }
  230. /**
  231. * testDestroy method
  232. *
  233. * @return void
  234. */
  235. public function testDestroy() {
  236. TestCakeSession::write('bulletProof', 'invincible');
  237. $id = TestCakeSession::id();
  238. TestCakeSession::destroy();
  239. $this->assertFalse(TestCakeSession::check('bulletProof'));
  240. $this->assertNotEquals(TestCakeSession::id(), $id);
  241. }
  242. /**
  243. * testCheckingSavedEmpty method
  244. *
  245. * @return void
  246. */
  247. public function testCheckingSavedEmpty() {
  248. $this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
  249. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  250. $this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
  251. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  252. $this->assertTrue(TestCakeSession::write('SessionTestCase', false));
  253. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  254. $this->assertTrue(TestCakeSession::write('SessionTestCase', null));
  255. $this->assertFalse(TestCakeSession::check('SessionTestCase'));
  256. }
  257. /**
  258. * testCheckKeyWithSpaces method
  259. *
  260. * @return void
  261. */
  262. public function testCheckKeyWithSpaces() {
  263. $this->assertTrue(TestCakeSession::write('Session Test', "test"));
  264. $this->assertTrue(TestCakeSession::check('Session Test'));
  265. TestCakeSession::delete('Session Test');
  266. $this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
  267. $this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
  268. }
  269. /**
  270. * testCheckEmpty
  271. *
  272. * @return void
  273. */
  274. public function testCheckEmpty() {
  275. $this->assertFalse(TestCakeSession::check());
  276. }
  277. /**
  278. * test key exploitation
  279. *
  280. * @return void
  281. */
  282. public function testKeyExploit() {
  283. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  284. $result = TestCakeSession::write($key, 'haxored');
  285. $this->assertFalse($result);
  286. $result = TestCakeSession::read($key);
  287. $this->assertNull($result);
  288. }
  289. /**
  290. * testReadingSavedEmpty method
  291. *
  292. * @return void
  293. */
  294. public function testReadingSavedEmpty() {
  295. TestCakeSession::write('SessionTestCase', 0);
  296. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  297. TestCakeSession::write('SessionTestCase', '0');
  298. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  299. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  300. TestCakeSession::write('SessionTestCase', false);
  301. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  302. TestCakeSession::write('SessionTestCase', null);
  303. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  304. }
  305. /**
  306. * testCheckUserAgentFalse method
  307. *
  308. * @return void
  309. */
  310. public function testCheckUserAgentFalse() {
  311. Configure::write('Session.checkAgent', false);
  312. TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
  313. $this->assertTrue(TestCakeSession::valid());
  314. }
  315. /**
  316. * testCheckUserAgentTrue method
  317. *
  318. * @return void
  319. */
  320. public function testCheckUserAgentTrue() {
  321. Configure::write('Session.checkAgent', true);
  322. TestCakeSession::$error = false;
  323. $agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  324. TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
  325. TestCakeSession::setUserAgent($agent);
  326. $this->assertFalse(TestCakeSession::valid());
  327. }
  328. /**
  329. * testReadAndWriteWithCakeStorage method
  330. *
  331. * @return void
  332. */
  333. public function testReadAndWriteWithCakeStorage() {
  334. Configure::write('Session.defaults', 'cake');
  335. TestCakeSession::init();
  336. TestCakeSession::start();
  337. TestCakeSession::write('SessionTestCase', 0);
  338. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  339. TestCakeSession::write('SessionTestCase', '0');
  340. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  341. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  342. TestCakeSession::write('SessionTestCase', false);
  343. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  344. TestCakeSession::write('SessionTestCase', null);
  345. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  346. TestCakeSession::write('SessionTestCase', 'This is a Test');
  347. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  348. TestCakeSession::write('SessionTestCase', 'This is a Test');
  349. TestCakeSession::write('SessionTestCase', 'This was updated');
  350. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  351. TestCakeSession::destroy();
  352. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  353. }
  354. /**
  355. * test using a handler from app/Model/Datasource/Session.
  356. *
  357. * @return void
  358. */
  359. public function testUsingAppLibsHandler() {
  360. Configure::write('App.namespace', 'TestApp');
  361. Configure::write('Session', array(
  362. 'defaults' => 'cake',
  363. 'handler' => array(
  364. 'engine' => 'TestAppLibSession'
  365. )
  366. ));
  367. TestCakeSession::start();
  368. $this->assertTrue(TestCakeSession::started());
  369. TestCakeSession::destroy();
  370. $this->assertFalse(TestCakeSession::started());
  371. }
  372. /**
  373. * test using a handler from a plugin.
  374. *
  375. * @return void
  376. */
  377. public function testUsingPluginHandler() {
  378. Plugin::load('TestPlugin');
  379. Configure::write('App.namespace', 'TestApp');
  380. Configure::write('Session', array(
  381. 'defaults' => 'cake',
  382. 'handler' => array(
  383. 'engine' => 'TestPlugin.TestPluginSession'
  384. )
  385. ));
  386. TestCakeSession::start();
  387. $this->assertTrue(TestCakeSession::started());
  388. TestCakeSession::destroy();
  389. $this->assertFalse(TestCakeSession::started());
  390. }
  391. /**
  392. * testReadAndWriteWithCacheStorage method
  393. *
  394. * @return void
  395. */
  396. public function testReadAndWriteWithCacheStorage() {
  397. Cache::config('default', [
  398. 'engine' => 'File',
  399. 'path' => TMP,
  400. 'prefix' => 'session_test_'
  401. ]);
  402. Configure::write('Session.defaults', 'cache');
  403. Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestCacheSession');
  404. TestCakeSession::init();
  405. TestCakeSession::destroy();
  406. TestCakeSession::write('SessionTestCase', 0);
  407. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  408. TestCakeSession::write('SessionTestCase', '0');
  409. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  410. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  411. TestCakeSession::write('SessionTestCase', false);
  412. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  413. TestCakeSession::write('SessionTestCase', null);
  414. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  415. TestCakeSession::write('SessionTestCase', 'This is a Test');
  416. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  417. TestCakeSession::write('SessionTestCase', 'This is a Test');
  418. TestCakeSession::write('SessionTestCase', 'This was updated');
  419. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  420. TestCakeSession::destroy();
  421. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  422. }
  423. /**
  424. * test that changing the config name of the cache config works.
  425. *
  426. * @return void
  427. */
  428. public function testReadAndWriteWithCustomCacheConfig() {
  429. Configure::write('Session.defaults', 'cache');
  430. Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestCacheSession');
  431. Configure::write('Session.handler.config', 'session_test');
  432. Cache::config('session_test', [
  433. 'engine' => 'File',
  434. 'prefix' => 'session_test_'
  435. ]);
  436. TestCakeSession::init();
  437. TestCakeSession::start();
  438. TestCakeSession::write('SessionTestCase', 'Some value');
  439. $this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
  440. $id = TestCakeSession::id();
  441. Cache::delete($id, 'session_test');
  442. }
  443. /**
  444. * testReadAndWriteWithDatabaseStorage method
  445. *
  446. * @return void
  447. */
  448. public function testReadAndWriteWithDatabaseStorage() {
  449. Configure::write('Session.defaults', 'database');
  450. Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestDatabaseSession');
  451. TestCakeSession::init();
  452. $this->assertNull(TestCakeSession::id());
  453. TestCakeSession::start();
  454. $expected = session_id();
  455. $this->assertEquals($expected, TestCakeSession::id());
  456. TestCakeSession::renew();
  457. $this->assertFalse($expected === TestCakeSession::id());
  458. $expected = session_id();
  459. $this->assertEquals($expected, TestCakeSession::id());
  460. TestCakeSession::write('SessionTestCase', 0);
  461. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  462. TestCakeSession::write('SessionTestCase', '0');
  463. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  464. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  465. TestCakeSession::write('SessionTestCase', false);
  466. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  467. TestCakeSession::write('SessionTestCase', null);
  468. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  469. TestCakeSession::write('SessionTestCase', 'This is a Test');
  470. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  471. TestCakeSession::write('SessionTestCase', 'Some additional data');
  472. $this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
  473. TestCakeSession::destroy();
  474. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  475. Configure::write('Session', array(
  476. 'defaults' => 'php'
  477. ));
  478. TestCakeSession::init();
  479. }
  480. /**
  481. * testSessionTimeout method
  482. *
  483. * @return void
  484. */
  485. public function testSessionTimeout() {
  486. Configure::write('debug', true);
  487. Configure::write('Session.defaults', 'cake');
  488. Configure::write('Session.autoRegenerate', false);
  489. $timeoutSeconds = Configure::read('Session.timeout') * 60;
  490. TestCakeSession::destroy();
  491. TestCakeSession::write('Test', 'some value');
  492. $this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
  493. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  494. $this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
  495. $this->assertWithinMargin(time(), Session::$time, 1);
  496. $this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  497. Configure::write('Session.harden', true);
  498. TestCakeSession::destroy();
  499. TestCakeSession::write('Test', 'some value');
  500. $this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
  501. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  502. $this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
  503. $this->assertWithinMargin(time(), Session::$time, 1);
  504. $this->assertWithinMargin(Session::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  505. }
  506. /**
  507. * Test that cookieTimeout matches timeout when unspecified.
  508. *
  509. * @return void
  510. */
  511. public function testCookieTimeoutFallback() {
  512. $_SESSION = null;
  513. Configure::write('Session', array(
  514. 'defaults' => 'cake',
  515. 'timeout' => 400,
  516. ));
  517. TestCakeSession::start();
  518. $this->assertEquals(400, Configure::read('Session.cookieTimeout'));
  519. $this->assertEquals(400, Configure::read('Session.timeout'));
  520. $this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
  521. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  522. $_SESSION = null;
  523. Configure::write('Session', array(
  524. 'defaults' => 'cake',
  525. 'timeout' => 400,
  526. 'cookieTimeout' => 600
  527. ));
  528. TestCakeSession::start();
  529. $this->assertEquals(600, Configure::read('Session.cookieTimeout'));
  530. $this->assertEquals(400, Configure::read('Session.timeout'));
  531. }
  532. /**
  533. * Proves that invalid sessions will be destroyed and re-created
  534. * if invalid
  535. *
  536. * @return void
  537. */
  538. public function testInvalidSessionRenew() {
  539. TestCakeSession::start();
  540. $this->assertNotEmpty($_SESSION['Config']);
  541. $data = $_SESSION;
  542. session_write_close();
  543. $_SESSION = null;
  544. TestCakeSession::start();
  545. $this->assertEquals($data, $_SESSION);
  546. TestCakeSession::write('Foo', 'Bar');
  547. session_write_close();
  548. $_SESSION = null;
  549. TestCakeSession::userAgent('bogus!');
  550. TestCakeSession::start();
  551. $this->assertNotEquals($data, $_SESSION);
  552. }
  553. }