You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectoryTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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\Storage\Wrapper\Quota;
  32. use OCA\DAV\Connector\Sabre\Directory;
  33. use OCP\Files\ForbiddenException;
  34. use OCP\Files\Mount\IMountPoint;
  35. class TestViewDirectory extends \OC\Files\View {
  36. private $updatables;
  37. private $deletables;
  38. private $canRename;
  39. public function __construct($updatables, $deletables, $canRename = true) {
  40. $this->updatables = $updatables;
  41. $this->deletables = $deletables;
  42. $this->canRename = $canRename;
  43. }
  44. public function isUpdatable($path) {
  45. return $this->updatables[$path];
  46. }
  47. public function isCreatable($path) {
  48. return $this->updatables[$path];
  49. }
  50. public function isDeletable($path) {
  51. return $this->deletables[$path];
  52. }
  53. public function rename($path1, $path2) {
  54. return $this->canRename;
  55. }
  56. public function getRelativePath($path) {
  57. return $path;
  58. }
  59. }
  60. /**
  61. * @group DB
  62. */
  63. class DirectoryTest extends \Test\TestCase {
  64. /** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */
  65. private $view;
  66. /** @var \OC\Files\FileInfo | \PHPUnit\Framework\MockObject\MockObject */
  67. private $info;
  68. protected function setUp(): void {
  69. parent::setUp();
  70. $this->view = $this->createMock('OC\Files\View');
  71. $this->info = $this->createMock('OC\Files\FileInfo');
  72. $this->info->expects($this->any())
  73. ->method('isReadable')
  74. ->willReturn(true);
  75. }
  76. private function getDir($path = '/') {
  77. $this->view->expects($this->once())
  78. ->method('getRelativePath')
  79. ->willReturn($path);
  80. $this->info->expects($this->once())
  81. ->method('getPath')
  82. ->willReturn($path);
  83. return new Directory($this->view, $this->info);
  84. }
  85. public function testDeleteRootFolderFails() {
  86. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  87. $this->info->expects($this->any())
  88. ->method('isDeletable')
  89. ->willReturn(true);
  90. $this->view->expects($this->never())
  91. ->method('rmdir');
  92. $dir = $this->getDir();
  93. $dir->delete();
  94. }
  95. public function testDeleteForbidden() {
  96. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\Forbidden::class);
  97. // deletion allowed
  98. $this->info->expects($this->once())
  99. ->method('isDeletable')
  100. ->willReturn(true);
  101. // but fails
  102. $this->view->expects($this->once())
  103. ->method('rmdir')
  104. ->with('sub')
  105. ->willThrowException(new ForbiddenException('', true));
  106. $dir = $this->getDir('sub');
  107. $dir->delete();
  108. }
  109. public function testDeleteFolderWhenAllowed() {
  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. ->willReturn(true);
  119. $dir = $this->getDir('sub');
  120. $dir->delete();
  121. }
  122. public function testDeleteFolderFailsWhenNotAllowed() {
  123. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  124. $this->info->expects($this->once())
  125. ->method('isDeletable')
  126. ->willReturn(false);
  127. $dir = $this->getDir('sub');
  128. $dir->delete();
  129. }
  130. public function testDeleteFolderThrowsWhenDeletionFailed() {
  131. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  132. // deletion allowed
  133. $this->info->expects($this->once())
  134. ->method('isDeletable')
  135. ->willReturn(true);
  136. // but fails
  137. $this->view->expects($this->once())
  138. ->method('rmdir')
  139. ->with('sub')
  140. ->willReturn(false);
  141. $dir = $this->getDir('sub');
  142. $dir->delete();
  143. }
  144. public function testGetChildren() {
  145. $info1 = $this->getMockBuilder(FileInfo::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $info2 = $this->getMockBuilder(FileInfo::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $info1->expects($this->any())
  152. ->method('getName')
  153. ->willReturn('first');
  154. $info1->expects($this->any())
  155. ->method('getEtag')
  156. ->willReturn('abc');
  157. $info2->expects($this->any())
  158. ->method('getName')
  159. ->willReturn('second');
  160. $info2->expects($this->any())
  161. ->method('getEtag')
  162. ->willReturn('def');
  163. $this->view->expects($this->once())
  164. ->method('getDirectoryContent')
  165. ->with('')
  166. ->willReturn([$info1, $info2]);
  167. $this->view->expects($this->any())
  168. ->method('getRelativePath')
  169. ->willReturn('');
  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() {
  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() {
  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() {
  195. $this->expectException(\Sabre\DAV\Exception\ServiceUnavailable::class);
  196. $this->view->expects($this->once())
  197. ->method('getFileInfo')
  198. ->willThrowException(new \OCP\Files\StorageNotAvailableException());
  199. $dir = new Directory($this->view, $this->info);
  200. $dir->getChild('.');
  201. }
  202. public function testGetChildThrowInvalidPath() {
  203. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
  204. $this->view->expects($this->once())
  205. ->method('verifyPath')
  206. ->willThrowException(new \OCP\Files\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() {
  213. $mountPoint = $this->createMock(IMountPoint::class);
  214. $storage = $this->getMockBuilder(Quota::class)
  215. ->disableOriginalConstructor()
  216. ->getMock();
  217. $mountPoint->method('getStorage')
  218. ->willReturn($storage);
  219. $storage->expects($this->any())
  220. ->method('instanceOfStorage')
  221. ->willReturnMap([
  222. '\OCA\Files_Sharing\SharedStorage' => false,
  223. '\OC\Files\Storage\Wrapper\Quota' => false,
  224. ]);
  225. $storage->expects($this->never())
  226. ->method('getQuota');
  227. $storage->expects($this->once())
  228. ->method('free_space')
  229. ->willReturn(800);
  230. $this->info->expects($this->once())
  231. ->method('getSize')
  232. ->willReturn(200);
  233. $this->info->expects($this->once())
  234. ->method('getMountPoint')
  235. ->willReturn($mountPoint);
  236. $this->view->expects($this->once())
  237. ->method('getFileInfo')
  238. ->willReturn($this->info);
  239. $mountPoint->method('getMountPoint')
  240. ->willReturn('/user/files/mymountpoint');
  241. $dir = new Directory($this->view, $this->info);
  242. $this->assertEquals([200, -3], $dir->getQuotaInfo()); //200 used, unlimited
  243. }
  244. public function testGetQuotaInfoSpecific() {
  245. $mountPoint = $this->createMock(IMountPoint::class);
  246. $storage = $this->getMockBuilder(Quota::class)
  247. ->disableOriginalConstructor()
  248. ->getMock();
  249. $mountPoint->method('getStorage')
  250. ->willReturn($storage);
  251. $storage->expects($this->any())
  252. ->method('instanceOfStorage')
  253. ->willReturnMap([
  254. ['\OCA\Files_Sharing\SharedStorage', false],
  255. ['\OC\Files\Storage\Wrapper\Quota', true],
  256. ]);
  257. $storage->expects($this->once())
  258. ->method('getQuota')
  259. ->willReturn(1000);
  260. $storage->expects($this->once())
  261. ->method('free_space')
  262. ->willReturn(800);
  263. $this->info->expects($this->once())
  264. ->method('getSize')
  265. ->willReturn(200);
  266. $this->info->expects($this->once())
  267. ->method('getMountPoint')
  268. ->willReturn($mountPoint);
  269. $mountPoint->method('getMountPoint')
  270. ->willReturn('/user/files/mymountpoint');
  271. $this->view->expects($this->once())
  272. ->method('getFileInfo')
  273. ->willReturn($this->info);
  274. $dir = new Directory($this->view, $this->info);
  275. $this->assertEquals([200, 800], $dir->getQuotaInfo()); //200 used, 800 free
  276. }
  277. /**
  278. * @dataProvider moveFailedProvider
  279. */
  280. public function testMoveFailed($source, $destination, $updatables, $deletables) {
  281. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  282. $this->moveTest($source, $destination, $updatables, $deletables);
  283. }
  284. /**
  285. * @dataProvider moveSuccessProvider
  286. */
  287. public function testMoveSuccess($source, $destination, $updatables, $deletables) {
  288. $this->moveTest($source, $destination, $updatables, $deletables);
  289. $this->addToAssertionCount(1);
  290. }
  291. /**
  292. * @dataProvider moveFailedInvalidCharsProvider
  293. */
  294. public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) {
  295. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
  296. $this->moveTest($source, $destination, $updatables, $deletables);
  297. }
  298. public function moveFailedInvalidCharsProvider() {
  299. return [
  300. ['a/b', 'a/*', ['a' => true, 'a/b' => true, 'a/c*' => false], []],
  301. ];
  302. }
  303. public function moveFailedProvider() {
  304. return [
  305. ['a/b', 'a/c', ['a' => false, 'a/b' => false, 'a/c' => false], []],
  306. ['a/b', 'b/b', ['a' => false, 'a/b' => false, 'b' => false, 'b/b' => false], []],
  307. ['a/b', 'b/b', ['a' => false, 'a/b' => true, 'b' => false, 'b/b' => false], []],
  308. ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => false, 'b/b' => false], []],
  309. ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => false]],
  310. ['a/b', 'a/c', ['a' => false, 'a/b' => true, 'a/c' => false], []],
  311. ];
  312. }
  313. public function moveSuccessProvider() {
  314. return [
  315. ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => true]],
  316. // older files with special chars can still be renamed to valid names
  317. ['a/b*', 'b/b', ['a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false], ['a/b*' => true]],
  318. ];
  319. }
  320. /**
  321. * @param $source
  322. * @param $destination
  323. * @param $updatables
  324. */
  325. private function moveTest($source, $destination, $updatables, $deletables) {
  326. $view = new TestViewDirectory($updatables, $deletables);
  327. $sourceInfo = new FileInfo($source, null, null, [], null);
  328. $targetInfo = new FileInfo(dirname($destination), null, null, [], null);
  329. $sourceNode = new Directory($view, $sourceInfo);
  330. $targetNode = $this->getMockBuilder(Directory::class)
  331. ->setMethods(['childExists'])
  332. ->setConstructorArgs([$view, $targetInfo])
  333. ->getMock();
  334. $targetNode->expects($this->any())->method('childExists')
  335. ->with(basename($destination))
  336. ->willReturn(false);
  337. $this->assertTrue($targetNode->moveInto(basename($destination), $source, $sourceNode));
  338. }
  339. public function testFailingMove() {
  340. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  341. $this->expectExceptionMessage('Could not copy directory b, target exists');
  342. $source = 'a/b';
  343. $destination = 'c/b';
  344. $updatables = ['a' => true, 'a/b' => true, 'b' => true, 'c/b' => false];
  345. $deletables = ['a/b' => true];
  346. $view = new TestViewDirectory($updatables, $deletables);
  347. $sourceInfo = new FileInfo($source, null, null, [], null);
  348. $targetInfo = new FileInfo(dirname($destination), null, null, [], null);
  349. $sourceNode = new Directory($view, $sourceInfo);
  350. $targetNode = $this->getMockBuilder(Directory::class)
  351. ->setMethods(['childExists'])
  352. ->setConstructorArgs([$view, $targetInfo])
  353. ->getMock();
  354. $targetNode->expects($this->once())->method('childExists')
  355. ->with(basename($destination))
  356. ->willReturn(true);
  357. $targetNode->moveInto(basename($destination), $source, $sourceNode);
  358. }
  359. }