1
0

UtilTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * @see https://bugs.php.net/bug.php?id=21641
  14. */
  15. protected int $headerSize = 8192;
  16. /** @var \PHPUnit\Framework\MockObject\MockObject */
  17. protected $view;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
  19. protected $userManager;
  20. /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
  21. protected $groupManager;
  22. /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
  23. private $config;
  24. private Util $util;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->view = $this->getMockBuilder(View::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->userManager = $this->createMock(IUserManager::class);
  31. $this->groupManager = $this->createMock(IGroupManager::class);
  32. $this->config = $this->createMock(IConfig::class);
  33. $this->util = new Util(
  34. $this->view,
  35. $this->userManager,
  36. $this->groupManager,
  37. $this->config
  38. );
  39. }
  40. /**
  41. * @dataProvider providesHeadersForEncryptionModule
  42. */
  43. public function testGetEncryptionModuleId($expected, $header) {
  44. $id = $this->util->getEncryptionModuleId($header);
  45. $this->assertEquals($expected, $id);
  46. }
  47. public function providesHeadersForEncryptionModule() {
  48. return [
  49. ['', []],
  50. ['', ['1']],
  51. [2, ['oc_encryption_module' => 2]],
  52. ];
  53. }
  54. /**
  55. * @dataProvider providesHeaders
  56. */
  57. public function testCreateHeader($expected, $header, $moduleId) {
  58. $em = $this->createMock(IEncryptionModule::class);
  59. $em->expects($this->any())->method('getId')->willReturn($moduleId);
  60. $result = $this->util->createHeader($header, $em);
  61. $this->assertEquals($expected, $result);
  62. }
  63. public function providesHeaders() {
  64. return [
  65. [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  66. , [], '0'],
  67. [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  68. , ['custom_header' => 'foo'], '0'],
  69. ];
  70. }
  71. public function testCreateHeaderFailed() {
  72. $this->expectException(\OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException::class);
  73. $header = ['header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo'];
  74. $em = $this->createMock(IEncryptionModule::class);
  75. $em->expects($this->any())->method('getId')->willReturn('moduleId');
  76. $this->util->createHeader($header, $em);
  77. }
  78. /**
  79. * @dataProvider providePathsForTestIsExcluded
  80. */
  81. public function testIsExcluded($path, $keyStorageRoot, $expected) {
  82. $this->config->expects($this->once())
  83. ->method('getAppValue')
  84. ->with('core', 'encryption_key_storage_root', '')
  85. ->willReturn($keyStorageRoot);
  86. $this->userManager
  87. ->expects($this->any())
  88. ->method('userExists')
  89. ->willReturnCallback([$this, 'isExcludedCallback']);
  90. $this->assertSame($expected,
  91. $this->util->isExcluded($path)
  92. );
  93. }
  94. public function providePathsForTestIsExcluded() {
  95. return [
  96. ['/files_encryption', '', true],
  97. ['files_encryption/foo.txt', '', true],
  98. ['test/foo.txt', '', false],
  99. ['/user1/files_encryption/foo.txt', '', true],
  100. ['/user1/files/foo.txt', '', false],
  101. ['/keyStorage/user1/files/foo.txt', 'keyStorage', true],
  102. ['/keyStorage/files_encryption', '/keyStorage', true],
  103. ['keyStorage/user1/files_encryption', '/keyStorage/', true],
  104. ];
  105. }
  106. public function isExcludedCallback() {
  107. $args = func_get_args();
  108. if ($args[0] === 'user1') {
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * @dataProvider dataTestIsFile
  115. */
  116. public function testIsFile($path, $expected) {
  117. $this->assertSame($expected,
  118. $this->util->isFile($path)
  119. );
  120. }
  121. public function dataTestIsFile() {
  122. return [
  123. ['/user/files/test.txt', true],
  124. ['/user/files', true],
  125. ['/user/files_versions/test.txt', false],
  126. ['/user/foo/files/test.txt', false],
  127. ['/files/foo/files/test.txt', false],
  128. ['/user', false],
  129. ['/user/test.txt', false],
  130. ];
  131. }
  132. /**
  133. * @dataProvider dataTestStripPartialFileExtension
  134. *
  135. * @param string $path
  136. * @param string $expected
  137. */
  138. public function testStripPartialFileExtension($path, $expected) {
  139. $this->assertSame($expected,
  140. $this->util->stripPartialFileExtension($path));
  141. }
  142. public function dataTestStripPartialFileExtension() {
  143. return [
  144. ['/foo/test.txt', '/foo/test.txt'],
  145. ['/foo/test.txt.part', '/foo/test.txt'],
  146. ['/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'],
  147. ['/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'],
  148. ];
  149. }
  150. }