PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/cake_session.test.php

https://github.com/t73biz/BaseApp
PHP | 469 lines | 240 code | 66 blank | 163 comment | 1 complexity | e7ce5a6c5f1d9b2b7840afe68d77a08b MD5 | raw file
  1. <?php
  2. /**
  3. * SessionTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. if (!class_exists('CakeSession')) {
  21. App::import('Core', 'CakeSession');
  22. }
  23. /**
  24. * CakeSessionTest class
  25. *
  26. * @package cake
  27. * @subpackage cake.tests.cases.libs
  28. */
  29. class CakeSessionTest extends CakeTestCase {
  30. /**
  31. * Fixtures used in the SessionTest
  32. *
  33. * @var array
  34. * @access public
  35. */
  36. var $fixtures = array('core.session');
  37. /**
  38. * startCase method
  39. *
  40. * @access public
  41. * @return void
  42. */
  43. function startCase() {
  44. // Make sure garbage colector will be called
  45. $this->__gc_divisor = ini_get('session.gc_divisor');
  46. ini_set('session.gc_divisor', '1');
  47. }
  48. /**
  49. * endCase method
  50. *
  51. * @access public
  52. * @return void
  53. */
  54. function endCase() {
  55. // Revert to the default setting
  56. ini_set('session.gc_divisor', $this->__gc_divisor);
  57. }
  58. /**
  59. * setUp method
  60. *
  61. * @access public
  62. * @return void
  63. */
  64. function setUp() {
  65. $this->Session =& new CakeSession();
  66. $this->Session->start();
  67. $this->Session->_checkValid();
  68. }
  69. /**
  70. * tearDown method
  71. *
  72. * @access public
  73. * @return void
  74. */
  75. function tearDown() {
  76. unset($_SESSION);
  77. session_destroy();
  78. }
  79. /**
  80. * testSessionPath
  81. *
  82. * @access public
  83. * @return void
  84. */
  85. function testSessionPath() {
  86. $Session = new CakeSession('/index.php');
  87. $this->assertEqual('/', $Session->path);
  88. $Session = new CakeSession('/sub_dir/index.php');
  89. $this->assertEqual('/sub_dir/', $Session->path);
  90. $Session = new CakeSession('');
  91. $this->assertEqual('/', $Session->path, 'Session path is empty, with "" as $base needs to be / %s');
  92. }
  93. /**
  94. * testCheck method
  95. *
  96. * @access public
  97. * @return void
  98. */
  99. function testCheck() {
  100. $this->Session->write('SessionTestCase', 'value');
  101. $this->assertTrue($this->Session->check('SessionTestCase'));
  102. $this->assertFalse($this->Session->check('NotExistingSessionTestCase'), false);
  103. }
  104. /**
  105. * testSimpleRead method
  106. *
  107. * @access public
  108. * @return void
  109. */
  110. function testSimpleRead() {
  111. $this->Session->write('testing', '1,2,3');
  112. $result = $this->Session->read('testing');
  113. $this->assertEqual($result, '1,2,3');
  114. $this->Session->write('testing', array('1' => 'one', '2' => 'two','3' => 'three'));
  115. $result = $this->Session->read('testing.1');
  116. $this->assertEqual($result, 'one');
  117. $result = $this->Session->read('testing');
  118. $this->assertEqual($result, array('1' => 'one', '2' => 'two', '3' => 'three'));
  119. $result = $this->Session->read();
  120. $this->assertTrue(isset($result['testing']));
  121. $this->assertTrue(isset($result['Config']));
  122. $this->assertTrue(isset($result['Config']['userAgent']));
  123. $this->Session->write('This.is.a.deep.array.my.friend', 'value');
  124. $result = $this->Session->read('This.is.a.deep.array.my.friend');
  125. $this->assertEqual('value', $result);
  126. }
  127. /**
  128. * testId method
  129. *
  130. * @access public
  131. * @return void
  132. */
  133. function testId() {
  134. $expected = session_id();
  135. $result = $this->Session->id();
  136. $this->assertEqual($result, $expected);
  137. $this->Session->id('MySessionId');
  138. $result = $this->Session->id();
  139. $this->assertEqual($result, 'MySessionId');
  140. }
  141. /**
  142. * testStarted method
  143. *
  144. * @access public
  145. * @return void
  146. */
  147. function testStarted() {
  148. $this->assertTrue($this->Session->started());
  149. unset($_SESSION);
  150. $_SESSION = null;
  151. $this->assertFalse($this->Session->started());
  152. $this->assertTrue($this->Session->start());
  153. }
  154. /**
  155. * testError method
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. function testError() {
  161. $this->Session->read('Does.not.exist');
  162. $result = $this->Session->error();
  163. $this->assertEqual($result, "Does.not.exist doesn't exist");
  164. $this->Session->delete('Failing.delete');
  165. $result = $this->Session->error();
  166. $this->assertEqual($result, "Failing.delete doesn't exist");
  167. }
  168. /**
  169. * testDel method
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function testDelete() {
  175. $this->assertTrue($this->Session->write('Delete.me', 'Clearing out'));
  176. $this->assertTrue($this->Session->delete('Delete.me'));
  177. $this->assertFalse($this->Session->check('Delete.me'));
  178. $this->assertTrue($this->Session->check('Delete'));
  179. $this->assertTrue($this->Session->write('Clearing.sale', 'everything must go'));
  180. $this->assertTrue($this->Session->delete('Clearing'));
  181. $this->assertFalse($this->Session->check('Clearing.sale'));
  182. $this->assertFalse($this->Session->check('Clearing'));
  183. }
  184. /**
  185. * testWatchVar method
  186. *
  187. * @access public
  188. * @return void
  189. */
  190. function testWatchVar() {
  191. $this->assertFalse($this->Session->watch(null));
  192. $this->Session->write('Watching', "I'm watching you");
  193. $this->Session->watch('Watching');
  194. $this->expectError('Writing session key {Watching}: "They found us!"');
  195. $this->Session->write('Watching', 'They found us!');
  196. $this->expectError('Deleting session key {Watching}');
  197. $this->Session->delete('Watching');
  198. $this->assertFalse($this->Session->watch('Invalid.key'));
  199. }
  200. /**
  201. * testIgnore method
  202. *
  203. * @access public
  204. * @return void
  205. */
  206. function testIgnore() {
  207. $this->Session->write('Watching', "I'm watching you");
  208. $this->Session->watch('Watching');
  209. $this->Session->ignore('Watching');
  210. $this->assertTrue($this->Session->write('Watching', 'They found us!'));
  211. }
  212. /**
  213. * testDestroy method
  214. *
  215. * @access public
  216. * @return void
  217. */
  218. function testDestroy() {
  219. $this->Session->write('bulletProof', 'invicible');
  220. $id = $this->Session->id();
  221. $this->Session->destroy();
  222. $this->assertFalse($this->Session->check('bulletProof'));
  223. $this->assertNotEqual($id, $this->Session->id());
  224. }
  225. /**
  226. * testCheckingSavedEmpty method
  227. *
  228. * @access public
  229. * @return void
  230. */
  231. function testCheckingSavedEmpty() {
  232. $this->assertTrue($this->Session->write('SessionTestCase', 0));
  233. $this->assertTrue($this->Session->check('SessionTestCase'));
  234. $this->assertTrue($this->Session->write('SessionTestCase', '0'));
  235. $this->assertTrue($this->Session->check('SessionTestCase'));
  236. $this->assertTrue($this->Session->write('SessionTestCase', false));
  237. $this->assertTrue($this->Session->check('SessionTestCase'));
  238. $this->assertTrue($this->Session->write('SessionTestCase', null));
  239. $this->assertFalse($this->Session->check('SessionTestCase'));
  240. }
  241. /**
  242. * testCheckKeyWithSpaces method
  243. *
  244. * @access public
  245. * @return void
  246. */
  247. function testCheckKeyWithSpaces() {
  248. $this->assertTrue($this->Session->write('Session Test', "test"));
  249. $this->assertEqual($this->Session->check('Session Test'), 'test');
  250. $this->Session->delete('Session Test');
  251. $this->assertTrue($this->Session->write('Session Test.Test Case', "test"));
  252. $this->assertTrue($this->Session->check('Session Test.Test Case'));
  253. }
  254. /**
  255. * test key exploitation
  256. *
  257. * @return void
  258. */
  259. function testKeyExploit() {
  260. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  261. $result = $this->Session->write($key, 'haxored');
  262. $this->assertTrue($result);
  263. $result = $this->Session->read($key);
  264. $this->assertEqual($result, 'haxored');
  265. }
  266. /**
  267. * testReadingSavedEmpty method
  268. *
  269. * @access public
  270. * @return void
  271. */
  272. function testReadingSavedEmpty() {
  273. $this->Session->write('SessionTestCase', 0);
  274. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  275. $this->Session->write('SessionTestCase', '0');
  276. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  277. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  278. $this->Session->write('SessionTestCase', false);
  279. $this->assertFalse($this->Session->read('SessionTestCase'));
  280. $this->Session->write('SessionTestCase', null);
  281. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  282. }
  283. /**
  284. * testCheckUserAgentFalse method
  285. *
  286. * @access public
  287. * @return void
  288. */
  289. function testCheckUserAgentFalse() {
  290. Configure::write('Session.checkAgent', false);
  291. $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  292. $this->assertTrue($this->Session->valid());
  293. }
  294. /**
  295. * testCheckUserAgentTrue method
  296. *
  297. * @access public
  298. * @return void
  299. */
  300. function testCheckUserAgentTrue() {
  301. Configure::write('Session.checkAgent', true);
  302. $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  303. $this->assertFalse($this->Session->valid());
  304. }
  305. /**
  306. * testReadAndWriteWithDatabaseStorage method
  307. *
  308. * @access public
  309. * @return void
  310. */
  311. function testReadAndWriteWithCakeStorage() {
  312. unset($_SESSION);
  313. session_destroy();
  314. ini_set('session.save_handler', 'files');
  315. Configure::write('Session.save', 'cake');
  316. $this->setUp();
  317. $this->Session->write('SessionTestCase', 0);
  318. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  319. $this->Session->write('SessionTestCase', '0');
  320. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  321. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  322. $this->Session->write('SessionTestCase', false);
  323. $this->assertFalse($this->Session->read('SessionTestCase'));
  324. $this->Session->write('SessionTestCase', null);
  325. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  326. $this->Session->write('SessionTestCase', 'This is a Test');
  327. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  328. $this->Session->write('SessionTestCase', 'This is a Test');
  329. $this->Session->write('SessionTestCase', 'This was updated');
  330. $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated');
  331. $this->Session->destroy();
  332. $this->assertFalse($this->Session->read('SessionTestCase'));
  333. }
  334. /**
  335. * testReadAndWriteWithDatabaseStorage method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testReadAndWriteWithCacheStorage() {
  341. unset($_SESSION);
  342. session_destroy();
  343. ini_set('session.save_handler', 'files');
  344. Configure::write('Session.save', 'cache');
  345. $this->setUp();
  346. $this->Session->write('SessionTestCase', 0);
  347. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  348. $this->Session->write('SessionTestCase', '0');
  349. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  350. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  351. $this->Session->write('SessionTestCase', false);
  352. $this->assertFalse($this->Session->read('SessionTestCase'));
  353. $this->Session->write('SessionTestCase', null);
  354. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  355. $this->Session->write('SessionTestCase', 'This is a Test');
  356. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  357. $this->Session->write('SessionTestCase', 'This is a Test');
  358. $this->Session->write('SessionTestCase', 'This was updated');
  359. $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated');
  360. $this->Session->destroy();
  361. $this->assertFalse($this->Session->read('SessionTestCase'));
  362. }
  363. /**
  364. * testReadAndWriteWithDatabaseStorage method
  365. *
  366. * @access public
  367. * @return void
  368. */
  369. function testReadAndWriteWithDatabaseStorage() {
  370. unset($_SESSION);
  371. session_destroy();
  372. Configure::write('Session.table', 'sessions');
  373. Configure::write('Session.model', 'Session');
  374. Configure::write('Session.database', 'test_suite');
  375. Configure::write('Session.save', 'database');
  376. $this->setUp();
  377. $this->Session->write('SessionTestCase', 0);
  378. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  379. $this->Session->write('SessionTestCase', '0');
  380. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  381. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  382. $this->Session->write('SessionTestCase', false);
  383. $this->assertFalse($this->Session->read('SessionTestCase'));
  384. $this->Session->write('SessionTestCase', null);
  385. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  386. $this->Session->write('SessionTestCase', 'This is a Test');
  387. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  388. $this->Session->write('SessionTestCase', 'Some additional data');
  389. $this->assertEqual($this->Session->read('SessionTestCase'), 'Some additional data');
  390. $this->Session->destroy();
  391. $this->assertFalse($this->Session->read('SessionTestCase'));
  392. session_write_close();
  393. unset($_SESSION);
  394. ini_set('session.save_handler', 'files');
  395. Configure::write('Session.save', 'php');
  396. $this->setUp();
  397. }
  398. }
  399. ?>