SharedMountTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Tests;
  31. use OCP\IGroupManager;
  32. use OCP\IUserManager;
  33. use OCP\Share\IShare;
  34. /**
  35. * Class SharedMountTest
  36. *
  37. * @group SLOWDB
  38. */
  39. class SharedMountTest extends TestCase {
  40. /** @var IGroupManager */
  41. private $groupManager;
  42. /** @var IUserManager */
  43. private $userManager;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->folder = '/folder_share_storage_test';
  47. $this->filename = '/share-api-storage.txt';
  48. $this->view->mkdir($this->folder);
  49. // save file with content
  50. $this->view->file_put_contents($this->filename, 'root file');
  51. $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder');
  52. $this->groupManager = \OC::$server->getGroupManager();
  53. $this->userManager = \OC::$server->getUserManager();
  54. }
  55. protected function tearDown(): void {
  56. if ($this->view) {
  57. if ($this->view->file_exists($this->folder)) {
  58. $this->view->unlink($this->folder);
  59. }
  60. if ($this->view->file_exists($this->filename)) {
  61. $this->view->unlink($this->filename);
  62. }
  63. }
  64. parent::tearDown();
  65. }
  66. /**
  67. * test if the mount point moves up if the parent folder no longer exists
  68. */
  69. public function testShareMountLoseParentFolder() {
  70. // share to user
  71. $share = $this->share(
  72. IShare::TYPE_USER,
  73. $this->folder,
  74. self::TEST_FILES_SHARING_API_USER1,
  75. self::TEST_FILES_SHARING_API_USER2,
  76. \OCP\Constants::PERMISSION_ALL);
  77. $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
  78. $share->setTarget('/foo/bar' . $this->folder);
  79. $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
  80. $share = $this->shareManager->getShareById($share->getFullId());
  81. $this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
  82. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  83. // share should have moved up
  84. $share = $this->shareManager->getShareById($share->getFullId());
  85. $this->assertSame($this->folder, $share->getTarget());
  86. //cleanup
  87. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  88. $this->shareManager->deleteShare($share);
  89. $this->view->unlink($this->folder);
  90. }
  91. /**
  92. * @medium
  93. */
  94. public function testDeleteParentOfMountPoint() {
  95. // share to user
  96. $share = $this->share(
  97. IShare::TYPE_USER,
  98. $this->folder,
  99. self::TEST_FILES_SHARING_API_USER1,
  100. self::TEST_FILES_SHARING_API_USER2,
  101. \OCP\Constants::PERMISSION_ALL
  102. );
  103. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  104. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  105. $this->assertTrue($user2View->file_exists($this->folder));
  106. // create a local folder
  107. $result = $user2View->mkdir('localfolder');
  108. $this->assertTrue($result);
  109. // move mount point to local folder
  110. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  111. $this->assertTrue($result);
  112. // mount point in the root folder should no longer exist
  113. $this->assertFalse($user2View->is_dir($this->folder));
  114. // delete the local folder
  115. $result = $user2View->unlink('/localfolder');
  116. $this->assertTrue($result);
  117. //enforce reload of the mount points
  118. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  119. //mount point should be back at the root
  120. $this->assertTrue($user2View->is_dir($this->folder));
  121. //cleanup
  122. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  123. $this->view->unlink($this->folder);
  124. }
  125. public function testMoveSharedFile() {
  126. $share = $this->share(
  127. IShare::TYPE_USER,
  128. $this->filename,
  129. self::TEST_FILES_SHARING_API_USER1,
  130. self::TEST_FILES_SHARING_API_USER2,
  131. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  132. );
  133. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  134. \OC\Files\Filesystem::rename($this->filename, $this->filename . '_renamed');
  135. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  136. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  137. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  138. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  139. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  140. // rename back to original name
  141. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  142. \OC\Files\Filesystem::rename($this->filename . '_renamed', $this->filename);
  143. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  144. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  145. //cleanup
  146. $this->shareManager->deleteShare($share);
  147. }
  148. /**
  149. * share file with a group if a user renames the file the filename should not change
  150. * for the other users
  151. */
  152. public function testMoveGroupShare() {
  153. $testGroup = $this->groupManager->createGroup('testGroup');
  154. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  155. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  156. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  157. $testGroup->addUser($user1);
  158. $testGroup->addUser($user2);
  159. $testGroup->addUser($user3);
  160. $fileinfo = $this->view->getFileInfo($this->filename);
  161. $share = $this->share(
  162. IShare::TYPE_GROUP,
  163. $this->filename,
  164. self::TEST_FILES_SHARING_API_USER1,
  165. 'testGroup',
  166. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  167. );
  168. $this->shareManager->acceptShare($share, $user1->getUID());
  169. $this->shareManager->acceptShare($share, $user2->getUID());
  170. $this->shareManager->acceptShare($share, $user3->getUID());
  171. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  172. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  173. \OC\Files\Filesystem::rename($this->filename, 'newFileName');
  174. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  175. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  176. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  177. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  178. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  179. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  180. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  181. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  182. //cleanup
  183. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  184. $this->shareManager->deleteShare($share);
  185. $testGroup->removeUser($user1);
  186. $testGroup->removeUser($user2);
  187. $testGroup->removeUser($user3);
  188. }
  189. /**
  190. * @dataProvider dataProviderTestStripUserFilesPath
  191. * @param string $path
  192. * @param string $expectedResult
  193. * @param bool $exception if a exception is expected
  194. */
  195. public function testStripUserFilesPath($path, $expectedResult, $exception) {
  196. $testClass = new DummyTestClassSharedMount(null, null);
  197. try {
  198. $result = $testClass->stripUserFilesPathDummy($path);
  199. $this->assertSame($expectedResult, $result);
  200. } catch (\Exception $e) {
  201. if ($exception) {
  202. $this->assertSame(10, $e->getCode());
  203. } else {
  204. $this->assertTrue(false, 'Exception caught, but expected: ' . $expectedResult);
  205. }
  206. }
  207. }
  208. public function dataProviderTestStripUserFilesPath() {
  209. return [
  210. ['/user/files/foo.txt', '/foo.txt', false],
  211. ['/user/files/folder/foo.txt', '/folder/foo.txt', false],
  212. ['/data/user/files/foo.txt', null, true],
  213. ['/data/user/files/', null, true],
  214. ['/files/foo.txt', null, true],
  215. ['/foo.txt', null, true],
  216. ];
  217. }
  218. /**
  219. * If the permissions on a group share are upgraded be sure to still respect
  220. * removed shares by a member of that group
  221. */
  222. public function testPermissionUpgradeOnUserDeletedGroupShare() {
  223. $testGroup = $this->groupManager->createGroup('testGroup');
  224. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  225. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  226. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  227. $testGroup->addUser($user1);
  228. $testGroup->addUser($user2);
  229. $testGroup->addUser($user3);
  230. $connection = \OC::$server->getDatabaseConnection();
  231. // Share item with group
  232. $fileinfo = $this->view->getFileInfo($this->folder);
  233. $share = $this->share(
  234. IShare::TYPE_GROUP,
  235. $this->folder,
  236. self::TEST_FILES_SHARING_API_USER1,
  237. 'testGroup',
  238. \OCP\Constants::PERMISSION_READ
  239. );
  240. $this->shareManager->acceptShare($share, $user1->getUID());
  241. $this->shareManager->acceptShare($share, $user2->getUID());
  242. $this->shareManager->acceptShare($share, $user3->getUID());
  243. // Login as user 2 and verify the item exists
  244. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  245. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
  246. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  247. $this->assertNotEmpty($result);
  248. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result->getPermissions());
  249. // Delete the share
  250. $this->assertTrue(\OC\Files\Filesystem::rmdir($this->folder));
  251. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  252. // Verify we do not get a share
  253. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  254. $this->assertEquals(0, $result->getPermissions());
  255. // Login as user 1 again and change permissions
  256. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  257. $share->setPermissions(\OCP\Constants::PERMISSION_ALL);
  258. $share = $this->shareManager->updateShare($share);
  259. // Login as user 2 and verify
  260. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  261. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  262. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  263. $this->assertEquals(0, $result->getPermissions());
  264. $this->shareManager->deleteShare($share);
  265. //cleanup
  266. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  267. $testGroup->removeUser($user1);
  268. $testGroup->removeUser($user2);
  269. $testGroup->removeUser($user3);
  270. }
  271. }
  272. class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
  273. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  274. // noop
  275. }
  276. public function stripUserFilesPathDummy($path) {
  277. return $this->stripUserFilesPath($path);
  278. }
  279. }