UtilTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace Test\Encryption;
  3. use OC\Encryption\Util;
  4. use OC\Files\View;
  5. use OCA\Files_External\Lib\StorageConfig;
  6. use OCA\Files_External\Service\GlobalStoragesService;
  7. use OCP\Encryption\IEncryptionModule;
  8. use OCP\IConfig;
  9. use OCP\IGroupManager;
  10. use OCP\IUserManager;
  11. use Test\TestCase;
  12. class UtilTest extends TestCase {
  13. /**
  14. * block size will always be 8192 for a PHP stream
  15. * @see https://bugs.php.net/bug.php?id=21641
  16. */
  17. protected int $headerSize = 8192;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject */
  19. protected $view;
  20. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
  21. protected $userManager;
  22. /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
  23. protected $groupManager;
  24. /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
  25. private $config;
  26. private Util $util;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->view = $this->getMockBuilder(View::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $this->userManager = $this->createMock(IUserManager::class);
  33. $this->groupManager = $this->createMock(IGroupManager::class);
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->util = new Util(
  36. $this->view,
  37. $this->userManager,
  38. $this->groupManager,
  39. $this->config
  40. );
  41. }
  42. /**
  43. * @dataProvider providesHeadersForEncryptionModule
  44. */
  45. public function testGetEncryptionModuleId($expected, $header) {
  46. $id = $this->util->getEncryptionModuleId($header);
  47. $this->assertEquals($expected, $id);
  48. }
  49. public function providesHeadersForEncryptionModule() {
  50. return [
  51. ['', []],
  52. ['', ['1']],
  53. [2, ['oc_encryption_module' => 2]],
  54. ];
  55. }
  56. /**
  57. * @dataProvider providesHeaders
  58. */
  59. public function testCreateHeader($expected, $header, $moduleId) {
  60. $em = $this->createMock(IEncryptionModule::class);
  61. $em->expects($this->any())->method('getId')->willReturn($moduleId);
  62. $result = $this->util->createHeader($header, $em);
  63. $this->assertEquals($expected, $result);
  64. }
  65. public function providesHeaders() {
  66. return [
  67. [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  68. , [], '0'],
  69. [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  70. , ['custom_header' => 'foo'], '0'],
  71. ];
  72. }
  73. public function testCreateHeaderFailed() {
  74. $this->expectException(\OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException::class);
  75. $header = ['header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo'];
  76. $em = $this->createMock(IEncryptionModule::class);
  77. $em->expects($this->any())->method('getId')->willReturn('moduleId');
  78. $this->util->createHeader($header, $em);
  79. }
  80. /**
  81. * @dataProvider providePathsForTestIsExcluded
  82. */
  83. public function testIsExcluded($path, $keyStorageRoot, $expected) {
  84. $this->config->expects($this->once())
  85. ->method('getAppValue')
  86. ->with('core', 'encryption_key_storage_root', '')
  87. ->willReturn($keyStorageRoot);
  88. $this->userManager
  89. ->expects($this->any())
  90. ->method('userExists')
  91. ->willReturnCallback([$this, 'isExcludedCallback']);
  92. $this->assertSame($expected,
  93. $this->util->isExcluded($path)
  94. );
  95. }
  96. public function providePathsForTestIsExcluded() {
  97. return [
  98. ['/files_encryption', '', true],
  99. ['files_encryption/foo.txt', '', true],
  100. ['test/foo.txt', '', false],
  101. ['/user1/files_encryption/foo.txt', '', true],
  102. ['/user1/files/foo.txt', '', false],
  103. ['/keyStorage/user1/files/foo.txt', 'keyStorage', true],
  104. ['/keyStorage/files_encryption', '/keyStorage', true],
  105. ['keyStorage/user1/files_encryption', '/keyStorage/', true],
  106. ];
  107. }
  108. public function isExcludedCallback() {
  109. $args = func_get_args();
  110. if ($args[0] === 'user1') {
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * @dataProvider dataTestIsFile
  117. */
  118. public function testIsFile($path, $expected) {
  119. $this->assertSame($expected,
  120. $this->util->isFile($path)
  121. );
  122. }
  123. public function dataTestIsFile() {
  124. return [
  125. ['/user/files/test.txt', true],
  126. ['/user/files', true],
  127. ['/user/files_versions/test.txt', false],
  128. ['/user/foo/files/test.txt', false],
  129. ['/files/foo/files/test.txt', false],
  130. ['/user', false],
  131. ['/user/test.txt', false],
  132. ];
  133. }
  134. /**
  135. * @dataProvider dataTestStripPartialFileExtension
  136. *
  137. * @param string $path
  138. * @param string $expected
  139. */
  140. public function testStripPartialFileExtension($path, $expected) {
  141. $this->assertSame($expected,
  142. $this->util->stripPartialFileExtension($path));
  143. }
  144. public function dataTestStripPartialFileExtension() {
  145. return [
  146. ['/foo/test.txt', '/foo/test.txt'],
  147. ['/foo/test.txt.part', '/foo/test.txt'],
  148. ['/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'],
  149. ['/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'],
  150. ];
  151. }
  152. public function dataTestIsSystemWideMountPoint() {
  153. return [
  154. [false, 'non-matching mount point name', [], [], '/mp_another'],
  155. [true, 'applicable to all', [], []],
  156. [true, 'applicable to user directly', ['user1'], []],
  157. [true, 'applicable to group directly', [], ['group1']],
  158. [false, 'non-applicable to current user', ['user2'], []],
  159. [false, 'non-applicable to current user\'s group', [], ['group2']],
  160. [true, 'mount point without leading slash', [], [], 'mp'],
  161. ];
  162. }
  163. /**
  164. * @dataProvider dataTestIsSystemWideMountPoint
  165. */
  166. public function testIsSystemWideMountPoint($expectedResult, $expectationText, $applicableUsers, $applicableGroups, $mountPointName = '/mp') {
  167. $this->groupManager->method('isInGroup')
  168. ->will($this->returnValueMap([
  169. ['user1', 'group1', true], // user is only in group1
  170. ['user1', 'group2', false],
  171. ]));
  172. $storages = [];
  173. $storageConfig = $this->createMock(StorageConfig::class);
  174. $storageConfig->method('getMountPoint')->willReturn($mountPointName);
  175. $storageConfig->method('getApplicableUsers')->willReturn($applicableUsers);
  176. $storageConfig->method('getApplicableGroups')->willReturn($applicableGroups);
  177. $storages[] = $storageConfig;
  178. $storagesServiceMock = $this->createMock(GlobalStoragesService::class);
  179. $storagesServiceMock->expects($this->atLeastOnce())->method('getAllStorages')
  180. ->willReturn($storages);
  181. $this->overwriteService(GlobalStoragesService::class, $storagesServiceMock);
  182. $this->assertEquals($expectedResult, $this->util->isSystemWideMountPoint('/files/mp', 'user1'), 'Test case: ' . $expectationText);
  183. }
  184. }