Merge pull request #15681 from owncloud/issue/15667-occ-encryption

OCC commands to manage encryption
This commit is contained in:
Morris Jobke 2015-04-27 13:28:59 +02:00
commit 6a7b7caa81
17 changed files with 674 additions and 85 deletions

View File

@ -2,5 +2,7 @@
$manager = \OC::$server->getEncryptionManager();
$module = new \OCA\Encryption_Dummy\DummyModule();
$manager->registerEncryptionModule('OC_DUMMY_MODULE', 'Dummy Encryption Module', $module);
$manager->registerEncryptionModule('OC_DUMMY_MODULE', 'Dummy Encryption Module', function() use ($module) {
return $module;
});

View File

@ -0,0 +1,56 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace OC\Core\Command\Encryption;
use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Disable extends Command {
/** @var IConfig */
protected $config;
/**
* @param IConfig $config
*/
public function __construct(IConfig $config) {
parent::__construct();
$this->config = $config;
}
protected function configure() {
$this
->setName('encryption:disable')
->setDescription('Disable encryption')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
if ($this->config->getAppValue('core', 'encryption_enabled', 'no') !== 'yes') {
$output->writeln('Encryption is already disabled');
} else {
$this->config->setAppValue('core', 'encryption_enabled', 'no');
$output->writeln('Encryption disabled');
}
}
}

View File

@ -0,0 +1,58 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace OC\Core\Command\Encryption;
use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Enable extends Command {
/** @var IConfig */
protected $config;
/**
* @param IConfig $config
*/
public function __construct(IConfig $config) {
parent::__construct();
$this->config = $config;
}
protected function configure() {
$this
->setName('encryption:enable')
->setDescription('Enable encryption')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
if ($this->config->getAppValue('core', 'encryption_enabled', 'no') === 'yes') {
$output->writeln('Encryption is already enabled');
} else {
$this->config->setAppValue('core', 'encryption_enabled', 'yes');
$output->writeln('Encryption enabled');
}
$output->writeln('Default module: ' . $this->config->getAppValue('core', 'default_encryption_module', 'OC_DEFAULT_MODULE'));
}
}

View File

@ -0,0 +1,80 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace OC\Core\Command\Encryption;
use OC\Core\Command\Base;
use OCP\Encryption\IManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ListModules extends Base {
/** @var IManager */
protected $encryptionManager;
/**
* @param IManager $encryptionManager
*/
public function __construct(IManager $encryptionManager) {
parent::__construct();
$this->encryptionManager = $encryptionManager;
}
protected function configure() {
parent::configure();
$this
->setName('encryption:list-modules')
->setDescription('List all available encryption modules')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$encryptionModules = $this->encryptionManager->getEncryptionModules();
$defaultEncryptionModuleId = $this->encryptionManager->getDefaultEncryptionModuleId();
$encModules = array();
foreach ($encryptionModules as $module) {
$encModules[$module['id']]['displayName'] = $module['displayName'];
$encModules[$module['id']]['default'] = $module['id'] === $defaultEncryptionModuleId;
}
$this->writeModuleList($input, $output, $encModules);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param array $items
*/
protected function writeModuleList(InputInterface $input, OutputInterface $output, $items) {
if ($input->getOption('output') === 'plain') {
array_walk($items, function(&$item) {
if (!$item['default']) {
$item = $item['displayName'];
} else {
$item = $item['displayName'] . ' [default*]';
}
});
}
$this->writeArrayInOutputFormat($input, $output, $items);
}
}

View File

@ -0,0 +1,68 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace OC\Core\Command\Encryption;
use OCP\Encryption\IManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SetDefaultModule extends Command {
/** @var IManager */
protected $encryptionManager;
/**
* @param IManager $encryptionManager
*/
public function __construct(IManager $encryptionManager) {
parent::__construct();
$this->encryptionManager = $encryptionManager;
}
protected function configure() {
parent::configure();
$this
->setName('encryption:set-default-module')
->setDescription('Set the encryption default module')
->addArgument(
'module',
InputArgument::REQUIRED,
'ID of the encryption module that should be used'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$moduleId = $input->getArgument('module');
if ($moduleId === $this->encryptionManager->getDefaultEncryptionModuleId()) {
$output->writeln('"' . $moduleId . '"" is already the default module');
} else if ($this->encryptionManager->setDefaultEncryptionModule($moduleId)) {
$output->writeln('<info>Set default module to "' . $moduleId . '"</info>');
} else {
$output->writeln('<error>The specified module "' . $moduleId . '" does not exist</error>');
}
}
}

View File

@ -50,6 +50,10 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) {
$application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Encryption\Enable(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Encryption\ListModules(\OC::$server->getEncryptionManager()));
$application->add(new OC\Core\Command\Encryption\SetDefaultModule(\OC::$server->getEncryptionManager()));
} else {
$application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getConfig()));
}

