2016-10-24 10:31:06 -04:00
|
|
|
<?php
|
2021-04-19 09:50:30 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-10-24 10:31:06 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-10-24 10:31:06 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Security\CSP;
|
|
|
|
|
|
2019-07-08 15:54:45 -04:00
|
|
|
use OC\AppFramework\Http\Request;
|
2016-10-24 10:31:06 -04:00
|
|
|
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
|
|
|
|
|
use OC\Security\CSRF\CsrfToken;
|
|
|
|
|
use OC\Security\CSRF\CsrfTokenManager;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
2020-04-10 08:19:56 -04:00
|
|
|
class ContentSecurityPolicyNonceManagerTest extends TestCase {
|
2016-10-24 10:31:06 -04:00
|
|
|
/** @var CsrfTokenManager */
|
|
|
|
|
private $csrfTokenManager;
|
2019-07-08 15:54:45 -04:00
|
|
|
/** @var Request */
|
|
|
|
|
private $request;
|
2016-10-24 10:31:06 -04:00
|
|
|
/** @var ContentSecurityPolicyNonceManager */
|
|
|
|
|
private $nonceManager;
|
|
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-10-24 10:31:06 -04:00
|
|
|
$this->csrfTokenManager = $this->createMock(CsrfTokenManager::class);
|
2019-07-08 15:54:45 -04:00
|
|
|
$this->request = $this->createMock(Request::class);
|
2016-10-24 10:31:06 -04:00
|
|
|
$this->nonceManager = new ContentSecurityPolicyNonceManager(
|
2016-10-25 15:36:17 -04:00
|
|
|
$this->csrfTokenManager,
|
2019-07-08 15:54:45 -04:00
|
|
|
$this->request
|
2016-10-24 10:31:06 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetNonce() {
|
|
|
|
|
$token = $this->createMock(CsrfToken::class);
|
|
|
|
|
$token
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('getEncryptedValue')
|
|
|
|
|
->willReturn('MyToken');
|
|
|
|
|
|
|
|
|
|
$this->csrfTokenManager
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('getToken')
|
|
|
|
|
->willReturn($token);
|
|
|
|
|
|
|
|
|
|
$this->assertSame('TXlUb2tlbg==', $this->nonceManager->getNonce());
|
|
|
|
|
$this->assertSame('TXlUb2tlbg==', $this->nonceManager->getNonce());
|
|
|
|
|
}
|
2019-07-08 15:54:45 -04:00
|
|
|
|
|
|
|
|
public function testGetNonceServerVar() {
|
|
|
|
|
$token = 'SERVERNONCE';
|
|
|
|
|
$this->request
|
|
|
|
|
->method('__isset')
|
|
|
|
|
->with('server')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
|
->method('__get')
|
|
|
|
|
->with('server')
|
|
|
|
|
->willReturn(['CSP_NONCE' => $token]);
|
|
|
|
|
|
|
|
|
|
$this->assertSame($token, $this->nonceManager->getNonce());
|
|
|
|
|
$this->assertSame($token, $this->nonceManager->getNonce());
|
|
|
|
|
}
|
2016-10-24 10:31:06 -04:00
|
|
|
}
|