NodeTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OC\Files\FileInfo;
  9. use OC\Files\Mount\MountPoint;
  10. use OC\Files\Node\Folder;
  11. use OC\Files\View;
  12. use OC\Share20\ShareAttributes;
  13. use OCA\DAV\Connector\Sabre\File;
  14. use OCA\Files_Sharing\SharedMount;
  15. use OCA\Files_Sharing\SharedStorage;
  16. use OCP\Constants;
  17. use OCP\Files\Cache\ICacheEntry;
  18. use OCP\Files\Mount\IMountPoint;
  19. use OCP\Files\Storage\IStorage;
  20. use OCP\ICache;
  21. use OCP\Share\IManager;
  22. use OCP\Share\IShare;
  23. use PHPUnit\Framework\MockObject\MockObject;
  24. /**
  25. * Class NodeTest
  26. *
  27. * @group DB
  28. * @package OCA\DAV\Tests\unit\Connector\Sabre
  29. */
  30. class NodeTest extends \Test\TestCase {
  31. public function davPermissionsProvider() {
  32. return [
  33. [Constants::PERMISSION_ALL, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVW'],
  34. [Constants::PERMISSION_ALL, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVCK'],
  35. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, false, 'test', 'SRGDNVW'],
  36. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, true, 'test', 'SRMGDNVW'],
  37. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, true, '' , 'SRMGDNVW'],
  38. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE, true, '' , 'SRMGDNV'],
  39. [Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, 'file', true, Constants::PERMISSION_ALL, false, 'test', 'SGDNVW'],
  40. [Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGD'],
  41. [Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGNVW'],
  42. [Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVW'],
  43. [Constants::PERMISSION_ALL - Constants::PERMISSION_READ, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RDNVW'],
  44. [Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNV'],
  45. [Constants::PERMISSION_ALL - Constants::PERMISSION_READ, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RDNVCK'],
  46. ];
  47. }
  48. /**
  49. * @dataProvider davPermissionsProvider
  50. */
  51. public function testDavPermissions($permissions, $type, $shared, $shareRootPermissions, $mounted, $internalPath, $expected): void {
  52. $info = $this->getMockBuilder(FileInfo::class)
  53. ->disableOriginalConstructor()
  54. ->onlyMethods(['getPermissions', 'isShared', 'isMounted', 'getType', 'getInternalPath', 'getStorage', 'getMountPoint'])
  55. ->getMock();
  56. $info->method('getPermissions')
  57. ->willReturn($permissions);
  58. $info->method('isShared')
  59. ->willReturn($shared);
  60. $info->method('isMounted')
  61. ->willReturn($mounted);
  62. $info->method('getType')
  63. ->willReturn($type);
  64. $info->method('getInternalPath')
  65. ->willReturn($internalPath);
  66. $info->method('getMountPoint')
  67. ->willReturnCallback(function () use ($shared) {
  68. if ($shared) {
  69. return $this->createMock(SharedMount::class);
  70. } else {
  71. return $this->createMock(MountPoint::class);
  72. }
  73. });
  74. $storage = $this->createMock(IStorage::class);
  75. if ($shared) {
  76. $storage->method('instanceOfStorage')
  77. ->willReturn(true);
  78. $cache = $this->createMock(ICache::class);
  79. $storage->method('getCache')
  80. ->willReturn($cache);
  81. $shareRootEntry = $this->createMock(ICacheEntry::class);
  82. $cache->method('get')
  83. ->willReturn($shareRootEntry);
  84. $shareRootEntry->method('getPermissions')
  85. ->willReturn($shareRootPermissions);
  86. } else {
  87. $storage->method('instanceOfStorage')
  88. ->willReturn(false);
  89. }
  90. $info->method('getStorage')
  91. ->willReturn($storage);
  92. $view = $this->getMockBuilder(View::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $node = new File($view, $info);
  96. $this->assertEquals($expected, $node->getDavPermissions());
  97. }
  98. public function sharePermissionsProvider() {
  99. return [
  100. [\OCP\Files\FileInfo::TYPE_FILE, null, 1, 1],
  101. [\OCP\Files\FileInfo::TYPE_FILE, null, 3, 3],
  102. [\OCP\Files\FileInfo::TYPE_FILE, null, 5, 1],
  103. [\OCP\Files\FileInfo::TYPE_FILE, null, 7, 3],
  104. [\OCP\Files\FileInfo::TYPE_FILE, null, 9, 1],
  105. [\OCP\Files\FileInfo::TYPE_FILE, null, 11, 3],
  106. [\OCP\Files\FileInfo::TYPE_FILE, null, 13, 1],
  107. [\OCP\Files\FileInfo::TYPE_FILE, null, 15, 3],
  108. [\OCP\Files\FileInfo::TYPE_FILE, null, 17, 17],
  109. [\OCP\Files\FileInfo::TYPE_FILE, null, 19, 19],
  110. [\OCP\Files\FileInfo::TYPE_FILE, null, 21, 17],
  111. [\OCP\Files\FileInfo::TYPE_FILE, null, 23, 19],
  112. [\OCP\Files\FileInfo::TYPE_FILE, null, 25, 17],
  113. [\OCP\Files\FileInfo::TYPE_FILE, null, 27, 19],
  114. [\OCP\Files\FileInfo::TYPE_FILE, null, 29, 17],
  115. [\OCP\Files\FileInfo::TYPE_FILE, null, 30, 18],
  116. [\OCP\Files\FileInfo::TYPE_FILE, null, 31, 19],
  117. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 1, 1],
  118. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 3, 3],
  119. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 5, 5],
  120. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 7, 7],
  121. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 9, 9],
  122. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 11, 11],
  123. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 13, 13],
  124. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 15, 15],
  125. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 17, 17],
  126. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 19, 19],
  127. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 21, 21],
  128. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 23, 23],
  129. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 25, 25],
  130. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 27, 27],
  131. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 29, 29],
  132. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 30, 30],
  133. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 31, 31],
  134. [\OCP\Files\FileInfo::TYPE_FOLDER, 'shareToken', 7, 7],
  135. ];
  136. }
  137. /**
  138. * @dataProvider sharePermissionsProvider
  139. */
  140. public function testSharePermissions($type, $user, $permissions, $expected): void {
  141. $storage = $this->getMockBuilder(IStorage::class)
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $storage->method('getPermissions')->willReturn($permissions);
  145. $mountpoint = $this->getMockBuilder(IMountPoint::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $mountpoint->method('getMountPoint')->willReturn('myPath');
  149. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  150. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  151. if ($user === null) {
  152. $shareManager->expects($this->never())->method('getShareByToken');
  153. $share->expects($this->never())->method('getPermissions');
  154. } else {
  155. $shareManager->expects($this->once())->method('getShareByToken')->with($user)
  156. ->willReturn($share);
  157. $share->expects($this->once())->method('getPermissions')->willReturn($permissions);
  158. }
  159. $info = $this->getMockBuilder(FileInfo::class)
  160. ->disableOriginalConstructor()
  161. ->setMethods(['getStorage', 'getType', 'getMountPoint', 'getPermissions'])
  162. ->getMock();
  163. $info->method('getStorage')->willReturn($storage);
  164. $info->method('getType')->willReturn($type);
  165. $info->method('getMountPoint')->willReturn($mountpoint);
  166. $info->method('getPermissions')->willReturn($permissions);
  167. $view = $this->getMockBuilder(View::class)
  168. ->disableOriginalConstructor()
  169. ->getMock();
  170. $node = new File($view, $info);
  171. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  172. $this->assertEquals($expected, $node->getSharePermissions($user));
  173. }
  174. public function testShareAttributes(): void {
  175. $storage = $this->getMockBuilder(SharedStorage::class)
  176. ->disableOriginalConstructor()
  177. ->setMethods(['getShare'])
  178. ->getMock();
  179. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  180. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  181. $storage->expects($this->once())
  182. ->method('getShare')
  183. ->willReturn($share);
  184. $attributes = new ShareAttributes();
  185. $attributes->setAttribute('permissions', 'download', false);
  186. $share->expects($this->once())->method('getAttributes')->willReturn($attributes);
  187. /** @var Folder&MockObject $info */
  188. $info = $this->getMockBuilder(Folder::class)
  189. ->disableOriginalConstructor()
  190. ->onlyMethods(['getStorage', 'getType'])
  191. ->getMock();
  192. $info->method('getStorage')->willReturn($storage);
  193. $info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);
  194. /** @var View&MockObject $view */
  195. $view = $this->getMockBuilder(View::class)
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $node = new File($view, $info);
  199. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  200. $this->assertEquals($attributes->toArray(), $node->getShareAttributes());
  201. }
  202. public function testShareAttributesNonShare(): void {
  203. $storage = $this->getMockBuilder(IStorage::class)
  204. ->disableOriginalConstructor()
  205. ->getMock();
  206. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  207. /** @var Folder&MockObject */
  208. $info = $this->getMockBuilder(Folder::class)
  209. ->disableOriginalConstructor()
  210. ->onlyMethods(['getStorage', 'getType'])
  211. ->getMock();
  212. $info->method('getStorage')->willReturn($storage);
  213. $info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);
  214. /** @var View&MockObject */
  215. $view = $this->getMockBuilder(View::class)
  216. ->disableOriginalConstructor()
  217. ->getMock();
  218. $node = new File($view, $info);
  219. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  220. $this->assertEquals([], $node->getShareAttributes());
  221. }
  222. public function sanitizeMtimeProvider() {
  223. return [
  224. [123456789, 123456789],
  225. ['987654321', 987654321],
  226. ];
  227. }
  228. /**
  229. * @dataProvider sanitizeMtimeProvider
  230. */
  231. public function testSanitizeMtime($mtime, $expected): void {
  232. $view = $this->getMockBuilder(View::class)
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $info = $this->getMockBuilder(FileInfo::class)
  236. ->disableOriginalConstructor()
  237. ->getMock();
  238. $node = new File($view, $info);
  239. $result = $this->invokePrivate($node, 'sanitizeMtime', [$mtime]);
  240. $this->assertEquals($expected, $result);
  241. }
  242. public function invalidSanitizeMtimeProvider() {
  243. return [
  244. [-1337], [0], ['abcdef'], ['-1337'], ['0'], [12321], [24 * 60 * 60 - 1],
  245. ];
  246. }
  247. /**
  248. * @dataProvider invalidSanitizeMtimeProvider
  249. */
  250. public function testInvalidSanitizeMtime($mtime): void {
  251. $this->expectException(\InvalidArgumentException::class);
  252. $view = $this->getMockBuilder(View::class)
  253. ->disableOriginalConstructor()
  254. ->getMock();
  255. $info = $this->getMockBuilder(FileInfo::class)
  256. ->disableOriginalConstructor()
  257. ->getMock();
  258. $node = new File($view, $info);
  259. $result = $this->invokePrivate($node, 'sanitizeMtime', [$mtime]);
  260. }
  261. }