CsrfTokenManagerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Csrf\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Csrf\CsrfToken;
  15. use Symfony\Component\Security\Csrf\CsrfTokenManager;
  16. /**
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class CsrfTokenManagerTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider getManagerGeneratorAndStorage
  23. */
  24. public function testGetNonExistingToken($namespace, $manager, $storage, $generator)
  25. {
  26. $storage->expects($this->once())
  27. ->method('hasToken')
  28. ->with($namespace.'token_id')
  29. ->will($this->returnValue(false));
  30. $generator->expects($this->once())
  31. ->method('generateToken')
  32. ->will($this->returnValue('TOKEN'));
  33. $storage->expects($this->once())
  34. ->method('setToken')
  35. ->with($namespace.'token_id', 'TOKEN');
  36. $token = $manager->getToken('token_id');
  37. $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
  38. $this->assertSame('token_id', $token->getId());
  39. $this->assertSame('TOKEN', $token->getValue());
  40. }
  41. /**
  42. * @dataProvider getManagerGeneratorAndStorage
  43. */
  44. public function testUseExistingTokenIfAvailable($namespace, $manager, $storage)
  45. {
  46. $storage->expects($this->once())
  47. ->method('hasToken')
  48. ->with($namespace.'token_id')
  49. ->will($this->returnValue(true));
  50. $storage->expects($this->once())
  51. ->method('getToken')
  52. ->with($namespace.'token_id')
  53. ->will($this->returnValue('TOKEN'));
  54. $token = $manager->getToken('token_id');
  55. $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
  56. $this->assertSame('token_id', $token->getId());
  57. $this->assertSame('TOKEN', $token->getValue());
  58. }
  59. /**
  60. * @dataProvider getManagerGeneratorAndStorage
  61. */
  62. public function testRefreshTokenAlwaysReturnsNewToken($namespace, $manager, $storage, $generator)
  63. {
  64. $storage->expects($this->never())
  65. ->method('hasToken');
  66. $generator->expects($this->once())
  67. ->method('generateToken')
  68. ->will($this->returnValue('TOKEN'));
  69. $storage->expects($this->once())
  70. ->method('setToken')
  71. ->with($namespace.'token_id', 'TOKEN');
  72. $token = $manager->refreshToken('token_id');
  73. $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
  74. $this->assertSame('token_id', $token->getId());
  75. $this->assertSame('TOKEN', $token->getValue());
  76. }
  77. /**
  78. * @dataProvider getManagerGeneratorAndStorage
  79. */
  80. public function testMatchingTokenIsValid($namespace, $manager, $storage)
  81. {
  82. $storage->expects($this->once())
  83. ->method('hasToken')
  84. ->with($namespace.'token_id')
  85. ->will($this->returnValue(true));
  86. $storage->expects($this->once())
  87. ->method('getToken')
  88. ->with($namespace.'token_id')
  89. ->will($this->returnValue('TOKEN'));
  90. $this->assertTrue($manager->isTokenValid(new CsrfToken('token_id', 'TOKEN')));
  91. }
  92. /**
  93. * @dataProvider getManagerGeneratorAndStorage
  94. */
  95. public function testNonMatchingTokenIsNotValid($namespace, $manager, $storage)
  96. {
  97. $storage->expects($this->once())
  98. ->method('hasToken')
  99. ->with($namespace.'token_id')
  100. ->will($this->returnValue(true));
  101. $storage->expects($this->once())
  102. ->method('getToken')
  103. ->with($namespace.'token_id')
  104. ->will($this->returnValue('TOKEN'));
  105. $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR')));
  106. }
  107. /**
  108. * @dataProvider getManagerGeneratorAndStorage
  109. */
  110. public function testNonExistingTokenIsNotValid($namespace, $manager, $storage)
  111. {
  112. $storage->expects($this->once())
  113. ->method('hasToken')
  114. ->with($namespace.'token_id')
  115. ->will($this->returnValue(false));
  116. $storage->expects($this->never())
  117. ->method('getToken');
  118. $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR')));
  119. }
  120. /**
  121. * @dataProvider getManagerGeneratorAndStorage
  122. */
  123. public function testRemoveToken($namespace, $manager, $storage)
  124. {
  125. $storage->expects($this->once())
  126. ->method('removeToken')
  127. ->with($namespace.'token_id')
  128. ->will($this->returnValue('REMOVED_TOKEN'));
  129. $this->assertSame('REMOVED_TOKEN', $manager->removeToken('token_id'));
  130. }
  131. public function testNamespaced()
  132. {
  133. $generator = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock();
  134. $storage = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock();
  135. $requestStack = new RequestStack();
  136. $requestStack->push(new Request(array(), array(), array(), array(), array(), array('HTTPS' => 'on')));
  137. $manager = new CsrfTokenManager($generator, $storage, null, $requestStack);
  138. $token = $manager->getToken('foo');
  139. $this->assertSame('foo', $token->getId());
  140. }
  141. public function getManagerGeneratorAndStorage()
  142. {
  143. $data = array();
  144. list($generator, $storage) = $this->getGeneratorAndStorage();
  145. $data[] = array('', new CsrfTokenManager($generator, $storage, ''), $storage, $generator);
  146. list($generator, $storage) = $this->getGeneratorAndStorage();
  147. $data[] = array('https-', new CsrfTokenManager($generator, $storage), $storage, $generator);
  148. list($generator, $storage) = $this->getGeneratorAndStorage();
  149. $data[] = array('aNamespace-', new CsrfTokenManager($generator, $storage, 'aNamespace-'), $storage, $generator);
  150. $requestStack = new RequestStack();
  151. $requestStack->push(new Request(array(), array(), array(), array(), array(), array('HTTPS' => 'on')));
  152. list($generator, $storage) = $this->getGeneratorAndStorage();
  153. $data[] = array('https-', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator);
  154. list($generator, $storage) = $this->getGeneratorAndStorage();
  155. $data[] = array('generated-', new CsrfTokenManager($generator, $storage, function () {
  156. return 'generated-';
  157. }), $storage, $generator);
  158. $requestStack = new RequestStack();
  159. $requestStack->push(new Request());
  160. list($generator, $storage) = $this->getGeneratorAndStorage();
  161. $data[] = array('', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator);
  162. return $data;
  163. }
  164. private function getGeneratorAndStorage()
  165. {
  166. return array(
  167. $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(),
  168. $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(),
  169. );
  170. }
  171. protected function setUp()
  172. {
  173. $_SERVER['HTTPS'] = 'on';
  174. }
  175. protected function tearDown()
  176. {
  177. parent::tearDown();
  178. unset($_SERVER['HTTPS']);
  179. }
  180. }