SessionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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->assertEquals($expected, $result);
  254. $session->id('MySessionId');
  255. $this->assertEquals('MySessionId', $session->id());
  256. $this->assertEquals('MySessionId', session_id());
  257. $session->id('');
  258. $this->assertEquals('', 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. Configure::write('App.namespace', 'TestApp');
  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. Configure::write('App.namespace', 'TestApp');
  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. Configure::write('App.namespace', 'TestApp');
  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. }