2015-09-08 09:35:13 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-09-08 09:35:13 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-09-08 09:35:13 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Traits;
|
|
|
|
|
|
2016-03-30 17:20:37 -04:00
|
|
|
use OC\Encryption\EncryptionWrapper;
|
2022-02-23 12:29:08 -05:00
|
|
|
use OC\Files\SetupManager;
|
2016-03-30 17:20:37 -04:00
|
|
|
use OC\Memcache\ArrayCache;
|
2015-09-08 09:35:13 -04:00
|
|
|
use OCA\Encryption\AppInfo\Application;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCA\Encryption\Crypto\Encryption;
|
2015-09-08 09:35:13 -04:00
|
|
|
use OCA\Encryption\KeyManager;
|
|
|
|
|
use OCA\Encryption\Users\Setup;
|
2025-07-10 04:17:42 -04:00
|
|
|
use OCP\App\IAppManager;
|
2020-11-22 16:09:09 -05:00
|
|
|
use OCP\Encryption\IManager;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\IConfig;
|
2022-02-23 12:29:08 -05:00
|
|
|
use OCP\IUserManager;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\IUserSession;
|
|
|
|
|
use OCP\Server;
|
2022-03-21 06:17:14 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-09-08 09:35:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enables encryption
|
|
|
|
|
*/
|
|
|
|
|
trait EncryptionTrait {
|
|
|
|
|
// from MountProviderTrait
|
|
|
|
|
abstract protected function registerStorageWrapper($name, $wrapper);
|
|
|
|
|
|
|
|
|
|
// from phpunit
|
2020-12-04 02:39:26 -05:00
|
|
|
abstract protected static function markTestSkipped(string $message = ''): void;
|
|
|
|
|
abstract protected static function assertTrue($condition, string $message = ''): void;
|
2015-09-08 09:35:13 -04:00
|
|
|
|
|
|
|
|
private $encryptionWasEnabled;
|
|
|
|
|
|
|
|
|
|
private $originalEncryptionModule;
|
|
|
|
|
|
2022-02-23 12:29:08 -05:00
|
|
|
/** @var IUserManager */
|
|
|
|
|
private $userManager;
|
|
|
|
|
/** @var SetupManager */
|
|
|
|
|
private $setupManager;
|
|
|
|
|
|
2015-09-08 09:35:13 -04:00
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @var IConfig
|
2015-09-08 09:35:13 -04:00
|
|
|
*/
|
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @var Application
|
2015-09-08 09:35:13 -04:00
|
|
|
*/
|
|
|
|
|
private $encryptionApp;
|
|
|
|
|
|
|
|
|
|
protected function loginWithEncryption($user = '') {
|
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
|
// needed for fully logout
|
2025-06-12 12:31:58 -04:00
|
|
|
Server::get(IUserSession::class)->setUser(null);
|
2015-09-08 09:35:13 -04:00
|
|
|
|
2022-02-23 12:29:08 -05:00
|
|
|
$this->setupManager->tearDown();
|
|
|
|
|
|
2015-09-08 09:35:13 -04:00
|
|
|
\OC_User::setUserId($user);
|
|
|
|
|
$this->postLogin();
|
|
|
|
|
\OC_Util::setupFS($user);
|
2022-02-23 12:29:08 -05:00
|
|
|
if ($this->userManager->userExists($user)) {
|
2015-09-08 09:35:13 -04:00
|
|
|
\OC::$server->getUserFolder($user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function setupForUser($name, $password) {
|
2022-02-23 12:29:08 -05:00
|
|
|
$this->setupManager->tearDown();
|
|
|
|
|
$this->setupManager->setupForUser($this->userManager->get($name));
|
|
|
|
|
|
2015-09-08 09:35:13 -04:00
|
|
|
$container = $this->encryptionApp->getContainer();
|
|
|
|
|
/** @var KeyManager $keyManager */
|
2020-11-11 14:46:22 -05:00
|
|
|
$keyManager = $container->query(KeyManager::class);
|
2015-09-08 09:35:13 -04:00
|
|
|
/** @var Setup $userSetup */
|
2020-11-11 14:46:22 -05:00
|
|
|
$userSetup = $container->query(Setup::class);
|
2016-03-02 07:58:06 -05:00
|
|
|
$userSetup->setupUser($name, $password);
|
2020-11-22 16:09:09 -05:00
|
|
|
$encryptionManager = $container->query(IManager::class);
|
|
|
|
|
$this->encryptionApp->setUp($encryptionManager);
|
2015-09-08 09:35:13 -04:00
|
|
|
$keyManager->init($name, $password);
|
2025-09-04 04:53:10 -04:00
|
|
|
$this->invokePrivate($keyManager, 'keyUid', [$name]);
|
2015-09-08 09:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function postLogin() {
|
2016-03-30 17:20:37 -04:00
|
|
|
$encryptionWrapper = new EncryptionWrapper(
|
|
|
|
|
new ArrayCache(),
|
2025-06-12 12:31:58 -04:00
|
|
|
Server::get(\OCP\Encryption\IManager::class),
|
|
|
|
|
Server::get(LoggerInterface::class)
|
2015-09-08 09:35:13 -04:00
|
|
|
);
|
2016-03-30 17:20:37 -04:00
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']);
|
2015-09-08 09:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function setUpEncryptionTrait() {
|
2025-06-12 12:31:58 -04:00
|
|
|
$isReady = Server::get(\OCP\Encryption\IManager::class)->isReady();
|
2015-09-08 09:35:13 -04:00
|
|
|
if (!$isReady) {
|
|
|
|
|
$this->markTestSkipped('Encryption not ready');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->userManager = Server::get(IUserManager::class);
|
|
|
|
|
$this->setupManager = Server::get(SetupManager::class);
|
2022-02-23 12:29:08 -05:00
|
|
|
|
2025-07-10 04:17:42 -04:00
|
|
|
Server::get(IAppManager::class)->loadApp('encryption');
|
2015-09-08 09:35:13 -04:00
|
|
|
|
|
|
|
|
$this->encryptionApp = new Application([], $isReady);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->config = Server::get(IConfig::class);
|
2015-09-08 09:35:13 -04:00
|
|
|
$this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
|
|
|
|
|
$this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->config->setAppValue('core', 'default_encryption_module', Encryption::ID);
|
2015-09-08 09:35:13 -04:00
|
|
|
$this->config->setAppValue('core', 'encryption_enabled', 'yes');
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled());
|
2015-09-08 09:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDownEncryptionTrait() {
|
|
|
|
|
if ($this->config) {
|
|
|
|
|
$this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled);
|
|
|
|
|
$this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule);
|
2017-05-31 09:03:37 -04:00
|
|
|
$this->config->deleteAppValue('encryption', 'useMasterKey');
|
2015-09-08 09:35:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|