UtilTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Test\Encryption;
  3. use OC\Encryption\Util;
  4. use OCP\Encryption\IEncryptionModule;
  5. use Test\TestCase;
  6. class UtilTest extends TestCase {
  7. /**
  8. * block size will always be 8192 for a PHP stream
  9. * @see https://bugs.php.net/bug.php?id=21641
  10. * @var integer
  11. */
  12. protected $headerSize = 8192;
  13. /** @var \PHPUnit_Framework_MockObject_MockObject */
  14. protected $view;
  15. /** @var \PHPUnit_Framework_MockObject_MockObject */
  16. protected $userManager;
  17. /** @var \PHPUnit_Framework_MockObject_MockObject */
  18. protected $groupManager;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject */
  20. private $config;
  21. /** @var \OC\Encryption\Util */
  22. private $util;
  23. public function setUp() {
  24. parent::setUp();
  25. $this->view = $this->getMockBuilder('OC\Files\View')
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->groupManager = $this->getMockBuilder('OC\Group\Manager')
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->config = $this->getMockBuilder('OCP\IConfig')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->util = new Util(
  38. $this->view,
  39. $this->userManager,
  40. $this->groupManager,
  41. $this->config
  42. );
  43. }
  44. /**
  45. * @dataProvider providesHeadersForEncryptionModule
  46. */
  47. public function testGetEncryptionModuleId($expected, $header) {
  48. $id = $this->util->getEncryptionModuleId($header);
  49. $this->assertEquals($expected, $id);
  50. }
  51. public function providesHeadersForEncryptionModule() {
  52. return [
  53. ['', []],
  54. ['', ['1']],
  55. [2, ['oc_encryption_module' => 2]],
  56. ];
  57. }
  58. /**
  59. * @dataProvider providesHeaders
  60. */
  61. public function testCreateHeader($expected, $header, $moduleId) {
  62. $em = $this->createMock(IEncryptionModule::class);
  63. $em->expects($this->any())->method('getId')->willReturn($moduleId);
  64. $result = $this->util->createHeader($header, $em);
  65. $this->assertEquals($expected, $result);
  66. }
  67. public function providesHeaders() {
  68. return [
  69. [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  70. , [], '0'],
  71. [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  72. , ['custom_header' => 'foo'], '0'],
  73. ];
  74. }
  75. /**
  76. * @expectedException \OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException
  77. */
  78. public function testCreateHeaderFailed() {
  79. $header = array('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. ->will($this->returnCallback(array($this, 'isExcludedCallback')));
  96. $this->assertSame($expected,
  97. $this->util->isExcluded($path)
  98. );
  99. }
  100. public function providePathsForTestIsExcluded() {
  101. return array(
  102. array('/files_encryption', '', true),
  103. array('files_encryption/foo.txt', '', true),
  104. array('test/foo.txt', '', false),
  105. array('/user1/files_encryption/foo.txt', '', true),
  106. array('/user1/files/foo.txt', '', false),
  107. array('/keyStorage/user1/files/foo.txt', 'keyStorage', true),
  108. array('/keyStorage/files_encryption', '/keyStorage', true),
  109. array('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 array(
  129. array('/user/files/test.txt', true),
  130. array('/user/files', true),
  131. array('/user/files_versions/test.txt', false),
  132. array('/user/foo/files/test.txt', false),
  133. array('/files/foo/files/test.txt', false),
  134. array('/user', false),
  135. array('/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 array(
  150. array('/foo/test.txt', '/foo/test.txt'),
  151. array('/foo/test.txt.part', '/foo/test.txt'),
  152. array('/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'),
  153. array('/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'),
  154. );
  155. }
  156. }