SessionTest.php 14 KB

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