| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 |
- <?php
- /**
- * SessionTest file
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Network;
- use Cake\Cache\Cache;
- use Cake\Core\App;
- use Cake\Core\Plugin;
- use Cake\Network\Session;
- use Cake\Network\Session\CacheSession;
- use Cake\Network\Session\DatabaseSession;
- use Cake\TestSuite\TestCase;
- /**
- * Class TestCacheSession
- *
- */
- class TestCacheSession extends CacheSession {
- protected function _writeSession() {
- return true;
- }
- }
- /**
- * Class TestDatabaseSession
- *
- */
- class TestDatabaseSession extends DatabaseSession {
- protected function _writeSession() {
- return true;
- }
- }
- /**
- * SessionTest class
- *
- */
- class SessionTest extends TestCase {
- protected static $_gcDivisor;
- /**
- * Fixtures used in the SessionTest
- *
- * @var array
- */
- public $fixtures = array('core.session', 'core.cake_session');
- /**
- * setup before class.
- *
- * @return void
- */
- public static function setupBeforeClass() {
- // Make sure garbage colector will be called
- static::$_gcDivisor = ini_get('session.gc_divisor');
- ini_set('session.gc_divisor', '1');
- }
- /**
- * teardown after class
- *
- * @return void
- */
- public static function teardownAfterClass() {
- // Revert to the default setting
- ini_set('session.gc_divisor', static::$_gcDivisor);
- }
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- unset($_SESSION);
- parent::tearDown();
- }
- /**
- * test setting ini properties with Session configuration.
- *
- * @return void
- */
- public function testSessionConfigIniSetting() {
- $_SESSION = null;
- $config = array(
- 'cookie' => 'test',
- 'checkAgent' => false,
- 'timeout' => 86400,
- 'ini' => array(
- 'session.referer_check' => 'example.com',
- 'session.use_trans_sid' => false
- )
- );
- $session = Session::create($config);
- $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
- $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
- $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
- }
- /**
- * testCheck method
- *
- * @return void
- */
- public function testCheck() {
- $session = new Session();
- $session->write('SessionTestCase', 'value');
- $this->assertTrue($session->check('SessionTestCase'));
- $this->assertFalse($session->check('NotExistingSessionTestCase'));
- $this->assertFalse($session->check('Crazy.foo'));
- $session->write('Crazy.foo', ['bar' => 'baz']);
- $this->assertTrue($session->check('Crazy.foo'));
- $this->assertTrue($session->check('Crazy.foo.bar'));
- }
- /**
- * testSimpleRead method
- *
- * @return void
- */
- public function testSimpleRead() {
- $session = new Session();
- $session->write('testing', '1,2,3');
- $result = $session->read('testing');
- $this->assertEquals('1,2,3', $result);
- $session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
- $result = $session->read('testing.1');
- $this->assertEquals('one', $result);
- $result = $session->read('testing');
- $this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);
- $result = $session->read();
- $this->assertTrue(isset($result['testing']));
- $session->write('This.is.a.deep.array.my.friend', 'value');
- $result = $session->read('This.is.a.deep.array');
- $this->assertEquals(['my' => ['friend' => 'value']], $result);
- }
- /**
- * testReadEmpty
- *
- * @return void
- */
- public function testReadEmpty() {
- $session = new Session();
- $this->assertNull($session->read(''));
- }
- /**
- * test writing a hash of values/
- *
- * @return void
- */
- public function testWriteArray() {
- $session = new Session();
- $session->write([
- 'one' => 1,
- 'two' => 2,
- 'three' => ['something'],
- 'null' => null
- ]);
- $this->assertEquals(1, $session->read('one'));
- $this->assertEquals(['something'], $session->read('three'));
- $this->assertEquals(null, $session->read('null'));
- }
- /**
- * Test overwriting a string value as if it were an array.
- *
- * @return void
- */
- public function testWriteOverwriteStringValue() {
- $session = new Session();
- $session->write('Some.string', 'value');
- $this->assertEquals('value', $session->read('Some.string'));
- $session->write('Some.string.array', ['values']);
- $this->assertEquals(['values'], $session->read('Some.string.array'));
- }
- /**
- * testId method
- *
- * @return void
- */
- public function testId() {
- $session = new Session();
- $result = $session->id();
- $expected = session_id();
- $this->assertEquals($expected, $result);
- $session->id('MySessionId');
- $result = $session->id();
- $this->assertEquals('MySessionId', $result);
- $this->assertEquals('MySessionId', session_id());
- $session->id('');
- $this->assertEquals('', session_id());
- }
- /**
- * testStarted method
- *
- * @return void
- */
- public function testStarted() {
- unset($_SESSION);
- $_SESSION = null;
- $this->assertFalse(TestCakeSession::started());
- $this->assertTrue(TestCakeSession::start());
- $this->assertTrue(TestCakeSession::started());
- }
- /**
- * testDel method
- *
- * @return void
- */
- public function testDelete() {
- $this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
- $this->assertTrue(TestCakeSession::delete('Delete.me'));
- $this->assertFalse(TestCakeSession::check('Delete.me'));
- $this->assertTrue(TestCakeSession::check('Delete'));
- $this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
- $this->assertTrue(TestCakeSession::delete('Clearing'));
- $this->assertFalse(TestCakeSession::check('Clearing.sale'));
- $this->assertFalse(TestCakeSession::check('Clearing'));
- }
- /**
- * testDestroy method
- *
- * @return void
- */
- public function testDestroy() {
- TestCakeSession::write('bulletProof', 'invincible');
- $id = TestCakeSession::id();
- TestCakeSession::destroy();
- $this->assertFalse(TestCakeSession::check('bulletProof'));
- $this->assertNotEquals(TestCakeSession::id(), $id);
- }
- /**
- * testCheckingSavedEmpty method
- *
- * @return void
- */
- public function testCheckingSavedEmpty() {
- $this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
- $this->assertTrue(TestCakeSession::check('SessionTestCase'));
- $this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
- $this->assertTrue(TestCakeSession::check('SessionTestCase'));
- $this->assertTrue(TestCakeSession::write('SessionTestCase', false));
- $this->assertTrue(TestCakeSession::check('SessionTestCase'));
- $this->assertTrue(TestCakeSession::write('SessionTestCase', null));
- $this->assertFalse(TestCakeSession::check('SessionTestCase'));
- }
- /**
- * testCheckKeyWithSpaces method
- *
- * @return void
- */
- public function testCheckKeyWithSpaces() {
- $this->assertTrue(TestCakeSession::write('Session Test', "test"));
- $this->assertTrue(TestCakeSession::check('Session Test'));
- TestCakeSession::delete('Session Test');
- $this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
- $this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
- }
- /**
- * testCheckEmpty
- *
- * @return void
- */
- public function testCheckEmpty() {
- $this->assertFalse(TestCakeSession::check());
- }
- /**
- * test key exploitation
- *
- * @return void
- */
- public function testKeyExploit() {
- $key = "a'] = 1; phpinfo(); \$_SESSION['a";
- $result = TestCakeSession::write($key, 'haxored');
- $this->assertFalse($result);
- $result = TestCakeSession::read($key);
- $this->assertNull($result);
- }
- /**
- * testReadingSavedEmpty method
- *
- * @return void
- */
- public function testReadingSavedEmpty() {
- TestCakeSession::write('SessionTestCase', 0);
- $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', '0');
- $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
- $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
- TestCakeSession::write('SessionTestCase', false);
- $this->assertFalse(TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', null);
- $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
- }
- /**
- * testCheckUserAgentFalse method
- *
- * @return void
- */
- public function testCheckUserAgentFalse() {
- Configure::write('Session.checkAgent', false);
- TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
- $this->assertTrue(TestCakeSession::valid());
- }
- /**
- * testCheckUserAgentTrue method
- *
- * @return void
- */
- public function testCheckUserAgentTrue() {
- Configure::write('Session.checkAgent', true);
- TestCakeSession::$error = false;
- $agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
- TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
- TestCakeSession::setUserAgent($agent);
- $this->assertFalse(TestCakeSession::valid());
- }
- /**
- * testReadAndWriteWithCakeStorage method
- *
- * @return void
- */
- public function testReadAndWriteWithCakeStorage() {
- Configure::write('Session.defaults', 'cake');
- TestCakeSession::init();
- TestCakeSession::start();
- TestCakeSession::write('SessionTestCase', 0);
- $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', '0');
- $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
- $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
- TestCakeSession::write('SessionTestCase', false);
- $this->assertFalse(TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', null);
- $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', 'This is a Test');
- $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', 'This is a Test');
- TestCakeSession::write('SessionTestCase', 'This was updated');
- $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
- TestCakeSession::destroy();
- $this->assertNull(TestCakeSession::read('SessionTestCase'));
- }
- /**
- * test using a handler from app/Model/Datasource/Session.
- *
- * @return void
- */
- public function testUsingAppLibsHandler() {
- Configure::write('App.namespace', 'TestApp');
- Configure::write('Session', array(
- 'defaults' => 'cake',
- 'handler' => array(
- 'engine' => 'TestAppLibSession'
- )
- ));
- TestCakeSession::start();
- $this->assertTrue(TestCakeSession::started());
- TestCakeSession::destroy();
- $this->assertFalse(TestCakeSession::started());
- }
- /**
- * test using a handler from a plugin.
- *
- * @return void
- */
- public function testUsingPluginHandler() {
- Plugin::load('TestPlugin');
- Configure::write('App.namespace', 'TestApp');
- Configure::write('Session', array(
- 'defaults' => 'cake',
- 'handler' => array(
- 'engine' => 'TestPlugin.TestPluginSession'
- )
- ));
- TestCakeSession::start();
- $this->assertTrue(TestCakeSession::started());
- TestCakeSession::destroy();
- $this->assertFalse(TestCakeSession::started());
- }
- /**
- * testReadAndWriteWithCacheStorage method
- *
- * @return void
- */
- public function testReadAndWriteWithCacheStorage() {
- Cache::config('default', [
- 'engine' => 'File',
- 'path' => TMP,
- 'prefix' => 'session_test_'
- ]);
- Configure::write('Session.defaults', 'cache');
- Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestCacheSession');
- TestCakeSession::init();
- TestCakeSession::destroy();
- TestCakeSession::write('SessionTestCase', 0);
- $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', '0');
- $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
- $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
- TestCakeSession::write('SessionTestCase', false);
- $this->assertFalse(TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', null);
- $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', 'This is a Test');
- $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', 'This is a Test');
- TestCakeSession::write('SessionTestCase', 'This was updated');
- $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
- TestCakeSession::destroy();
- $this->assertNull(TestCakeSession::read('SessionTestCase'));
- }
- /**
- * test that changing the config name of the cache config works.
- *
- * @return void
- */
- public function testReadAndWriteWithCustomCacheConfig() {
- Configure::write('Session.defaults', 'cache');
- Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestCacheSession');
- Configure::write('Session.handler.config', 'session_test');
- Cache::config('session_test', [
- 'engine' => 'File',
- 'prefix' => 'session_test_'
- ]);
- TestCakeSession::init();
- TestCakeSession::start();
- TestCakeSession::write('SessionTestCase', 'Some value');
- $this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
- $id = TestCakeSession::id();
- Cache::delete($id, 'session_test');
- }
- /**
- * testReadAndWriteWithDatabaseStorage method
- *
- * @return void
- */
- public function testReadAndWriteWithDatabaseStorage() {
- Configure::write('Session.defaults', 'database');
- Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestDatabaseSession');
- TestCakeSession::init();
- $this->assertNull(TestCakeSession::id());
- TestCakeSession::start();
- $expected = session_id();
- $this->assertEquals($expected, TestCakeSession::id());
- TestCakeSession::renew();
- $this->assertFalse($expected === TestCakeSession::id());
- $expected = session_id();
- $this->assertEquals($expected, TestCakeSession::id());
- TestCakeSession::write('SessionTestCase', 0);
- $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', '0');
- $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
- $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
- TestCakeSession::write('SessionTestCase', false);
- $this->assertFalse(TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', null);
- $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', 'This is a Test');
- $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
- TestCakeSession::write('SessionTestCase', 'Some additional data');
- $this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
- TestCakeSession::destroy();
- $this->assertNull(TestCakeSession::read('SessionTestCase'));
- Configure::write('Session', array(
- 'defaults' => 'php'
- ));
- TestCakeSession::init();
- }
- /**
- * testSessionTimeout method
- *
- * @return void
- */
- public function testSessionTimeout() {
- Configure::write('debug', true);
- Configure::write('Session.defaults', 'cake');
- Configure::write('Session.autoRegenerate', false);
- $timeoutSeconds = Configure::read('Session.timeout') * 60;
- TestCakeSession::destroy();
- TestCakeSession::write('Test', 'some value');
- $this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
- $this->assertEquals(10, $_SESSION['Config']['countdown']);
- $this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
- $this->assertWithinMargin(time(), Session::$time, 1);
- $this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
- Configure::write('Session.harden', true);
- TestCakeSession::destroy();
- TestCakeSession::write('Test', 'some value');
- $this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
- $this->assertEquals(10, $_SESSION['Config']['countdown']);
- $this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
- $this->assertWithinMargin(time(), Session::$time, 1);
- $this->assertWithinMargin(Session::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
- }
- /**
- * Test that cookieTimeout matches timeout when unspecified.
- *
- * @return void
- */
- public function testCookieTimeoutFallback() {
- $_SESSION = null;
- Configure::write('Session', array(
- 'defaults' => 'cake',
- 'timeout' => 400,
- ));
- TestCakeSession::start();
- $this->assertEquals(400, Configure::read('Session.cookieTimeout'));
- $this->assertEquals(400, Configure::read('Session.timeout'));
- $this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
- $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
- $_SESSION = null;
- Configure::write('Session', array(
- 'defaults' => 'cake',
- 'timeout' => 400,
- 'cookieTimeout' => 600
- ));
- TestCakeSession::start();
- $this->assertEquals(600, Configure::read('Session.cookieTimeout'));
- $this->assertEquals(400, Configure::read('Session.timeout'));
- }
- /**
- * Proves that invalid sessions will be destroyed and re-created
- * if invalid
- *
- * @return void
- */
- public function testInvalidSessionRenew() {
- TestCakeSession::start();
- $this->assertNotEmpty($_SESSION['Config']);
- $data = $_SESSION;
- session_write_close();
- $_SESSION = null;
- TestCakeSession::start();
- $this->assertEquals($data, $_SESSION);
- TestCakeSession::write('Foo', 'Bar');
- session_write_close();
- $_SESSION = null;
- TestCakeSession::userAgent('bogus!');
- TestCakeSession::start();
- $this->assertNotEquals($data, $_SESSION);
- }
- }
|