2015-04-03 11:37:54 -04:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2025-05-28 04:59:55 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2015-04-03 11:37:54 -04:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-04-03 11:37:54 -04:00
|
|
|
*/
|
2025-09-04 04:53:10 -04:00
|
|
|
|
2015-04-03 11:37:54 -04:00
|
|
|
namespace OCA\Encryption\Tests;
|
|
|
|
|
|
2016-09-02 04:29:05 -04:00
|
|
|
use OC\Files\View;
|
2017-10-26 07:46:16 -04:00
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
|
|
|
|
use OCA\Encryption\KeyManager;
|
2015-04-03 11:37:54 -04:00
|
|
|
use OCA\Encryption\Recovery;
|
2026-05-06 20:03:17 -04:00
|
|
|
use OCP\Config\IUserConfig;
|
2016-09-02 04:29:05 -04:00
|
|
|
use OCP\Encryption\IFile;
|
2026-05-06 20:03:17 -04:00
|
|
|
use OCP\IAppConfig;
|
2019-08-26 09:22:00 -04:00
|
|
|
use OCP\IUser;
|
2017-10-26 07:46:16 -04:00
|
|
|
use OCP\IUserSession;
|
2019-08-26 09:22:00 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2015-04-03 11:37:54 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class RecoveryTest extends TestCase {
|
|
|
|
|
private static $tempStorage = [];
|
2025-09-04 04:53:10 -04:00
|
|
|
|
|
|
|
|
private IFile&MockObject $fileMock;
|
|
|
|
|
private View&MockObject $viewMock;
|
|
|
|
|
private IUserSession&MockObject $userSessionMock;
|
|
|
|
|
private IUser&MockObject $user;
|
|
|
|
|
private KeyManager&MockObject $keyManagerMock;
|
2026-05-06 20:03:17 -04:00
|
|
|
private IAppConfig&MockObject $appConfigMock;
|
|
|
|
|
private IUserConfig&MockObject $userConfigMock;
|
2025-09-04 04:53:10 -04:00
|
|
|
private Crypt&MockObject $cryptMock;
|
|
|
|
|
|
|
|
|
|
private Recovery $instance;
|
2015-04-03 11:37:54 -04:00
|
|
|
|
2015-12-08 03:28:49 -05:00
|
|
|
public function testEnableAdminRecoverySuccessful(): void {
|
2015-04-03 11:37:54 -04:00
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
|
->method('recoveryKeyExists')
|
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
|
->method('createKeyPair')
|
2015-12-08 03:28:49 -05:00
|
|
|
->willReturn([
|
|
|
|
|
'publicKey' => 'privateKey',
|
|
|
|
|
'privateKey' => 'publicKey',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('setRecoveryKey')
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
|
->method('checkRecoveryPassword')
|
|
|
|
|
->willReturnOnConsecutiveCalls(true, true);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->enableAdminRecovery('password'));
|
|
|
|
|
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->assertTrue(self::$tempStorage['recoveryAdminEnabled']);
|
2015-12-08 03:28:49 -05:00
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->enableAdminRecovery('password'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEnableAdminRecoveryCouldNotCheckPassword(): void {
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
|
->method('recoveryKeyExists')
|
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
|
->method('createKeyPair')
|
|
|
|
|
->willReturn([
|
2020-04-09 03:22:29 -04:00
|
|
|
'publicKey' => 'privateKey',
|
|
|
|
|
'privateKey' => 'publicKey',
|
2015-12-08 03:28:49 -05:00
|
|
|
]);
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('setRecoveryKey')
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
|
->method('checkRecoveryPassword')
|
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->enableAdminRecovery('password'));
|
|
|
|
|
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->assertTrue(self::$tempStorage['recoveryAdminEnabled']);
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->enableAdminRecovery('password'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-08 03:28:49 -05:00
|
|
|
public function testEnableAdminRecoveryCouldNotCreateKey(): void {
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('recoveryKeyExists')
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
|
->method('createKeyPair')
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->enableAdminRecovery('password'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChangeRecoveryKeyPasswordSuccessful(): void {
|
2025-09-04 04:53:10 -04:00
|
|
|
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
2025-09-04 04:53:10 -04:00
|
|
|
->method('getSystemPrivateKey')
|
|
|
|
|
->willReturn('privateKey');
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
2025-09-04 04:53:10 -04:00
|
|
|
->method('decryptPrivateKey')
|
|
|
|
|
->with('privateKey', 'passwordOld')
|
|
|
|
|
->willReturn('decryptedPrivateKey');
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
2015-08-07 08:04:17 -04:00
|
|
|
->method('encryptPrivateKey')
|
2025-09-04 04:53:10 -04:00
|
|
|
->with('decryptedPrivateKey', 'password')
|
|
|
|
|
->willReturn('privateKey');
|
2015-04-03 11:37:54 -04:00
|
|
|
|
2025-09-04 04:53:10 -04:00
|
|
|
$this->assertTrue($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
|
2015-04-03 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
2015-12-08 03:28:49 -05:00
|
|
|
public function testChangeRecoveryKeyPasswordCouldNotDecryptPrivateRecoveryKey(): void {
|
|
|
|
|
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('getSystemPrivateKey');
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
|
->method('decryptPrivateKey')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(false);
|
2015-12-08 03:28:49 -05:00
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-03 11:37:54 -04:00
|
|
|
public function testDisableAdminRecovery(): void {
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
|
->method('checkRecoveryPassword')
|
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
|
|
|
|
|
$this->assertTrue($this->instance->disableAdminRecovery('password'));
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->assertFalse(self::$tempStorage['recoveryAdminEnabled']);
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->disableAdminRecovery('password'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsRecoveryEnabledForUser(): void {
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->userConfigMock->expects($this->exactly(2))
|
|
|
|
|
->method('getValueBool')
|
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->isRecoveryEnabledForUser());
|
|
|
|
|
$this->assertFalse($this->instance->isRecoveryEnabledForUser('admin'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsRecoveryKeyEnabled(): void {
|
|
|
|
|
$this->assertFalse($this->instance->isRecoveryKeyEnabled());
|
2026-05-06 20:03:17 -04:00
|
|
|
self::$tempStorage['recoveryAdminEnabled'] = true;
|
2015-04-03 11:37:54 -04:00
|
|
|
$this->assertTrue($this->instance->isRecoveryKeyEnabled());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetRecoveryFolderForUser(): void {
|
|
|
|
|
$this->viewMock->expects($this->exactly(2))
|
|
|
|
|
->method('getDirectoryContent')
|
|
|
|
|
->willReturn([]);
|
2026-05-28 08:24:06 -04:00
|
|
|
$this->assertTrue($this->instance->setRecoveryForUser(false));
|
|
|
|
|
$this->assertTrue($this->instance->setRecoveryForUser(true));
|
2015-04-03 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRecoverUserFiles(): void {
|
|
|
|
|
$this->viewMock->expects($this->once())
|
|
|
|
|
->method('getDirectoryContent')
|
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
2023-03-17 05:32:51 -04:00
|
|
|
->method('decryptPrivateKey')
|
|
|
|
|
->willReturn('privateKey');
|
2016-05-12 03:42:19 -04:00
|
|
|
$this->instance->recoverUsersFiles('password', 'admin');
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2015-04-03 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-06 11:49:18 -04:00
|
|
|
public function testRecoverFile(): void {
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('getEncryptedFileKey')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('getShareKey')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
2023-03-17 05:32:51 -04:00
|
|
|
->method('multiKeyDecryptLegacy')
|
|
|
|
|
->willReturn('multiKeyDecryptLegacyResult');
|
2015-04-06 11:49:18 -04:00
|
|
|
|
|
|
|
|
$this->fileMock->expects($this->once())
|
|
|
|
|
->method('getAccessList')
|
|
|
|
|
->willReturn(['users' => ['admin']]);
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('getPublicKey')
|
|
|
|
|
->willReturn('publicKey');
|
|
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('addSystemKeys')
|
2015-04-29 11:18:41 -04:00
|
|
|
->with($this->anything(), $this->anything(), $this->equalTo('admin'))
|
2015-04-06 11:49:18 -04:00
|
|
|
->willReturn(['admin' => 'publicKey']);
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
2023-03-17 05:32:51 -04:00
|
|
|
->method('multiKeyEncrypt')
|
|
|
|
|
->willReturn(['admin' => 'shareKey']);
|
2015-04-06 11:49:18 -04:00
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
2023-03-17 05:32:51 -04:00
|
|
|
->method('deleteLegacyFileKey');
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
|
->method('setShareKey');
|
2015-04-06 11:49:18 -04:00
|
|
|
|
2015-06-03 06:03:02 -04:00
|
|
|
$this->assertNull(self::invokePrivate($this->instance,
|
2015-04-06 11:49:18 -04:00
|
|
|
'recoverFile',
|
2015-04-29 11:18:41 -04:00
|
|
|
['/', 'testkey', 'admin']));
|
2015-04-06 11:49:18 -04:00
|
|
|
}
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-04-03 11:37:54 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2019-08-26 09:22:00 -04:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn('admin');
|
2015-04-03 11:37:54 -04:00
|
|
|
|
2019-08-26 09:22:00 -04:00
|
|
|
$this->userSessionMock = $this->createMock(IUserSession::class);
|
|
|
|
|
$this->userSessionMock->expects($this->any())
|
|
|
|
|
->method('getUser')
|
|
|
|
|
->willReturn($this->user);
|
2015-04-03 11:37:54 -04:00
|
|
|
$this->userSessionMock->expects($this->any())
|
2019-08-26 09:22:00 -04:00
|
|
|
->method('isLoggedIn')
|
|
|
|
|
->willReturn(true);
|
2015-04-03 11:37:54 -04:00
|
|
|
|
2017-10-26 07:46:16 -04:00
|
|
|
$this->cryptMock = $this->getMockBuilder(Crypt::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
$this->keyManagerMock = $this->getMockBuilder(KeyManager::class)->disableOriginalConstructor()->getMock();
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->appConfigMock = $this->createMock(IAppConfig::class);
|
|
|
|
|
$this->userConfigMock = $this->createMock(IUserConfig::class);
|
2016-09-02 04:29:05 -04:00
|
|
|
$this->fileMock = $this->createMock(IFile::class);
|
|
|
|
|
$this->viewMock = $this->createMock(View::class);
|
2015-04-03 11:37:54 -04:00
|
|
|
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->appConfigMock->expects($this->any())
|
|
|
|
|
->method('setValueBool')
|
|
|
|
|
->willReturnCallback(function (string $app, string $key, bool $value): bool {
|
|
|
|
|
self::$tempStorage[$key] = $value;
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2015-04-03 11:37:54 -04:00
|
|
|
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->appConfigMock->expects($this->any())
|
|
|
|
|
->method('getValueBool')
|
2026-05-10 08:38:59 -04:00
|
|
|
->willReturnCallback(function (string $app, string $key, bool $default = false): bool {
|
|
|
|
|
return self::$tempStorage[$key] ?? $default;
|
|
|
|
|
});
|
2015-04-03 11:37:54 -04:00
|
|
|
|
|
|
|
|
$this->instance = new Recovery($this->userSessionMock,
|
|
|
|
|
$this->cryptMock,
|
|
|
|
|
$this->keyManagerMock,
|
2026-05-06 20:03:17 -04:00
|
|
|
$this->appConfigMock,
|
|
|
|
|
$this->userConfigMock,
|
2015-04-06 11:49:18 -04:00
|
|
|
$this->fileMock,
|
2015-04-03 11:37:54 -04:00
|
|
|
$this->viewMock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|