DirectoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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\Filesystem;
  10. use OC\Files\Node\Node;
  11. use OC\Files\Storage\Wrapper\Quota;
  12. use OC\Files\View;
  13. use OCA\DAV\Connector\Sabre\Directory;
  14. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  15. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  16. use OCP\Constants;
  17. use OCP\Files\ForbiddenException;
  18. use OCP\Files\InvalidPathException;
  19. use OCP\Files\Mount\IMountPoint;
  20. use OCP\Files\StorageNotAvailableException;
  21. use Test\Traits\UserTrait;
  22. class TestViewDirectory extends View {
  23. public function __construct(
  24. private $updatables,
  25. private $deletables,
  26. private $canRename = true,
  27. ) {
  28. }
  29. public function isUpdatable($path) {
  30. return $this->updatables[$path];
  31. }
  32. public function isCreatable($path) {
  33. return $this->updatables[$path];
  34. }
  35. public function isDeletable($path) {
  36. return $this->deletables[$path];
  37. }
  38. public function rename($path1, $path2) {
  39. return $this->canRename;
  40. }
  41. public function getRelativePath($path): ?string {
  42. return $path;
  43. }
  44. }
  45. /**
  46. * @group DB
  47. */
  48. class DirectoryTest extends \Test\TestCase {
  49. use UserTrait;
  50. /** @var View|\PHPUnit\Framework\MockObject\MockObject */
  51. private $view;
  52. /** @var FileInfo|\PHPUnit\Framework\MockObject\MockObject */
  53. private $info;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->view = $this->createMock('OC\Files\View');
  57. $this->info = $this->createMock('OC\Files\FileInfo');
  58. $this->info->method('isReadable')
  59. ->willReturn(true);
  60. $this->info->method('getType')
  61. ->willReturn(Node::TYPE_FOLDER);
  62. $this->info->method('getName')
  63. ->willReturn('folder');
  64. $this->info->method('getPath')
  65. ->willReturn('/admin/files/folder');
  66. $this->info->method('getPermissions')
  67. ->willReturn(Constants::PERMISSION_READ);
  68. }
  69. private function getDir($path = '/') {
  70. $this->view->expects($this->once())
  71. ->method('getRelativePath')
  72. ->willReturn($path);
  73. $this->info->expects($this->once())
  74. ->method('getPath')
  75. ->willReturn($path);
  76. return new Directory($this->view, $this->info);
  77. }
  78. public function testDeleteRootFolderFails(): void {
  79. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  80. $this->info->expects($this->any())
  81. ->method('isDeletable')
  82. ->willReturn(true);
  83. $this->view->expects($this->never())
  84. ->method('rmdir');
  85. $dir = $this->getDir();
  86. $dir->delete();
  87. }
  88. public function testDeleteForbidden(): void {
  89. $this->expectException(Forbidden::class);
  90. // deletion allowed
  91. $this->info->expects($this->once())
  92. ->method('isDeletable')
  93. ->willReturn(true);
  94. // but fails
  95. $this->view->expects($this->once())
  96. ->method('rmdir')
  97. ->with('sub')
  98. ->willThrowException(new ForbiddenException('', true));
  99. $dir = $this->getDir('sub');
  100. $dir->delete();
  101. }
  102. public function testDeleteFolderWhenAllowed(): void {
  103. // deletion allowed
  104. $this->info->expects($this->once())
  105. ->method('isDeletable')
  106. ->willReturn(true);
  107. // but fails
  108. $this->view->expects($this->once())
  109. ->method('rmdir')
  110. ->with('sub')
  111. ->willReturn(true);
  112. $dir = $this->getDir('sub');
  113. $dir->delete();
  114. }
  115. public function testDeleteFolderFailsWhenNotAllowed(): void {
  116. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  117. $this->info->expects($this->once())
  118. ->method('isDeletable')
  119. ->willReturn(false);
  120. $dir = $this->getDir('sub');
  121. $dir->delete();
  122. }
  123. public function testDeleteFolderThrowsWhenDeletionFailed(): void {
  124. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  125. // deletion allowed
  126. $this->info->expects($this->once())
  127. ->method('isDeletable')
  128. ->willReturn(true);
  129. // but fails
  130. $this->view->expects($this->once())
  131. ->method('rmdir')
  132. ->with('sub')
  133. ->willReturn(false);
  134. $dir = $this->getDir('sub');
  135. $dir->delete();
  136. }
  137. public function testGetChildren(): void {
  138. $info1 = $this->getMockBuilder(FileInfo::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $info2 = $this->getMockBuilder(FileInfo::class)
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $info1->method('getName')
  145. ->willReturn('first');
  146. $info1->method('getPath')
  147. ->willReturn('folder/first');
  148. $info1->method('getEtag')
  149. ->willReturn('abc');
  150. $info2->method('getName')
  151. ->willReturn('second');
  152. $info2->method('getPath')
  153. ->willReturn('folder/second');
  154. $info2->method('getEtag')
  155. ->willReturn('def');
  156. $this->view->expects($this->once())
  157. ->method('getDirectoryContent')
  158. ->willReturn([$info1, $info2]);
  159. $this->view->expects($this->any())
  160. ->method('getRelativePath')
  161. ->willReturnCallback(function ($path) {
  162. return str_replace('/admin/files/', '', $path);
  163. });
  164. $this->view->expects($this->any())
  165. ->method('getAbsolutePath')
  166. ->willReturnCallback(function ($path) {
  167. return Filesystem::normalizePath('/admin/files' . $path);
  168. });
  169. $this->overwriteService(View::class, $this->view);
  170. $dir = new Directory($this->view, $this->info);
  171. $nodes = $dir->getChildren();
  172. $this->assertEquals(2, count($nodes));
  173. // calling a second time just returns the cached values,
  174. // does not call getDirectoryContents again
  175. $dir->getChildren();
  176. }
  177. public function testGetChildrenNoPermission(): void {
  178. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  179. $info = $this->createMock(FileInfo::class);
  180. $info->expects($this->any())
  181. ->method('isReadable')
  182. ->willReturn(false);
  183. $dir = new Directory($this->view, $info);
  184. $dir->getChildren();
  185. }
  186. public function testGetChildNoPermission(): void {
  187. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  188. $this->info->expects($this->any())
  189. ->method('isReadable')
  190. ->willReturn(false);
  191. $dir = new Directory($this->view, $this->info);
  192. $dir->getChild('test');
  193. }
  194. public function testGetChildThrowStorageNotAvailableException(): void {
  195. $this->expectException(\Sabre\DAV\Exception\ServiceUnavailable::class);
  196. $this->view->expects($this->once())
  197. ->method('getFileInfo')
  198. ->willThrowException(new StorageNotAvailableException());
  199. $dir = new Directory($this->view, $this->info);
  200. $dir->getChild('.');
  201. }
  202. public function testGetChildThrowInvalidPath(): void {
  203. $this->expectException(InvalidPath::class);
  204. $this->view->expects($this->once())
  205. ->method('verifyPath')
  206. ->willThrowException(new InvalidPathException());
  207. $this->view->expects($this->never())
  208. ->method('getFileInfo');
  209. $dir = new Directory($this->view, $this->info);
  210. $dir->getChild('.');
  211. }
  212. public function testGetQuotaInfoUnlimited(): void {
  213. self::createUser('user', 'password');
  214. self::loginAsUser('user');
  215. $mountPoint = $this->createMock(IMountPoint::class);
  216. $storage = $this->getMockBuilder(Quota::class)
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. $mountPoint->method('getStorage')
  220. ->willReturn($storage);
  221. $storage->expects($this->any())
  222. ->method('instanceOfStorage')
  223. ->willReturnMap([
  224. ['\OCA\Files_Sharing\SharedStorage', false],
  225. ['\OC\Files\Storage\Wrapper\Quota', false],
  226. ]);
  227. $storage->expects($this->once())
  228. ->method('getOwner')
  229. ->willReturn('user');
  230. $storage->expects($this->never())
  231. ->method('getQuota');
  232. $storage->expects($this->once())
  233. ->method('free_space')
  234. ->willReturn(800);
  235. $this->info->expects($this->any())
  236. ->method('getPath')
  237. ->willReturn('/admin/files/foo');
  238. $this->info->expects($this->once())
  239. ->method('getSize')
  240. ->willReturn(200);
  241. $this->info->expects($this->once())
  242. ->method('getMountPoint')
  243. ->willReturn($mountPoint);
  244. $this->view->expects($this->any())
  245. ->method('getRelativePath')
  246. ->willReturn('/foo');
  247. $this->info->expects($this->once())
  248. ->method('getInternalPath')
  249. ->willReturn('/foo');
  250. $mountPoint->method('getMountPoint')
  251. ->willReturn('/user/files/mymountpoint');
  252. $dir = new Directory($this->view, $this->info);
  253. $this->assertEquals([200, -3], $dir->getQuotaInfo()); //200 used, unlimited
  254. }
  255. public function testGetQuotaInfoSpecific(): void {
  256. self::createUser('user', 'password');
  257. self::loginAsUser('user');
  258. $mountPoint = $this->createMock(IMountPoint::class);
  259. $storage = $this->getMockBuilder(Quota::class)
  260. ->disableOriginalConstructor()
  261. ->getMock();
  262. $mountPoint->method('getStorage')
  263. ->willReturn($storage);
  264. $storage->expects($this->any())
  265. ->method('instanceOfStorage')
  266. ->willReturnMap([
  267. ['\OCA\Files_Sharing\SharedStorage', false],
  268. ['\OC\Files\Storage\Wrapper\Quota', true],
  269. ]);
  270. $storage->expects($this->once())
  271. ->method('getOwner')
  272. ->willReturn('user');
  273. $storage->expects($this->once())
  274. ->method('getQuota')
  275. ->willReturn(1000);
  276. $storage->expects($this->once())
  277. ->method('free_space')
  278. ->willReturn(800);
  279. $this->info->expects($this->once())
  280. ->method('getSize')
  281. ->willReturn(200);
  282. $this->info->expects($this->once())
  283. ->method('getMountPoint')
  284. ->willReturn($mountPoint);
  285. $this->info->expects($this->once())
  286. ->method('getInternalPath')
  287. ->willReturn('/foo');
  288. $mountPoint->method('getMountPoint')
  289. ->willReturn('/user/files/mymountpoint');
  290. $this->view->expects($this->any())
  291. ->method('getRelativePath')
  292. ->willReturn('/foo');
  293. $dir = new Directory($this->view, $this->info);
  294. $this->assertEquals([200, 800], $dir->getQuotaInfo()); //200 used, 800 free
  295. }
  296. /**
  297. * @dataProvider moveFailedProvider
  298. */
  299. public function testMoveFailed($source, $destination, $updatables, $deletables): void {
  300. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  301. $this->moveTest($source, $destination, $updatables, $deletables);
  302. }
  303. /**
  304. * @dataProvider moveSuccessProvider
  305. */
  306. public function testMoveSuccess($source, $destination, $updatables, $deletables): void {
  307. $this->moveTest($source, $destination, $updatables, $deletables);
  308. $this->addToAssertionCount(1);
  309. }
  310. /**
  311. * @dataProvider moveFailedInvalidCharsProvider
  312. */
  313. public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables): void {
  314. $this->expectException(InvalidPath::class);
  315. $this->moveTest($source, $destination, $updatables, $deletables);
  316. }
  317. public function moveFailedInvalidCharsProvider() {
  318. return [
  319. ['a/valid', "a/i\nvalid", ['a' => true, 'a/valid' => true, 'a/c*' => false], []],
  320. ];
  321. }
  322. public function moveFailedProvider() {
  323. return [
  324. ['a/b', 'a/c', ['a' => false, 'a/b' => false, 'a/c' => false], []],
  325. ['a/b', 'b/b', ['a' => false, 'a/b' => false, 'b' => false, 'b/b' => false], []],
  326. ['a/b', 'b/b', ['a' => false, 'a/b' => true, 'b' => false, 'b/b' => false], []],
  327. ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => false, 'b/b' => false], []],
  328. ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => false]],
  329. ['a/b', 'a/c', ['a' => false, 'a/b' => true, 'a/c' => false], []],
  330. ];
  331. }
  332. public function moveSuccessProvider() {
  333. return [
  334. ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => true]],
  335. // older files with special chars can still be renamed to valid names
  336. ['a/b*', 'b/b', ['a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false], ['a/b*' => true]],
  337. ];
  338. }
  339. /**
  340. * @param $source
  341. * @param $destination
  342. * @param $updatables
  343. */
  344. private function moveTest($source, $destination, $updatables, $deletables): void {
  345. $view = new TestViewDirectory($updatables, $deletables);
  346. $sourceInfo = new FileInfo($source, null, null, [
  347. 'type' => FileInfo::TYPE_FOLDER,
  348. ], null);
  349. $targetInfo = new FileInfo(dirname($destination), null, null, [
  350. 'type' => FileInfo::TYPE_FOLDER,
  351. ], null);
  352. $sourceNode = new Directory($view, $sourceInfo);
  353. $targetNode = $this->getMockBuilder(Directory::class)
  354. ->setMethods(['childExists'])
  355. ->setConstructorArgs([$view, $targetInfo])
  356. ->getMock();
  357. $targetNode->expects($this->any())->method('childExists')
  358. ->with(basename($destination))
  359. ->willReturn(false);
  360. $this->assertTrue($targetNode->moveInto(basename($destination), $source, $sourceNode));
  361. }
  362. public function testFailingMove(): void {
  363. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  364. $this->expectExceptionMessage('Could not copy directory b, target exists');
  365. $source = 'a/b';
  366. $destination = 'c/b';
  367. $updatables = ['a' => true, 'a/b' => true, 'b' => true, 'c/b' => false];
  368. $deletables = ['a/b' => true];
  369. $view = new TestViewDirectory($updatables, $deletables);
  370. $sourceInfo = new FileInfo($source, null, null, ['type' => FileInfo::TYPE_FOLDER], null);
  371. $targetInfo = new FileInfo(dirname($destination), null, null, ['type' => FileInfo::TYPE_FOLDER], null);
  372. $sourceNode = new Directory($view, $sourceInfo);
  373. $targetNode = $this->getMockBuilder(Directory::class)
  374. ->onlyMethods(['childExists'])
  375. ->setConstructorArgs([$view, $targetInfo])
  376. ->getMock();
  377. $targetNode->expects($this->once())->method('childExists')
  378. ->with(basename($destination))
  379. ->willReturn(true);
  380. $targetNode->moveInto(basename($destination), $source, $sourceNode);
  381. }
  382. }