1
0

UtilTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Util;
  9. use OC\Files\View;
  10. use OCP\Encryption\IEncryptionModule;
  11. use OCP\IConfig;
  12. use OCP\IGroupManager;
  13. use OCP\IUserManager;
  14. use Test\TestCase;
  15. class UtilTest extends TestCase {
  16. /**
  17. * block size will always be 8192 for a PHP stream
  18. *
  19. * @see https://bugs.php.net/bug.php?id=21641
  20. */
  21. protected int $headerSize = 8192;
  22. /** @var \PHPUnit\Framework\MockObject\MockObject */
  23. protected $view;
  24. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
  25. protected $userManager;
  26. /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
  27. protected $groupManager;
  28. /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
  29. private $config;
  30. private Util $util;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->view = $this->getMockBuilder(View::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->userManager = $this->createMock(IUserManager::class);
  37. $this->groupManager = $this->createMock(IGroupManager::class);
  38. $this->config = $this->createMock(IConfig::class);
  39. $this->util = new Util(
  40. $this->view,
  41. $this->userManager,
  42. $this->groupManager,
  43. $this->config
  44. );
  45. }
  46. /**
  47. * @dataProvider providesHeadersForEncryptionModule
  48. */
  49. public function testGetEncryptionModuleId($expected, $header): void {
  50. $id = $this->util->getEncryptionModuleId($header);
  51. $this->assertEquals($expected, $id);
  52. }
  53. public function providesHeadersForEncryptionModule() {
  54. return [
  55. ['', []],
  56. ['', ['1']],
  57. [2, ['oc_encryption_module' => 2]],
  58. ];
  59. }
  60. /**
  61. * @dataProvider providesHeaders
  62. */
  63. public function testCreateHeader($expected, $header, $moduleId): void {
  64. $em = $this->createMock(IEncryptionModule::class);
  65. $em->expects($this->any())->method('getId')->willReturn($moduleId);
  66. $result = $this->util->createHeader($header, $em);
  67. $this->assertEquals($expected, $result);
  68. }
  69. public function providesHeaders() {
  70. return [
  71. [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  72. , [], '0'],
  73. [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  74. , ['custom_header' => 'foo'], '0'],
  75. ];
  76. }
  77. public function testCreateHeaderFailed(): void {
  78. $this->expectException(\OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException::class);
  79. $header = ['header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo'];
  80. $em = $this->createMock(IEncryptionModule::class);
  81. $em->expects($this->any())->method('getId')->willReturn('moduleId');
  82. $this->util->createHeader($header, $em);
  83. }
  84. /**
  85. * @dataProvider providePathsForTestIsExcluded
  86. */
  87. public function testIsExcluded($path, $keyStorageRoot, $expected): void {
  88. $this->config->expects($this->once())
  89. ->method('getAppValue')
  90. ->with('core', 'encryption_key_storage_root', '')
  91. ->willReturn($keyStorageRoot);
  92. $this->userManager
  93. ->expects($this->any())
  94. ->method('userExists')
  95. ->willReturnCallback([$this, 'isExcludedCallback']);
  96. $this->assertSame($expected,
  97. $this->util->isExcluded($path)
  98. );
  99. }
  100. public function providePathsForTestIsExcluded() {
  101. return [
  102. ['/files_encryption', '', true],
  103. ['files_encryption/foo.txt', '', true],
  104. ['test/foo.txt', '', false],
  105. ['/user1/files_encryption/foo.txt', '', true],
  106. ['/user1/files/foo.txt', '', false],
  107. ['/keyStorage/user1/files/foo.txt', 'keyStorage', true],
  108. ['/keyStorage/files_encryption', '/keyStorage', true],
  109. ['keyStorage/user1/files_encryption', '/keyStorage/', true],
  110. ];
  111. }
  112. public function isExcludedCallback() {
  113. $args = func_get_args();
  114. if ($args[0] === 'user1') {
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * @dataProvider dataTestIsFile
  121. */
  122. public function testIsFile($path, $expected): void {
  123. $this->assertSame($expected,
  124. $this->util->isFile($path)
  125. );
  126. }
  127. public function dataTestIsFile() {
  128. return [
  129. ['/user/files/test.txt', true],
  130. ['/user/files', true],
  131. ['/user/files_versions/test.txt', false],
  132. ['/user/foo/files/test.txt', false],
  133. ['/files/foo/files/test.txt', false],
  134. ['/user', false],
  135. ['/user/test.txt', false],
  136. ];
  137. }
  138. /**
  139. * @dataProvider dataTestStripPartialFileExtension
  140. *
  141. * @param string $path
  142. * @param string $expected
  143. */
  144. public function testStripPartialFileExtension($path, $expected): void {
  145. $this->assertSame($expected,
  146. $this->util->stripPartialFileExtension($path));
  147. }
  148. public function dataTestStripPartialFileExtension() {
  149. return [
  150. ['/foo/test.txt', '/foo/test.txt'],
  151. ['/foo/test.txt.part', '/foo/test.txt'],
  152. ['/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'],
  153. ['/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'],
  154. ];
  155. }
  156. /**
  157. * @dataProvider dataTestParseRawHeader
  158. */
  159. public function testParseRawHeader($rawHeader, $expected): void {
  160. $result = $this->util->parseRawHeader($rawHeader);
  161. $this->assertSameSize($expected, $result);
  162. foreach ($result as $key => $value) {
  163. $this->assertArrayHasKey($key, $expected);
  164. $this->assertSame($expected[$key], $value);
  165. }
  166. }
  167. public function dataTestParseRawHeader() {
  168. return [
  169. [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  170. , [Util::HEADER_ENCRYPTION_MODULE_KEY => '0']],
  171. [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  172. , ['custom_header' => 'foo', Util::HEADER_ENCRYPTION_MODULE_KEY => '0']],
  173. [str_pad('HelloWorld', $this->headerSize, '-', STR_PAD_RIGHT), []],
  174. ['', []],
  175. [str_pad('HBEGIN:oc_encryption_module:0', $this->headerSize, '-', STR_PAD_RIGHT)
  176. , []],
  177. [str_pad('oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  178. , []],
  179. ];
  180. }
  181. /**
  182. * @dataProvider dataTestGetFileKeyDir
  183. *
  184. * @param bool $isSystemWideMountPoint
  185. * @param string $storageRoot
  186. * @param string $expected
  187. */
  188. public function testGetFileKeyDir($isSystemWideMountPoint, $storageRoot, $expected): void {
  189. $path = '/user1/files/foo/bar.txt';
  190. $owner = 'user1';
  191. $relativePath = '/foo/bar.txt';
  192. $util = $this->getMockBuilder(Util::class)
  193. ->onlyMethods(['isSystemWideMountPoint', 'getUidAndFilename', 'getKeyStorageRoot'])
  194. ->setConstructorArgs([
  195. $this->view,
  196. $this->userManager,
  197. $this->groupManager,
  198. $this->config
  199. ])
  200. ->getMock();
  201. $util->expects($this->once())->method('getKeyStorageRoot')
  202. ->willReturn($storageRoot);
  203. $util->expects($this->once())->method('isSystemWideMountPoint')
  204. ->willReturn($isSystemWideMountPoint);
  205. $util->expects($this->once())->method('getUidAndFilename')
  206. ->with($path)->willReturn([$owner, $relativePath]);
  207. $this->assertSame($expected,
  208. $util->getFileKeyDir('OC_DEFAULT_MODULE', $path)
  209. );
  210. }
  211. public function dataTestGetFileKeyDir() {
  212. return [
  213. [false, '', '/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  214. [true, '', '/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  215. [false, 'newStorageRoot', '/newStorageRoot/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  216. [true, 'newStorageRoot', '/newStorageRoot/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  217. ];
  218. }
  219. }