Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/cake_session.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
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 /** | |
25 * CakeSessionTest class | |
26 * | |
27 * @package cake | |
28 * @subpackage cake.tests.cases.libs | |
29 */ | |
30 class CakeSessionTest extends CakeTestCase { | |
31 | |
32 /** | |
33 * Fixtures used in the SessionTest | |
34 * | |
35 * @var array | |
36 * @access public | |
37 */ | |
38 var $fixtures = array('core.session'); | |
39 | |
40 /** | |
41 * startCase method | |
42 * | |
43 * @access public | |
44 * @return void | |
45 */ | |
46 function startCase() { | |
47 // Make sure garbage colector will be called | |
48 $this->__gc_divisor = ini_get('session.gc_divisor'); | |
49 ini_set('session.gc_divisor', '1'); | |
50 } | |
51 | |
52 /** | |
53 * endCase method | |
54 * | |
55 * @access public | |
56 * @return void | |
57 */ | |
58 function endCase() { | |
59 // Revert to the default setting | |
60 ini_set('session.gc_divisor', $this->__gc_divisor); | |
61 } | |
62 | |
63 /** | |
64 * setUp method | |
65 * | |
66 * @access public | |
67 * @return void | |
68 */ | |
69 function setUp() { | |
70 $this->Session =& new CakeSession(); | |
71 $this->Session->start(); | |
72 $this->Session->_checkValid(); | |
73 } | |
74 | |
75 /** | |
76 * tearDown method | |
77 * | |
78 * @access public | |
79 * @return void | |
80 */ | |
81 function tearDown() { | |
82 unset($_SESSION); | |
83 session_destroy(); | |
84 } | |
85 | |
86 /** | |
87 * testSessionPath | |
88 * | |
89 * @access public | |
90 * @return void | |
91 */ | |
92 function testSessionPath() { | |
93 $Session = new CakeSession('/index.php'); | |
94 $this->assertEqual('/', $Session->path); | |
95 | |
96 $Session = new CakeSession('/sub_dir/index.php'); | |
97 $this->assertEqual('/sub_dir/', $Session->path); | |
98 | |
99 $Session = new CakeSession(''); | |
100 $this->assertEqual('/', $Session->path, 'Session path is empty, with "" as $base needs to be / %s'); | |
101 } | |
102 | |
103 /** | |
104 * testCheck method | |
105 * | |
106 * @access public | |
107 * @return void | |
108 */ | |
109 function testCheck() { | |
110 $this->Session->write('SessionTestCase', 'value'); | |
111 $this->assertTrue($this->Session->check('SessionTestCase')); | |
112 | |
113 $this->assertFalse($this->Session->check('NotExistingSessionTestCase'), false); | |
114 } | |
115 | |
116 /** | |
117 * testSimpleRead method | |
118 * | |
119 * @access public | |
120 * @return void | |
121 */ | |
122 function testSimpleRead() { | |
123 $this->Session->write('testing', '1,2,3'); | |
124 $result = $this->Session->read('testing'); | |
125 $this->assertEqual($result, '1,2,3'); | |
126 | |
127 $this->Session->write('testing', array('1' => 'one', '2' => 'two','3' => 'three')); | |
128 $result = $this->Session->read('testing.1'); | |
129 $this->assertEqual($result, 'one'); | |
130 | |
131 $result = $this->Session->read('testing'); | |
132 $this->assertEqual($result, array('1' => 'one', '2' => 'two', '3' => 'three')); | |
133 | |
134 $result = $this->Session->read(); | |
135 $this->assertTrue(isset($result['testing'])); | |
136 $this->assertTrue(isset($result['Config'])); | |
137 $this->assertTrue(isset($result['Config']['userAgent'])); | |
138 | |
139 $this->Session->write('This.is.a.deep.array.my.friend', 'value'); | |
140 $result = $this->Session->read('This.is.a.deep.array.my.friend'); | |
141 $this->assertEqual('value', $result); | |
142 } | |
143 | |
144 /** | |
145 * testId method | |
146 * | |
147 * @access public | |
148 * @return void | |
149 */ | |
150 function testId() { | |
151 $expected = session_id(); | |
152 $result = $this->Session->id(); | |
153 $this->assertEqual($result, $expected); | |
154 | |
155 $this->Session->id('MySessionId'); | |
156 $result = $this->Session->id(); | |
157 $this->assertEqual($result, 'MySessionId'); | |
158 } | |
159 | |
160 /** | |
161 * testStarted method | |
162 * | |
163 * @access public | |
164 * @return void | |
165 */ | |
166 function testStarted() { | |
167 $this->assertTrue($this->Session->started()); | |
168 | |
169 unset($_SESSION); | |
170 $_SESSION = null; | |
171 $this->assertFalse($this->Session->started()); | |
172 $this->assertTrue($this->Session->start()); | |
173 | |
174 $session = new CakeSession(null, false); | |
175 $this->assertTrue($session->started()); | |
176 unset($session); | |
177 } | |
178 | |
179 /** | |
180 * testError method | |
181 * | |
182 * @access public | |
183 * @return void | |
184 */ | |
185 function testError() { | |
186 $this->Session->read('Does.not.exist'); | |
187 $result = $this->Session->error(); | |
188 $this->assertEqual($result, "Does.not.exist doesn't exist"); | |
189 | |
190 $this->Session->delete('Failing.delete'); | |
191 $result = $this->Session->error(); | |
192 $this->assertEqual($result, "Failing.delete doesn't exist"); | |
193 } | |
194 | |
195 /** | |
196 * testDel method | |
197 * | |
198 * @access public | |
199 * @return void | |
200 */ | |
201 function testDelete() { | |
202 $this->assertTrue($this->Session->write('Delete.me', 'Clearing out')); | |
203 $this->assertTrue($this->Session->delete('Delete.me')); | |
204 $this->assertFalse($this->Session->check('Delete.me')); | |
205 $this->assertTrue($this->Session->check('Delete')); | |
206 | |
207 $this->assertTrue($this->Session->write('Clearing.sale', 'everything must go')); | |
208 $this->assertTrue($this->Session->delete('Clearing')); | |
209 $this->assertFalse($this->Session->check('Clearing.sale')); | |
210 $this->assertFalse($this->Session->check('Clearing')); | |
211 } | |
212 | |
213 /** | |
214 * testWatchVar method | |
215 * | |
216 * @access public | |
217 * @return void | |
218 */ | |
219 function testWatchVar() { | |
220 $this->assertFalse($this->Session->watch(null)); | |
221 | |
222 $this->Session->write('Watching', "I'm watching you"); | |
223 $this->Session->watch('Watching'); | |
224 $this->expectError('Writing session key {Watching}: "They found us!"'); | |
225 $this->Session->write('Watching', 'They found us!'); | |
226 | |
227 $this->expectError('Deleting session key {Watching}'); | |
228 $this->Session->delete('Watching'); | |
229 | |
230 $this->assertFalse($this->Session->watch('Invalid.key')); | |
231 } | |
232 | |
233 /** | |
234 * testIgnore method | |
235 * | |
236 * @access public | |
237 * @return void | |
238 */ | |
239 function testIgnore() { | |
240 $this->Session->write('Watching', "I'm watching you"); | |
241 $this->Session->watch('Watching'); | |
242 $this->Session->ignore('Watching'); | |
243 $this->assertTrue($this->Session->write('Watching', 'They found us!')); | |
244 } | |
245 | |
246 /** | |
247 * testDestroy method | |
248 * | |
249 * @access public | |
250 * @return void | |
251 */ | |
252 function testDestroy() { | |
253 $this->Session->write('bulletProof', 'invicible'); | |
254 $id = $this->Session->id(); | |
255 $this->Session->destroy(); | |
256 $this->assertFalse($this->Session->check('bulletProof')); | |
257 $this->assertNotEqual($id, $this->Session->id()); | |
258 $this->assertTrue($this->Session->started()); | |
259 | |
260 $this->Session->cookieLifeTime = 'test'; | |
261 $this->Session->destroy(); | |
262 $this->assertNotEqual('test', $this->Session->cookieLifeTime); | |
263 } | |
264 | |
265 /** | |
266 * testCheckingSavedEmpty method | |
267 * | |
268 * @access public | |
269 * @return void | |
270 */ | |
271 function testCheckingSavedEmpty() { | |
272 $this->assertTrue($this->Session->write('SessionTestCase', 0)); | |
273 $this->assertTrue($this->Session->check('SessionTestCase')); | |
274 | |
275 $this->assertTrue($this->Session->write('SessionTestCase', '0')); | |
276 $this->assertTrue($this->Session->check('SessionTestCase')); | |
277 | |
278 $this->assertTrue($this->Session->write('SessionTestCase', false)); | |
279 $this->assertTrue($this->Session->check('SessionTestCase')); | |
280 | |
281 $this->assertTrue($this->Session->write('SessionTestCase', null)); | |
282 $this->assertFalse($this->Session->check('SessionTestCase')); | |
283 } | |
284 | |
285 /** | |
286 * testCheckKeyWithSpaces method | |
287 * | |
288 * @access public | |
289 * @return void | |
290 */ | |
291 function testCheckKeyWithSpaces() { | |
292 $this->assertTrue($this->Session->write('Session Test', "test")); | |
293 $this->assertEqual($this->Session->check('Session Test'), 'test'); | |
294 $this->Session->delete('Session Test'); | |
295 | |
296 $this->assertTrue($this->Session->write('Session Test.Test Case', "test")); | |
297 $this->assertTrue($this->Session->check('Session Test.Test Case')); | |
298 } | |
299 | |
300 /** | |
301 * test key exploitation | |
302 * | |
303 * @return void | |
304 */ | |
305 function testKeyExploit() { | |
306 $key = "a'] = 1; phpinfo(); \$_SESSION['a"; | |
307 $result = $this->Session->write($key, 'haxored'); | |
308 $this->assertTrue($result); | |
309 | |
310 $result = $this->Session->read($key); | |
311 $this->assertEqual($result, 'haxored'); | |
312 } | |
313 | |
314 /** | |
315 * testReadingSavedEmpty method | |
316 * | |
317 * @access public | |
318 * @return void | |
319 */ | |
320 function testReadingSavedEmpty() { | |
321 $this->Session->write('SessionTestCase', 0); | |
322 $this->assertEqual($this->Session->read('SessionTestCase'), 0); | |
323 | |
324 $this->Session->write('SessionTestCase', '0'); | |
325 $this->assertEqual($this->Session->read('SessionTestCase'), '0'); | |
326 $this->assertFalse($this->Session->read('SessionTestCase') === 0); | |
327 | |
328 $this->Session->write('SessionTestCase', false); | |
329 $this->assertFalse($this->Session->read('SessionTestCase')); | |
330 | |
331 $this->Session->write('SessionTestCase', null); | |
332 $this->assertEqual($this->Session->read('SessionTestCase'), null); | |
333 } | |
334 | |
335 /** | |
336 * testCheckUserAgentFalse method | |
337 * | |
338 * @access public | |
339 * @return void | |
340 */ | |
341 function testCheckUserAgentFalse() { | |
342 Configure::write('Session.checkAgent', false); | |
343 $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt')); | |
344 $this->assertTrue($this->Session->valid()); | |
345 } | |
346 | |
347 /** | |
348 * testCheckUserAgentTrue method | |
349 * | |
350 * @access public | |
351 * @return void | |
352 */ | |
353 function testCheckUserAgentTrue() { | |
354 Configure::write('Session.checkAgent', true); | |
355 $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt')); | |
356 $this->assertFalse($this->Session->valid()); | |
357 } | |
358 | |
359 /** | |
360 * testReadAndWriteWithDatabaseStorage method | |
361 * | |
362 * @access public | |
363 * @return void | |
364 */ | |
365 function testReadAndWriteWithCakeStorage() { | |
366 unset($_SESSION); | |
367 session_destroy(); | |
368 ini_set('session.save_handler', 'files'); | |
369 Configure::write('Session.save', 'cake'); | |
370 $this->setUp(); | |
371 | |
372 $this->Session->write('SessionTestCase', 0); | |
373 $this->assertEqual($this->Session->read('SessionTestCase'), 0); | |
374 | |
375 $this->Session->write('SessionTestCase', '0'); | |
376 $this->assertEqual($this->Session->read('SessionTestCase'), '0'); | |
377 $this->assertFalse($this->Session->read('SessionTestCase') === 0); | |
378 | |
379 $this->Session->write('SessionTestCase', false); | |
380 $this->assertFalse($this->Session->read('SessionTestCase')); | |
381 | |
382 $this->Session->write('SessionTestCase', null); | |
383 $this->assertEqual($this->Session->read('SessionTestCase'), null); | |
384 | |
385 $this->Session->write('SessionTestCase', 'This is a Test'); | |
386 $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test'); | |
387 | |
388 $this->Session->write('SessionTestCase', 'This is a Test'); | |
389 $this->Session->write('SessionTestCase', 'This was updated'); | |
390 $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated'); | |
391 | |
392 $this->Session->destroy(); | |
393 $this->assertFalse($this->Session->read('SessionTestCase')); | |
394 } | |
395 | |
396 /** | |
397 * testReadAndWriteWithDatabaseStorage method | |
398 * | |
399 * @access public | |
400 * @return void | |
401 */ | |
402 function testReadAndWriteWithCacheStorage() { | |
403 unset($_SESSION); | |
404 session_destroy(); | |
405 ini_set('session.save_handler', 'files'); | |
406 Configure::write('Session.save', 'cache'); | |
407 $this->setUp(); | |
408 | |
409 $this->Session->write('SessionTestCase', 0); | |
410 $this->assertEqual($this->Session->read('SessionTestCase'), 0); | |
411 | |
412 $this->Session->write('SessionTestCase', '0'); | |
413 $this->assertEqual($this->Session->read('SessionTestCase'), '0'); | |
414 $this->assertFalse($this->Session->read('SessionTestCase') === 0); | |
415 | |
416 $this->Session->write('SessionTestCase', false); | |
417 $this->assertFalse($this->Session->read('SessionTestCase')); | |
418 | |
419 $this->Session->write('SessionTestCase', null); | |
420 $this->assertEqual($this->Session->read('SessionTestCase'), null); | |
421 | |
422 $this->Session->write('SessionTestCase', 'This is a Test'); | |
423 $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test'); | |
424 | |
425 $this->Session->write('SessionTestCase', 'This is a Test'); | |
426 $this->Session->write('SessionTestCase', 'This was updated'); | |
427 $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated'); | |
428 | |
429 $this->Session->destroy(); | |
430 $this->assertFalse($this->Session->read('SessionTestCase')); | |
431 } | |
432 | |
433 /** | |
434 * testReadAndWriteWithDatabaseStorage method | |
435 * | |
436 * @access public | |
437 * @return void | |
438 */ | |
439 function testReadAndWriteWithDatabaseStorage() { | |
440 unset($_SESSION); | |
441 session_destroy(); | |
442 Configure::write('Session.table', 'sessions'); | |
443 Configure::write('Session.model', 'Session'); | |
444 Configure::write('Session.database', 'test_suite'); | |
445 Configure::write('Session.save', 'database'); | |
446 $this->setUp(); | |
447 | |
448 $this->Session->write('SessionTestCase', 0); | |
449 $this->assertEqual($this->Session->read('SessionTestCase'), 0); | |
450 | |
451 $this->Session->write('SessionTestCase', '0'); | |
452 $this->assertEqual($this->Session->read('SessionTestCase'), '0'); | |
453 $this->assertFalse($this->Session->read('SessionTestCase') === 0); | |
454 | |
455 $this->Session->write('SessionTestCase', false); | |
456 $this->assertFalse($this->Session->read('SessionTestCase')); | |
457 | |
458 $this->Session->write('SessionTestCase', null); | |
459 $this->assertEqual($this->Session->read('SessionTestCase'), null); | |
460 | |
461 $this->Session->write('SessionTestCase', 'This is a Test'); | |
462 $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test'); | |
463 | |
464 $this->Session->write('SessionTestCase', 'Some additional data'); | |
465 $this->assertEqual($this->Session->read('SessionTestCase'), 'Some additional data'); | |
466 | |
467 $this->Session->destroy(); | |
468 $this->assertFalse($this->Session->read('SessionTestCase')); | |
469 session_write_close(); | |
470 | |
471 unset($_SESSION); | |
472 ini_set('session.save_handler', 'files'); | |
473 Configure::write('Session.save', 'php'); | |
474 $this->setUp(); | |
475 } | |
476 | |
477 /** | |
478 * testReadAndWriteWithDatabaseStorage method | |
479 * | |
480 * @access public | |
481 * @return void | |
482 */ | |
483 function testDatabaseStorageEmptySessionId() { | |
484 unset($_SESSION); | |
485 session_destroy(); | |
486 Configure::write('Session.table', 'sessions'); | |
487 Configure::write('Session.model', 'Session'); | |
488 Configure::write('Session.database', 'test_suite'); | |
489 Configure::write('Session.save', 'database'); | |
490 $this->setUp(); | |
491 $id = $this->Session->id(); | |
492 | |
493 $this->Session->id = ''; | |
494 session_id(''); | |
495 | |
496 $this->Session->write('SessionTestCase', 'This is a Test'); | |
497 $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test'); | |
498 | |
499 session_write_close(); | |
500 | |
501 unset($_SESSION); | |
502 ini_set('session.save_handler', 'files'); | |
503 Configure::write('Session.save', 'php'); | |
504 session_id($id); | |
505 $this->setUp(); | |
506 } | |
507 | |
508 } |