DirectoryTest.php 12 KB

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