2015-01-14 22:39:23 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Test\Encryption;
|
|
|
|
|
|
|
|
use OC\Encryption\Manager;
|
2016-09-07 20:51:45 +03:00
|
|
|
use OC\Encryption\Util;
|
|
|
|
use OC\Files\View;
|
|
|
|
use OC\Memcache\ArrayCache;
|
|
|
|
use OCP\Encryption\IEncryptionModule;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
2015-01-14 22:39:23 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
|
2015-04-07 19:05:54 +03:00
|
|
|
/** @var Manager */
|
|
|
|
private $manager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2015-04-07 19:05:54 +03:00
|
|
|
private $config;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2015-04-07 19:05:54 +03:00
|
|
|
private $logger;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2015-05-27 11:37:12 +03:00
|
|
|
private $l10n;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var View|\PHPUnit\Framework\MockObject\MockObject */
|
2015-07-24 13:24:18 +03:00
|
|
|
private $view;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Util|\PHPUnit\Framework\MockObject\MockObject */
|
2015-07-24 13:24:18 +03:00
|
|
|
private $util;
|
2020-08-11 22:32:18 +03:00
|
|
|
|
|
|
|
/** @var ArrayCache|\PHPUnit\Framework\MockObject\MockObject */
|
2016-03-31 00:20:37 +03:00
|
|
|
private $arrayCache;
|
2015-07-24 13:24:18 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-04-07 19:05:54 +03:00
|
|
|
parent::setUp();
|
2016-09-07 20:51:45 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
$this->view = $this->createMock(View::class);
|
|
|
|
$this->util = $this->createMock(Util::class);
|
|
|
|
$this->arrayCache = $this->createMock(ArrayCache::class);
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->manager = new Manager($this->config, $this->logger, $this->l10n, $this->view, $this->util, $this->arrayCache);
|
2015-04-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
public function testManagerIsDisabled() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->assertFalse($this->manager->isEnabled());
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testManagerIsDisabledIfEnabledButNoModules() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
|
|
|
$this->assertFalse($this->manager->isEnabled());
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testManagerIsDisabledIfDisabledButModules() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(false);
|
2016-09-07 20:51:45 +03:00
|
|
|
$em = $this->createMock(IEncryptionModule::class);
|
2015-04-14 17:48:39 +03:00
|
|
|
$em->expects($this->any())->method('getId')->willReturn('id');
|
2015-01-14 22:39:23 +03:00
|
|
|
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
2020-04-10 15:19:56 +03:00
|
|
|
$this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) {
|
|
|
|
return $em;
|
|
|
|
});
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->assertFalse($this->manager->isEnabled());
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testManagerIsEnabled() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->config->expects($this->any())->method('getSystemValue')->willReturn(true);
|
|
|
|
$this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
|
|
|
|
$this->assertTrue($this->manager->isEnabled());
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testModuleRegistration() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
|
2015-04-14 17:48:39 +03:00
|
|
|
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->addNewEncryptionModule($this->manager, 0);
|
|
|
|
$this->assertCount(1, $this->manager->getEncryptionModules());
|
|
|
|
|
|
|
|
return $this->manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testModuleRegistration
|
|
|
|
*/
|
|
|
|
public function testModuleReRegistration($manager) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OC\Encryption\Exceptions\ModuleAlreadyExistsException::class);
|
|
|
|
$this->expectExceptionMessage('Id "ID0" already used by encryption module "TestDummyModule0"');
|
|
|
|
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->addNewEncryptionModule($manager, 0);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testModuleUnRegistration() {
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->addNewEncryptionModule($this->manager, 0);
|
|
|
|
$this->assertCount(1, $this->manager->getEncryptionModules());
|
2015-04-14 17:48:39 +03:00
|
|
|
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->manager->unregisterEncryptionModule('ID0');
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->assertEmpty($this->manager->getEncryptionModules());
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
public function testGetEncryptionModuleUnknown() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OC\Encryption\Exceptions\ModuleDoesNotExistsException::class);
|
|
|
|
$this->expectExceptionMessage('Module with ID: unknown does not exist.');
|
|
|
|
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->addNewEncryptionModule($this->manager, 0);
|
|
|
|
$this->assertCount(1, $this->manager->getEncryptionModules());
|
2015-04-07 19:05:54 +03:00
|
|
|
$this->manager->getEncryptionModule('unknown');
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-17 08:57:32 +03:00
|
|
|
public function testGetEncryptionModuleEmpty() {
|
|
|
|
global $defaultId;
|
|
|
|
$defaultId = null;
|
|
|
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('core', 'default_encryption_module')
|
2020-04-10 15:19:56 +03:00
|
|
|
->willReturnCallback(function () {
|
|
|
|
global $defaultId;
|
|
|
|
return $defaultId;
|
|
|
|
});
|
2015-04-17 08:57:32 +03:00
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
public function testGetEncryptionModule() {
|
2015-04-17 13:23:04 +03:00
|
|
|
global $defaultId;
|
|
|
|
$defaultId = null;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('core', 'default_encryption_module')
|
2020-04-10 15:19:56 +03:00
|
|
|
->willReturnCallback(function () {
|
|
|
|
global $defaultId;
|
|
|
|
return $defaultId;
|
|
|
|
});
|
2015-04-17 13:23:04 +03:00
|
|
|
|
|
|
|
$this->addNewEncryptionModule($this->manager, 0);
|
|
|
|
$defaultId = 'ID0';
|
|
|
|
$this->assertCount(1, $this->manager->getEncryptionModules());
|
|
|
|
|
|
|
|
$en0 = $this->manager->getEncryptionModule('ID0');
|
|
|
|
$this->assertEquals('ID0', $en0->getId());
|
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
$en0 = self::invokePrivate($this->manager, 'getDefaultEncryptionModule');
|
2015-04-17 13:23:04 +03:00
|
|
|
$this->assertEquals('ID0', $en0->getId());
|
2015-04-20 12:11:52 +03:00
|
|
|
|
|
|
|
$this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-04-17 09:37:34 +03:00
|
|
|
public function testSetDefaultEncryptionModule() {
|
|
|
|
global $defaultId;
|
|
|
|
$defaultId = null;
|
|
|
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('core', 'default_encryption_module')
|
2020-04-10 15:19:56 +03:00
|
|
|
->willReturnCallback(function () {
|
|
|
|
global $defaultId;
|
|
|
|
return $defaultId;
|
|
|
|
});
|
2015-04-17 09:37:34 +03:00
|
|
|
|
|
|
|
$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';
|
2015-04-20 12:11:52 +03:00
|
|
|
$this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
|
2015-04-17 09:37:34 +03:00
|
|
|
|
|
|
|
// 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';
|
2015-04-20 12:11:52 +03:00
|
|
|
$this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
|
2015-04-17 09:37:34 +03:00
|
|
|
|
|
|
|
// Set to an unexisting module
|
|
|
|
$this->assertFalse($this->manager->setDefaultEncryptionModule('ID2'));
|
2015-04-20 12:11:52 +03:00
|
|
|
$this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
|
2015-04-17 09:37:34 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:07:59 +03:00
|
|
|
// /**
|
|
|
|
// * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
|
|
|
|
// * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
|
|
|
|
// */
|
|
|
|
// public function testModuleRegistration() {
|
2017-04-20 13:27:32 +03:00
|
|
|
// $config = $this->createMock(IConfig::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $config->expects($this->any())->method('getSystemValue')->willReturn(true);
|
2017-04-20 13:27:32 +03:00
|
|
|
// $em = $this->createMock(IEncryptionModule::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $em->expects($this->any())->method('getId')->willReturn(0);
|
|
|
|
// $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
|
|
|
// $m = new Manager($config);
|
|
|
|
// $m->registerEncryptionModule($em);
|
|
|
|
// $this->assertTrue($m->isEnabled());
|
|
|
|
// $m->registerEncryptionModule($em);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public function testModuleUnRegistration() {
|
2017-04-20 13:27:32 +03:00
|
|
|
// $config = $this->createMock(IConfig::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $config->expects($this->any())->method('getSystemValue')->willReturn(true);
|
2017-04-20 13:27:32 +03:00
|
|
|
// $em = $this->createMock(IEncryptionModule::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $em->expects($this->any())->method('getId')->willReturn(0);
|
|
|
|
// $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
|
|
|
// $m = new Manager($config);
|
|
|
|
// $m->registerEncryptionModule($em);
|
|
|
|
// $this->assertTrue($m->isEnabled());
|
|
|
|
// $m->unregisterEncryptionModule($em);
|
|
|
|
// $this->assertFalse($m->isEnabled());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException
|
2017-04-18 10:53:02 +03:00
|
|
|
// * @expectedExceptionMessage Module with ID: unknown does not exist.
|
2015-03-27 12:07:59 +03:00
|
|
|
// */
|
|
|
|
// public function testGetEncryptionModuleUnknown() {
|
2017-04-20 13:27:32 +03:00
|
|
|
// $config = $this->createMock(IConfig::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $config->expects($this->any())->method('getSystemValue')->willReturn(true);
|
2017-04-20 13:27:32 +03:00
|
|
|
// $em = $this->createMock(IEncryptionModule::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $em->expects($this->any())->method('getId')->willReturn(0);
|
|
|
|
// $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
|
|
|
// $m = new Manager($config);
|
|
|
|
// $m->registerEncryptionModule($em);
|
|
|
|
// $this->assertTrue($m->isEnabled());
|
|
|
|
// $m->getEncryptionModule('unknown');
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public function testGetEncryptionModule() {
|
2017-04-20 13:27:32 +03:00
|
|
|
// $config = $this->createMock(IConfig::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $config->expects($this->any())->method('getSystemValue')->willReturn(true);
|
2017-04-20 13:27:32 +03:00
|
|
|
// $em = $this->createMock(IEncryptionModule::class);
|
2015-03-27 12:07:59 +03:00
|
|
|
// $em->expects($this->any())->method('getId')->willReturn(0);
|
|
|
|
// $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
|
|
|
|
// $m = new Manager($config);
|
|
|
|
// $m->registerEncryptionModule($em);
|
|
|
|
// $this->assertTrue($m->isEnabled());
|
|
|
|
// $en0 = $m->getEncryptionModule(0);
|
|
|
|
// $this->assertEquals(0, $en0->getId());
|
|
|
|
// }
|
2015-04-17 08:57:32 +03:00
|
|
|
|
|
|
|
protected function addNewEncryptionModule(Manager $manager, $id) {
|
2016-09-07 20:51:45 +03:00
|
|
|
$encryptionModule = $this->createMock(IEncryptionModule::class);
|
2015-04-17 08:57:32 +03:00
|
|
|
$encryptionModule->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('ID' . $id);
|
|
|
|
$encryptionModule->expects($this->any())
|
|
|
|
->method('getDisplayName')
|
|
|
|
->willReturn('TestDummyModule' . $id);
|
|
|
|
/** @var \OCP\Encryption\IEncryptionModule $encryptionModule */
|
2020-04-09 14:53:40 +03:00
|
|
|
$manager->registerEncryptionModule('ID' . $id, 'TestDummyModule' . $id, function () use ($encryptionModule) {
|
2015-04-17 08:57:32 +03:00
|
|
|
return $encryptionModule;
|
|
|
|
});
|
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|