UtilTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Test\TestCase;
  8. class UtilTest extends TestCase {
  9. /**
  10. * block size will always be 8192 for a PHP stream
  11. * @see https://bugs.php.net/bug.php?id=21641
  12. * @var integer
  13. */
  14. protected $headerSize = 8192;
  15. /** @var \PHPUnit\Framework\MockObject\MockObject */
  16. protected $view;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject */
  18. protected $userManager;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject */
  20. protected $groupManager;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject */
  22. private $config;
  23. /** @var \OC\Encryption\Util */
  24. private $util;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->view = $this->getMockBuilder(View::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->groupManager = $this->getMockBuilder('OC\Group\Manager')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->config = $this->getMockBuilder(IConfig::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  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) {
  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) {
  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() {
  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) {
  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) {
  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) {
  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. }