View File

@ -101,17 +101,17 @@ class Manager implements IManager {
throw new Exceptions\ModuleAlreadyExistsException($id, $displayName);
}
$defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
if (empty($defaultEncryptionModuleId)) {
$this->setDefaultEncryptionModule($id);
}
$this->encryptionModules[$id] = [
'id' => $id,
'displayName' => $displayName,
'callback' => $callback,
];
$defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
if (empty($defaultEncryptionModuleId)) {
$this->setDefaultEncryptionModule($id);
}
}
/**
@ -158,7 +158,7 @@ class Manager implements IManager {
* @return \OCP\Encryption\IEncryptionModule
* @throws Exceptions\ModuleDoesNotExistsException
*/
public function getDefaultEncryptionModule() {
protected function getDefaultEncryptionModule() {
$defaultModuleId = $this->getDefaultEncryptionModuleId();
if (!empty($defaultModuleId)) {
if (isset($this->encryptionModules[$defaultModuleId])) {
@ -182,12 +182,13 @@ class Manager implements IManager {
*/
public function setDefaultEncryptionModule($moduleId) {
try {
$this->config->setAppValue('core', 'default_encryption_module', $moduleId);
return true;
$this->getEncryptionModule($moduleId);
} catch (\Exception $e) {
return false;
}
$this->config->setAppValue('core', 'default_encryption_module', $moduleId);
return true;
}
/**
@ -195,12 +196,8 @@ class Manager implements IManager {
*
* @return string
*/
protected function getDefaultEncryptionModuleId() {
try {
return $this->config->getAppValue('core', 'default_encryption_module');
} catch (\Exception $e) {
return '';
}
public function getDefaultEncryptionModuleId() {
return $this->config->getAppValue('core', 'default_encryption_module');
}
public static function setupStorage() {

View File

@ -137,7 +137,7 @@ class Update {
$allFiles = array($path);
}
$encryptionModule = $this->encryptionManager->getDefaultEncryptionModule();
$encryptionModule = $this->encryptionManager->getEncryptionModule();
foreach ($allFiles as $file) {
$usersSharing = $this->file->getAccessList($file);

View File

@ -311,12 +311,10 @@ class Encryption extends Wrapper {
|| $mode === 'wb'
|| $mode === 'wb+'
) {
if (!empty($encryptionModuleId)) {
if ($encryptionEnabled) {
// if $encryptionModuleId is empty, the default module will be used
$encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
$shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath);
} elseif ($encryptionEnabled) {
$encryptionModule = $this->encryptionManager->getDefaultEncryptionModule();
$shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath);
}
} else {
// only get encryption module if we found one in the header

View File

@ -37,7 +37,7 @@ interface IManager {
* @return bool true if enabled, false if not
* @since 8.1.0
*/
function isEnabled();
public function isEnabled();
/**
* Registers an callback function which must return an encryption module instance
@ -48,7 +48,7 @@ interface IManager {
* @throws ModuleAlreadyExistsException
* @since 8.1.0
*/
function registerEncryptionModule($id, $displayName, callable $callback);
public function registerEncryptionModule($id, $displayName, callable $callback);
/**
* Unregisters an encryption module
@ -56,7 +56,7 @@ interface IManager {
* @param string $moduleId
* @since 8.1.0
*/
function unregisterEncryptionModule($moduleId);
public function unregisterEncryptionModule($moduleId);
/**
* get a list of all encryption modules
@ -64,27 +64,26 @@ interface IManager {
* @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]]
* @since 8.1.0
*/
function getEncryptionModules();
public function getEncryptionModules();
/**
* get a specific encryption module
*
* @param string $moduleId
* @param string $moduleId Empty to get the default module
* @return IEncryptionModule
* @throws ModuleDoesNotExistsException
* @since 8.1.0
*/
function getEncryptionModule($moduleId);
public function getEncryptionModule($moduleId = '');
/**
* get default encryption module
* get default encryption module Id
*
* @return \OCP\Encryption\IEncryptionModule
* @throws ModuleDoesNotExistsException
* @return string
* @since 8.1.0
*/
public function getDefaultEncryptionModule();
public function getDefaultEncryptionModuleId();
/**
* set default encryption module Id

View File

@ -86,14 +86,10 @@ $backends = \OC::$server->getUserManager()->getBackends();
$externalBackends = (count($backends) > 1) ? true : false;
$template->assign('encryptionReady', \OC::$server->getEncryptionManager()->isReady());
$template->assign('externalBackendsEnabled', $externalBackends);
$encryptionModules = \OC::$server->getEncryptionManager()->getEncryptionModules();
try {
$defaultEncryptionModule = \OC::$server->getEncryptionManager()->getDefaultEncryptionModule();
$defaultEncryptionModuleId = $defaultEncryptionModule->getId();
} catch (Exception $e) {
$defaultEncryptionModuleId = null;
}
$encryptionModules = \OC::$server->getEncryptionManager()->getEncryptionModules();
$defaultEncryptionModuleId = \OC::$server->getEncryptionManager()->getDefaultEncryptionModuleId();
$encModulues = array();
foreach ($encryptionModules as $module) {
$encModulues[$module['id']]['displayName'] = $module['displayName'];

View File

@ -0,0 +1,85 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace Tests\Core\Command\Encryption;
use OC\Core\Command\Encryption\Disable;
use Test\TestCase;
class DisableTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleInput;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleOutput;
/** @var \Symfony\Component\Console\Command\Command */
protected $command;
protected function setUp() {
parent::setUp();
$config = $this->config = $this->getMockBuilder('OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
/** @var \OCP\IConfig $config */
$this->command = new Disable($config);
}
public function dataDisable() {
return [
['yes', true, 'Encryption disabled'],
['no', false, 'Encryption is already disabled'],
];
}
/**
* @dataProvider dataDisable
*
* @param string $oldStatus
* @param bool $isUpdating
* @param string $expectedString
*/
public function testDisable($oldStatus, $isUpdating, $expectedString) {
$this->config->expects($this->once())
->method('getAppValue')
->with('core', 'encryption_enabled', $this->anything())
->willReturn($oldStatus);
$this->consoleOutput->expects($this->once())
->method('writeln')
->with($expectedString);
if ($isUpdating) {
$this->config->expects($this->once())
->method('setAppValue')
->with('core', 'encryption_enabled', 'no');
}
\Test_Helper::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace Tests\Core\Command\Encryption;
use OC\Core\Command\Encryption\Enable;
use Test\TestCase;
class EnableTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleInput;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleOutput;
/** @var \Symfony\Component\Console\Command\Command */
protected $command;
protected function setUp() {
parent::setUp();
$config = $this->config = $this->getMockBuilder('OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
/** @var \OCP\IConfig $config */
$this->command = new Enable($config);
}
public function dataEnable() {
return [
['no', true, 'Encryption enabled'],
['yes', false, 'Encryption is already enabled'],
];
}
/**
* @dataProvider dataEnable
*
* @param string $oldStatus
* @param bool $isUpdating
* @param string $expectedString
*/
public function testEnable($oldStatus, $isUpdating, $expectedString) {
$invoceCount = 0;
$this->config->expects($this->at($invoceCount))
->method('getAppValue')
->with('core', 'encryption_enabled', $this->anything())
->willReturn($oldStatus);
$invoceCount++;
if ($isUpdating) {
$this->config->expects($this->once())
->method('setAppValue')
->with('core', 'encryption_enabled', 'yes');
$invoceCount++;
}
$this->config->expects($this->at($invoceCount))
->method('getAppValue')
->with('core', 'default_encryption_module', $this->anything())
->willReturnArgument(2);
$this->consoleOutput->expects($this->at(0))
->method('writeln')
->with($expectedString);
$this->consoleOutput->expects($this->at(1))
->method('writeln')
->with($this->stringContains('Default module'));
\Test_Helper::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}
}

View File

@ -0,0 +1,92 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace Tests\Core\Command\Encryption;
use OC\Core\Command\Encryption\SetDefaultModule;
use Test\TestCase;
class SetDefaultModuleTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $manager;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleInput;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleOutput;
/** @var \Symfony\Component\Console\Command\Command */
protected $command;
protected function setUp() {
parent::setUp();
$manager = $this->manager = $this->getMockBuilder('OCP\Encryption\IManager')
->disableOriginalConstructor()
->getMock();
$this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
/** @var \OCP\Encryption\IManager $manager */
$this->command = new SetDefaultModule($manager);
}
public function dataSetDefaultModule() {
return [
['ID0', 'ID0', null, null, 'already'],
['ID0', 'ID1', 'ID1', true, 'info'],
['ID0', 'ID1', 'ID1', false, 'error'],
];
}
/**
* @dataProvider dataSetDefaultModule
*
* @param string $oldModule
* @param string $newModule
* @param string $updateModule
* @param bool $updateSuccess
* @param string $expectedString
*/
public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) {
$this->consoleInput->expects($this->once())
->method('getArgument')
->with('module')
->willReturn($newModule);
$this->manager->expects($this->once())
->method('getDefaultEncryptionModuleId')
->willReturn($oldModule);
if ($updateModule) {
$this->manager->expects($this->once())
->method('setDefaultEncryptionModule')
->with($updateModule)
->willReturn($updateSuccess);
}
$this->consoleOutput->expects($this->once())
->method('writeln')
->with($this->stringContains($expectedString));
\Test_Helper::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}
}

View File

@ -21,7 +21,6 @@ class ManagerTest extends TestCase {
$this->config = $this->getMock('\OCP\IConfig');
$this->logger = $this->getMock('\OCP\ILogger');
$this->manager = new Manager($this->config, $this->logger);
}
public function testManagerIsDisabled() {
@ -48,32 +47,30 @@ class ManagerTest extends TestCase {
$this->assertTrue($this->manager->isEnabled());
}
/**
* @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
* @expectedExceptionMessage Id "id" already used by encryption module "TestDummyModule0"
*/
public function testModuleRegistration() {
$this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
$em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->addNewEncryptionModule($this->manager, 0);
$this->assertCount(1, $this->manager->getEncryptionModules());
return $this->manager;
}
/**
* @depends testModuleRegistration
* @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
* @expectedExceptionMessage Id "ID0" already used by encryption module "TestDummyModule0"
*/
public function testModuleReRegistration($manager) {
$this->addNewEncryptionModule($manager, 0);
}
public function testModuleUnRegistration() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
$em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1,
count($this->manager->getEncryptionModules())
);
$this->addNewEncryptionModule($this->manager, 0);
$this->assertCount(1, $this->manager->getEncryptionModules());
$this->manager->unregisterEncryptionModule('id');
$this->manager->unregisterEncryptionModule('ID0');
$this->assertEmpty($this->manager->getEncryptionModules());
}
@ -84,34 +81,83 @@ class ManagerTest extends TestCase {
*/
public function testGetEncryptionModuleUnknown() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
$em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
$this->addNewEncryptionModule($this->manager, 0);
$this->assertCount(1, $this->manager->getEncryptionModules());
$this->manager->getEncryptionModule('unknown');
}
public function testGetEncryptionModule() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
$em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
$en0 = $this->manager->getEncryptionModule('id');
$this->assertEquals('id', $en0->getId());
public function testGetEncryptionModuleEmpty() {
global $defaultId;
$defaultId = null;
$this->config->expects($this->any())
->method('getAppValue')
->with('core', 'default_encryption_module')
->willReturnCallback(function() { global $defaultId; return $defaultId; });
$this->addNewEncryptionModule($this->manager, 0);
$this->assertCount(1, $this->manager->getEncryptionModules());
$this->addNewEncryptionModule($this->manager, 1);
$this->assertCount(2, $this->manager->getEncryptionModules());
// Should return the default module
$defaultId = 'ID0';
$this->assertEquals('ID0', $this->manager->getEncryptionModule()->getId());
$defaultId = 'ID1';
$this->assertEquals('ID1', $this->manager->getEncryptionModule()->getId());
}
public function testGetDefaultEncryptionModule() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
$em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
$en0 = $this->manager->getEncryptionModule('id');
$this->assertEquals('id', $en0->getId());
public function testGetEncryptionModule() {
global $defaultId;
$defaultId = null;
$this->config->expects($this->any())
->method('getAppValue')
->with('core', 'default_encryption_module')
->willReturnCallback(function() { global $defaultId; return $defaultId; });
$this->addNewEncryptionModule($this->manager, 0);
$defaultId = 'ID0';
$this->assertCount(1, $this->manager->getEncryptionModules());
$en0 = $this->manager->getEncryptionModule('ID0');
$this->assertEquals('ID0', $en0->getId());
$en0 = \Test_Helper::invokePrivate($this->manager, 'getDefaultEncryptionModule');
$this->assertEquals('ID0', $en0->getId());
$this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
}
public function testSetDefaultEncryptionModule() {
global $defaultId;
$defaultId = null;
$this->config->expects($this->any())
->method('getAppValue')
->with('core', 'default_encryption_module')
->willReturnCallback(function() { global $defaultId; return $defaultId; });
$this->addNewEncryptionModule($this->manager, 0);
$this->assertCount(1, $this->manager->getEncryptionModules());
$this->addNewEncryptionModule($this->manager, 1);
$this->assertCount(2, $this->manager->getEncryptionModules());
// Default module is the first we set
$defaultId = 'ID0';
$this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
// Set to an existing module
$this->config->expects($this->once())
->method('setAppValue')
->with('core', 'default_encryption_module', 'ID1');
$this->assertTrue($this->manager->setDefaultEncryptionModule('ID1'));
$defaultId = 'ID1';
$this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
// Set to an unexisting module
$this->assertFalse($this->manager->setDefaultEncryptionModule('ID2'));
$this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
}
// /**
@ -171,4 +217,18 @@ class ManagerTest extends TestCase {
// $en0 = $m->getEncryptionModule(0);
// $this->assertEquals(0, $en0->getId());
// }
protected function addNewEncryptionModule(Manager $manager, $id) {
$encryptionModule = $this->getMock('\OCP\Encryption\IEncryptionModule');
$encryptionModule->expects($this->any())
->method('getId')
->willReturn('ID' . $id);
$encryptionModule->expects($this->any())
->method('getDisplayName')
->willReturn('TestDummyModule' . $id);
/** @var \OCP\Encryption\IEncryptionModule $encryptionModule */
$manager->registerEncryptionModule('ID' . $id, 'TestDummyModule' . $id, function() use ($encryptionModule) {
return $encryptionModule;
});
}
}

View File

@ -69,7 +69,7 @@ class UpdateTest extends TestCase {
->disableOriginalConstructor()->getMock();
$this->encryptionManager->expects($this->once())
->method('getDefaultEncryptionModule')
->method('getEncryptionModule')
->willReturn($this->encryptionModule);
$this->uid = 'testUser1';

View File

@ -51,11 +51,8 @@ class Encryption extends \Test\Files\Storage\Storage {
$mockModule = $this->buildMockModule();
$this->encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
->disableOriginalConstructor()
->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule', 'isEnabled'])
->setMethods(['getEncryptionModule', 'isEnabled'])
->getMock();
$this->encryptionManager->expects($this->any())
->method('getDefaultEncryptionModule')
->willReturn($mockModule);
$this->encryptionManager->expects($this->any())
->method('getEncryptionModule')
->willReturn($mockModule);