1
0

ManagerTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Encryption;
  8. use OC\Encryption\Manager;
  9. use OC\Encryption\Util;
  10. use OC\Files\View;
  11. use OC\Memcache\ArrayCache;
  12. use OCP\Encryption\IEncryptionModule;
  13. use OCP\IConfig;
  14. use OCP\IL10N;
  15. use Psr\Log\LoggerInterface;
  16. use Test\TestCase;
  17. class ManagerTest extends TestCase {
  18. /** @var Manager */
  19. private $manager;
  20. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  21. private $config;
  22. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  23. private $logger;
  24. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  25. private $l10n;
  26. /** @var View|\PHPUnit\Framework\MockObject\MockObject */
  27. private $view;
  28. /** @var Util|\PHPUnit\Framework\MockObject\MockObject */
  29. private $util;
  30. /** @var ArrayCache|\PHPUnit\Framework\MockObject\MockObject */
  31. private $arrayCache;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->logger = $this->createMock(LoggerInterface::class);
  36. $this->l10n = $this->createMock(IL10N::class);
  37. $this->view = $this->createMock(View::class);
  38. $this->util = $this->createMock(Util::class);
  39. $this->arrayCache = $this->createMock(ArrayCache::class);
  40. $this->manager = new Manager($this->config, $this->logger, $this->l10n, $this->view, $this->util, $this->arrayCache);
  41. }
  42. public function testManagerIsDisabled(): void {
  43. $this->assertFalse($this->manager->isEnabled());
  44. }
  45. public function testManagerIsDisabledIfEnabledButNoModules(): void {
  46. $this->config->expects($this->any())->method('getAppValue')->willReturn(true);
  47. $this->assertFalse($this->manager->isEnabled());
  48. }
  49. public function testManagerIsDisabledIfDisabledButModules(): void {
  50. $this->config->expects($this->any())->method('getAppValue')->willReturn(false);
  51. $em = $this->createMock(IEncryptionModule::class);
  52. $em->expects($this->any())->method('getId')->willReturn('id');
  53. $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  54. $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) {
  55. return $em;
  56. });
  57. $this->assertFalse($this->manager->isEnabled());
  58. }
  59. public function testManagerIsEnabled(): void {
  60. $this->config->expects($this->any())->method('getSystemValueBool')->willReturn(true);
  61. $this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
  62. $this->assertTrue($this->manager->isEnabled());
  63. }
  64. public function testModuleRegistration() {
  65. $this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
  66. $this->addNewEncryptionModule($this->manager, 0);
  67. $this->assertCount(1, $this->manager->getEncryptionModules());
  68. return $this->manager;
  69. }
  70. /**
  71. * @depends testModuleRegistration
  72. */
  73. public function testModuleReRegistration($manager): void {
  74. $this->expectException(\OC\Encryption\Exceptions\ModuleAlreadyExistsException::class);
  75. $this->expectExceptionMessage('Id "ID0" already used by encryption module "TestDummyModule0"');
  76. $this->addNewEncryptionModule($manager, 0);
  77. }
  78. public function testModuleUnRegistration(): void {
  79. $this->config->expects($this->any())->method('getAppValue')->willReturn(true);
  80. $this->addNewEncryptionModule($this->manager, 0);
  81. $this->assertCount(1, $this->manager->getEncryptionModules());
  82. $this->manager->unregisterEncryptionModule('ID0');
  83. $this->assertEmpty($this->manager->getEncryptionModules());
  84. }
  85. public function testGetEncryptionModuleUnknown(): void {
  86. $this->expectException(\OC\Encryption\Exceptions\ModuleDoesNotExistsException::class);
  87. $this->expectExceptionMessage('Module with ID: unknown does not exist.');
  88. $this->config->expects($this->any())->method('getAppValue')->willReturn(true);
  89. $this->addNewEncryptionModule($this->manager, 0);
  90. $this->assertCount(1, $this->manager->getEncryptionModules());
  91. $this->manager->getEncryptionModule('unknown');
  92. }
  93. public function testGetEncryptionModuleEmpty(): void {
  94. global $defaultId;
  95. $defaultId = null;
  96. $this->config->expects($this->any())
  97. ->method('getAppValue')
  98. ->with('core', 'default_encryption_module')
  99. ->willReturnCallback(function () {
  100. global $defaultId;
  101. return $defaultId;
  102. });
  103. $this->addNewEncryptionModule($this->manager, 0);
  104. $this->assertCount(1, $this->manager->getEncryptionModules());
  105. $this->addNewEncryptionModule($this->manager, 1);
  106. $this->assertCount(2, $this->manager->getEncryptionModules());
  107. // Should return the default module
  108. $defaultId = 'ID0';
  109. $this->assertEquals('ID0', $this->manager->getEncryptionModule()->getId());
  110. $defaultId = 'ID1';
  111. $this->assertEquals('ID1', $this->manager->getEncryptionModule()->getId());
  112. }
  113. public function testGetEncryptionModule(): void {
  114. global $defaultId;
  115. $defaultId = null;
  116. $this->config->expects($this->any())
  117. ->method('getAppValue')
  118. ->with('core', 'default_encryption_module')
  119. ->willReturnCallback(function () {
  120. global $defaultId;
  121. return $defaultId;
  122. });
  123. $this->addNewEncryptionModule($this->manager, 0);
  124. $defaultId = 'ID0';
  125. $this->assertCount(1, $this->manager->getEncryptionModules());
  126. $en0 = $this->manager->getEncryptionModule('ID0');
  127. $this->assertEquals('ID0', $en0->getId());
  128. $en0 = self::invokePrivate($this->manager, 'getDefaultEncryptionModule');
  129. $this->assertEquals('ID0', $en0->getId());
  130. $this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
  131. }
  132. public function testSetDefaultEncryptionModule(): void {
  133. global $defaultId;
  134. $defaultId = null;
  135. $this->config->expects($this->any())
  136. ->method('getAppValue')
  137. ->with('core', 'default_encryption_module')
  138. ->willReturnCallback(function () {
  139. global $defaultId;
  140. return $defaultId;
  141. });
  142. $this->addNewEncryptionModule($this->manager, 0);
  143. $this->assertCount(1, $this->manager->getEncryptionModules());
  144. $this->addNewEncryptionModule($this->manager, 1);
  145. $this->assertCount(2, $this->manager->getEncryptionModules());
  146. // Default module is the first we set
  147. $defaultId = 'ID0';
  148. $this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
  149. // Set to an existing module
  150. $this->config->expects($this->once())
  151. ->method('setAppValue')
  152. ->with('core', 'default_encryption_module', 'ID1');
  153. $this->assertTrue($this->manager->setDefaultEncryptionModule('ID1'));
  154. $defaultId = 'ID1';
  155. $this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
  156. // Set to an unexisting module
  157. $this->assertFalse($this->manager->setDefaultEncryptionModule('ID2'));
  158. $this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
  159. }
  160. // /**
  161. // * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
  162. // * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
  163. // */
  164. // public function testModuleRegistration() {
  165. // $config = $this->createMock(IConfig::class);
  166. // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true);
  167. // $em = $this->createMock(IEncryptionModule::class);
  168. // $em->expects($this->any())->method('getId')->willReturn(0);
  169. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  170. // $m = new Manager($config);
  171. // $m->registerEncryptionModule($em);
  172. // $this->assertTrue($m->isEnabled());
  173. // $m->registerEncryptionModule($em);
  174. // }
  175. //
  176. // public function testModuleUnRegistration() {
  177. // $config = $this->createMock(IConfig::class);
  178. // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true);
  179. // $em = $this->createMock(IEncryptionModule::class);
  180. // $em->expects($this->any())->method('getId')->willReturn(0);
  181. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  182. // $m = new Manager($config);
  183. // $m->registerEncryptionModule($em);
  184. // $this->assertTrue($m->isEnabled());
  185. // $m->unregisterEncryptionModule($em);
  186. // $this->assertFalse($m->isEnabled());
  187. // }
  188. //
  189. // /**
  190. // * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException
  191. // * @expectedExceptionMessage Module with ID: unknown does not exist.
  192. // */
  193. // public function testGetEncryptionModuleUnknown() {
  194. // $config = $this->createMock(IConfig::class);
  195. // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true);
  196. // $em = $this->createMock(IEncryptionModule::class);
  197. // $em->expects($this->any())->method('getId')->willReturn(0);
  198. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  199. // $m = new Manager($config);
  200. // $m->registerEncryptionModule($em);
  201. // $this->assertTrue($m->isEnabled());
  202. // $m->getEncryptionModule('unknown');
  203. // }
  204. //
  205. // public function testGetEncryptionModule() {
  206. // $config = $this->createMock(IConfig::class);
  207. // $config->expects($this->any())->method('getSystemValueBool')->willReturn(true);
  208. // $em = $this->createMock(IEncryptionModule::class);
  209. // $em->expects($this->any())->method('getId')->willReturn(0);
  210. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  211. // $m = new Manager($config);
  212. // $m->registerEncryptionModule($em);
  213. // $this->assertTrue($m->isEnabled());
  214. // $en0 = $m->getEncryptionModule(0);
  215. // $this->assertEquals(0, $en0->getId());
  216. // }
  217. protected function addNewEncryptionModule(Manager $manager, $id) {
  218. $encryptionModule = $this->createMock(IEncryptionModule::class);
  219. $encryptionModule->expects($this->any())
  220. ->method('getId')
  221. ->willReturn('ID' . $id);
  222. $encryptionModule->expects($this->any())
  223. ->method('getDisplayName')
  224. ->willReturn('TestDummyModule' . $id);
  225. /** @var \OCP\Encryption\IEncryptionModule $encryptionModule */
  226. $manager->registerEncryptionModule('ID' . $id, 'TestDummyModule' . $id, function () use ($encryptionModule) {
  227. return $encryptionModule;
  228. });
  229. }
  230. }