diff options
Diffstat (limited to 'tests/lib/Security/IdentityProof/ManagerTest.php')
-rw-r--r-- | tests/lib/Security/IdentityProof/ManagerTest.php | 118 |
1 files changed, 65 insertions, 53 deletions
diff --git a/tests/lib/Security/IdentityProof/ManagerTest.php b/tests/lib/Security/IdentityProof/ManagerTest.php index 8733d5de8be..921d72388a1 100644 --- a/tests/lib/Security/IdentityProof/ManagerTest.php +++ b/tests/lib/Security/IdentityProof/ManagerTest.php @@ -3,23 +3,8 @@ 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; @@ -31,6 +16,8 @@ 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\IUser; use OCP\Security\ICrypto; @@ -39,18 +26,14 @@ 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 LoggerInterface|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(); @@ -64,6 +47,12 @@ class ManagerTest extends TestCase { ->with('identityproof') ->willReturn($this->appData); $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']); @@ -81,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) @@ -89,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()) @@ -119,25 +112,48 @@ class ManagerTest extends TestCase { $folder ->expects($this->exactly(2)) ->method('getFile') - ->withConsecutive( - ['private'], - ['public'] - ) - ->willReturnOnConsecutiveCalls( - $privateFile, - $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 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() { + public function testGetKeyWithNotExistingKey(): void { $user = $this->createMock(IUser::class); $user ->expects($this->once()) @@ -170,14 +186,10 @@ class ManagerTest extends TestCase { $folder ->expects($this->exactly(2)) ->method('newFile') - ->withConsecutive( - ['private'], - ['public'] - ) - ->willReturnOnConsecutiveCalls( - $privateFile, - $publicFile - ); + ->willReturnMap([ + ['private', null, $privateFile], + ['public', null, $publicFile], + ]); $this->appData ->expects($this->exactly(2)) ->method('getFolder') @@ -192,7 +204,7 @@ class ManagerTest extends TestCase { $this->assertEquals($expected, $this->manager->getKey($user)); } - public function testGenerateKeyPair() { + public function testGenerateKeyPair(): void { $manager = $this->getManager(); $data = 'MyTestData'; @@ -204,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 */ @@ -221,7 +233,7 @@ class ManagerTest extends TestCase { - public function testGetSystemKeyFailure() { + public function testGetSystemKeyFailure(): void { $this->expectException(\RuntimeException::class); $manager = $this->getManager(['retrieveKey']); |