UtilTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. public function setUp() {
  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. /**
  78. * @expectedException \OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException
  79. */
  80. public function testCreateHeaderFailed() {
  81. $header = array('header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo');
  82. $em = $this->createMock(IEncryptionModule::class);
  83. $em->expects($this->any())->method('getId')->willReturn('moduleId');
  84. $this->util->createHeader($header, $em);
  85. }
  86. /**
  87. * @dataProvider providePathsForTestIsExcluded
  88. */
  89. public function testIsExcluded($path, $keyStorageRoot, $expected) {
  90. $this->config->expects($this->once())
  91. ->method('getAppValue')
  92. ->with('core', 'encryption_key_storage_root', '')
  93. ->willReturn($keyStorageRoot);
  94. $this->userManager
  95. ->expects($this->any())
  96. ->method('userExists')
  97. ->will($this->returnCallback(array($this, 'isExcludedCallback')));
  98. $this->assertSame($expected,
  99. $this->util->isExcluded($path)
  100. );
  101. }
  102. public function providePathsForTestIsExcluded() {
  103. return array(
  104. array('/files_encryption', '', true),
  105. array('files_encryption/foo.txt', '', true),
  106. array('test/foo.txt', '', false),
  107. array('/user1/files_encryption/foo.txt', '', true),
  108. array('/user1/files/foo.txt', '', false),
  109. array('/keyStorage/user1/files/foo.txt', 'keyStorage', true),
  110. array('/keyStorage/files_encryption', '/keyStorage', true),
  111. array('keyStorage/user1/files_encryption', '/keyStorage/', true),
  112. );
  113. }
  114. public function isExcludedCallback() {
  115. $args = func_get_args();
  116. if ($args[0] === 'user1') {
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * @dataProvider dataTestIsFile
  123. */
  124. public function testIsFile($path, $expected) {
  125. $this->assertSame($expected,
  126. $this->util->isFile($path)
  127. );
  128. }
  129. public function dataTestIsFile() {
  130. return array(
  131. array('/user/files/test.txt', true),
  132. array('/user/files', true),
  133. array('/user/files_versions/test.txt', false),
  134. array('/user/foo/files/test.txt', false),
  135. array('/files/foo/files/test.txt', false),
  136. array('/user', false),
  137. array('/user/test.txt', false),
  138. );
  139. }
  140. /**
  141. * @dataProvider dataTestStripPartialFileExtension
  142. *
  143. * @param string $path
  144. * @param string $expected
  145. */
  146. public function testStripPartialFileExtension($path, $expected) {
  147. $this->assertSame($expected,
  148. $this->util->stripPartialFileExtension($path));
  149. }
  150. public function dataTestStripPartialFileExtension() {
  151. return array(
  152. array('/foo/test.txt', '/foo/test.txt'),
  153. array('/foo/test.txt.part', '/foo/test.txt'),
  154. array('/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'),
  155. array('/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'),
  156. );
  157. }
  158. }