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\TokenStorage;
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\Security\CSRF\TokenStorage\SessionStorage;
|
2016-11-02 16:10:51 -04:00
|
|
|
use OCP\ISession;
|
|
|
|
|
|
2016-01-25 11:15:54 -05:00
|
|
|
class SessionStorageTest extends \Test\TestCase {
|
2025-06-30 10:56:59 -04:00
|
|
|
/** @var ISession */
|
2016-01-25 11:15:54 -05:00
|
|
|
private $session;
|
|
|
|
|
/** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
|
|
|
|
|
private $sessionStorage;
|
|
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-01-25 11:15:54 -05:00
|
|
|
parent::setUp();
|
2017-10-24 09:26:53 -04:00
|
|
|
$this->session = $this->getMockBuilder(ISession::class)
|
2016-01-25 11:15:54 -05:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->sessionStorage = new SessionStorage($this->session);
|
2016-01-25 11:15:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2025-05-13 02:49:30 -04:00
|
|
|
public static function getTokenDataProvider(): array {
|
2016-01-25 11:15:54 -05:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
null,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $token
|
|
|
|
|
*
|
|
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('getTokenDataProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetTokenWithEmptyToken($token): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
|
$this->expectExceptionMessage('Session does not contain a requesttoken');
|
|
|
|
|
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('get')
|
|
|
|
|
->with('requesttoken')
|
|
|
|
|
->willReturn($token);
|
|
|
|
|
$this->sessionStorage->getToken();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetTokenWithValidToken(): void {
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('get')
|
|
|
|
|
->with('requesttoken')
|
|
|
|
|
->willReturn('MyFancyCsrfToken');
|
|
|
|
|
$this->assertSame('MyFancyCsrfToken', $this->sessionStorage->getToken());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetToken(): void {
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('set')
|
|
|
|
|
->with('requesttoken', 'TokenToSet');
|
|
|
|
|
$this->sessionStorage->setToken('TokenToSet');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveToken(): void {
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('remove')
|
|
|
|
|
->with('requesttoken');
|
|
|
|
|
$this->sessionStorage->removeToken();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testHasTokenWithExistingToken(): void {
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('exists')
|
|
|
|
|
->with('requesttoken')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$this->assertSame(true, $this->sessionStorage->hasToken());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testHasTokenWithoutExistingToken(): void {
|
2016-01-25 11:15:54 -05:00
|
|
|
$this->session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('exists')
|
|
|
|
|
->with('requesttoken')
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
$this->assertSame(false, $this->sessionStorage->hasToken());
|
|
|
|
|
}
|
2016-11-02 16:10:51 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetSession(): void {
|
2016-11-02 16:10:51 -04:00
|
|
|
$session = $this->createMock(ISession::class);
|
|
|
|
|
$session
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('get')
|
|
|
|
|
->with('requesttoken')
|
|
|
|
|
->willReturn('MyToken');
|
|
|
|
|
$this->sessionStorage->setSession($session);
|
|
|
|
|
$this->assertSame('MyToken', $this->sessionStorage->getToken());
|
|
|
|
|
}
|
2016-01-25 11:15:54 -05:00
|
|
|
}
|