diff options
Diffstat (limited to 'apps/encryption/tests/KeyManagerTest.php')
-rw-r--r-- | apps/encryption/tests/KeyManagerTest.php | 245 |
1 files changed, 100 insertions, 145 deletions
diff --git a/apps/encryption/tests/KeyManagerTest.php b/apps/encryption/tests/KeyManagerTest.php index d51d4dde528..3fe76fc4f59 100644 --- a/apps/encryption/tests/KeyManagerTest.php +++ b/apps/encryption/tests/KeyManagerTest.php @@ -1,45 +1,26 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Clark Tomlinson <fallen013@gmail.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Encryption\Tests; use OC\Files\FileInfo; use OC\Files\View; use OCA\Encryption\Crypto\Crypt; +use OCA\Encryption\Crypto\Encryption; +use OCA\Encryption\Exceptions\PrivateKeyMissingException; +use OCA\Encryption\Exceptions\PublicKeyMissingException; use OCA\Encryption\KeyManager; use OCA\Encryption\Session; use OCA\Encryption\Util; use OCP\Encryption\Keys\IStorage; use OCP\Files\Cache\ICache; -use OCP\Files\Storage; +use OCP\Files\Storage\IStorage as FilesIStorage; use OCP\IConfig; use OCP\IUserSession; use OCP\Lock\ILockingProvider; @@ -49,41 +30,19 @@ use Psr\Log\LoggerInterface; use Test\TestCase; class KeyManagerTest extends TestCase { - /** - * @var KeyManager - */ - private $instance; - /** - * @var string - */ - private $userId; - - /** @var string */ - private $systemKeyId; - - /** @var \OCP\Encryption\Keys\IStorage|\PHPUnit\Framework\MockObject\MockObject */ - private $keyStorageMock; - /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject */ - private $cryptMock; + protected KeyManager $instance; - /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */ - private $userMock; - - /** @var \OCA\Encryption\Session|\PHPUnit\Framework\MockObject\MockObject */ - private $sessionMock; - - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $logMock; - - /** @var \OCA\Encryption\Util|\PHPUnit\Framework\MockObject\MockObject */ - private $utilMock; - - /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $configMock; - - /** @var ILockingProvider|MockObject */ - private $lockingProviderMock; + protected string $userId; + protected string $systemKeyId; + protected IStorage&MockObject $keyStorageMock; + protected Crypt&MockObject $cryptMock; + protected IUserSession&MockObject $userMock; + protected Session&MockObject $sessionMock; + protected LoggerInterface&MockObject $logMock; + protected Util&MockObject $utilMock; + protected IConfig&MockObject $configMock; + protected ILockingProvider&MockObject $lockingProviderMock; protected function setUp(): void { parent::setUp(); @@ -119,7 +78,7 @@ class KeyManagerTest extends TestCase { ); } - public function testDeleteShareKey() { + public function testDeleteShareKey(): void { $this->keyStorageMock->expects($this->any()) ->method('deleteFileKey') ->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey')) @@ -130,7 +89,7 @@ class KeyManagerTest extends TestCase { ); } - public function testGetPrivateKey() { + public function testGetPrivateKey(): void { $this->keyStorageMock->expects($this->any()) ->method('getUserKey') ->with($this->equalTo($this->userId), $this->equalTo('privateKey')) @@ -142,7 +101,7 @@ class KeyManagerTest extends TestCase { ); } - public function testGetPublicKey() { + public function testGetPublicKey(): void { $this->keyStorageMock->expects($this->any()) ->method('getUserKey') ->with($this->equalTo($this->userId), $this->equalTo('publicKey')) @@ -154,7 +113,7 @@ class KeyManagerTest extends TestCase { ); } - public function testRecoveryKeyExists() { + public function testRecoveryKeyExists(): void { $this->keyStorageMock->expects($this->any()) ->method('getSystemUserKey') ->with($this->equalTo($this->systemKeyId . '.publicKey')) @@ -164,7 +123,7 @@ class KeyManagerTest extends TestCase { $this->assertTrue($this->instance->recoveryKeyExists()); } - public function testCheckRecoveryKeyPassword() { + public function testCheckRecoveryKeyPassword(): void { $this->keyStorageMock->expects($this->any()) ->method('getSystemUserKey') ->with($this->equalTo($this->systemKeyId . '.privateKey')) @@ -177,7 +136,7 @@ class KeyManagerTest extends TestCase { $this->assertTrue($this->instance->checkRecoveryPassword('pass')); } - public function testSetPublicKey() { + public function testSetPublicKey(): void { $this->keyStorageMock->expects($this->any()) ->method('setUserKey') ->with( @@ -192,7 +151,7 @@ class KeyManagerTest extends TestCase { ); } - public function testSetPrivateKey() { + public function testSetPrivateKey(): void { $this->keyStorageMock->expects($this->any()) ->method('setUserKey') ->with( @@ -207,10 +166,8 @@ class KeyManagerTest extends TestCase { ); } - /** - * @dataProvider dataTestUserHasKeys - */ - public function testUserHasKeys($key, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestUserHasKeys')] + public function testUserHasKeys($key, $expected): void { $this->keyStorageMock->expects($this->exactly(2)) ->method('getUserKey') ->with($this->equalTo($this->userId), $this->anything()) @@ -222,7 +179,7 @@ class KeyManagerTest extends TestCase { ); } - public function dataTestUserHasKeys() { + public static function dataTestUserHasKeys(): array { return [ ['key', true], ['', false] @@ -230,8 +187,8 @@ class KeyManagerTest extends TestCase { } - public function testUserHasKeysMissingPrivateKey() { - $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class); + public function testUserHasKeysMissingPrivateKey(): void { + $this->expectException(PrivateKeyMissingException::class); $this->keyStorageMock->expects($this->exactly(2)) ->method('getUserKey') @@ -246,8 +203,8 @@ class KeyManagerTest extends TestCase { } - public function testUserHasKeysMissingPublicKey() { - $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class); + public function testUserHasKeysMissingPublicKey(): void { + $this->expectException(PublicKeyMissingException::class); $this->keyStorageMock->expects($this->exactly(2)) ->method('getUserKey') @@ -262,12 +219,11 @@ class KeyManagerTest extends TestCase { } /** - * @dataProvider dataTestInit - * * @param bool $useMasterKey */ - public function testInit($useMasterKey) { - /** @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject $instance */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestInit')] + public function testInit($useMasterKey): void { + /** @var KeyManager&MockObject $instance */ $instance = $this->getMockBuilder(KeyManager::class) ->setConstructorArgs( [ @@ -280,17 +236,18 @@ class KeyManagerTest extends TestCase { $this->utilMock, $this->lockingProviderMock ] - )->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey']) + )->onlyMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey']) ->getMock(); $this->utilMock->expects($this->once())->method('isMasterKeyEnabled') ->willReturn($useMasterKey); - $this->sessionMock->expects($this->exactly(2))->method('setStatus') - ->withConsecutive( - [Session::INIT_EXECUTED], - [Session::INIT_SUCCESSFUL], - ); + $sessionSetStatusCalls = []; + $this->sessionMock->expects($this->exactly(2)) + ->method('setStatus') + ->willReturnCallback(function (string $status) use (&$sessionSetStatusCalls): void { + $sessionSetStatusCalls[] = $status; + }); $instance->expects($this->any())->method('getMasterKeyId')->willReturn('masterKeyId'); $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword'); @@ -311,9 +268,13 @@ class KeyManagerTest extends TestCase { ->with('key'); $this->assertTrue($instance->init($this->userId, 'pass')); + self::assertEquals([ + Session::INIT_EXECUTED, + Session::INIT_SUCCESSFUL, + ], $sessionSetStatusCalls); } - public function dataTestInit() { + public static function dataTestInit(): array { return [ [true], [false] @@ -321,7 +282,7 @@ class KeyManagerTest extends TestCase { } - public function testSetRecoveryKey() { + public function testSetRecoveryKey(): void { $this->keyStorageMock->expects($this->exactly(2)) ->method('setSystemUserKey') ->willReturn(true); @@ -337,7 +298,7 @@ class KeyManagerTest extends TestCase { ); } - public function testSetSystemPrivateKey() { + public function testSetSystemPrivateKey(): void { $this->keyStorageMock->expects($this->exactly(1)) ->method('setSystemUserKey') ->with($this->equalTo('keyId.privateKey'), $this->equalTo('key')) @@ -349,7 +310,7 @@ class KeyManagerTest extends TestCase { ); } - public function testGetSystemPrivateKey() { + public function testGetSystemPrivateKey(): void { $this->keyStorageMock->expects($this->exactly(1)) ->method('getSystemUserKey') ->with($this->equalTo('keyId.privateKey')) @@ -361,7 +322,7 @@ class KeyManagerTest extends TestCase { ); } - public function testGetEncryptedFileKey() { + public function testGetEncryptedFileKey(): void { $this->keyStorageMock->expects($this->once()) ->method('getFileKey') ->with('/', 'fileKey') @@ -370,7 +331,7 @@ class KeyManagerTest extends TestCase { $this->assertTrue($this->instance->getEncryptedFileKey('/')); } - public function dataTestGetFileKey() { + public static function dataTestGetFileKey(): array { return [ ['user1', false, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'], ['user1', false, 'privateKey', '', 'multiKeyDecryptResult'], @@ -392,14 +353,14 @@ class KeyManagerTest extends TestCase { } /** - * @dataProvider dataTestGetFileKey * * @param $uid * @param $isMasterKeyEnabled * @param $privateKey * @param $expected */ - public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $encryptedFileKey, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetFileKey')] + public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $encryptedFileKey, $expected): void { $path = '/foo.txt'; if ($isMasterKeyEnabled) { @@ -416,14 +377,10 @@ class KeyManagerTest extends TestCase { $this->keyStorageMock->expects($this->exactly(2)) ->method('getFileKey') - ->withConsecutive( - [$path, 'fileKey', 'OC_DEFAULT_MODULE'], - [$path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE'], - ) - ->willReturnOnConsecutiveCalls( - $encryptedFileKey, - 'fileKey', - ); + ->willReturnMap([ + [$path, 'fileKey', 'OC_DEFAULT_MODULE', $encryptedFileKey], + [$path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE', 'fileKey'], + ]); $this->utilMock->expects($this->any())->method('isMasterKeyEnabled') ->willReturn($isMasterKeyEnabled); @@ -470,7 +427,7 @@ class KeyManagerTest extends TestCase { ); } - public function testDeletePrivateKey() { + public function testDeletePrivateKey(): void { $this->keyStorageMock->expects($this->once()) ->method('deleteUserKey') ->with('user1', 'privateKey') @@ -481,7 +438,7 @@ class KeyManagerTest extends TestCase { [$this->userId])); } - public function testDeleteAllFileKeys() { + public function testDeleteAllFileKeys(): void { $this->keyStorageMock->expects($this->once()) ->method('deleteAllFileKeys') ->willReturn(true); @@ -492,14 +449,14 @@ class KeyManagerTest extends TestCase { /** * test add public share key and or recovery key to the list of public keys * - * @dataProvider dataTestAddSystemKeys * * @param array $accessList * @param array $publicKeys * @param string $uid * @param array $expectedKeys */ - public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestAddSystemKeys')] + public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys): void { $publicShareKeyId = 'publicShareKey'; $recoveryKeyId = 'recoveryKey'; @@ -536,7 +493,7 @@ class KeyManagerTest extends TestCase { * * @return array */ - public function dataTestAddSystemKeys() { + public static function dataTestAddSystemKeys(): array { return [ [['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']], [['public' => false], [], 'user1', ['recoveryKey']], @@ -545,13 +502,13 @@ class KeyManagerTest extends TestCase { ]; } - public function testGetMasterKeyId() { + public function testGetMasterKeyId(): void { $this->assertSame('systemKeyId', $this->instance->getMasterKeyId()); } - public function testGetPublicMasterKey() { + public function testGetPublicMasterKey(): void { $this->keyStorageMock->expects($this->once())->method('getSystemUserKey') - ->with('systemKeyId.publicKey', \OCA\Encryption\Crypto\Encryption::ID) + ->with('systemKeyId.publicKey', Encryption::ID) ->willReturn(true); $this->assertTrue( @@ -559,7 +516,7 @@ class KeyManagerTest extends TestCase { ); } - public function testGetMasterKeyPassword() { + public function testGetMasterKeyPassword(): void { $this->configMock->expects($this->once())->method('getSystemValue')->with('secret') ->willReturn('password'); @@ -569,7 +526,7 @@ class KeyManagerTest extends TestCase { } - public function testGetMasterKeyPasswordException() { + public function testGetMasterKeyPasswordException(): void { $this->expectException(\Exception::class); $this->configMock->expects($this->once())->method('getSystemValue')->with('secret') @@ -579,12 +536,11 @@ class KeyManagerTest extends TestCase { } /** - * @dataProvider dataTestValidateMasterKey - * * @param $masterKey */ - public function testValidateMasterKey($masterKey) { - /** @var \OCA\Encryption\KeyManager | \PHPUnit\Framework\MockObject\MockObject $instance */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestValidateMasterKey')] + public function testValidateMasterKey($masterKey): void { + /** @var KeyManager&MockObject $instance */ $instance = $this->getMockBuilder(KeyManager::class) ->setConstructorArgs( [ @@ -597,7 +553,7 @@ class KeyManagerTest extends TestCase { $this->utilMock, $this->lockingProviderMock ] - )->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword']) + )->onlyMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword']) ->getMock(); $this->utilMock->expects($this->once())->method('isMasterKeyEnabled') @@ -613,7 +569,7 @@ class KeyManagerTest extends TestCase { $this->cryptMock->expects($this->once())->method('createKeyPair') ->willReturn(['publicKey' => 'public', 'privateKey' => 'private']); $this->keyStorageMock->expects($this->once())->method('setSystemUserKey') - ->with('systemKeyId.publicKey', 'public', \OCA\Encryption\Crypto\Encryption::ID); + ->with('systemKeyId.publicKey', 'public', Encryption::ID); $this->cryptMock->expects($this->once())->method('encryptPrivateKey') ->with('private', 'masterKeyPassword', 'systemKeyId') ->willReturn('EncryptedKey'); @@ -631,21 +587,20 @@ class KeyManagerTest extends TestCase { $instance->validateMasterKey(); } - public function testValidateMasterKeyLocked() { - /** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */ + public function testValidateMasterKeyLocked(): void { + /** @var KeyManager&MockObject $instance */ $instance = $this->getMockBuilder(KeyManager::class) - ->setConstructorArgs( - [ - $this->keyStorageMock, - $this->cryptMock, - $this->configMock, - $this->userMock, - $this->sessionMock, - $this->logMock, - $this->utilMock, - $this->lockingProviderMock - ] - )->setMethods(['getPublicMasterKey', 'getPrivateMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword']) + ->setConstructorArgs([ + $this->keyStorageMock, + $this->cryptMock, + $this->configMock, + $this->userMock, + $this->sessionMock, + $this->logMock, + $this->utilMock, + $this->lockingProviderMock + ]) + ->onlyMethods(['getPublicMasterKey', 'getPrivateMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword']) ->getMock(); $this->utilMock->expects($this->once())->method('isMasterKeyEnabled') @@ -667,14 +622,14 @@ class KeyManagerTest extends TestCase { $instance->validateMasterKey(); } - public function dataTestValidateMasterKey() { + public static function dataTestValidateMasterKey(): array { return [ ['masterKey'], [''] ]; } - public function testGetVersionWithoutFileInfo() { + public function testGetVersionWithoutFileInfo(): void { $view = $this->getMockBuilder(View::class) ->disableOriginalConstructor()->getMock(); $view->expects($this->once()) @@ -682,11 +637,11 @@ class KeyManagerTest extends TestCase { ->with('/admin/files/myfile.txt') ->willReturn(false); - /** @var \OC\Files\View $view */ + /** @var View $view */ $this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view)); } - public function testGetVersionWithFileInfo() { + public function testGetVersionWithFileInfo(): void { $view = $this->getMockBuilder(View::class) ->disableOriginalConstructor()->getMock(); $fileInfo = $this->getMockBuilder(FileInfo::class) @@ -699,11 +654,11 @@ class KeyManagerTest extends TestCase { ->with('/admin/files/myfile.txt') ->willReturn($fileInfo); - /** @var \OC\Files\View $view */ + /** @var View $view */ $this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view)); } - public function testSetVersionWithFileInfo() { + public function testSetVersionWithFileInfo(): void { $view = $this->getMockBuilder(View::class) ->disableOriginalConstructor()->getMock(); $cache = $this->getMockBuilder(ICache::class) @@ -711,7 +666,7 @@ class KeyManagerTest extends TestCase { $cache->expects($this->once()) ->method('update') ->with(123, ['encrypted' => 5, 'encryptedVersion' => 5]); - $storage = $this->getMockBuilder(Storage::class) + $storage = $this->getMockBuilder(FilesIStorage::class) ->disableOriginalConstructor()->getMock(); $storage->expects($this->once()) ->method('getCache') @@ -729,11 +684,11 @@ class KeyManagerTest extends TestCase { ->with('/admin/files/myfile.txt') ->willReturn($fileInfo); - /** @var \OC\Files\View $view */ + /** @var View $view */ $this->instance->setVersion('/admin/files/myfile.txt', 5, $view); } - public function testSetVersionWithoutFileInfo() { + public function testSetVersionWithoutFileInfo(): void { $view = $this->getMockBuilder(View::class) ->disableOriginalConstructor()->getMock(); $view->expects($this->once()) @@ -741,11 +696,11 @@ class KeyManagerTest extends TestCase { ->with('/admin/files/myfile.txt') ->willReturn(false); - /** @var \OC\Files\View $view */ + /** @var View $view */ $this->instance->setVersion('/admin/files/myfile.txt', 5, $view); } - public function testBackupUserKeys() { + public function testBackupUserKeys(): void { $this->keyStorageMock->expects($this->once())->method('backupUserKeys') ->with('OC_DEFAULT_MODULE', 'test', 'user1'); $this->instance->backupUserKeys('test', 'user1'); |