aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Security/IdentityProof
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Security/IdentityProof')
-rw-r--r--tests/lib/Security/IdentityProof/KeyTest.php28
-rw-r--r--tests/lib/Security/IdentityProof/ManagerTest.php143
-rw-r--r--tests/lib/Security/IdentityProof/SignerTest.php33
3 files changed, 95 insertions, 109 deletions
diff --git a/tests/lib/Security/IdentityProof/KeyTest.php b/tests/lib/Security/IdentityProof/KeyTest.php
index ae5f77f1609..572bfbea619 100644
--- a/tests/lib/Security/IdentityProof/KeyTest.php
+++ b/tests/lib/Security/IdentityProof/KeyTest.php
@@ -1,24 +1,10 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Security\IdentityProof;
@@ -36,11 +22,11 @@ class KeyTest extends TestCase {
$this->key = new Key('public', 'private');
}
- public function testGetPrivate() {
+ public function testGetPrivate(): void {
$this->assertSame('private', $this->key->getPrivate());
}
- public function testGetPublic() {
+ public function testGetPublic(): void {
$this->assertSame('public', $this->key->getPublic());
}
}
diff --git a/tests/lib/Security/IdentityProof/ManagerTest.php b/tests/lib/Security/IdentityProof/ManagerTest.php
index 760c4911873..921d72388a1 100644
--- a/tests/lib/Security/IdentityProof/ManagerTest.php
+++ b/tests/lib/Security/IdentityProof/ManagerTest.php
@@ -1,22 +1,10 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Security\IdentityProof;
@@ -28,26 +16,24 @@ use OC\Security\IdentityProof\Manager;
use OCP\Files\IAppData;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
+use OCP\ICache;
+use OCP\ICacheFactory;
use OCP\IConfig;
-use OCP\ILogger;
use OCP\IUser;
use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
class ManagerTest extends TestCase {
- /** @var Factory|MockObject */
- private $factory;
- /** @var IAppData|MockObject */
- private $appData;
- /** @var ICrypto|MockObject */
- private $crypto;
- /** @var Manager|MockObject */
- private $manager;
- /** @var IConfig|MockObject */
- private $config;
- /** @var ILogger|MockObject */
- private $logger;
+ private Factory&MockObject $factory;
+ private IAppData&MockObject $appData;
+ private ICrypto&MockObject $crypto;
+ private Manager&MockObject $manager;
+ private IConfig&MockObject $config;
+ private LoggerInterface&MockObject $logger;
+ private ICacheFactory&MockObject $cacheFactory;
+ private ICache&MockObject $cache;
protected function setUp(): void {
parent::setUp();
@@ -60,7 +46,13 @@ class ManagerTest extends TestCase {
->method('get')
->with('identityproof')
->willReturn($this->appData);
- $this->logger = $this->createMock(ILogger::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
+ $this->cacheFactory = $this->createMock(ICacheFactory::class);
+ $this->cache = $this->createMock(ICache::class);
+
+ $this->cacheFactory->expects($this->any())
+ ->method('createDistributed')
+ ->willReturn($this->cache);
$this->crypto = $this->createMock(ICrypto::class);
$this->manager = $this->getManager(['generateKeyPair']);
@@ -78,7 +70,8 @@ class ManagerTest extends TestCase {
$this->factory,
$this->crypto,
$this->config,
- $this->logger
+ $this->logger,
+ $this->cacheFactory,
);
} else {
return $this->getMockBuilder(Manager::class)
@@ -86,12 +79,15 @@ class ManagerTest extends TestCase {
$this->factory,
$this->crypto,
$this->config,
- $this->logger
- ])->setMethods($setMethods)->getMock();
+ $this->logger,
+ $this->cacheFactory,
+ ])
+ ->onlyMethods($setMethods)
+ ->getMock();
}
}
- public function testGetKeyWithExistingKey() {
+ public function testGetKeyWithExistingKey(): void {
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
@@ -114,42 +110,61 @@ class ManagerTest extends TestCase {
->with('EncryptedPrivateKey')
->willReturn('MyPrivateKey');
$folder
- ->expects($this->at(0))
- ->method('getFile')
- ->with('private')
- ->willReturn($privateFile);
- $folder
- ->expects($this->at(1))
+ ->expects($this->exactly(2))
->method('getFile')
- ->with('public')
- ->willReturn($publicFile);
+ ->willReturnMap([
+ ['private', $privateFile],
+ ['public', $publicFile],
+ ]);
$this->appData
->expects($this->once())
->method('getFolder')
->with('user-MyUid')
->willReturn($folder);
+ $this->cache
+ ->expects($this->exactly(2))
+ ->method('get')
+ ->willReturn(null);
$expected = new Key('MyPublicKey', 'MyPrivateKey');
$this->assertEquals($expected, $this->manager->getKey($user));
}
- public function testGetKeyWithNotExistingKey() {
+ public function testGetKeyWithExistingKeyCached(): void {
+ $user = $this->createMock(IUser::class);
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->willReturn('MyUid');
+ $this->crypto
+ ->expects($this->once())
+ ->method('decrypt')
+ ->with('EncryptedPrivateKey')
+ ->willReturn('MyPrivateKey');
+ $this->cache
+ ->expects($this->exactly(2))
+ ->method('get')
+ ->willReturnMap([
+ ['user-MyUid-public', 'MyPublicKey'],
+ ['user-MyUid-private', 'EncryptedPrivateKey'],
+ ]);
+
+ $expected = new Key('MyPublicKey', 'MyPrivateKey');
+ $this->assertEquals($expected, $this->manager->getKey($user));
+ }
+
+ public function testGetKeyWithNotExistingKey(): void {
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
->method('getUID')
->willReturn('MyUid');
- $this->appData
- ->expects($this->at(0))
- ->method('getFolder')
- ->with('user-MyUid')
- ->willThrowException(new \Exception());
$this->manager
->expects($this->once())
->method('generateKeyPair')
->willReturn(['MyNewPublicKey', 'MyNewPrivateKey']);
$this->appData
- ->expects($this->at(1))
+ ->expects($this->once())
->method('newFolder')
->with('user-MyUid');
$folder = $this->createMock(ISimpleFolder::class);
@@ -169,31 +184,31 @@ class ManagerTest extends TestCase {
->method('putContent')
->with('MyNewPublicKey');
$folder
- ->expects($this->at(0))
+ ->expects($this->exactly(2))
->method('newFile')
- ->with('private')
- ->willReturn($privateFile);
- $folder
- ->expects($this->at(1))
- ->method('newFile')
- ->with('public')
- ->willReturn($publicFile);
+ ->willReturnMap([
+ ['private', null, $privateFile],
+ ['public', null, $publicFile],
+ ]);
$this->appData
- ->expects($this->at(2))
+ ->expects($this->exactly(2))
->method('getFolder')
->with('user-MyUid')
- ->willReturn($folder);
+ ->willReturnOnConsecutiveCalls(
+ $this->throwException(new \Exception()),
+ $folder
+ );
$expected = new Key('MyNewPublicKey', 'MyNewPrivateKey');
$this->assertEquals($expected, $this->manager->getKey($user));
}
- public function testGenerateKeyPair() {
+ public function testGenerateKeyPair(): void {
$manager = $this->getManager();
$data = 'MyTestData';
- list($resultPublicKey, $resultPrivateKey) = self::invokePrivate($manager, 'generateKeyPair');
+ [$resultPublicKey, $resultPrivateKey] = self::invokePrivate($manager, 'generateKeyPair');
openssl_sign($data, $signature, $resultPrivateKey);
$details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey));
@@ -201,7 +216,7 @@ class ManagerTest extends TestCase {
$this->assertSame(2048, $details['bits']);
}
- public function testGetSystemKey() {
+ public function testGetSystemKey(): void {
$manager = $this->getManager(['retrieveKey']);
/** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */
@@ -218,7 +233,7 @@ class ManagerTest extends TestCase {
- public function testGetSystemKeyFailure() {
+ public function testGetSystemKeyFailure(): void {
$this->expectException(\RuntimeException::class);
$manager = $this->getManager(['retrieveKey']);
diff --git a/tests/lib/Security/IdentityProof/SignerTest.php b/tests/lib/Security/IdentityProof/SignerTest.php
index 3ce211d51e7..6dc73c072e4 100644
--- a/tests/lib/Security/IdentityProof/SignerTest.php
+++ b/tests/lib/Security/IdentityProof/SignerTest.php
@@ -1,24 +1,10 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Security\IdentityProof;
@@ -32,7 +18,6 @@ use OCP\IUserManager;
use Test\TestCase;
class SignerTest extends TestCase {
-
/** @var string */
private $private = '-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDImc6QvHBjBIoo
@@ -105,7 +90,7 @@ gQIDAQAB
);
}
- public function testSign() {
+ public function testSign(): void {
$user = $this->createMock(IUser::class);
$user->method('getCloudId')
->willReturn('foo@example.com');
@@ -138,7 +123,7 @@ gQIDAQAB
$this->assertEquals($expects, $result);
}
- public function testVerifyValid() {
+ public function testVerifyValid(): void {
$data = [
'message' => [
'data' => [
@@ -166,7 +151,7 @@ gQIDAQAB
$this->assertTrue($this->signer->verify($data));
}
- public function testVerifyInvalid() {
+ public function testVerifyInvalid(): void {
$data = [
'message' => [
'data' => [
@@ -194,7 +179,7 @@ gQIDAQAB
$this->assertFalse($this->signer->verify($data));
}
- public function testVerifyInvalidData() {
+ public function testVerifyInvalidData(): void {
$data = [
];