UtilTest.php 7.0 KB

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