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

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

https://github.com/FlorianH/Markab
PHP | 472 lines | 242 code | 67 blank | 163 comment | 1 complexity | 828e64a38aa953de0633f1b4ceda53c5 MD5 | raw file
  1. <?php
  2. /**
  3. * SessionTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  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 http://book.cakephp.org/view/1196/Testing 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. $session = new CakeSession(null, false);
  154. $this->assertTrue($session->started());
  155. unset($session);
  156. }
  157. /**
  158. * testError method
  159. *
  160. * @access public
  161. * @return void
  162. */
  163. function testError() {
  164. $this->Session->read('Does.not.exist');
  165. $result = $this->Session->error();
  166. $this->assertEqual($result, "Does.not.exist doesn't exist");
  167. $this->Session->delete('Failing.delete');
  168. $result = $this->Session->error();
  169. $this->assertEqual($result, "Failing.delete doesn't exist");
  170. }
  171. /**
  172. * testDel method
  173. *
  174. * @access public
  175. * @return void
  176. */
  177. function testDelete() {
  178. $this->assertTrue($this->Session->write('Delete.me', 'Clearing out'));
  179. $this->assertTrue($this->Session->delete('Delete.me'));
  180. $this->assertFalse($this->Session->check('Delete.me'));
  181. $this->assertTrue($this->Session->check('Delete'));
  182. $this->assertTrue($this->Session->write('Clearing.sale', 'everything must go'));
  183. $this->assertTrue($this->Session->delete('Clearing'));
  184. $this->assertFalse($this->Session->check('Clearing.sale'));
  185. $this->assertFalse($this->Session->check('Clearing'));
  186. }
  187. /**
  188. * testWatchVar method
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. function testWatchVar() {
  194. $this->assertFalse($this->Session->watch(null));
  195. $this->Session->write('Watching', "I'm watching you");
  196. $this->Session->watch('Watching');
  197. $this->expectError('Writing session key {Watching}: "They found us!"');
  198. $this->Session->write('Watching', 'They found us!');
  199. $this->expectError('Deleting session key {Watching}');
  200. $this->Session->delete('Watching');
  201. $this->assertFalse($this->Session->watch('Invalid.key'));
  202. }
  203. /**
  204. * testIgnore method
  205. *
  206. * @access public
  207. * @return void
  208. */
  209. function testIgnore() {
  210. $this->Session->write('Watching', "I'm watching you");
  211. $this->Session->watch('Watching');
  212. $this->Session->ignore('Watching');
  213. $this->assertTrue($this->Session->write('Watching', 'They found us!'));
  214. }
  215. /**
  216. * testDestroy method
  217. *
  218. * @access public
  219. * @return void
  220. */
  221. function testDestroy() {
  222. $this->Session->write('bulletProof', 'invicible');
  223. $id = $this->Session->id();
  224. $this->Session->destroy();
  225. $this->assertFalse($this->Session->check('bulletProof'));
  226. $this->assertNotEqual($id, $this->Session->id());
  227. }
  228. /**
  229. * testCheckingSavedEmpty method
  230. *
  231. * @access public
  232. * @return void
  233. */
  234. function testCheckingSavedEmpty() {
  235. $this->assertTrue($this->Session->write('SessionTestCase', 0));
  236. $this->assertTrue($this->Session->check('SessionTestCase'));
  237. $this->assertTrue($this->Session->write('SessionTestCase', '0'));
  238. $this->assertTrue($this->Session->check('SessionTestCase'));
  239. $this->assertTrue($this->Session->write('SessionTestCase', false));
  240. $this->assertTrue($this->Session->check('SessionTestCase'));
  241. $this->assertTrue($this->Session->write('SessionTestCase', null));
  242. $this->assertFalse($this->Session->check('SessionTestCase'));
  243. }
  244. /**
  245. * testCheckKeyWithSpaces method
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. function testCheckKeyWithSpaces() {
  251. $this->assertTrue($this->Session->write('Session Test', "test"));
  252. $this->assertEqual($this->Session->check('Session Test'), 'test');
  253. $this->Session->delete('Session Test');
  254. $this->assertTrue($this->Session->write('Session Test.Test Case', "test"));
  255. $this->assertTrue($this->Session->check('Session Test.Test Case'));
  256. }
  257. /**
  258. * test key exploitation
  259. *
  260. * @return void
  261. */
  262. function testKeyExploit() {
  263. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  264. $result = $this->Session->write($key, 'haxored');
  265. $this->assertTrue($result);
  266. $result = $this->Session->read($key);
  267. $this->assertEqual($result, 'haxored');
  268. }
  269. /**
  270. * testReadingSavedEmpty method
  271. *
  272. * @access public
  273. * @return void
  274. */
  275. function testReadingSavedEmpty() {
  276. $this->Session->write('SessionTestCase', 0);
  277. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  278. $this->Session->write('SessionTestCase', '0');
  279. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  280. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  281. $this->Session->write('SessionTestCase', false);
  282. $this->assertFalse($this->Session->read('SessionTestCase'));
  283. $this->Session->write('SessionTestCase', null);
  284. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  285. }
  286. /**
  287. * testCheckUserAgentFalse method
  288. *
  289. * @access public
  290. * @return void
  291. */
  292. function testCheckUserAgentFalse() {
  293. Configure::write('Session.checkAgent', false);
  294. $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  295. $this->assertTrue($this->Session->valid());
  296. }
  297. /**
  298. * testCheckUserAgentTrue method
  299. *
  300. * @access public
  301. * @return void
  302. */
  303. function testCheckUserAgentTrue() {
  304. Configure::write('Session.checkAgent', true);
  305. $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  306. $this->assertFalse($this->Session->valid());
  307. }
  308. /**
  309. * testReadAndWriteWithDatabaseStorage method
  310. *
  311. * @access public
  312. * @return void
  313. */
  314. function testReadAndWriteWithCakeStorage() {
  315. unset($_SESSION);
  316. session_destroy();
  317. ini_set('session.save_handler', 'files');
  318. Configure::write('Session.save', 'cake');
  319. $this->setUp();
  320. $this->Session->write('SessionTestCase', 0);
  321. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  322. $this->Session->write('SessionTestCase', '0');
  323. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  324. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  325. $this->Session->write('SessionTestCase', false);
  326. $this->assertFalse($this->Session->read('SessionTestCase'));
  327. $this->Session->write('SessionTestCase', null);
  328. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  329. $this->Session->write('SessionTestCase', 'This is a Test');
  330. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  331. $this->Session->write('SessionTestCase', 'This is a Test');
  332. $this->Session->write('SessionTestCase', 'This was updated');
  333. $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated');
  334. $this->Session->destroy();
  335. $this->assertFalse($this->Session->read('SessionTestCase'));
  336. }
  337. /**
  338. * testReadAndWriteWithDatabaseStorage method
  339. *
  340. * @access public
  341. * @return void
  342. */
  343. function testReadAndWriteWithCacheStorage() {
  344. unset($_SESSION);
  345. session_destroy();
  346. ini_set('session.save_handler', 'files');
  347. Configure::write('Session.save', 'cache');
  348. $this->setUp();
  349. $this->Session->write('SessionTestCase', 0);
  350. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  351. $this->Session->write('SessionTestCase', '0');
  352. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  353. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  354. $this->Session->write('SessionTestCase', false);
  355. $this->assertFalse($this->Session->read('SessionTestCase'));
  356. $this->Session->write('SessionTestCase', null);
  357. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  358. $this->Session->write('SessionTestCase', 'This is a Test');
  359. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  360. $this->Session->write('SessionTestCase', 'This is a Test');
  361. $this->Session->write('SessionTestCase', 'This was updated');
  362. $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated');
  363. $this->Session->destroy();
  364. $this->assertFalse($this->Session->read('SessionTestCase'));
  365. }
  366. /**
  367. * testReadAndWriteWithDatabaseStorage method
  368. *
  369. * @access public
  370. * @return void
  371. */
  372. function testReadAndWriteWithDatabaseStorage() {
  373. unset($_SESSION);
  374. session_destroy();
  375. Configure::write('Session.table', 'sessions');
  376. Configure::write('Session.model', 'Session');
  377. Configure::write('Session.database', 'test_suite');
  378. Configure::write('Session.save', 'database');
  379. $this->setUp();
  380. $this->Session->write('SessionTestCase', 0);
  381. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  382. $this->Session->write('SessionTestCase', '0');
  383. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  384. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  385. $this->Session->write('SessionTestCase', false);
  386. $this->assertFalse($this->Session->read('SessionTestCase'));
  387. $this->Session->write('SessionTestCase', null);
  388. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  389. $this->Session->write('SessionTestCase', 'This is a Test');
  390. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  391. $this->Session->write('SessionTestCase', 'Some additional data');
  392. $this->assertEqual($this->Session->read('SessionTestCase'), 'Some additional data');
  393. $this->Session->destroy();
  394. $this->assertFalse($this->Session->read('SessionTestCase'));
  395. session_write_close();
  396. unset($_SESSION);
  397. ini_set('session.save_handler', 'files');
  398. Configure::write('Session.save', 'php');
  399. $this->setUp();
  400. }
  401. }