CsrfTokenTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 CsrfTokenTest extends \Test\TestCase {
  24. public function testGetEncryptedValue() {
  25. $csrfToken = new \OC\Security\CSRF\CsrfToken('MyCsrfToken');
  26. $this->assertSame(33, strlen($csrfToken->getEncryptedValue()));
  27. $this->assertSame(':', $csrfToken->getEncryptedValue()[16]);
  28. }
  29. public function testGetEncryptedValueStaysSameOnSecondRequest() {
  30. $csrfToken = new \OC\Security\CSRF\CsrfToken('MyCsrfToken');
  31. $tokenValue = $csrfToken->getEncryptedValue();
  32. $this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
  33. $this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
  34. }
  35. public function testGetDecryptedValue() {
  36. $a = 'abc';
  37. $b = 'def';
  38. $xorB64 = 'BQcF';
  39. $tokenVal = sprintf('%s:%s', $xorB64, base64_encode($a));
  40. $csrfToken = new \OC\Security\CSRF\CsrfToken($tokenVal);
  41. $this->assertSame($b, $csrfToken->getDecryptedValue());
  42. }
  43. }