aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Security/CSRF/CsrfTokenTest.php
blob: c6f9aaa5be74016d1ebd50648e398122dbed96dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace Test\Security\CSRF;

class CsrfTokenTest extends \Test\TestCase {
	public function testGetEncryptedValue() {
		$csrfToken = new \OC\Security\CSRF\CsrfToken('MyCsrfToken');
		$this->assertSame(33, strlen($csrfToken->getEncryptedValue()));
		$this->assertSame(':', $csrfToken->getEncryptedValue()[16]);
	}

	public function testGetEncryptedValueStaysSameOnSecondRequest() {
		$csrfToken = new \OC\Security\CSRF\CsrfToken('MyCsrfToken');
		$tokenValue = $csrfToken->getEncryptedValue();
		$this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
		$this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
	}

	public function testGetDecryptedValue() {
		$a = 'abc';
		$b = 'def';
		$xorB64 = 'BQcF';
		$tokenVal = sprintf('%s:%s', $xorB64, base64_encode($a));
		$csrfToken = new \OC\Security\CSRF\CsrfToken($tokenVal);
		$this->assertSame($b, $csrfToken->getDecryptedValue());
	}
}