2016-01-25 11:15:54 -05:00
|
|
|
<?php
|
2021-04-19 09:50:30 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-01-25 11:15:54 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-01-25 11:15:54 -05:00
|
|
|
*/
|
|
|
|
|
|
2016-05-19 03:02:58 -04:00
|
|
|
namespace Test\Security\CSRF;
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\Security\CSRF\CsrfToken;
|
|
|
|
|
|
2016-01-25 11:15:54 -05:00
|
|
|
class CsrfTokenTest extends \Test\TestCase {
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetEncryptedValue(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$csrfToken = new CsrfToken('MyCsrfToken');
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->assertSame(33, strlen($csrfToken->getEncryptedValue()));
|
|
|
|
|
$this->assertSame(':', $csrfToken->getEncryptedValue()[16]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetEncryptedValueStaysSameOnSecondRequest(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$csrfToken = new CsrfToken('MyCsrfToken');
|
2016-10-24 05:00:00 -04:00
|
|
|
$tokenValue = $csrfToken->getEncryptedValue();
|
|
|
|
|
$this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
|
|
|
|
|
$this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetDecryptedValue(): void {
|
2016-10-31 13:22:42 -04:00
|
|
|
$a = 'abc';
|
|
|
|
|
$b = 'def';
|
|
|
|
|
$xorB64 = 'BQcF';
|
|
|
|
|
$tokenVal = sprintf('%s:%s', $xorB64, base64_encode($a));
|
2025-06-12 12:31:58 -04:00
|
|
|
$csrfToken = new CsrfToken($tokenVal);
|
2016-10-31 13:22:42 -04:00
|
|
|
$this->assertSame($b, $csrfToken->getDecryptedValue());
|
2016-01-25 11:15:54 -05:00
|
|
|
}
|
|
|
|
|
}
|