mirror of
https://github.com/nextcloud/server.git
synced 2026-10-17 04:50:54 +02:00
This is clearer, safer and better typed. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace Test\Security\CSP;
|
|
|
|
use OC\AppFramework\Http\Request;
|
|
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
|
|
use OC\Security\CSRF\CsrfToken;
|
|
use OC\Security\CSRF\CsrfTokenManager;
|
|
use Test\TestCase;
|
|
|
|
class ContentSecurityPolicyNonceManagerTest extends TestCase {
|
|
private ContentSecurityPolicyNonceManager $nonceManager;
|
|
|
|
#[\Override]
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
$this->nonceManager = $this->createInstanceWithMocks(
|
|
ContentSecurityPolicyNonceManager::class,
|
|
[
|
|
/* We mock the private class */
|
|
'request' => $this->getAutoMock(Request::class),
|
|
],
|
|
);
|
|
}
|
|
|
|
public function testGetNonce(): void {
|
|
$secret = base64_encode('secret');
|
|
$tokenValue = base64_encode('secret' ^ 'value_') . ':' . $secret;
|
|
$token = $this->createMock(CsrfToken::class);
|
|
$token
|
|
->expects($this->once())
|
|
->method('getEncryptedValue')
|
|
->willReturn($tokenValue);
|
|
|
|
$this->getAutoMock(CsrfTokenManager::class)
|
|
->expects($this->once())
|
|
->method('getToken')
|
|
->willReturn($token);
|
|
|
|
$this->assertSame($secret, $this->nonceManager->getNonce());
|
|
// call it twice but `getEncryptedValue` is expected to be called only once
|
|
$this->assertSame($secret, $this->nonceManager->getNonce());
|
|
}
|
|
|
|
public function testGetNonceServerVar(): void {
|
|
$token = 'SERVERNONCE';
|
|
$this->getAutoMock(Request::class)
|
|
->method('__isset')
|
|
->with('server')
|
|
->willReturn(true);
|
|
|
|
$this->getAutoMock(Request::class)
|
|
->method('__get')
|
|
->with('server')
|
|
->willReturn(['CSP_NONCE' => $token]);
|
|
|
|
$this->assertSame($token, $this->nonceManager->getNonce());
|
|
// call it twice but `CSP_NONCE` variable is expected to be loaded only once
|
|
$this->assertSame($token, $this->nonceManager->getNonce());
|
|
}
|
|
}
|