DirectoryTest.php 14 KB

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