diff options
Diffstat (limited to 'apps/encryption/tests/RecoveryTest.php')
-rw-r--r-- | apps/encryption/tests/RecoveryTest.php | 138 |
1 files changed, 55 insertions, 83 deletions
diff --git a/apps/encryption/tests/RecoveryTest.php b/apps/encryption/tests/RecoveryTest.php index b83b71737f3..0627724a856 100644 --- a/apps/encryption/tests/RecoveryTest.php +++ b/apps/encryption/tests/RecoveryTest.php @@ -1,69 +1,53 @@ <?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Clark Tomlinson <fallen013@gmail.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/> - * - */ +declare(strict_types=1); +/** + * 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\View; use OCA\Encryption\Crypto\Crypt; use OCA\Encryption\KeyManager; use OCA\Encryption\Recovery; use OCP\Encryption\IFile; -use OCP\Encryption\Keys\IStorage; use OCP\IConfig; +use OCP\IUser; use OCP\IUserSession; -use OCP\Security\ISecureRandom; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class RecoveryTest extends TestCase { private static $tempStorage = []; /** - * @var \OCP\Encryption\IFile|\PHPUnit_Framework_MockObject_MockObject + * @var IFile|\PHPUnit\Framework\MockObject\MockObject */ private $fileMock; /** - * @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject + * @var View|\PHPUnit\Framework\MockObject\MockObject */ private $viewMock; /** - * @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject + * @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ private $userSessionMock; /** - * @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject + * @var MockObject|IUser + */ + private $user; + /** + * @var KeyManager|\PHPUnit\Framework\MockObject\MockObject */ private $keyManagerMock; /** - * @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject + * @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ private $configMock; /** - * @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject + * @var Crypt|\PHPUnit\Framework\MockObject\MockObject */ private $cryptMock; /** @@ -71,7 +55,7 @@ class RecoveryTest extends TestCase { */ private $instance; - public function testEnableAdminRecoverySuccessful() { + public function testEnableAdminRecoverySuccessful(): void { $this->keyManagerMock->expects($this->exactly(2)) ->method('recoveryKeyExists') ->willReturnOnConsecutiveCalls(false, true); @@ -98,7 +82,7 @@ class RecoveryTest extends TestCase { $this->assertTrue($this->instance->enableAdminRecovery('password')); } - public function testEnableAdminRecoveryCouldNotCheckPassword() { + public function testEnableAdminRecoveryCouldNotCheckPassword(): void { $this->keyManagerMock->expects($this->exactly(2)) ->method('recoveryKeyExists') ->willReturnOnConsecutiveCalls(false, true); @@ -106,8 +90,8 @@ class RecoveryTest extends TestCase { $this->cryptMock->expects($this->once()) ->method('createKeyPair') ->willReturn([ - 'publicKey' => 'privateKey', - 'privateKey' => 'publicKey', + 'publicKey' => 'privateKey', + 'privateKey' => 'publicKey', ]); $this->keyManagerMock->expects($this->once()) @@ -125,7 +109,7 @@ class RecoveryTest extends TestCase { $this->assertFalse($this->instance->enableAdminRecovery('password')); } - public function testEnableAdminRecoveryCouldNotCreateKey() { + public function testEnableAdminRecoveryCouldNotCreateKey(): void { $this->keyManagerMock->expects($this->once()) ->method('recoveryKeyExists') ->willReturn(false); @@ -137,7 +121,7 @@ class RecoveryTest extends TestCase { $this->assertFalse($this->instance->enableAdminRecovery('password')); } - public function testChangeRecoveryKeyPasswordSuccessful() { + public function testChangeRecoveryKeyPasswordSuccessful(): void { $this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld')); @@ -155,7 +139,7 @@ class RecoveryTest extends TestCase { 'passwordOld')); } - public function testChangeRecoveryKeyPasswordCouldNotDecryptPrivateRecoveryKey() { + public function testChangeRecoveryKeyPasswordCouldNotDecryptPrivateRecoveryKey(): void { $this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld')); $this->keyManagerMock->expects($this->once()) @@ -163,13 +147,12 @@ class RecoveryTest extends TestCase { $this->cryptMock->expects($this->once()) ->method('decryptPrivateKey') - ->will($this->returnValue(false)); + ->willReturn(false); $this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld')); } - public function testDisableAdminRecovery() { - + public function testDisableAdminRecovery(): void { $this->keyManagerMock->expects($this->exactly(2)) ->method('checkRecoveryPassword') ->willReturnOnConsecutiveCalls(true, false); @@ -181,8 +164,7 @@ class RecoveryTest extends TestCase { $this->assertFalse($this->instance->disableAdminRecovery('password')); } - public function testIsRecoveryEnabledForUser() { - + public function testIsRecoveryEnabledForUser(): void { $this->configMock->expects($this->exactly(2)) ->method('getUserValue') ->willReturnOnConsecutiveCalls('1', '0'); @@ -191,13 +173,13 @@ class RecoveryTest extends TestCase { $this->assertFalse($this->instance->isRecoveryEnabledForUser('admin')); } - public function testIsRecoveryKeyEnabled() { + public function testIsRecoveryKeyEnabled(): void { $this->assertFalse($this->instance->isRecoveryKeyEnabled()); self::$tempStorage['recoveryAdminEnabled'] = '1'; $this->assertTrue($this->instance->isRecoveryKeyEnabled()); } - public function testSetRecoveryFolderForUser() { + public function testSetRecoveryFolderForUser(): void { $this->viewMock->expects($this->exactly(2)) ->method('getDirectoryContent') ->willReturn([]); @@ -205,18 +187,19 @@ class RecoveryTest extends TestCase { $this->assertTrue($this->instance->setRecoveryForUser('1')); } - public function testRecoverUserFiles() { + public function testRecoverUserFiles(): void { $this->viewMock->expects($this->once()) ->method('getDirectoryContent') ->willReturn([]); $this->cryptMock->expects($this->once()) - ->method('decryptPrivateKey'); + ->method('decryptPrivateKey') + ->willReturn('privateKey'); $this->instance->recoverUsersFiles('password', 'admin'); - $this->assertTrue(true); + $this->addToAssertionCount(1); } - public function testRecoverFile() { + public function testRecoverFile(): void { $this->keyManagerMock->expects($this->once()) ->method('getEncryptedFileKey') ->willReturn(true); @@ -226,8 +209,8 @@ class RecoveryTest extends TestCase { ->willReturn(true); $this->cryptMock->expects($this->once()) - ->method('multiKeyDecrypt') - ->willReturn(true); + ->method('multiKeyDecryptLegacy') + ->willReturn('multiKeyDecryptLegacyResult'); $this->fileMock->expects($this->once()) ->method('getAccessList') @@ -244,62 +227,53 @@ class RecoveryTest extends TestCase { $this->cryptMock->expects($this->once()) - ->method('multiKeyEncrypt'); + ->method('multiKeyEncrypt') + ->willReturn(['admin' => 'shareKey']); $this->keyManagerMock->expects($this->once()) - ->method('setAllFileKeys'); + ->method('deleteLegacyFileKey'); + $this->keyManagerMock->expects($this->once()) + ->method('setShareKey'); $this->assertNull(self::invokePrivate($this->instance, 'recoverFile', ['/', 'testkey', 'admin'])); } - protected function setUp() { + protected function setUp(): void { parent::setUp(); + $this->user = $this->createMock(IUser::class); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn('admin'); - $this->userSessionMock = $this->getMockBuilder(IUserSession::class) - ->disableOriginalConstructor() - ->setMethods([ - 'isLoggedIn', - 'getUID', - 'login', - 'logout', - 'setUser', - 'getUser' - ]) - ->getMock(); - - $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('admin')); - + $this->userSessionMock = $this->createMock(IUserSession::class); + $this->userSessionMock->expects($this->any()) + ->method('getUser') + ->willReturn($this->user); $this->userSessionMock->expects($this->any()) - ->method($this->anything()) - ->will($this->returnSelf()); + ->method('isLoggedIn') + ->willReturn(true); $this->cryptMock = $this->getMockBuilder(Crypt::class)->disableOriginalConstructor()->getMock(); - /** @var \OCP\Security\ISecureRandom $randomMock */ - $randomMock = $this->createMock(ISecureRandom::class); $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)->disableOriginalConstructor()->getMock(); $this->configMock = $this->createMock(IConfig::class); - /** @var \OCP\Encryption\Keys\IStorage $keyStorageMock */ - $keyStorageMock = $this->createMock(IStorage::class); $this->fileMock = $this->createMock(IFile::class); $this->viewMock = $this->createMock(View::class); $this->configMock->expects($this->any()) ->method('setAppValue') - ->will($this->returnCallback([$this, 'setValueTester'])); + ->willReturnCallback([$this, 'setValueTester']); $this->configMock->expects($this->any()) ->method('getAppValue') - ->will($this->returnCallback([$this, 'getValueTester'])); + ->willReturnCallback([$this, 'getValueTester']); $this->instance = new Recovery($this->userSessionMock, $this->cryptMock, - $randomMock, $this->keyManagerMock, $this->configMock, - $keyStorageMock, $this->fileMock, $this->viewMock); } @@ -332,6 +306,4 @@ class RecoveryTest extends TestCase { } return null; } - - } |