SessionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network;
  16. use Cake\Core\Configure;
  17. use Cake\Network\Session;
  18. use Cake\Network\Session\CacheSession;
  19. use Cake\Network\Session\DatabaseSession;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * TestCacheSession
  23. */
  24. class TestCacheSession extends CacheSession
  25. {
  26. protected function _writeSession()
  27. {
  28. return true;
  29. }
  30. }
  31. /**
  32. * TestDatabaseSession
  33. */
  34. class TestDatabaseSession extends DatabaseSession
  35. {
  36. protected function _writeSession()
  37. {
  38. return true;
  39. }
  40. }
  41. /**
  42. * SessionTest class
  43. */
  44. class SessionTest extends TestCase
  45. {
  46. protected static $_gcDivisor;
  47. /**
  48. * Fixtures used in the SessionTest
  49. *
  50. * @var array
  51. */
  52. public $fixtures = ['core.cake_sessions', 'core.sessions'];
  53. /**
  54. * setup before class.
  55. *
  56. * @return void
  57. */
  58. public static function setupBeforeClass()
  59. {
  60. // Make sure garbage collector will be called
  61. static::$_gcDivisor = ini_get('session.gc_divisor');
  62. ini_set('session.gc_divisor', '1');
  63. }
  64. /**
  65. * teardown after class
  66. *
  67. * @return void
  68. */
  69. public static function teardownAfterClass()
  70. {
  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. {
  81. parent::setUp();
  82. }
  83. /**
  84. * tearDown method
  85. *
  86. * @return void
  87. */
  88. public function tearDown()
  89. {
  90. unset($_SESSION);
  91. parent::tearDown();
  92. }
  93. /**
  94. * test setting ini properties with Session configuration.
  95. *
  96. * @return void
  97. */
  98. public function testSessionConfigIniSetting()
  99. {
  100. $_SESSION = null;
  101. $config = [
  102. 'cookie' => 'test',
  103. 'checkAgent' => false,
  104. 'timeout' => 86400,
  105. 'ini' => [
  106. 'session.referer_check' => 'example.com',
  107. 'session.use_trans_sid' => false
  108. ]
  109. ];
  110. $session = Session::create($config);
  111. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  112. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  113. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  114. }
  115. /**
  116. * test session cookie path setting
  117. *
  118. * @return void
  119. */
  120. public function testCookiePath()
  121. {
  122. ini_set('session.cookie_path', '/foo');
  123. $session = new Session();
  124. $this->assertEquals('/', ini_get('session.cookie_path'));
  125. $session = new Session(['cookiePath' => '/base']);
  126. $this->assertEquals('/base', ini_get('session.cookie_path'));
  127. }
  128. /**
  129. * testCheck method
  130. *
  131. * @return void
  132. */
  133. public function testCheck()
  134. {
  135. $session = new Session();
  136. $session->write('SessionTestCase', 'value');
  137. $this->assertTrue($session->check('SessionTestCase'));
  138. $this->assertFalse($session->check('NotExistingSessionTestCase'));
  139. $this->assertFalse($session->check('Crazy.foo'));
  140. $session->write('Crazy.foo', ['bar' => 'baz']);
  141. $this->assertTrue($session->check('Crazy.foo'));
  142. $this->assertTrue($session->check('Crazy.foo.bar'));
  143. }
  144. /**
  145. * test read with simple values
  146. *
  147. * @return void
  148. */
  149. public function testReadSimple()
  150. {
  151. $session = new Session();
  152. $session->write('testing', '1,2,3');
  153. $result = $session->read('testing');
  154. $this->assertEquals('1,2,3', $result);
  155. $session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
  156. $result = $session->read('testing.1');
  157. $this->assertEquals('one', $result);
  158. $result = $session->read('testing');
  159. $this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);
  160. $result = $session->read();
  161. $this->assertArrayHasKey('testing', $result);
  162. $session->write('This.is.a.deep.array.my.friend', 'value');
  163. $result = $session->read('This.is.a.deep.array');
  164. $this->assertEquals(['my' => ['friend' => 'value']], $result);
  165. }
  166. /**
  167. * testReadEmpty
  168. *
  169. * @return void
  170. */
  171. public function testReadEmpty()
  172. {
  173. $session = new Session();
  174. $this->assertNull($session->read(''));
  175. }
  176. /**
  177. * Test writing simple keys
  178. *
  179. * @return void
  180. */
  181. public function testWriteSimple()
  182. {
  183. $session = new Session();
  184. $session->write('', 'empty');
  185. $this->assertEquals('empty', $session->read(''));
  186. $session->write('Simple', ['values']);
  187. $this->assertEquals(['values'], $session->read('Simple'));
  188. }
  189. /**
  190. * test writing a hash of values
  191. *
  192. * @return void
  193. */
  194. public function testWriteArray()
  195. {
  196. $session = new Session();
  197. $session->write([
  198. 'one' => 1,
  199. 'two' => 2,
  200. 'three' => ['something'],
  201. 'null' => null
  202. ]);
  203. $this->assertEquals(1, $session->read('one'));
  204. $this->assertEquals(['something'], $session->read('three'));
  205. $this->assertEquals(null, $session->read('null'));
  206. }
  207. /**
  208. * Test overwriting a string value as if it were an array.
  209. *
  210. * @return void
  211. */
  212. public function testWriteOverwriteStringValue()
  213. {
  214. $session = new Session();
  215. $session->write('Some.string', 'value');
  216. $this->assertEquals('value', $session->read('Some.string'));
  217. $session->write('Some.string.array', ['values']);
  218. $this->assertEquals(['values'], $session->read('Some.string.array'));
  219. }
  220. /**
  221. * Test consuming session data.
  222. *
  223. * @return void
  224. */
  225. public function testConsume()
  226. {
  227. $session = new Session();
  228. $session->write('Some.string', 'value');
  229. $session->write('Some.array', ['key1' => 'value1', 'key2' => 'value2']);
  230. $this->assertEquals('value', $session->read('Some.string'));
  231. $value = $session->consume('Some.string');
  232. $this->assertEquals('value', $value);
  233. $this->assertFalse($session->check('Some.string'));
  234. $value = $session->consume('');
  235. $this->assertNull($value);
  236. $value = $session->consume(null);
  237. $this->assertNull($value);
  238. $value = $session->consume('Some.array');
  239. $expected = ['key1' => 'value1', 'key2' => 'value2'];
  240. $this->assertEquals($expected, $value);
  241. $this->assertFalse($session->check('Some.array'));
  242. }
  243. /**
  244. * testId method
  245. *
  246. * @return void
  247. */
  248. public function testId()
  249. {
  250. $session = new Session();
  251. $result = $session->id();
  252. $expected = session_id();
  253. $this->assertNotEmpty($result);
  254. $this->assertSame($expected, $result);
  255. $session->id('MySessionId');
  256. $this->assertSame('MySessionId', $session->id());
  257. $this->assertSame('MySessionId', session_id());
  258. $session->id('');
  259. $this->assertSame('', session_id());
  260. }
  261. /**
  262. * testStarted method
  263. *
  264. * @return void
  265. */
  266. public function testStarted()
  267. {
  268. $session = new Session();
  269. $this->assertFalse($session->started());
  270. $this->assertTrue($session->start());
  271. $this->assertTrue($session->started());
  272. }
  273. /**
  274. * testClear method
  275. *
  276. * @return void
  277. */
  278. public function testClear()
  279. {
  280. $session = new Session();
  281. $session->write('Delete.me', 'Clearing out');
  282. $session->clear();
  283. $this->assertFalse($session->check('Delete.me'));
  284. $this->assertFalse($session->check('Delete'));
  285. }
  286. /**
  287. * testDelete method
  288. *
  289. * @return void
  290. */
  291. public function testDelete()
  292. {
  293. $session = new Session();
  294. $session->write('Delete.me', 'Clearing out');
  295. $session->delete('Delete.me');
  296. $this->assertFalse($session->check('Delete.me'));
  297. $this->assertTrue($session->check('Delete'));
  298. $session->write('Clearing.sale', 'everything must go');
  299. $session->delete('');
  300. $this->assertTrue($session->check('Clearing.sale'));
  301. $session->delete(null);
  302. $this->assertTrue($session->check('Clearing.sale'));
  303. $session->delete('Clearing');
  304. $this->assertFalse($session->check('Clearing.sale'));
  305. $this->assertFalse($session->check('Clearing'));
  306. }
  307. /**
  308. * test delete
  309. *
  310. * @return void
  311. */
  312. public function testDeleteEmptyString()
  313. {
  314. $session = new Session();
  315. $session->write('', 'empty string');
  316. $session->delete('');
  317. $this->assertFalse($session->check(''));
  318. }
  319. /**
  320. * testDestroy method
  321. *
  322. * @return void
  323. */
  324. public function testDestroy()
  325. {
  326. $session = new Session();
  327. $session->start();
  328. $session->write('bulletProof', 'invincible');
  329. $session->id('foo');
  330. $session->destroy();
  331. $this->assertFalse($session->check('bulletProof'));
  332. }
  333. /**
  334. * testCheckingSavedEmpty method
  335. *
  336. * @return void
  337. */
  338. public function testCheckingSavedEmpty()
  339. {
  340. $session = new Session();
  341. $session->write('SessionTestCase', 0);
  342. $this->assertTrue($session->check('SessionTestCase'));
  343. $session->write('SessionTestCase', '0');
  344. $this->assertTrue($session->check('SessionTestCase'));
  345. $session->write('SessionTestCase', false);
  346. $this->assertTrue($session->check('SessionTestCase'));
  347. $session->write('SessionTestCase', null);
  348. $this->assertFalse($session->check('SessionTestCase'));
  349. }
  350. /**
  351. * testCheckKeyWithSpaces method
  352. *
  353. * @return void
  354. */
  355. public function testCheckKeyWithSpaces()
  356. {
  357. $session = new Session();
  358. $session->write('Session Test', "test");
  359. $this->assertTrue($session->check('Session Test'));
  360. $session->delete('Session Test');
  361. $session->write('Session Test.Test Case', "test");
  362. $this->assertTrue($session->check('Session Test.Test Case'));
  363. }
  364. /**
  365. * testCheckEmpty
  366. *
  367. * @return void
  368. */
  369. public function testCheckEmpty()
  370. {
  371. $session = new Session();
  372. $this->assertFalse($session->check());
  373. }
  374. /**
  375. * test key exploitation
  376. *
  377. * @return void
  378. */
  379. public function testKeyExploit()
  380. {
  381. $session = new Session();
  382. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  383. $session->write($key, 'haxored');
  384. $result = $session->read($key);
  385. $this->assertNull($result);
  386. }
  387. /**
  388. * testReadingSavedEmpty method
  389. *
  390. * @return void
  391. */
  392. public function testReadingSavedEmpty()
  393. {
  394. $session = new Session();
  395. $session->write('', 'empty string');
  396. $this->assertTrue($session->check(''));
  397. $this->assertEquals('empty string', $session->read(''));
  398. $session->write('SessionTestCase', 0);
  399. $this->assertEquals(0, $session->read('SessionTestCase'));
  400. $session->write('SessionTestCase', '0');
  401. $this->assertEquals('0', $session->read('SessionTestCase'));
  402. $this->assertFalse($session->read('SessionTestCase') === 0);
  403. $session->write('SessionTestCase', false);
  404. $this->assertFalse($session->read('SessionTestCase'));
  405. $session->write('SessionTestCase', null);
  406. $this->assertEquals(null, $session->read('SessionTestCase'));
  407. }
  408. /**
  409. * test using a handler from app/Model/Datasource/Session.
  410. *
  411. * @return void
  412. */
  413. public function testUsingAppLibsHandler()
  414. {
  415. Configure::write('App.namespace', 'TestApp');
  416. $config = [
  417. 'defaults' => 'cake',
  418. 'handler' => [
  419. 'engine' => 'TestAppLibSession',
  420. 'these' => 'are',
  421. 'a few' => 'options'
  422. ]
  423. ];
  424. $session = Session::create($config);
  425. $this->assertInstanceOf('TestApp\Network\Session\TestAppLibSession', $session->engine());
  426. $this->assertEquals('user', ini_get('session.save_handler'));
  427. $this->assertEquals(['these' => 'are', 'a few' => 'options'], $session->engine()->options);
  428. }
  429. /**
  430. * test using a handler from a plugin.
  431. *
  432. * @return void
  433. */
  434. public function testUsingPluginHandler()
  435. {
  436. Configure::write('App.namespace', 'TestApp');
  437. \Cake\Core\Plugin::load('TestPlugin');
  438. $config = [
  439. 'defaults' => 'cake',
  440. 'handler' => [
  441. 'engine' => 'TestPlugin.TestPluginSession'
  442. ]
  443. ];
  444. $session = Session::create($config);
  445. $this->assertInstanceOf('TestPlugin\Network\Session\TestPluginSession', $session->engine());
  446. $this->assertEquals('user', ini_get('session.save_handler'));
  447. }
  448. /**
  449. * Tests that it is possible to pass an already made instance as the session engine
  450. *
  451. * @return void
  452. */
  453. public function testEngineWithPreMadeInstance()
  454. {
  455. Configure::write('App.namespace', 'TestApp');
  456. $engine = new \TestApp\Network\Session\TestAppLibSession;
  457. $session = new Session(['handler' => ['engine' => $engine]]);
  458. $this->assertSame($engine, $session->engine());
  459. $session = new Session();
  460. $session->engine($engine);
  461. $this->assertSame($engine, $session->engine());
  462. }
  463. /**
  464. * Tests instantiating a missing engine
  465. *
  466. * @expectedException \InvalidArgumentException
  467. * @expectedExceptionMessage The class "Derp" does not exist and cannot be used as a session engine
  468. * @return void
  469. */
  470. public function testBadEngine()
  471. {
  472. $session = new Session();
  473. $session->engine('Derp');
  474. }
  475. /**
  476. * Test that cookieTimeout matches timeout when unspecified.
  477. *
  478. * @return void
  479. */
  480. public function testCookieTimeoutFallback()
  481. {
  482. $config = [
  483. 'defaults' => 'cake',
  484. 'timeout' => 400,
  485. ];
  486. new Session($config);
  487. $this->assertEquals(0, ini_get('session.cookie_lifetime'));
  488. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  489. }
  490. /**
  491. * Tests that the cookie name can be changed with configuration
  492. *
  493. * @return void
  494. */
  495. public function testSessionName()
  496. {
  497. new Session(['cookie' => 'made_up_name']);
  498. $this->assertEquals('made_up_name', session_name());
  499. }
  500. }