CsrfTokenGeneratorTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Security\CSRF;
  23. class CsrfTokenGeneratorTest extends \Test\TestCase {
  24. /** @var \OCP\Security\ISecureRandom */
  25. private $random;
  26. /** @var \OC\Security\CSRF\CsrfTokenGenerator */
  27. private $csrfTokenGenerator;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->random = $this->getMockBuilder('\OCP\Security\ISecureRandom')
  31. ->disableOriginalConstructor()->getMock();
  32. $this->csrfTokenGenerator = new \OC\Security\CSRF\CsrfTokenGenerator($this->random);
  33. }
  34. public function testGenerateTokenWithCustomNumber() {
  35. $this->random
  36. ->expects($this->once())
  37. ->method('generate')
  38. ->with(3)
  39. ->willReturn('abc');
  40. $this->assertSame('abc', $this->csrfTokenGenerator->generateToken(3));
  41. }
  42. public function testGenerateTokenWithDefault() {
  43. $this->random
  44. ->expects($this->once())
  45. ->method('generate')
  46. ->with(32)
  47. ->willReturn('12345678901234567890123456789012');
  48. $this->assertSame('12345678901234567890123456789012', $this->csrfTokenGenerator->generateToken(32));
  49. }
  50. }