LegacyDefaultCsrfProviderTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Form\Tests\Extension\Csrf\CsrfProvider;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider;
  13. /**
  14. * @runTestsInSeparateProcesses
  15. * @preserveGlobalState disabled
  16. * @group legacy
  17. */
  18. class LegacyDefaultCsrfProviderTest extends TestCase
  19. {
  20. protected $provider;
  21. protected function setUp()
  22. {
  23. $this->provider = new DefaultCsrfProvider('SECRET');
  24. }
  25. protected function tearDown()
  26. {
  27. $this->provider = null;
  28. }
  29. public function testGenerateCsrfToken()
  30. {
  31. session_start();
  32. $token = $this->provider->generateCsrfToken('foo');
  33. $this->assertEquals(sha1('SECRET'.'foo'.session_id()), $token);
  34. }
  35. /**
  36. * @requires PHP 5.4
  37. */
  38. public function testGenerateCsrfTokenOnUnstartedSession()
  39. {
  40. session_id('touti');
  41. $this->assertSame(PHP_SESSION_NONE, session_status());
  42. $token = $this->provider->generateCsrfToken('foo');
  43. $this->assertEquals(sha1('SECRET'.'foo'.session_id()), $token);
  44. $this->assertSame(PHP_SESSION_ACTIVE, session_status());
  45. }
  46. public function testIsCsrfTokenValidSucceeds()
  47. {
  48. session_start();
  49. $token = sha1('SECRET'.'foo'.session_id());
  50. $this->assertTrue($this->provider->isCsrfTokenValid('foo', $token));
  51. }
  52. public function testIsCsrfTokenValidFails()
  53. {
  54. session_start();
  55. $token = sha1('SECRET'.'bar'.session_id());
  56. $this->assertFalse($this->provider->isCsrfTokenValid('foo', $token));
  57. }
  58. }