123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Security\Csrf\Tests;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\Security\Csrf\CsrfToken;
- use Symfony\Component\Security\Csrf\CsrfTokenManager;
- /**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class CsrfTokenManagerTest extends TestCase
- {
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testGetNonExistingToken($namespace, $manager, $storage, $generator)
- {
- $storage->expects($this->once())
- ->method('hasToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue(false));
- $generator->expects($this->once())
- ->method('generateToken')
- ->will($this->returnValue('TOKEN'));
- $storage->expects($this->once())
- ->method('setToken')
- ->with($namespace.'token_id', 'TOKEN');
- $token = $manager->getToken('token_id');
- $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
- $this->assertSame('token_id', $token->getId());
- $this->assertSame('TOKEN', $token->getValue());
- }
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testUseExistingTokenIfAvailable($namespace, $manager, $storage)
- {
- $storage->expects($this->once())
- ->method('hasToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue(true));
- $storage->expects($this->once())
- ->method('getToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue('TOKEN'));
- $token = $manager->getToken('token_id');
- $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
- $this->assertSame('token_id', $token->getId());
- $this->assertSame('TOKEN', $token->getValue());
- }
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testRefreshTokenAlwaysReturnsNewToken($namespace, $manager, $storage, $generator)
- {
- $storage->expects($this->never())
- ->method('hasToken');
- $generator->expects($this->once())
- ->method('generateToken')
- ->will($this->returnValue('TOKEN'));
- $storage->expects($this->once())
- ->method('setToken')
- ->with($namespace.'token_id', 'TOKEN');
- $token = $manager->refreshToken('token_id');
- $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
- $this->assertSame('token_id', $token->getId());
- $this->assertSame('TOKEN', $token->getValue());
- }
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testMatchingTokenIsValid($namespace, $manager, $storage)
- {
- $storage->expects($this->once())
- ->method('hasToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue(true));
- $storage->expects($this->once())
- ->method('getToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue('TOKEN'));
- $this->assertTrue($manager->isTokenValid(new CsrfToken('token_id', 'TOKEN')));
- }
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testNonMatchingTokenIsNotValid($namespace, $manager, $storage)
- {
- $storage->expects($this->once())
- ->method('hasToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue(true));
- $storage->expects($this->once())
- ->method('getToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue('TOKEN'));
- $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR')));
- }
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testNonExistingTokenIsNotValid($namespace, $manager, $storage)
- {
- $storage->expects($this->once())
- ->method('hasToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue(false));
- $storage->expects($this->never())
- ->method('getToken');
- $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR')));
- }
- /**
- * @dataProvider getManagerGeneratorAndStorage
- */
- public function testRemoveToken($namespace, $manager, $storage)
- {
- $storage->expects($this->once())
- ->method('removeToken')
- ->with($namespace.'token_id')
- ->will($this->returnValue('REMOVED_TOKEN'));
- $this->assertSame('REMOVED_TOKEN', $manager->removeToken('token_id'));
- }
- public function testNamespaced()
- {
- $generator = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock();
- $storage = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock();
- $requestStack = new RequestStack();
- $requestStack->push(new Request(array(), array(), array(), array(), array(), array('HTTPS' => 'on')));
- $manager = new CsrfTokenManager($generator, $storage, null, $requestStack);
- $token = $manager->getToken('foo');
- $this->assertSame('foo', $token->getId());
- }
- public function getManagerGeneratorAndStorage()
- {
- $data = array();
- list($generator, $storage) = $this->getGeneratorAndStorage();
- $data[] = array('', new CsrfTokenManager($generator, $storage, ''), $storage, $generator);
- list($generator, $storage) = $this->getGeneratorAndStorage();
- $data[] = array('https-', new CsrfTokenManager($generator, $storage), $storage, $generator);
- list($generator, $storage) = $this->getGeneratorAndStorage();
- $data[] = array('aNamespace-', new CsrfTokenManager($generator, $storage, 'aNamespace-'), $storage, $generator);
- $requestStack = new RequestStack();
- $requestStack->push(new Request(array(), array(), array(), array(), array(), array('HTTPS' => 'on')));
- list($generator, $storage) = $this->getGeneratorAndStorage();
- $data[] = array('https-', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator);
- list($generator, $storage) = $this->getGeneratorAndStorage();
- $data[] = array('generated-', new CsrfTokenManager($generator, $storage, function () {
- return 'generated-';
- }), $storage, $generator);
- $requestStack = new RequestStack();
- $requestStack->push(new Request());
- list($generator, $storage) = $this->getGeneratorAndStorage();
- $data[] = array('', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator);
- return $data;
- }
- private function getGeneratorAndStorage()
- {
- return array(
- $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(),
- $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(),
- );
- }
- protected function setUp()
- {
- $_SERVER['HTTPS'] = 'on';
- }
- protected function tearDown()
- {
- parent::tearDown();
- unset($_SERVER['HTTPS']);
- }
- }
|