Merge pull request #4524 from nextcloud/downstream-27508

Keep file id on move
This commit is contained in:
Roeland Jago Douma 2017-04-28 09:37:40 +02:00 committed by GitHub
commit 9da697b11a
16 changed files with 680 additions and 297 deletions

View File

@ -34,12 +34,20 @@ use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCP\Files\ForbiddenException;
use OCP\Files\InvalidPathException;
use OCP\Files\StorageNotAvailableException;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Sabre\DAV\Exception\Locked;
use Sabre\DAV\Exception\ServiceUnavailable;
use Sabre\DAV\INode;
use Sabre\DAV\Exception\BadRequest;
use OC\Files\Mount\MoveableMount;
use Sabre\DAV\IFile;
use Sabre\DAV\Exception\NotFound;
class Directory extends \OCA\DAV\Connector\Sabre\Node
implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota {
implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota, \Sabre\DAV\IMoveTarget {
/**
* Cached directory content
@ -113,9 +121,9 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
// exit if we can't create a new file and we don't updatable existing file
$info = \OC_FileChunking::decodeName($name);
$chunkInfo = \OC_FileChunking::decodeName($name);
if (!$this->fileView->isCreatable($this->path) &&
!$this->fileView->isUpdatable($this->path . '/' . $info['name'])
!$this->fileView->isUpdatable($this->path . '/' . $chunkInfo['name'])
) {
throw new \Sabre\DAV\Exception\Forbidden();
}
@ -130,14 +138,18 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
$this->fileView->verifyPath($this->path, $name);
$path = $this->fileView->getAbsolutePath($this->path) . '/' . $name;
// using a dummy FileInfo is acceptable here since it will be refreshed after the put is complete
$info = new \OC\Files\FileInfo($path, null, null, array(), null);
// in case the file already exists/overwriting
$info = $this->fileView->getFileInfo($this->path . '/' . $name);
if (!$info) {
// use a dummy FileInfo which is acceptable here since it will be refreshed after the put is complete
$info = new \OC\Files\FileInfo($path, null, null, [], null);
}
$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
$node->acquireLock(ILockingProvider::LOCK_SHARED);
return $node->put($data);
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
@ -168,7 +180,7 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
}
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
@ -188,6 +200,11 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
* @throws \Sabre\DAV\Exception\ServiceUnavailable
*/
public function getChild($name, $info = null) {
if (!$this->info->isReadable()) {
// avoid detecting files through this way
throw new NotFound();
}
$path = $this->path . '/' . $name;
if (is_null($info)) {
try {
@ -195,7 +212,7 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
$info = $this->fileView->getFileInfo($path);
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (ForbiddenException $e) {
throw new \Sabre\DAV\Exception\Forbidden();
@ -221,12 +238,19 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
* Returns an array with all the child nodes
*
* @return \Sabre\DAV\INode[]
* @throws \Sabre\DAV\Exception\Locked
* @throws \OCA\DAV\Connector\Sabre\Exception\Forbidden
*/
public function getChildren() {
if (!is_null($this->dirContent)) {
return $this->dirContent;
}
try {
if (!$this->info->isReadable()) {
// return 403 instead of 404 because a 404 would make
// the caller believe that the collection itself does not exist
throw new Forbidden('No read permissions');
}
$folderContent = $this->fileView->getDirectoryContent($this->path);
} catch (LockedException $e) {
throw new Locked();
@ -311,4 +335,109 @@ class Directory extends \OCA\DAV\Connector\Sabre\Node
}
}
/**
* Moves a node into this collection.
*
* It is up to the implementors to:
* 1. Create the new resource.
* 2. Remove the old resource.
* 3. Transfer any properties or other data.
*
* Generally you should make very sure that your collection can easily move
* the move.
*
* If you don't, just return false, which will trigger sabre/dav to handle
* the move itself. If you return true from this function, the assumption
* is that the move was successful.
*
* @param string $targetName New local file/collection name.
* @param string $fullSourcePath Full path to source node
* @param INode $sourceNode Source node itself
* @return bool
* @throws BadRequest
* @throws ServiceUnavailable
* @throws Forbidden
* @throws FileLocked
* @throws \Sabre\DAV\Exception\Forbidden
*/
public function moveInto($targetName, $fullSourcePath, INode $sourceNode) {
if (!$sourceNode instanceof Node) {
// it's a file of another kind, like FutureFile
if ($sourceNode instanceof IFile) {
// fallback to default copy+delete handling
return false;
}
throw new BadRequest('Incompatible node types');
}
if (!$this->fileView) {
throw new ServiceUnavailable('filesystem not setup');
}
$destinationPath = $this->getPath() . '/' . $targetName;
$targetNodeExists = $this->childExists($targetName);
// at getNodeForPath we also check the path for isForbiddenFileOrDir
// with that we have covered both source and destination
if ($sourceNode instanceof Directory && $targetNodeExists) {
throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists');
}
list($sourceDir,) = \Sabre\HTTP\URLUtil::splitPath($sourceNode->getPath());
$destinationDir = $this->getPath();
$sourcePath = $sourceNode->getPath();
$isMovableMount = false;
$sourceMount = \OC::$server->getMountManager()->find($this->fileView->getAbsolutePath($sourcePath));
$internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath));
if ($sourceMount instanceof MoveableMount && $internalPath === '') {
$isMovableMount = true;
}
try {
$sameFolder = ($sourceDir === $destinationDir);
// if we're overwriting or same folder
if ($targetNodeExists || $sameFolder) {
// note that renaming a share mount point is always allowed
if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) {
throw new \Sabre\DAV\Exception\Forbidden();
}
} else {
if (!$this->fileView->isCreatable($destinationDir)) {
throw new \Sabre\DAV\Exception\Forbidden();
}
}
if (!$sameFolder) {
// moving to a different folder, source will be gone, like a deletion
// note that moving a share mount point is always allowed
if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
throw new \Sabre\DAV\Exception\Forbidden();
}
}
$fileName = basename($destinationPath);
try {
$this->fileView->verifyPath($destinationDir, $fileName);
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
}
$renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
if (!$renameOkay) {
throw new \Sabre\DAV\Exception\Forbidden('');
}
} catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable($e->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
return true;
}
}

View File

@ -54,6 +54,7 @@ use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotImplemented;
use Sabre\DAV\Exception\ServiceUnavailable;
use Sabre\DAV\IFile;
use Sabre\DAV\Exception\NotFound;
class File extends Node implements IFile {
@ -307,6 +308,10 @@ class File extends Node implements IFile {
public function get() {
//throw exception if encryption is disabled but files are still encrypted
try {
if (!$this->info->isReadable()) {
// do a if the file did not exist
throw new NotFound();
}
$res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb');
if ($res === false) {
throw new ServiceUnavailable("Could not open file");

View File

@ -45,6 +45,7 @@ use \Sabre\HTTP\ResponseInterface;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\IRequest;
use OCA\DAV\Upload\FutureFile;
class FilesPlugin extends ServerPlugin {
@ -177,6 +178,7 @@ class FilesPlugin extends ServerPlugin {
}
});
$this->server->on('beforeMove', [$this, 'checkMove']);
$this->server->on('beforeMove', [$this, 'beforeMoveFutureFile']);
}
/**
@ -284,6 +286,10 @@ class FilesPlugin extends ServerPlugin {
$httpRequest = $this->server->httpRequest;
if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
if (!$node->getFileInfo()->isReadable()) {
// avoid detecting files through this means
throw new NotFound();
}
$propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
return $node->getFileId();
@ -436,4 +442,43 @@ class FilesPlugin extends ServerPlugin {
}
}
}
/**
* Move handler for future file.
*
* This overrides the default move behavior to prevent Sabre
* to delete the target file before moving. Because deleting would
* lose the file id and metadata.
*
* @param string $path source path
* @param string $destination destination path
* @return bool|void false to stop handling, void to skip this handler
*/
public function beforeMoveFutureFile($path, $destination) {
$sourceNode = $this->tree->getNodeForPath($path);
if (!$sourceNode instanceof FutureFile) {
// skip handling as the source is not a chunked FutureFile
return;
}
if (!$this->tree->nodeExists($destination)) {
// skip and let the default handler do its work
return;
}
// do a move manually, skipping Sabre's default "delete" for existing nodes
$this->tree->move($path, $destination);
// trigger all default events (copied from CorePlugin::move)
$this->server->emit('afterMove', [$path, $destination]);
$this->server->emit('afterUnbind', [$path]);
$this->server->emit('afterBind', [$destination]);
$response = $this->server->httpResponse;
$response->setHeader('Content-Length', '0');
$response->setStatus(204);
return false;
}
}

View File

@ -184,104 +184,6 @@ class ObjectTree extends \Sabre\DAV\Tree {
}
/**
* Moves a file from one location to another
*
* @param string $sourcePath The path to the file which should be moved
* @param string $destinationPath The full destination path, so not just the destination parent node
* @throws FileLocked
* @throws Forbidden
* @throws InvalidPath
* @throws \Sabre\DAV\Exception\Forbidden
* @throws \Sabre\DAV\Exception\Locked
* @throws \Sabre\DAV\Exception\NotFound
* @throws \Sabre\DAV\Exception\ServiceUnavailable
* @return int
*/
public function move($sourcePath, $destinationPath) {
if (!$this->fileView) {
throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
}
$infoDestination = $this->fileView->getFileInfo(dirname($destinationPath));
if (dirname($destinationPath) === dirname($sourcePath)) {
$sourcePermission = $infoDestination && $infoDestination->isUpdateable();
$destinationPermission = $sourcePermission;
} else {
$infoSource = $this->fileView->getFileInfo($sourcePath);
if ($this->fileView->file_exists($destinationPath)) {
$destinationPermission = $infoDestination && $infoDestination->isUpdateable();
} else {
$destinationPermission = $infoDestination && $infoDestination->isCreatable();
}
$sourcePermission = $infoSource && $infoSource->isDeletable();
}
if (!$destinationPermission || !$sourcePermission) {
throw new Forbidden('No permissions to move object.');
}
$targetNodeExists = $this->nodeExists($destinationPath);
$sourceNode = $this->getNodeForPath($sourcePath);
if ($sourceNode instanceof \Sabre\DAV\ICollection && $targetNodeExists) {
throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists');
}
list($sourceDir,) = \Sabre\HTTP\URLUtil::splitPath($sourcePath);
list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destinationPath);
$isMovableMount = false;
$sourceMount = $this->mountManager->find($this->fileView->getAbsolutePath($sourcePath));
$internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath));
if ($sourceMount instanceof MoveableMount && $internalPath === '') {
$isMovableMount = true;
}
try {
$sameFolder = ($sourceDir === $destinationDir);
// if we're overwriting or same folder
if ($targetNodeExists || $sameFolder) {
// note that renaming a share mount point is always allowed
if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) {
throw new \Sabre\DAV\Exception\Forbidden();
}
} else {
if (!$this->fileView->isCreatable($destinationDir)) {
throw new \Sabre\DAV\Exception\Forbidden();
}
}
if (!$sameFolder) {
// moving to a different folder, source will be gone, like a deletion
// note that moving a share mount point is always allowed
if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
throw new \Sabre\DAV\Exception\Forbidden();
}
}
$fileName = basename($destinationPath);
try {
$this->fileView->verifyPath($destinationDir, $fileName);
} catch (\OCP\Files\InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
}
$renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
if (!$renameOkay) {
throw new \Sabre\DAV\Exception\Forbidden('');
}
} catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
$this->markDirty($sourceDir);
$this->markDirty($destinationDir);
}
/**
* Copies a file or directory.
*

View File

@ -27,6 +27,42 @@
namespace OCA\DAV\Tests\Unit\Connector\Sabre;
use OCP\Files\ForbiddenException;
use OC\Files\FileInfo;
use OCA\DAV\Connector\Sabre\Directory;
class TestViewDirectory extends \OC\Files\View {
private $updatables;
private $deletables;
private $canRename;
public function __construct($updatables, $deletables, $canRename = true) {
$this->updatables = $updatables;
$this->deletables = $deletables;
$this->canRename = $canRename;
}
public function isUpdatable($path) {
return $this->updatables[$path];
}
public function isCreatable($path) {
return $this->updatables[$path];
}
public function isDeletable($path) {
return $this->deletables[$path];
}
public function rename($path1, $path2) {
return $this->canRename;
}
public function getRelativePath($path) {
return $path;
}
}
/**
* @group DB
@ -41,12 +77,11 @@ class DirectoryTest extends \Test\TestCase {
protected function setUp() {
parent::setUp();
$this->view = $this->getMockBuilder('OC\Files\View')
->disableOriginalConstructor()
->getMock();
$this->info = $this->getMockBuilder('OC\Files\FileInfo')
->disableOriginalConstructor()
->getMock();
$this->view = $this->createMock('OC\Files\View');
$this->info = $this->createMock('OC\Files\FileInfo');
$this->info->expects($this->any())
->method('isReadable')
->willReturn(true);
}
private function getDir($path = '/') {
@ -58,7 +93,7 @@ class DirectoryTest extends \Test\TestCase {
->method('getPath')
->will($this->returnValue($path));
return new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
return new Directory($this->view, $this->info);
}
/**
@ -172,7 +207,7 @@ class DirectoryTest extends \Test\TestCase {
->method('getRelativePath')
->will($this->returnValue(''));
$dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
$dir = new Directory($this->view, $this->info);
$nodes = $dir->getChildren();
$this->assertEquals(2, count($nodes));
@ -182,6 +217,31 @@ class DirectoryTest extends \Test\TestCase {
$dir->getChildren();
}
/**
* @expectedException \Sabre\DAV\Exception\Forbidden
*/
public function testGetChildrenNoPermission() {
$info = $this->createMock(FileInfo::class);
$info->expects($this->any())
->method('isReadable')
->will($this->returnValue(false));
$dir = new Directory($this->view, $info);
$dir->getChildren();
}
/**
* @expectedException \Sabre\DAV\Exception\NotFound
*/
public function testGetChildNoPermission() {
$this->info->expects($this->any())
->method('isReadable')
->will($this->returnValue(false));
$dir = new Directory($this->view, $this->info);
$dir->getChild('test');
}
/**
* @expectedException \Sabre\DAV\Exception\ServiceUnavailable
*/
@ -190,7 +250,7 @@ class DirectoryTest extends \Test\TestCase {
->method('getFileInfo')
->willThrowException(new \OCP\Files\StorageNotAvailableException());
$dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
$dir = new Directory($this->view, $this->info);
$dir->getChild('.');
}
@ -204,7 +264,7 @@ class DirectoryTest extends \Test\TestCase {
$this->view->expects($this->never())
->method('getFileInfo');
$dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
$dir = new Directory($this->view, $this->info);
$dir->getChild('.');
}
@ -235,7 +295,7 @@ class DirectoryTest extends \Test\TestCase {
->method('getStorage')
->will($this->returnValue($storage));
$dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
$dir = new Directory($this->view, $this->info);
$this->assertEquals([200, -3], $dir->getQuotaInfo()); //200 used, unlimited
}
@ -267,7 +327,105 @@ class DirectoryTest extends \Test\TestCase {
->method('getStorage')
->will($this->returnValue($storage));
$dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
$dir = new Directory($this->view, $this->info);
$this->assertEquals([200, 800], $dir->getQuotaInfo()); //200 used, 800 free
}
/**
* @dataProvider moveFailedProvider
* @expectedException \Sabre\DAV\Exception\Forbidden
*/
public function testMoveFailed($source, $destination, $updatables, $deletables) {
$this->moveTest($source, $destination, $updatables, $deletables);
}
/**
* @dataProvider moveSuccessProvider
*/
public function testMoveSuccess($source, $destination, $updatables, $deletables) {
$this->moveTest($source, $destination, $updatables, $deletables);
$this->assertTrue(true);
}
/**
* @dataProvider moveFailedInvalidCharsProvider
* @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath
*/
public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) {
$this->moveTest($source, $destination, $updatables, $deletables);
}
public function moveFailedInvalidCharsProvider() {
return [
['a/b', 'a/*', ['a' => true, 'a/b' => true, 'a/c*' => false], []],
];
}
public function moveFailedProvider() {
return [
['a/b', 'a/c', ['a' => false, 'a/b' => false, 'a/c' => false], []],
['a/b', 'b/b', ['a' => false, 'a/b' => false, 'b' => false, 'b/b' => false], []],
['a/b', 'b/b', ['a' => false, 'a/b' => true, 'b' => false, 'b/b' => false], []],
['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => false, 'b/b' => false], []],
['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => false]],
['a/b', 'a/c', ['a' => false, 'a/b' => true, 'a/c' => false], []],
];
}
public function moveSuccessProvider() {
return [
['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => true]],
// older files with special chars can still be renamed to valid names
['a/b*', 'b/b', ['a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false], ['a/b*' => true]],
];
}
/**
* @param $source
* @param $destination
* @param $updatables
*/
private function moveTest($source, $destination, $updatables, $deletables) {
$view = new TestViewDirectory($updatables, $deletables);
$sourceInfo = new FileInfo($source, null, null, [], null);
$targetInfo = new FileInfo(dirname($destination), null, null, [], null);
$sourceNode = new Directory($view, $sourceInfo);
$targetNode = $this->getMockBuilder(Directory::class)
->setMethods(['childExists'])
->setConstructorArgs([$view, $targetInfo])
->getMock();
$targetNode->expects($this->any())->method('childExists')
->with(basename($destination))
->willReturn(false);
$this->assertTrue($targetNode->moveInto(basename($destination), $source, $sourceNode));
}
/**
* @expectedException \Sabre\DAV\Exception\Forbidden
* @expectedExceptionMessage Could not copy directory b, target exists
*/
public function testFailingMove() {
$source = 'a/b';
$destination = 'c/b';
$updatables = ['a' => true, 'a/b' => true, 'b' => true, 'c/b' => false];
$deletables = ['a/b' => true];
$view = new TestViewDirectory($updatables, $deletables);
$sourceInfo = new FileInfo($source, null, null, [], null);
$targetInfo = new FileInfo(dirname($destination), null, null, [], null);
$sourceNode = new Directory($view, $sourceInfo);
$targetNode = $this->getMockBuilder(Directory::class)
->setMethods(['childExists'])
->setConstructorArgs([$view, $targetInfo])
->getMock();
$targetNode->expects($this->once())->method('childExists')
->with(basename($destination))
->willReturn(true);
$targetNode->moveInto(basename($destination), $source, $sourceNode);
}
}

View File

@ -1003,4 +1003,23 @@ class FileTest extends \Test\TestCase {
$file->get();
}
/**
* @expectedException \Sabre\DAV\Exception\NotFound
*/
public function testGetThrowsIfNoPermission() {
$view = $this->getMockBuilder('\OC\Files\View')
->setMethods(['fopen'])
->getMock();
$view->expects($this->never())
->method('fopen');
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, [
'permissions' => \OCP\Constants::PERMISSION_CREATE // no read perm
], null);
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
$file->get();
}
}

View File

@ -32,6 +32,9 @@ use Sabre\DAV\PropPatch;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Test\TestCase;
use OCA\DAV\Upload\FutureFile;
use OCA\DAV\Connector\Sabre\Directory;
use OCP\Files\FileInfo;
/**
* Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
@ -107,6 +110,12 @@ class FilesPluginTest extends TestCase {
$this->request,
$this->previewManager
);
$response = $this->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()
->getMock();
$this->server->httpResponse = $response;
$this->plugin->initialize($this->server);
}
@ -140,13 +149,15 @@ class FilesPluginTest extends TestCase {
$node->expects($this->any())
->method('getDavPermissions')
->will($this->returnValue('DWCKMSR'));
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())
->method('isReadable')
->willReturn(true);
$node->expects($this->any())
->method('getFileInfo')
->will($this->returnValue(
$this->getMockBuilder('\OCP\Files\FileInfo')
->disableOriginalConstructor()
->getMock()
));
->willReturn($fileInfo);
return $node;
}
@ -305,6 +316,15 @@ class FilesPluginTest extends TestCase {
->getMock();
$node->expects($this->any())->method('getPath')->willReturn('/');
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())
->method('isReadable')
->willReturn(true);
$node->expects($this->any())
->method('getFileInfo')
->willReturn($fileInfo);
$propFind = new PropFind(
'/',
[
@ -321,6 +341,39 @@ class FilesPluginTest extends TestCase {
$this->assertEquals('my_fingerprint', $propFind->get(self::DATA_FINGERPRINT_PROPERTYNAME));
}
/**
* @expectedException \Sabre\DAV\Exception\NotFound
*/
public function testGetPropertiesWhenNoPermission() {
/** @var \OCA\DAV\Connector\Sabre\Directory | \PHPUnit_Framework_MockObject_MockObject $node */
$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory')
->disableOriginalConstructor()
->getMock();
$node->expects($this->any())->method('getPath')->willReturn('/');
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())
->method('isReadable')
->willReturn(false);
$node->expects($this->any())
->method('getFileInfo')
->willReturn($fileInfo);
$propFind = new PropFind(
'/test',
[
self::DATA_FINGERPRINT_PROPERTYNAME,
],
0
);
$this->plugin->handleGetProperties(
$propFind,
$node
);
}
public function testUpdateProps() {
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File');
@ -535,4 +588,59 @@ class FilesPluginTest extends TestCase {
$this->assertEquals("false", $propFind->get(self::HAS_PREVIEW_PROPERTYNAME));
}
public function testBeforeMoveFutureFileSkip() {
$node = $this->createMock(Directory::class);
$this->tree->expects($this->any())
->method('getNodeForPath')
->with('source')
->will($this->returnValue($node));
$this->server->httpResponse->expects($this->never())
->method('setStatus');
$this->assertNull($this->plugin->beforeMoveFutureFile('source', 'target'));
}
public function testBeforeMoveFutureFileSkipNonExisting() {
$sourceNode = $this->createMock(FutureFile::class);
$this->tree->expects($this->any())
->method('getNodeForPath')
->with('source')
->will($this->returnValue($sourceNode));
$this->tree->expects($this->any())
->method('nodeExists')
->with('target')
->will($this->returnValue(false));
$this->server->httpResponse->expects($this->never())
->method('setStatus');
$this->assertNull($this->plugin->beforeMoveFutureFile('source', 'target'));
}
public function testBeforeMoveFutureFileMoveIt() {
$sourceNode = $this->createMock(FutureFile::class);
$this->tree->expects($this->any())
->method('getNodeForPath')
->with('source')
->will($this->returnValue($sourceNode));
$this->tree->expects($this->any())
->method('nodeExists')
->with('target')
->will($this->returnValue(true));
$this->tree->expects($this->once())
->method('move')
->with('source', 'target');
$this->server->httpResponse->expects($this->once())
->method('setHeader')
->with('Content-Length', '0');
$this->server->httpResponse->expects($this->once())
->method('setStatus')
->with(204);
$this->assertFalse($this->plugin->beforeMoveFutureFile('source', 'target'));
}
}

View File

@ -34,6 +34,7 @@ use OCP\Files\Folder;
use OCP\IGroupManager;
use OCP\SystemTag\ISystemTagManager;
use OCP\ITags;
use OCP\Files\FileInfo;
class FilesReportPluginTest extends \Test\TestCase {
/** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */
@ -349,6 +350,9 @@ class FilesReportPluginTest extends \Test\TestCase {
public function testPrepareResponses() {
$requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype'];
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->method('isReadable')->willReturn(true);
$node1 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory')
->disableOriginalConstructor()
->getMock();
@ -362,6 +366,7 @@ class FilesReportPluginTest extends \Test\TestCase {
$node1->expects($this->any())
->method('getPath')
->will($this->returnValue('/node1'));
$node1->method('getFileInfo')->willReturn($fileInfo);
$node2->expects($this->once())
->method('getInternalFileId')
->will($this->returnValue('222'));
@ -371,6 +376,7 @@ class FilesReportPluginTest extends \Test\TestCase {
$node2->expects($this->any())
->method('getPath')
->will($this->returnValue('/sub/node2'));
$node2->method('getFileInfo')->willReturn($fileInfo);
$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()

View File

@ -30,47 +30,11 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre;
use OC\Files\FileInfo;
use OC\Files\Filesystem;
use OC\Files\Storage\Temporary;
class TestDoubleFileView extends \OC\Files\View {
public function __construct($creatables, $updatables, $deletables, $canRename = true) {
$this->creatables = $creatables;
$this->updatables = $updatables;
$this->deletables = $deletables;
$this->canRename = $canRename;
$this->lockingProvider = \OC::$server->getLockingProvider();
}
public function isUpdatable($path) {
return !empty($this->updatables[$path]);
}
public function isCreatable($path) {
return !empty($this->creatables[$path]);
}
public function isDeletable($path) {
return !empty($this->deletables[$path]);
}
public function rename($path1, $path2) {
return $this->canRename;
}
public function getRelativePath($path) {
return $path;
}
public function getFileInfo($path, $includeMountPoints = true) {
$objectTreeTest = new ObjectTreeTest();
return $objectTreeTest->getFileInfoMock(
$this->isCreatable($path),
$this->isUpdatable($path),
$this->isDeletable($path)
);
}
}
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\ObjectTree;
/**
* Class ObjectTreeTest
@ -81,103 +45,100 @@ class TestDoubleFileView extends \OC\Files\View {
*/
class ObjectTreeTest extends \Test\TestCase {
public function getFileInfoMock($create = true, $update = true, $delete = true) {
$mock = $this->getMockBuilder('\OCP\Files\FileInfo')
->disableOriginalConstructor()
->getMock();
$mock
->expects($this->any())
public function copyDataProvider() {
return [
// copy into same dir
['a', 'b', ''],
// copy into same dir
['a/a', 'a/b', 'a'],
// copy into another dir
['a', 'sub/a', 'sub'],
];
}
/**
* @dataProvider copyDataProvider
*/
public function testCopy($sourcePath, $targetPath, $targetParent) {
$view = $this->createMock(View::class);
$view->expects($this->once())
->method('verifyPath')
->with($targetParent)
->will($this->returnValue(true));
$view->expects($this->once())
->method('file_exists')
->with($targetPath)
->willReturn(false);
$view->expects($this->once())
->method('copy')
->with($sourcePath, $targetPath)
->will($this->returnValue(true));
$info = $this->createMock(FileInfo::class);
$info->expects($this->once())
->method('isCreatable')
->willReturn($create);
$mock
->expects($this->any())
->method('isUpdateable')
->willReturn($update);
$mock
->expects($this->any())
->method('isDeletable')
->willReturn($delete);
->willReturn(true);
return $mock;
}
$view->expects($this->once())
->method('getFileInfo')
->with($targetParent === '' ? '.' : $targetParent)
->willReturn($info);
/**
* @dataProvider moveFailedProvider
* @expectedException \Sabre\DAV\Exception\Forbidden
*/
public function testMoveFailed($source, $destination, $updatables, $deletables) {
$this->moveTest($source, $destination, $updatables, $updatables, $deletables, true);
}
/**
* @dataProvider moveSuccessProvider
*/
public function testMoveSuccess($source, $destination, $updatables, $deletables) {
$this->moveTest($source, $destination, $updatables, $updatables, $deletables);
$this->assertTrue(true);
}
/**
* @dataProvider moveFailedInvalidCharsProvider
* @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath
*/
public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) {
$this->moveTest($source, $destination, $updatables, $updatables, $deletables);
}
function moveFailedInvalidCharsProvider() {
return array(
array('a/b', 'a/*', array('a' => true, 'a/b' => true, 'a/c*' => false), array()),
);
}
function moveFailedProvider() {
return array(
array('a/b', 'a/c', array('a' => false, 'a/b' => false, 'a/c' => false), array()),
array('a/b', 'b/b', array('a' => false, 'a/b' => false, 'b' => false, 'b/b' => false), array()),
array('a/b', 'b/b', array('a' => false, 'a/b' => true, 'b' => false, 'b/b' => false), array()),
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => false, 'b/b' => false), array()),
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => false)),
array('a/b', 'a/c', array('a' => false, 'a/b' => true, 'a/c' => false), array()),
);
}
function moveSuccessProvider() {
return array(
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => true)),
// older files with special chars can still be renamed to valid names
array('a/b*', 'b/b', array('a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false), array('a/b*' => true)),
);
}
/**
* @param $source
* @param $destination
* @param $creatables
* @param $updatables
* @param $deletables
* @param $throwsBeforeGetNode
*/
private function moveTest($source, $destination, $creatables, $updatables, $deletables, $throwsBeforeGetNode = false) {
$view = new TestDoubleFileView($creatables, $updatables, $deletables);
$info = new FileInfo('', null, null, array(), null);
$rootDir = new \OCA\DAV\Connector\Sabre\Directory($view, $info);
$objectTree = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\ObjectTree')
$rootDir = new Directory($view, $info);
$objectTree = $this->getMockBuilder(ObjectTree::class)
->setMethods(['nodeExists', 'getNodeForPath'])
->setConstructorArgs([$rootDir, $view])
->getMock();
$objectTree->expects($throwsBeforeGetNode ? $this->never() : $this->once())
$objectTree->expects($this->once())
->method('getNodeForPath')
->with($this->identicalTo($source))
->with($this->identicalTo($sourcePath))
->will($this->returnValue(false));
/** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
$mountManager = \OC\Files\Filesystem::getMountManager();
$mountManager = Filesystem::getMountManager();
$objectTree->init($rootDir, $view, $mountManager);
$objectTree->move($source, $destination);
$objectTree->copy($sourcePath, $targetPath);
}
/**
* @dataProvider copyDataProvider
* @expectedException \Sabre\DAV\Exception\Forbidden
*/
public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) {
$view = $this->createMock(View::class);
$view->expects($this->never())
->method('verifyPath');
$view->expects($this->once())
->method('file_exists')
->with($targetPath)
->willReturn(false);
$view->expects($this->never())
->method('copy');
$info = $this->createMock(FileInfo::class);
$info->expects($this->once())
->method('isCreatable')
->willReturn(false);
$view->expects($this->once())
->method('getFileInfo')
->with($targetParent === '' ? '.' : $targetParent)
->willReturn($info);
$rootDir = new Directory($view, $info);
$objectTree = $this->getMockBuilder(ObjectTree::class)
->setMethods(['nodeExists', 'getNodeForPath'])
->setConstructorArgs([$rootDir, $view])
->getMock();
$objectTree->expects($this->never())
->method('getNodeForPath');
/** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
$mountManager = Filesystem::getMountManager();
$objectTree->init($rootDir, $view, $mountManager);
$objectTree->copy($sourcePath, $targetPath);
}
/**
@ -361,46 +322,4 @@ class ObjectTreeTest extends \Test\TestCase {
$this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
}
/**
* @expectedException \Sabre\DAV\Exception\Forbidden
* @expectedExceptionMessage Could not copy directory nameOfSourceNode, target exists
*/
public function testFailingMove() {
$source = 'a/b';
$destination = 'b/b';
$updatables = array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false);
$deletables = array('a/b' => true);
$view = new TestDoubleFileView($updatables, $updatables, $deletables);
$info = new FileInfo('', null, null, array(), null);
$rootDir = new \OCA\DAV\Connector\Sabre\Directory($view, $info);
$objectTree = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\ObjectTree')
->setMethods(['nodeExists', 'getNodeForPath'])
->setConstructorArgs([$rootDir, $view])
->getMock();
$sourceNode = $this->getMockBuilder('\Sabre\DAV\ICollection')
->disableOriginalConstructor()
->getMock();
$sourceNode->expects($this->once())
->method('getName')
->will($this->returnValue('nameOfSourceNode'));
$objectTree->expects($this->once())
->method('nodeExists')
->with($this->identicalTo($destination))
->will($this->returnValue(true));
$objectTree->expects($this->once())
->method('getNodeForPath')
->with($this->identicalTo($source))
->will($this->returnValue($sourceNode));
/** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
$mountManager = \OC\Files\Filesystem::getMountManager();
$objectTree->init($rootDir, $view, $mountManager);
$objectTree->move($source, $destination);
}
}

View File

@ -31,7 +31,7 @@ use OCP\AppFramework\Http;
*
* @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest
*/
class DeleteTest extends RequestTest {
class DeleteTest extends RequestTestCase {
public function testBasicUpload() {
$user = $this->getUniqueID();
$view = $this->setupUser($user, 'pass');

View File

@ -34,7 +34,7 @@ use OCP\Lock\ILockingProvider;
*
* @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest
*/
class DownloadTest extends RequestTest {
class DownloadTest extends RequestTestCase {
public function testDownload() {
$user = $this->getUniqueID();
$view = $this->setupUser($user, 'pass');

View File

@ -34,7 +34,7 @@ use Test\TestCase;
use Test\Traits\MountProviderTrait;
use Test\Traits\UserTrait;
abstract class RequestTest extends TestCase {
abstract class RequestTestCase extends TestCase {
use UserTrait;
use MountProviderTrait;

View File

@ -35,7 +35,7 @@ use OCP\Lock\ILockingProvider;
*
* @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest
*/
class UploadTest extends RequestTest {
class UploadTest extends RequestTestCase {
public function testBasicUpload() {
$user = $this->getUniqueID();
$view = $this->setupUser($user, 'pass');

View File

@ -45,8 +45,10 @@ trait WebDav {
private $usingOldDavPath = true;
/** @var ResponseInterface */
private $response;
/** @var map with user as key and another map as value, which has path as key and etag as value */
private $storedETAG = NULL;
/** @var array map with user as key and another map as value, which has path as key and etag as value */
private $storedETAG = null;
/** @var int */
private $storedFileID = null;
/**
* @Given /^using dav path "([^"]*)"$/
@ -749,4 +751,33 @@ trait WebDav {
}
/**
* @param string $user
* @param string $path
* @return int
*/
private function getFileIdForPath($user, $path) {
$propertiesTable = new \Behat\Gherkin\Node\TableNode([["{http://owncloud.org/ns}fileid"]]);
$this->asGetsPropertiesOfFolderWith($user, 'file', $path, $propertiesTable);
return (int) $this->response['{http://owncloud.org/ns}fileid'];
}
/**
* @Given /^User "([^"]*)" stores id of file "([^"]*)"$/
* @param string $user
* @param string $path
*/
public function userStoresFileIdForPath($user, $path) {
$this->storedFileID = $this->getFileIdForPath($user, $path);
}
/**
* @Given /^User "([^"]*)" checks id of file "([^"]*)"$/
* @param string $user
* @param string $path
*/
public function userChecksFileIdForPath($user, $path) {
$currentFileID = $this->getFileIdForPath($user, $path);
PHPUnit_Framework_Assert::assertEquals($currentFileID, $this->storedFileID);
}
}

View File

@ -517,3 +517,64 @@ Feature: webdav-related
| /textfile2.txt |
| /textfile3.txt |
| /textfile4.txt |
Scenario: Checking file id after a move using new endpoint
Given using new dav path
And user "user0" exists
And User "user0" stores id of file "/textfile0.txt"
When User "user0" moves file "/textfile0.txt" to "/FOLDER/textfile0.txt"
Then User "user0" checks id of file "/FOLDER/textfile0.txt"
Scenario: Checking file id after a move overwrite using new chunking endpoint
Given using new dav path
And user "user0" exists
And User "user0" copies file "/textfile0.txt" to "/existingFile.txt"
And User "user0" stores id of file "/existingFile.txt"
And user "user0" creates a new chunking upload with id "chunking-42"
And user "user0" uploads new chunk file "1" with "AAAAA" to id "chunking-42"
And user "user0" uploads new chunk file "2" with "BBBBB" to id "chunking-42"
And user "user0" uploads new chunk file "3" with "CCCCC" to id "chunking-42"
When user "user0" moves new chunk file with id "chunking-42" to "/existingFile.txt"
Then User "user0" checks id of file "/existingFile.txt"
Scenario: Renaming a folder to a backslash encoded should return an error using old endpoint
Given using old dav path
And user "user0" exists
And user "user0" created a folder "/testshare"
When User "user0" moves folder "/testshare" to "/%5C"
Then the HTTP status code should be "400"
Scenario: Renaming a folder beginning with a backslash encoded should return an error using old endpoint
Given using old dav path
And user "user0" exists
And user "user0" created a folder "/testshare"
When User "user0" moves folder "/testshare" to "/%5Ctestshare"
Then the HTTP status code should be "400"
Scenario: Renaming a folder including a backslash encoded should return an error using old endpoint
Given using old dav path
And user "user0" exists
And user "user0" created a folder "/testshare"
When User "user0" moves folder "/testshare" to "/hola%5Chola"
Then the HTTP status code should be "400"
Scenario: Renaming a folder to a backslash encoded should return an error using new endpoint
Given using new dav path
And user "user0" exists
And user "user0" created a folder "/testshare"
When User "user0" moves folder "/testshare" to "/%5C"
Then the HTTP status code should be "400"
Scenario: Renaming a folder beginning with a backslash encoded should return an error using new endpoint
Given using new dav path
And user "user0" exists
And user "user0" created a folder "/testshare"
When User "user0" moves folder "/testshare" to "/%5Ctestshare"
Then the HTTP status code should be "400"
Scenario: Renaming a folder including a backslash encoded should return an error using new endpoint
Given using new dav path
And user "user0" exists
And user "user0" created a folder "/testshare"
When User "user0" moves folder "/testshare" to "/hola%5Chola"
Then the HTTP status code should be "400"

View File

@ -88,7 +88,7 @@ class View {
/**
* @var \OCP\Lock\ILockingProvider
*/
private $lockingProvider;
protected $lockingProvider;
private $lockingEnabled;