1
0

ManagerTest.php 9.5 KB

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