CsrfTokenGeneratorTest.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace Test\Security\CSRF;
  9. class CsrfTokenGeneratorTest extends \Test\TestCase {
  10. /** @var \OCP\Security\ISecureRandom */
  11. private $random;
  12. /** @var \OC\Security\CSRF\CsrfTokenGenerator */
  13. private $csrfTokenGenerator;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->random = $this->getMockBuilder('\OCP\Security\ISecureRandom')
  17. ->disableOriginalConstructor()->getMock();
  18. $this->csrfTokenGenerator = new \OC\Security\CSRF\CsrfTokenGenerator($this->random);
  19. }
  20. public function testGenerateTokenWithCustomNumber(): void {
  21. $this->random
  22. ->expects($this->once())
  23. ->method('generate')
  24. ->with(3)
  25. ->willReturn('abc');
  26. $this->assertSame('abc', $this->csrfTokenGenerator->generateToken(3));
  27. }
  28. public function testGenerateTokenWithDefault(): void {
  29. $this->random
  30. ->expects($this->once())
  31. ->method('generate')
  32. ->with(32)
  33. ->willReturn('12345678901234567890123456789012');
  34. $this->assertSame('12345678901234567890123456789012', $this->csrfTokenGenerator->generateToken(32));
  35. }
  36. }