1
0

ManagerTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 OCP\ILogger;
  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 ILogger|\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. public function setUp() {
  28. parent::setUp();
  29. $this->config = $this->createMock(IConfig::class);
  30. $this->logger = $this->createMock(ILogger::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) {return $em;});
  50. $this->assertFalse($this->manager->isEnabled());
  51. }
  52. public function testManagerIsEnabled() {
  53. $this->config->expects($this->any())->method('getSystemValue')->willReturn(true);
  54. $this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
  55. $this->assertTrue($this->manager->isEnabled());
  56. }
  57. public function testModuleRegistration() {
  58. $this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
  59. $this->addNewEncryptionModule($this->manager, 0);
  60. $this->assertCount(1, $this->manager->getEncryptionModules());
  61. return $this->manager;
  62. }
  63. /**
  64. * @depends testModuleRegistration
  65. * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
  66. * @expectedExceptionMessage Id "ID0" already used by encryption module "TestDummyModule0"
  67. */
  68. public function testModuleReRegistration($manager) {
  69. $this->addNewEncryptionModule($manager, 0);
  70. }
  71. public function testModuleUnRegistration() {
  72. $this->config->expects($this->any())->method('getAppValue')->willReturn(true);
  73. $this->addNewEncryptionModule($this->manager, 0);
  74. $this->assertCount(1, $this->manager->getEncryptionModules());
  75. $this->manager->unregisterEncryptionModule('ID0');
  76. $this->assertEmpty($this->manager->getEncryptionModules());
  77. }
  78. /**
  79. * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException
  80. * @expectedExceptionMessage Module with ID: unknown does not exist.
  81. */
  82. public function testGetEncryptionModuleUnknown() {
  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() { global $defaultId; return $defaultId; });
  95. $this->addNewEncryptionModule($this->manager, 0);
  96. $this->assertCount(1, $this->manager->getEncryptionModules());
  97. $this->addNewEncryptionModule($this->manager, 1);
  98. $this->assertCount(2, $this->manager->getEncryptionModules());
  99. // Should return the default module
  100. $defaultId = 'ID0';
  101. $this->assertEquals('ID0', $this->manager->getEncryptionModule()->getId());
  102. $defaultId = 'ID1';
  103. $this->assertEquals('ID1', $this->manager->getEncryptionModule()->getId());
  104. }
  105. public function testGetEncryptionModule() {
  106. global $defaultId;
  107. $defaultId = null;
  108. $this->config->expects($this->any())
  109. ->method('getAppValue')
  110. ->with('core', 'default_encryption_module')
  111. ->willReturnCallback(function() { global $defaultId; return $defaultId; });
  112. $this->addNewEncryptionModule($this->manager, 0);
  113. $defaultId = 'ID0';
  114. $this->assertCount(1, $this->manager->getEncryptionModules());
  115. $en0 = $this->manager->getEncryptionModule('ID0');
  116. $this->assertEquals('ID0', $en0->getId());
  117. $en0 = self::invokePrivate($this->manager, 'getDefaultEncryptionModule');
  118. $this->assertEquals('ID0', $en0->getId());
  119. $this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
  120. }
  121. public function testSetDefaultEncryptionModule() {
  122. global $defaultId;
  123. $defaultId = null;
  124. $this->config->expects($this->any())
  125. ->method('getAppValue')
  126. ->with('core', 'default_encryption_module')
  127. ->willReturnCallback(function() { global $defaultId; return $defaultId; });
  128. $this->addNewEncryptionModule($this->manager, 0);
  129. $this->assertCount(1, $this->manager->getEncryptionModules());
  130. $this->addNewEncryptionModule($this->manager, 1);
  131. $this->assertCount(2, $this->manager->getEncryptionModules());
  132. // Default module is the first we set
  133. $defaultId = 'ID0';
  134. $this->assertEquals('ID0', $this->manager->getDefaultEncryptionModuleId());
  135. // Set to an existing module
  136. $this->config->expects($this->once())
  137. ->method('setAppValue')
  138. ->with('core', 'default_encryption_module', 'ID1');
  139. $this->assertTrue($this->manager->setDefaultEncryptionModule('ID1'));
  140. $defaultId = 'ID1';
  141. $this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
  142. // Set to an unexisting module
  143. $this->assertFalse($this->manager->setDefaultEncryptionModule('ID2'));
  144. $this->assertEquals('ID1', $this->manager->getDefaultEncryptionModuleId());
  145. }
  146. // /**
  147. // * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
  148. // * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
  149. // */
  150. // public function testModuleRegistration() {
  151. // $config = $this->createMock(IConfig::class);
  152. // $config->expects($this->any())->method('getSystemValue')->willReturn(true);
  153. // $em = $this->createMock(IEncryptionModule::class);
  154. // $em->expects($this->any())->method('getId')->willReturn(0);
  155. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  156. // $m = new Manager($config);
  157. // $m->registerEncryptionModule($em);
  158. // $this->assertTrue($m->isEnabled());
  159. // $m->registerEncryptionModule($em);
  160. // }
  161. //
  162. // public function testModuleUnRegistration() {
  163. // $config = $this->createMock(IConfig::class);
  164. // $config->expects($this->any())->method('getSystemValue')->willReturn(true);
  165. // $em = $this->createMock(IEncryptionModule::class);
  166. // $em->expects($this->any())->method('getId')->willReturn(0);
  167. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  168. // $m = new Manager($config);
  169. // $m->registerEncryptionModule($em);
  170. // $this->assertTrue($m->isEnabled());
  171. // $m->unregisterEncryptionModule($em);
  172. // $this->assertFalse($m->isEnabled());
  173. // }
  174. //
  175. // /**
  176. // * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException
  177. // * @expectedExceptionMessage Module with ID: unknown does not exist.
  178. // */
  179. // public function testGetEncryptionModuleUnknown() {
  180. // $config = $this->createMock(IConfig::class);
  181. // $config->expects($this->any())->method('getSystemValue')->willReturn(true);
  182. // $em = $this->createMock(IEncryptionModule::class);
  183. // $em->expects($this->any())->method('getId')->willReturn(0);
  184. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  185. // $m = new Manager($config);
  186. // $m->registerEncryptionModule($em);
  187. // $this->assertTrue($m->isEnabled());
  188. // $m->getEncryptionModule('unknown');
  189. // }
  190. //
  191. // public function testGetEncryptionModule() {
  192. // $config = $this->createMock(IConfig::class);
  193. // $config->expects($this->any())->method('getSystemValue')->willReturn(true);
  194. // $em = $this->createMock(IEncryptionModule::class);
  195. // $em->expects($this->any())->method('getId')->willReturn(0);
  196. // $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
  197. // $m = new Manager($config);
  198. // $m->registerEncryptionModule($em);
  199. // $this->assertTrue($m->isEnabled());
  200. // $en0 = $m->getEncryptionModule(0);
  201. // $this->assertEquals(0, $en0->getId());
  202. // }
  203. protected function addNewEncryptionModule(Manager $manager, $id) {
  204. $encryptionModule = $this->createMock(IEncryptionModule::class);
  205. $encryptionModule->expects($this->any())
  206. ->method('getId')
  207. ->willReturn('ID' . $id);
  208. $encryptionModule->expects($this->any())
  209. ->method('getDisplayName')
  210. ->willReturn('TestDummyModule' . $id);
  211. /** @var \OCP\Encryption\IEncryptionModule $encryptionModule */
  212. $manager->registerEncryptionModule('ID' . $id, 'TestDummyModule' . $id, function() use ($encryptionModule) {
  213. return $encryptionModule;
  214. });
  215. }
  216. }