PageRenderTime 180ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 676 lines | 363 code | 104 blank | 209 comment | 1 complexity | 0d928f7913ea316161fc5d24b609a69f MD5 | raw file
  1. <?php
  2. /**
  3. * SessionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model.Datasource
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeSession', 'Model/Datasource');
  20. class TestCakeSession extends CakeSession {
  21. public static function setUserAgent($value) {
  22. self::$_userAgent = $value;
  23. }
  24. public static function setHost($host) {
  25. self::_setHost($host);
  26. }
  27. }
  28. /**
  29. * CakeSessionTest class
  30. *
  31. * @package Cake.Test.Case.Model.Datasource
  32. */
  33. class CakeSessionTest extends CakeTestCase {
  34. protected static $_gcDivisor;
  35. /**
  36. * Fixtures used in the SessionTest
  37. *
  38. * @var array
  39. */
  40. public $fixtures = array('core.session');
  41. /**
  42. * setup before class.
  43. *
  44. * @return void
  45. */
  46. public static function setupBeforeClass() {
  47. // Make sure garbage colector will be called
  48. self::$_gcDivisor = ini_get('session.gc_divisor');
  49. ini_set('session.gc_divisor', '1');
  50. }
  51. /**
  52. * teardown after class
  53. *
  54. * @return void
  55. */
  56. public static function teardownAfterClass() {
  57. // Revert to the default setting
  58. ini_set('session.gc_divisor', self::$_gcDivisor);
  59. }
  60. /**
  61. * setUp method
  62. *
  63. * @return void
  64. */
  65. public function setUp() {
  66. parent::setUp();
  67. Configure::write('Session', array(
  68. 'defaults' => 'php',
  69. 'cookie' => 'cakephp',
  70. 'timeout' => 120,
  71. 'cookieTimeout' => 120,
  72. 'ini' => array(),
  73. ));
  74. TestCakeSession::init();
  75. }
  76. /**
  77. * tearDown method
  78. *
  79. * @return void
  80. */
  81. public function teardown() {
  82. if (TestCakeSession::started()) {
  83. TestCakeSession::clear();
  84. }
  85. unset($_SESSION);
  86. parent::teardown();
  87. }
  88. /**
  89. * test setting ini properties with Session configuration.
  90. *
  91. * @return void
  92. */
  93. public function testSessionConfigIniSetting() {
  94. $_SESSION = null;
  95. Configure::write('Session', array(
  96. 'cookie' => 'test',
  97. 'checkAgent' => false,
  98. 'timeout' => 86400,
  99. 'ini' => array(
  100. 'session.referer_check' => 'example.com',
  101. 'session.use_trans_sid' => false
  102. )
  103. ));
  104. TestCakeSession::start();
  105. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  106. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  107. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  108. }
  109. /**
  110. * testSessionPath
  111. *
  112. * @return void
  113. */
  114. public function testSessionPath() {
  115. TestCakeSession::init('/index.php');
  116. $this->assertEquals(TestCakeSession::$path, '/');
  117. TestCakeSession::init('/sub_dir/index.php');
  118. $this->assertEquals(TestCakeSession::$path, '/sub_dir/');
  119. }
  120. /**
  121. * testCakeSessionPathEmpty
  122. *
  123. * @return void
  124. */
  125. public function testCakeSessionPathEmpty() {
  126. TestCakeSession::init('');
  127. $this->assertEquals(TestCakeSession::$path, '/', 'Session path is empty, with "" as $base needs to be /');
  128. }
  129. /**
  130. * testCakeSessionPathContainsParams
  131. *
  132. * @return void
  133. */
  134. public function testCakeSessionPathContainsQuestion() {
  135. TestCakeSession::init('/index.php?');
  136. $this->assertEquals(TestCakeSession::$path, '/');
  137. }
  138. /**
  139. * testSetHost
  140. *
  141. * @return void
  142. */
  143. public function testSetHost() {
  144. TestCakeSession::init();
  145. TestCakeSession::setHost('cakephp.org');
  146. $this->assertEquals(TestCakeSession::$host, 'cakephp.org');
  147. }
  148. /**
  149. * testSetHostWithPort
  150. *
  151. * @return void
  152. */
  153. public function testSetHostWithPort() {
  154. TestCakeSession::init();
  155. TestCakeSession::setHost('cakephp.org:443');
  156. $this->assertEquals(TestCakeSession::$host, 'cakephp.org');
  157. }
  158. /**
  159. * test valid with bogus user agent.
  160. *
  161. * @return void
  162. */
  163. public function testValidBogusUserAgent() {
  164. Configure::write('Session.checkAgent', true);
  165. TestCakeSession::start();
  166. $this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
  167. TestCakeSession::userAgent('bogus!');
  168. $this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
  169. }
  170. /**
  171. * test valid with bogus user agent.
  172. *
  173. * @return void
  174. */
  175. public function testValidTimeExpiry() {
  176. Configure::write('Session.checkAgent', true);
  177. TestCakeSession::start();
  178. $this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
  179. TestCakeSession::$time = strtotime('next year');
  180. $this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
  181. }
  182. /**
  183. * testCheck method
  184. *
  185. * @return void
  186. */
  187. public function testCheck() {
  188. TestCakeSession::write('SessionTestCase', 'value');
  189. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  190. $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false);
  191. }
  192. /**
  193. * testSimpleRead method
  194. *
  195. * @return void
  196. */
  197. public function testSimpleRead() {
  198. TestCakeSession::write('testing', '1,2,3');
  199. $result = TestCakeSession::read('testing');
  200. $this->assertEquals('1,2,3', $result);
  201. TestCakeSession::write('testing', array('1' => 'one', '2' => 'two','3' => 'three'));
  202. $result = TestCakeSession::read('testing.1');
  203. $this->assertEquals('one', $result);
  204. $result = TestCakeSession::read('testing');
  205. $this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
  206. $result = TestCakeSession::read();
  207. $this->assertTrue(isset($result['testing']));
  208. $this->assertTrue(isset($result['Config']));
  209. $this->assertTrue(isset($result['Config']['userAgent']));
  210. TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
  211. $result = TestCakeSession::read('This.is.a.deep.array.my.friend');
  212. $this->assertEquals($result, 'value');
  213. }
  214. /**
  215. * testReadyEmpty
  216. *
  217. * @return void
  218. */
  219. public function testReadyEmpty() {
  220. $this->assertFalse(TestCakeSession::read(''));
  221. }
  222. /**
  223. * test writing a hash of values/
  224. *
  225. * @return void
  226. */
  227. public function testWriteArray() {
  228. $result = TestCakeSession::write(array(
  229. 'one' => 1,
  230. 'two' => 2,
  231. 'three' => array('something'),
  232. 'null' => null
  233. ));
  234. $this->assertTrue($result);
  235. $this->assertEquals(1, TestCakeSession::read('one'));
  236. $this->assertEquals(array('something'), TestCakeSession::read('three'));
  237. $this->assertEquals(null, TestCakeSession::read('null'));
  238. }
  239. /**
  240. * testWriteEmptyKey
  241. *
  242. * @return void
  243. */
  244. public function testWriteEmptyKey() {
  245. $this->assertFalse(TestCakeSession::write('', 'graham'));
  246. $this->assertFalse(TestCakeSession::write('', ''));
  247. $this->assertFalse(TestCakeSession::write(''));
  248. }
  249. /**
  250. * testId method
  251. *
  252. * @return void
  253. */
  254. public function testId() {
  255. TestCakeSession::destroy();
  256. $result = TestCakeSession::id();
  257. $expected = session_id();
  258. $this->assertEquals($expected, $result);
  259. TestCakeSession::id('MySessionId');
  260. $result = TestCakeSession::id();
  261. $this->assertEquals('MySessionId', $result);
  262. }
  263. /**
  264. * testStarted method
  265. *
  266. * @return void
  267. */
  268. public function testStarted() {
  269. unset($_SESSION);
  270. $_SESSION = null;
  271. $this->assertFalse(TestCakeSession::started());
  272. $this->assertTrue(TestCakeSession::start());
  273. $this->assertTrue(TestCakeSession::started());
  274. }
  275. /**
  276. * testError method
  277. *
  278. * @return void
  279. */
  280. public function testError() {
  281. TestCakeSession::read('Does.not.exist');
  282. $result = TestCakeSession::error();
  283. $this->assertEquals("Does.not.exist doesn't exist", $result);
  284. TestCakeSession::delete('Failing.delete');
  285. $result = TestCakeSession::error();
  286. $this->assertEquals("Failing.delete doesn't exist", $result);
  287. }
  288. /**
  289. * testDel method
  290. *
  291. * @return void
  292. */
  293. public function testDelete() {
  294. $this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
  295. $this->assertTrue(TestCakeSession::delete('Delete.me'));
  296. $this->assertFalse(TestCakeSession::check('Delete.me'));
  297. $this->assertTrue(TestCakeSession::check('Delete'));
  298. $this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
  299. $this->assertTrue(TestCakeSession::delete('Clearing'));
  300. $this->assertFalse(TestCakeSession::check('Clearing.sale'));
  301. $this->assertFalse(TestCakeSession::check('Clearing'));
  302. }
  303. /**
  304. * testDestroy method
  305. *
  306. * @return void
  307. */
  308. public function testDestroy() {
  309. TestCakeSession::write('bulletProof', 'invicible');
  310. $id = TestCakeSession::id();
  311. TestCakeSession::destroy();
  312. $this->assertFalse(TestCakeSession::check('bulletProof'));
  313. $this->assertNotEquals($id, TestCakeSession::id());
  314. }
  315. /**
  316. * testCheckingSavedEmpty method
  317. *
  318. * @return void
  319. */
  320. public function testCheckingSavedEmpty() {
  321. $this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
  322. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  323. $this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
  324. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  325. $this->assertTrue(TestCakeSession::write('SessionTestCase', false));
  326. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  327. $this->assertTrue(TestCakeSession::write('SessionTestCase', null));
  328. $this->assertFalse(TestCakeSession::check('SessionTestCase'));
  329. }
  330. /**
  331. * testCheckKeyWithSpaces method
  332. *
  333. * @return void
  334. */
  335. public function testCheckKeyWithSpaces() {
  336. $this->assertTrue(TestCakeSession::write('Session Test', "test"));
  337. $this->assertTrue(TestCakeSession::check('Session Test'));
  338. TestCakeSession::delete('Session Test');
  339. $this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
  340. $this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
  341. }
  342. /**
  343. * testCheckEmpty
  344. *
  345. * @return void
  346. */
  347. public function testCheckEmpty() {
  348. $this->assertFalse(TestCakeSession::check());
  349. }
  350. /**
  351. * test key exploitation
  352. *
  353. * @return void
  354. */
  355. public function testKeyExploit() {
  356. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  357. $result = TestCakeSession::write($key, 'haxored');
  358. $this->assertTrue($result);
  359. $result = TestCakeSession::read($key);
  360. $this->assertEquals('haxored', $result);
  361. }
  362. /**
  363. * testReadingSavedEmpty method
  364. *
  365. * @return void
  366. */
  367. public function testReadingSavedEmpty() {
  368. TestCakeSession::write('SessionTestCase', 0);
  369. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  370. TestCakeSession::write('SessionTestCase', '0');
  371. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  372. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  373. TestCakeSession::write('SessionTestCase', false);
  374. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  375. TestCakeSession::write('SessionTestCase', null);
  376. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  377. }
  378. /**
  379. * testCheckUserAgentFalse method
  380. *
  381. * @return void
  382. */
  383. public function testCheckUserAgentFalse() {
  384. Configure::write('Session.checkAgent', false);
  385. TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
  386. $this->assertTrue(TestCakeSession::valid());
  387. }
  388. /**
  389. * testCheckUserAgentTrue method
  390. *
  391. * @return void
  392. */
  393. public function testCheckUserAgentTrue() {
  394. Configure::write('Session.checkAgent', true);
  395. TestCakeSession::$error = false;
  396. $agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  397. TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
  398. TestCakeSession::setUserAgent($agent);
  399. $this->assertFalse(TestCakeSession::valid());
  400. }
  401. /**
  402. * testReadAndWriteWithDatabaseStorage method
  403. *
  404. * @return void
  405. */
  406. public function testReadAndWriteWithCakeStorage() {
  407. Configure::write('Session.defaults', 'cake');
  408. TestCakeSession::init();
  409. TestCakeSession::start();
  410. TestCakeSession::write('SessionTestCase', 0);
  411. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  412. TestCakeSession::write('SessionTestCase', '0');
  413. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  414. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  415. TestCakeSession::write('SessionTestCase', false);
  416. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  417. TestCakeSession::write('SessionTestCase', null);
  418. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  419. TestCakeSession::write('SessionTestCase', 'This is a Test');
  420. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  421. TestCakeSession::write('SessionTestCase', 'This is a Test');
  422. TestCakeSession::write('SessionTestCase', 'This was updated');
  423. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  424. TestCakeSession::destroy();
  425. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  426. }
  427. /**
  428. * test using a handler from app/Model/Datasource/Session.
  429. *
  430. * @return void
  431. */
  432. public function testUsingAppLibsHandler() {
  433. App::build(array(
  434. 'Model/Datasource/Session' => array(
  435. CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
  436. ),
  437. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  438. ), true);
  439. Configure::write('Session', array(
  440. 'defaults' => 'cake',
  441. 'handler' => array(
  442. 'engine' => 'TestAppLibSession'
  443. )
  444. ));
  445. TestCakeSession::destroy();
  446. $this->assertTrue(TestCakeSession::started());
  447. App::build();
  448. }
  449. /**
  450. * test using a handler from a plugin.
  451. *
  452. * @return void
  453. */
  454. public function testUsingPluginHandler() {
  455. App::build(array(
  456. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  457. ), true);
  458. CakePlugin::load('TestPlugin');
  459. Configure::write('Session', array(
  460. 'defaults' => 'cake',
  461. 'handler' => array(
  462. 'engine' => 'TestPlugin.TestPluginSession'
  463. )
  464. ));
  465. TestCakeSession::destroy();
  466. $this->assertTrue(TestCakeSession::started());
  467. App::build();
  468. }
  469. /**
  470. * testReadAndWriteWithDatabaseStorage method
  471. *
  472. * @return void
  473. */
  474. public function testReadAndWriteWithCacheStorage() {
  475. Configure::write('Session.defaults', 'cache');
  476. TestCakeSession::init();
  477. TestCakeSession::destroy();
  478. TestCakeSession::write('SessionTestCase', 0);
  479. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  480. TestCakeSession::write('SessionTestCase', '0');
  481. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  482. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  483. TestCakeSession::write('SessionTestCase', false);
  484. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  485. TestCakeSession::write('SessionTestCase', null);
  486. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  487. TestCakeSession::write('SessionTestCase', 'This is a Test');
  488. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  489. TestCakeSession::write('SessionTestCase', 'This is a Test');
  490. TestCakeSession::write('SessionTestCase', 'This was updated');
  491. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  492. TestCakeSession::destroy();
  493. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  494. }
  495. /**
  496. * test that changing the config name of the cache config works.
  497. *
  498. * @return void
  499. */
  500. public function testReadAndWriteWithCustomCacheConfig() {
  501. Configure::write('Session.defaults', 'cache');
  502. Configure::write('Session.handler.config', 'session_test');
  503. Cache::config('session_test', array(
  504. 'engine' => 'File',
  505. 'prefix' => 'session_test_',
  506. ));
  507. TestCakeSession::init();
  508. TestCakeSession::start();
  509. TestCakeSession::write('SessionTestCase', 'Some value');
  510. $this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
  511. $id = TestCakeSession::id();
  512. Cache::delete($id, 'session_test');
  513. }
  514. /**
  515. * testReadAndWriteWithDatabaseStorage method
  516. *
  517. * @return void
  518. */
  519. public function testReadAndWriteWithDatabaseStorage() {
  520. Configure::write('Session.defaults', 'database');
  521. Configure::write('Session.handler.table', 'sessions');
  522. Configure::write('Session.handler.model', 'Session');
  523. Configure::write('Session.handler.database', 'test');
  524. TestCakeSession::init();
  525. TestCakeSession::start();
  526. TestCakeSession::write('SessionTestCase', 0);
  527. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  528. TestCakeSession::write('SessionTestCase', '0');
  529. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  530. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  531. TestCakeSession::write('SessionTestCase', false);
  532. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  533. TestCakeSession::write('SessionTestCase', null);
  534. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  535. TestCakeSession::write('SessionTestCase', 'This is a Test');
  536. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  537. TestCakeSession::write('SessionTestCase', 'Some additional data');
  538. $this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
  539. TestCakeSession::destroy();
  540. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  541. Configure::write('Session', array(
  542. 'defaults' => 'php'
  543. ));
  544. TestCakeSession::init();
  545. }
  546. /**
  547. * testSessionTimeout method
  548. *
  549. * @return void
  550. */
  551. public function testSessionTimeout() {
  552. Configure::write('debug', 2);
  553. Configure::write('Session.autoRegenerate', false);
  554. $timeoutSeconds = Configure::read('Session.timeout') * 60;
  555. TestCakeSession::destroy();
  556. TestCakeSession::write('Test', 'some value');
  557. $this->assertEquals(time() + $timeoutSeconds, CakeSession::$sessionTime);
  558. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  559. $this->assertEquals(CakeSession::$sessionTime, $_SESSION['Config']['time']);
  560. $this->assertEquals(time(), CakeSession::$time);
  561. $this->assertEquals(time() + $timeoutSeconds, $_SESSION['Config']['time']);
  562. Configure::write('Session.harden', true);
  563. TestCakeSession::destroy();
  564. TestCakeSession::write('Test', 'some value');
  565. $this->assertEquals(time() + $timeoutSeconds, CakeSession::$sessionTime);
  566. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  567. $this->assertEquals(CakeSession::$sessionTime, $_SESSION['Config']['time']);
  568. $this->assertEquals(time(), CakeSession::$time);
  569. $this->assertEquals(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time']);
  570. }
  571. }