SharedMountTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  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 <pvince81@owncloud.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\Files_Sharing\Tests;
  30. use OCP\IGroupManager;
  31. use OCP\IUserManager;
  32. /**
  33. * Class SharedMountTest
  34. *
  35. * @group SLOWDB
  36. */
  37. class SharedMountTest extends TestCase {
  38. /** @var IGroupManager */
  39. private $groupManager;
  40. /** @var IUserManager */
  41. private $userManager;
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->folder = '/folder_share_storage_test';
  45. $this->filename = '/share-api-storage.txt';
  46. $this->view->mkdir($this->folder);
  47. // save file with content
  48. $this->view->file_put_contents($this->filename, 'root file');
  49. $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder');
  50. $this->groupManager = \OC::$server->getGroupManager();
  51. $this->userManager = \OC::$server->getUserManager();
  52. }
  53. protected function tearDown() {
  54. if ($this->view) {
  55. if ($this->view->file_exists($this->folder)) {
  56. $this->view->unlink($this->folder);
  57. }
  58. if ($this->view->file_exists($this->filename)) {
  59. $this->view->unlink($this->filename);
  60. }
  61. }
  62. parent::tearDown();
  63. }
  64. /**
  65. * test if the mount point moves up if the parent folder no longer exists
  66. */
  67. public function testShareMountLoseParentFolder() {
  68. // share to user
  69. $share = $this->share(
  70. \OCP\Share::SHARE_TYPE_USER,
  71. $this->folder,
  72. self::TEST_FILES_SHARING_API_USER1,
  73. self::TEST_FILES_SHARING_API_USER2,
  74. \OCP\Constants::PERMISSION_ALL);
  75. $share->setTarget('/foo/bar' . $this->folder);
  76. $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
  77. $share = $this->shareManager->getShareById($share->getFullId());
  78. $this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
  79. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  80. // share should have moved up
  81. $share = $this->shareManager->getShareById($share->getFullId());
  82. $this->assertSame($this->folder, $share->getTarget());
  83. //cleanup
  84. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  85. $this->shareManager->deleteShare($share);
  86. $this->view->unlink($this->folder);
  87. }
  88. /**
  89. * @medium
  90. */
  91. public function testDeleteParentOfMountPoint() {
  92. // share to user
  93. $share = $this->share(
  94. \OCP\Share::SHARE_TYPE_USER,
  95. $this->folder,
  96. self::TEST_FILES_SHARING_API_USER1,
  97. self::TEST_FILES_SHARING_API_USER2,
  98. \OCP\Constants::PERMISSION_ALL
  99. );
  100. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  101. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  102. $this->assertTrue($user2View->file_exists($this->folder));
  103. // create a local folder
  104. $result = $user2View->mkdir('localfolder');
  105. $this->assertTrue($result);
  106. // move mount point to local folder
  107. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  108. $this->assertTrue($result);
  109. // mount point in the root folder should no longer exist
  110. $this->assertFalse($user2View->is_dir($this->folder));
  111. // delete the local folder
  112. $result = $user2View->unlink('/localfolder');
  113. $this->assertTrue($result);
  114. //enforce reload of the mount points
  115. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  116. //mount point should be back at the root
  117. $this->assertTrue($user2View->is_dir($this->folder));
  118. //cleanup
  119. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  120. $this->view->unlink($this->folder);
  121. }
  122. public function testMoveSharedFile() {
  123. $share = $this->share(
  124. \OCP\Share::SHARE_TYPE_USER,
  125. $this->filename,
  126. self::TEST_FILES_SHARING_API_USER1,
  127. self::TEST_FILES_SHARING_API_USER2,
  128. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  129. );
  130. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  131. \OC\Files\Filesystem::rename($this->filename, $this->filename . '_renamed');
  132. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  133. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  134. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  135. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  136. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  137. // rename back to original name
  138. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  139. \OC\Files\Filesystem::rename($this->filename . '_renamed', $this->filename);
  140. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  141. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  142. //cleanup
  143. $this->shareManager->deleteShare($share);
  144. }
  145. /**
  146. * share file with a group if a user renames the file the filename should not change
  147. * for the other users
  148. */
  149. public function testMoveGroupShare () {
  150. $testGroup = $this->groupManager->createGroup('testGroup');
  151. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  152. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  153. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  154. $testGroup->addUser($user1);
  155. $testGroup->addUser($user2);
  156. $testGroup->addUser($user3);
  157. $fileinfo = $this->view->getFileInfo($this->filename);
  158. $share = $this->share(
  159. \OCP\Share::SHARE_TYPE_GROUP,
  160. $this->filename,
  161. self::TEST_FILES_SHARING_API_USER1,
  162. 'testGroup',
  163. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  164. );
  165. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  166. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  167. \OC\Files\Filesystem::rename($this->filename, 'newFileName');
  168. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  169. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  170. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  171. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  172. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  173. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  174. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  175. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  176. //cleanup
  177. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  178. $this->shareManager->deleteShare($share);
  179. $testGroup->removeUser($user1);
  180. $testGroup->removeUser($user2);
  181. $testGroup->removeUser($user3);
  182. }
  183. /**
  184. * @dataProvider dataProviderTestStripUserFilesPath
  185. * @param string $path
  186. * @param string $expectedResult
  187. * @param bool $exception if a exception is expected
  188. */
  189. public function testStripUserFilesPath($path, $expectedResult, $exception) {
  190. $testClass = new DummyTestClassSharedMount(null, null);
  191. try {
  192. $result = $testClass->stripUserFilesPathDummy($path);
  193. $this->assertSame($expectedResult, $result);
  194. } catch (\Exception $e) {
  195. if ($exception) {
  196. $this->assertSame(10, $e->getCode());
  197. } else {
  198. $this->assertTrue(false, 'Exception catched, but expected: ' . $expectedResult);
  199. }
  200. }
  201. }
  202. public function dataProviderTestStripUserFilesPath() {
  203. return array(
  204. array('/user/files/foo.txt', '/foo.txt', false),
  205. array('/user/files/folder/foo.txt', '/folder/foo.txt', false),
  206. array('/data/user/files/foo.txt', null, true),
  207. array('/data/user/files/', null, true),
  208. array('/files/foo.txt', null, true),
  209. array('/foo.txt', null, true),
  210. );
  211. }
  212. public function dataPermissionMovedGroupShare() {
  213. $data = [];
  214. $powerset = function($permissions) {
  215. $results = [\OCP\Constants::PERMISSION_READ];
  216. foreach ($permissions as $permission) {
  217. foreach ($results as $combination) {
  218. $results[] = $permission | $combination;
  219. }
  220. }
  221. return $results;
  222. };
  223. //Generate file permissions
  224. $permissions = [
  225. \OCP\Constants::PERMISSION_UPDATE,
  226. \OCP\Constants::PERMISSION_SHARE,
  227. ];
  228. $allPermissions = $powerset($permissions);
  229. foreach ($allPermissions as $before) {
  230. foreach ($allPermissions as $after) {
  231. if ($before === $after) { continue; }
  232. $data[] = [
  233. 'file',
  234. $before,
  235. $after,
  236. ];
  237. }
  238. }
  239. //Generate folder permissions
  240. $permissions = [
  241. \OCP\Constants::PERMISSION_UPDATE,
  242. \OCP\Constants::PERMISSION_CREATE,
  243. \OCP\Constants::PERMISSION_SHARE,
  244. \OCP\Constants::PERMISSION_DELETE,
  245. ];
  246. $allPermissions = $powerset($permissions);
  247. foreach ($allPermissions as $before) {
  248. foreach ($allPermissions as $after) {
  249. if ($before === $after) { continue; }
  250. $data[] = [
  251. 'folder',
  252. $before,
  253. $after,
  254. ];
  255. }
  256. }
  257. return $data;
  258. }
  259. /**
  260. * moved mountpoints of a group share should keep the same permission as their parent group share.
  261. * See #15253
  262. *
  263. * @dataProvider dataPermissionMovedGroupShare
  264. */
  265. public function testPermissionMovedGroupShare($type, $beforePerm, $afterPerm) {
  266. if ($type === 'file') {
  267. $path = $this->filename;
  268. } else if ($type === 'folder') {
  269. $path = $this->folder;
  270. }
  271. $testGroup = $this->groupManager->createGroup('testGroup');
  272. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  273. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  274. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  275. $testGroup->addUser($user1);
  276. $testGroup->addUser($user2);
  277. $testGroup->addUser($user3);
  278. // Share item with group
  279. $share = $this->share(
  280. \OCP\Share::SHARE_TYPE_GROUP,
  281. $path,
  282. self::TEST_FILES_SHARING_API_USER1,
  283. 'testGroup',
  284. $beforePerm
  285. );
  286. // Login as user 2 and verify the item exists
  287. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  288. $this->assertTrue(\OC\Files\Filesystem::file_exists($path));
  289. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  290. $this->assertEquals($beforePerm, $result->getPermissions());
  291. // Now move the item forcing a new entry in the share table
  292. \OC\Files\Filesystem::rename($path, 'newPath');
  293. $this->assertTrue(\OC\Files\Filesystem::file_exists('newPath'));
  294. $this->assertFalse(\OC\Files\Filesystem::file_exists($path));
  295. // change permissions
  296. $share->setPermissions($afterPerm);
  297. $this->shareManager->updateShare($share);
  298. // Login as user 3 and verify that the permissions are changed
  299. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  300. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER3);
  301. $this->assertNotEmpty($result);
  302. $this->assertEquals($afterPerm, $result->getPermissions());
  303. // Login as user 2 and verify that the permissions are changed
  304. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  305. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  306. $this->assertNotEmpty($result);
  307. $this->assertEquals($afterPerm, $result->getPermissions());
  308. $this->assertEquals('/newPath', $result->getTarget());
  309. //cleanup
  310. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  311. $this->shareManager->deleteShare($share);
  312. $testGroup->removeUser($user1);
  313. $testGroup->removeUser($user2);
  314. $testGroup->removeUser($user3);
  315. }
  316. /**
  317. * If the permissions on a group share are upgraded be sure to still respect
  318. * removed shares by a member of that group
  319. */
  320. public function testPermissionUpgradeOnUserDeletedGroupShare() {
  321. $testGroup = $this->groupManager->createGroup('testGroup');
  322. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  323. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  324. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  325. $testGroup->addUser($user1);
  326. $testGroup->addUser($user2);
  327. $testGroup->addUser($user3);
  328. $connection = \OC::$server->getDatabaseConnection();
  329. // Share item with group
  330. $fileinfo = $this->view->getFileInfo($this->folder);
  331. $share = $this->share(
  332. \OCP\Share::SHARE_TYPE_GROUP,
  333. $this->folder,
  334. self::TEST_FILES_SHARING_API_USER1,
  335. 'testGroup',
  336. \OCP\Constants::PERMISSION_READ
  337. );
  338. // Login as user 2 and verify the item exists
  339. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  340. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
  341. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  342. $this->assertNotEmpty($result);
  343. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result->getPermissions());
  344. // Delete the share
  345. $this->assertTrue(\OC\Files\Filesystem::rmdir($this->folder));
  346. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  347. // Verify we do not get a share
  348. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  349. $this->assertEquals(0, $result->getPermissions());
  350. // Login as user 1 again and change permissions
  351. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  352. $share->setPermissions(\OCP\Constants::PERMISSION_ALL);
  353. $share = $this->shareManager->updateShare($share);
  354. // Login as user 2 and verify
  355. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  356. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  357. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  358. $this->assertEquals(0, $result->getPermissions());
  359. $this->shareManager->deleteShare($share);
  360. //cleanup
  361. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  362. $testGroup->removeUser($user1);
  363. $testGroup->removeUser($user2);
  364. $testGroup->removeUser($user3);
  365. }
  366. }
  367. class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
  368. public function __construct($storage, $mountpoint, $arguments = null, $loader = null){
  369. // noop
  370. }
  371. public function stripUserFilesPathDummy($path) {
  372. return $this->stripUserFilesPath($path);
  373. }
  374. }