123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\Files_Sharing\Tests;
- use OC\Files\View;
- use OCA\Files_Sharing\SharedStorage;
- use OCA\Files_Trashbin\AppInfo\Application;
- use OCP\AppFramework\Bootstrap\IBootContext;
- use OCP\Files\NotFoundException;
- use OCP\Share\IShare;
- /**
- * Class SharedStorageTest
- *
- * @group DB
- */
- class SharedStorageTest extends TestCase {
- protected function setUp(): void {
- parent::setUp();
- // register trashbin hooks
- $trashbinApp = new Application();
- $trashbinApp->boot($this->createMock(IBootContext::class));
- $this->folder = '/folder_share_storage_test';
- $this->filename = '/share-api-storage.txt';
- $this->view->mkdir($this->folder);
- // save file with content
- $this->view->file_put_contents($this->filename, "root file");
- $this->view->file_put_contents($this->folder . $this->filename, "file in subfolder");
- }
- protected function tearDown(): void {
- if ($this->view) {
- if ($this->view->file_exists($this->folder)) {
- $this->view->unlink($this->folder);
- }
- if ($this->view->file_exists($this->filename)) {
- $this->view->unlink($this->filename);
- }
- }
- \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
- parent::tearDown();
- }
- /**
- * if the parent of the mount point is gone then the mount point should move up
- *
- * @medium
- */
- public function testParentOfMountPointIsGone() {
- // share to user
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($user2View->file_exists($this->folder));
- // create a local folder
- $result = $user2View->mkdir('localfolder');
- $this->assertTrue($result);
- // move mount point to local folder
- $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
- $this->assertTrue($result);
- // mount point in the root folder should no longer exist
- $this->assertFalse($user2View->is_dir($this->folder));
- // delete the local folder
- /** @var \OC\Files\Storage\Storage $storage */
- [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder');
- $storage->rmdir($internalPath);
- //enforce reload of the mount points
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- //mount point should be back at the root
- $this->assertTrue($user2View->is_dir($this->folder));
- //cleanup
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->view->unlink($this->folder);
- }
- /**
- * @medium
- */
- public function testRenamePartFile() {
- // share to user
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($user2View->file_exists($this->folder));
- // create part file
- $result = $user2View->file_put_contents($this->folder . '/foo.txt.part', 'some test data');
- $this->assertTrue(is_int($result));
- // rename part file to real file
- $result = $user2View->rename($this->folder . '/foo.txt.part', $this->folder . '/foo.txt');
- $this->assertTrue($result);
- // check if the new file really exists
- $this->assertTrue($user2View->file_exists($this->folder . '/foo.txt'));
- // check if the rename also affected the owner
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->assertTrue($this->view->file_exists($this->folder . '/foo.txt'));
- //cleanup
- $this->shareManager->deleteShare($share);
- }
- public function testFilesize() {
- $folderSize = $this->view->filesize($this->folder);
- $file1Size = $this->view->filesize($this->folder . $this->filename);
- $file2Size = $this->view->filesize($this->filename);
- $share1 = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- $share2 = $this->share(
- IShare::TYPE_USER,
- $this->filename,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- // compare file size between user1 and user2, should always be the same
- $this->assertSame($folderSize, \OC\Files\Filesystem::filesize($this->folder));
- $this->assertSame($file1Size, \OC\Files\Filesystem::filesize($this->folder . $this->filename));
- $this->assertSame($file2Size, \OC\Files\Filesystem::filesize($this->filename));
- //cleanup
- $this->shareManager->deleteShare($share1);
- $this->shareManager->deleteShare($share2);
- }
- public function testGetPermissions() {
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->assertTrue(\OC\Files\Filesystem::is_dir($this->folder));
- // for the share root we expect:
- // the read permissions (1)
- // the delete permission (8), to enable unshare
- $rootInfo = \OC\Files\Filesystem::getFileInfo($this->folder);
- $this->assertSame(9, $rootInfo->getPermissions());
- // for the file within the shared folder we expect:
- // the read permissions (1)
- $subfileInfo = \OC\Files\Filesystem::getFileInfo($this->folder . $this->filename);
- $this->assertSame(1, $subfileInfo->getPermissions());
- //cleanup
- $this->shareManager->deleteShare($share);
- }
- public function testFopenWithReadOnlyPermission() {
- $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- // part file should be forbidden
- $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
- $this->assertFalse($handle);
- // regular file forbidden
- $handle = $user2View->fopen($this->folder . '/test.txt', 'w');
- $this->assertFalse($handle);
- // rename forbidden
- $this->assertFalse($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing2.txt'));
- // delete forbidden
- $this->assertFalse($user2View->unlink($this->folder . '/existing.txt'));
- //cleanup
- $this->shareManager->deleteShare($share);
- }
- public function testFopenWithCreateOnlyPermission() {
- $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
- $fileinfoFolder = $this->view->getFileInfo($this->folder);
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- // create part file allowed
- $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
- $this->assertNotFalse($handle);
- fclose($handle);
- // create regular file allowed
- $handle = $user2View->fopen($this->folder . '/test-create.txt', 'w');
- $this->assertNotFalse($handle);
- fclose($handle);
- // rename file never allowed
- $this->assertFalse($user2View->rename($this->folder . '/test-create.txt', $this->folder . '/newtarget.txt'));
- $this->assertFalse($user2View->file_exists($this->folder . '/newtarget.txt'));
- // rename file not allowed if target exists
- $this->assertFalse($user2View->rename($this->folder . '/newtarget.txt', $this->folder . '/existing.txt'));
- // overwriting file not allowed
- $handle = $user2View->fopen($this->folder . '/existing.txt', 'w');
- $this->assertFalse($handle);
- // overwrite forbidden (no update permission)
- $this->assertFalse($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/existing.txt'));
- // delete forbidden
- $this->assertFalse($user2View->unlink($this->folder . '/existing.txt'));
- //cleanup
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->shareManager->deleteShare($share);
- }
- public function testFopenWithUpdateOnlyPermission() {
- $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- // create part file allowed
- $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
- $this->assertNotFalse($handle);
- fclose($handle);
- // create regular file not allowed
- $handle = $user2View->fopen($this->folder . '/test-create.txt', 'w');
- $this->assertFalse($handle);
- // rename part file not allowed to non-existing file
- $this->assertFalse($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/nonexist.txt'));
- // rename part file allowed to target existing file
- $this->assertTrue($user2View->rename($this->folder . '/test.txt.part', $this->folder . '/existing.txt'));
- $this->assertTrue($user2View->file_exists($this->folder . '/existing.txt'));
- // rename regular file allowed
- $this->assertTrue($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing-renamed.txt'));
- $this->assertTrue($user2View->file_exists($this->folder . '/existing-renamed.txt'));
- // overwriting file directly is allowed
- $handle = $user2View->fopen($this->folder . '/existing-renamed.txt', 'w');
- $this->assertNotFalse($handle);
- fclose($handle);
- // delete forbidden
- $this->assertFalse($user2View->unlink($this->folder . '/existing-renamed.txt'));
- //cleanup
- $this->shareManager->deleteShare($share);
- }
- public function testFopenWithDeleteOnlyPermission() {
- $this->view->file_put_contents($this->folder . '/existing.txt', 'foo');
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- // part file should be forbidden
- $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w');
- $this->assertFalse($handle);
- // regular file forbidden
- $handle = $user2View->fopen($this->folder . '/test.txt', 'w');
- $this->assertFalse($handle);
- // rename forbidden
- $this->assertFalse($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing2.txt'));
- // delete allowed
- $this->assertTrue($user2View->unlink($this->folder . '/existing.txt'));
- //cleanup
- $this->shareManager->deleteShare($share);
- }
- public function testMountSharesOtherUser() {
- $rootView = new \OC\Files\View('');
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- // share 2 different files with 2 different users
- $share1 = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- $share2 = $this->share(
- IShare::TYPE_USER,
- $this->filename,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER3,
- \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder));
- $mountConfigManager = \OC::$server->getMountProviderCollection();
- $mounts = $mountConfigManager->getMountsForUser(\OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3));
- array_walk($mounts, [\OC\Files\Filesystem::getMountManager(), 'addMount']);
- $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER3 . '/files/' . $this->filename));
- // make sure we didn't double setup shares for user 2 or mounted the shares for user 3 in user's 2 home
- $this->assertFalse($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder . ' (2)'));
- $this->assertFalse($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->filename));
- //cleanup
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->view->unlink($this->folder);
- $this->shareManager->deleteShare($share1);
- $this->shareManager->deleteShare($share2);
- }
- public function testCopyFromStorage() {
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($view->file_exists($this->folder));
- /**
- * @var \OCP\Files\Storage $sharedStorage
- */
- [$sharedStorage,] = $view->resolvePath($this->folder);
- $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));
- $sourceStorage = new \OC\Files\Storage\Temporary([]);
- $sourceStorage->file_put_contents('foo.txt', 'asd');
- $sharedStorage->copyFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
- $this->assertTrue($sharedStorage->file_exists('bar.txt'));
- $this->assertEquals('asd', $sharedStorage->file_get_contents('bar.txt'));
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->view->unlink($this->folder);
- $this->shareManager->deleteShare($share);
- }
- public function testMoveFromStorage() {
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($view->file_exists($this->folder));
- /**
- * @var \OCP\Files\Storage $sharedStorage
- */
- [$sharedStorage,] = $view->resolvePath($this->folder);
- $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));
- $sourceStorage = new \OC\Files\Storage\Temporary([]);
- $sourceStorage->file_put_contents('foo.txt', 'asd');
- $sharedStorage->moveFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
- $this->assertTrue($sharedStorage->file_exists('bar.txt'));
- $this->assertEquals('asd', $sharedStorage->file_get_contents('bar.txt'));
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->view->unlink($this->folder);
- $this->shareManager->deleteShare($share);
- }
- public function testNameConflict() {
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $view1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
- $view1->mkdir('foo');
- self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
- $view3 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
- $view3->mkdir('foo');
- // share a folder with the same name from two different users to the same user
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $share1 = $this->share(
- IShare::TYPE_GROUP,
- 'foo',
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_GROUP1,
- \OCP\Constants::PERMISSION_ALL
- );
- $this->shareManager->acceptShare($share1, self::TEST_FILES_SHARING_API_USER2);
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
- $share2 = $this->share(
- IShare::TYPE_GROUP,
- 'foo',
- self::TEST_FILES_SHARING_API_USER3,
- self::TEST_FILES_SHARING_API_GROUP1,
- \OCP\Constants::PERMISSION_ALL
- );
- $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($view2->file_exists('/foo'));
- $this->assertTrue($view2->file_exists('/foo (2)'));
- $mount = $view2->getMount('/foo');
- $this->assertInstanceOf('\OCA\Files_Sharing\SharedMount', $mount);
- /** @var \OCA\Files_Sharing\SharedStorage $storage */
- $storage = $mount->getStorage();
- $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $storage->getOwner(''));
- $this->shareManager->deleteShare($share1);
- $this->shareManager->deleteShare($share2);
- }
- public function testOwnerPermissions() {
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $share = $this->share(
- IShare::TYPE_USER,
- $this->folder,
- self::TEST_FILES_SHARING_API_USER1,
- self::TEST_FILES_SHARING_API_USER2,
- \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE
- );
- self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
- $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
- $this->assertTrue($view->file_exists($this->folder));
- $view->file_put_contents($this->folder . '/newfile.txt', 'asd');
- self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
- $this->assertTrue($this->view->file_exists($this->folder . '/newfile.txt'));
- $this->assertEquals(\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
- $this->view->getFileInfo($this->folder . '/newfile.txt')->getPermissions());
- $this->view->unlink($this->folder);
- $this->shareManager->deleteShare($share);
- }
- public function testInitWithNonExistingUser() {
- $share = $this->createMock(IShare::class);
- $share->method('getShareOwner')->willReturn('unexist');
- $ownerView = $this->createMock(View::class);
- $storage = new SharedStorage([
- 'ownerView' => $ownerView,
- 'superShare' => $share,
- 'groupedShares' => [$share],
- 'user' => 'user1',
- ]);
- // trigger init
- $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
- $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
- }
- public function testInitWithNotFoundSource() {
- $share = $this->createMock(IShare::class);
- $share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1);
- $share->method('getNodeId')->willReturn(1);
- $ownerView = $this->createMock(View::class);
- $ownerView->method('getPath')->will($this->throwException(new NotFoundException()));
- $storage = new SharedStorage([
- 'ownerView' => $ownerView,
- 'superShare' => $share,
- 'groupedShares' => [$share],
- 'user' => 'user1',
- ]);
- // trigger init
- $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
- $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
- }
- }
|