SessionTest.php 15 KB

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