|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\Tests\Extension\Csrf\CsrfProvider; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Csrf\CsrfTokenGenerator; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Bernhard Schussek <[email protected]> |
| 18 | + */ |
| 19 | +class CsrfTokenGeneratorTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * A non alpha-numeric byte string |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private static $bytes; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $random; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $storage; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var CsrfTokenGenerator |
| 39 | + */ |
| 40 | + private $generator; |
| 41 | + |
| 42 | + public static function setUpBeforeClass() |
| 43 | + { |
| 44 | + self::$bytes = base64_decode('aMf+Tct/RLn2WQ=='); |
| 45 | + } |
| 46 | + |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->random = $this->getMock('Symfony\Component\Security\Core\Util\SecureRandomInterface'); |
| 50 | + $this->storage = $this->getMock('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface'); |
| 51 | + $this->generator = new CsrfTokenGenerator($this->storage, $this->random); |
| 52 | + } |
| 53 | + |
| 54 | + protected function tearDown() |
| 55 | + { |
| 56 | + $this->random = null; |
| 57 | + $this->storage = null; |
| 58 | + $this->generator = null; |
| 59 | + } |
| 60 | + |
| 61 | + public function testGenerateNewToken() |
| 62 | + { |
| 63 | + $this->storage->expects($this->once()) |
| 64 | + ->method('getToken') |
| 65 | + ->with('token_id', false) |
| 66 | + ->will($this->returnValue(false)); |
| 67 | + |
| 68 | + $this->storage->expects($this->once()) |
| 69 | + ->method('setToken') |
| 70 | + ->with('token_id', $this->anything()) |
| 71 | + ->will($this->returnCallback(function ($tokenId, $token) use (&$storedToken) { |
| 72 | + $storedToken = $token; |
| 73 | + })); |
| 74 | + |
| 75 | + $this->random->expects($this->once()) |
| 76 | + ->method('nextBytes') |
| 77 | + ->will($this->returnValue(self::$bytes)); |
| 78 | + |
| 79 | + $token = $this->generator->generateCsrfToken('token_id'); |
| 80 | + |
| 81 | + $this->assertSame($token, $storedToken); |
| 82 | + $this->assertTrue(ctype_print($token), 'is printable'); |
| 83 | + $this->assertStringNotMatchesFormat('%S+%S', $token, 'is URI safe'); |
| 84 | + $this->assertStringNotMatchesFormat('%S/%S', $token, 'is URI safe'); |
| 85 | + $this->assertStringNotMatchesFormat('%S=%S', $token, 'is URI safe'); |
| 86 | + } |
| 87 | + |
| 88 | + public function testUseExistingTokenIfAvailable() |
| 89 | + { |
| 90 | + $this->storage->expects($this->once()) |
| 91 | + ->method('getToken') |
| 92 | + ->with('token_id', false) |
| 93 | + ->will($this->returnValue('TOKEN')); |
| 94 | + |
| 95 | + $this->storage->expects($this->never()) |
| 96 | + ->method('setToken'); |
| 97 | + |
| 98 | + $this->random->expects($this->never()) |
| 99 | + ->method('nextBytes'); |
| 100 | + |
| 101 | + $token = $this->generator->generateCsrfToken('token_id'); |
| 102 | + |
| 103 | + $this->assertEquals('TOKEN', $token); |
| 104 | + } |
| 105 | + |
| 106 | + public function testMatchingTokenIsValid() |
| 107 | + { |
| 108 | + $this->storage->expects($this->once()) |
| 109 | + ->method('hasToken') |
| 110 | + ->with('token_id') |
| 111 | + ->will($this->returnValue(true)); |
| 112 | + |
| 113 | + $this->storage->expects($this->once()) |
| 114 | + ->method('getToken') |
| 115 | + ->with('token_id') |
| 116 | + ->will($this->returnValue('TOKEN')); |
| 117 | + |
| 118 | + $this->assertTrue($this->generator->isCsrfTokenValid('token_id', 'TOKEN')); |
| 119 | + } |
| 120 | + |
| 121 | + public function testNonMatchingTokenIsNotValid() |
| 122 | + { |
| 123 | + $this->storage->expects($this->once()) |
| 124 | + ->method('hasToken') |
| 125 | + ->with('token_id') |
| 126 | + ->will($this->returnValue(true)); |
| 127 | + |
| 128 | + $this->storage->expects($this->once()) |
| 129 | + ->method('getToken') |
| 130 | + ->with('token_id') |
| 131 | + ->will($this->returnValue('TOKEN')); |
| 132 | + |
| 133 | + $this->assertFalse($this->generator->isCsrfTokenValid('token_id', 'FOOBAR')); |
| 134 | + } |
| 135 | + |
| 136 | + public function testNonExistingTokenIsNotValid() |
| 137 | + { |
| 138 | + $this->storage->expects($this->once()) |
| 139 | + ->method('hasToken') |
| 140 | + ->with('token_id') |
| 141 | + ->will($this->returnValue(false)); |
| 142 | + |
| 143 | + $this->storage->expects($this->never()) |
| 144 | + ->method('getToken'); |
| 145 | + |
| 146 | + $this->assertFalse($this->generator->isCsrfTokenValid('token_id', 'FOOBAR')); |
| 147 | + } |
| 148 | +} |
0 commit comments