2013-09-24 17:44:02 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-09-24 17:44:02 +04:00
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2015-02-18 20:28:24 +03:00
|
|
|
|
2015-10-12 18:21:54 +03:00
|
|
|
use OC\Files\Storage\Local;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OC\Files\View;
|
2015-11-13 16:13:16 +03:00
|
|
|
use OCP\Files\ForbiddenException;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Files\Storage;
|
2017-11-27 21:41:39 +03:00
|
|
|
use OCP\IConfig;
|
2015-05-12 20:08:04 +03:00
|
|
|
use Test\HookHelper;
|
|
|
|
use OC\Files\Filesystem;
|
2015-06-26 11:38:59 +03:00
|
|
|
use OCP\Lock\ILockingProvider;
|
2015-05-12 20:08:04 +03:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
|
|
|
* Class File
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
2016-05-25 17:04:15 +03:00
|
|
|
* @package OCA\DAV\Tests\unit\Connector\Sabre
|
2015-11-03 03:52:41 +03:00
|
|
|
*/
|
2016-05-25 17:04:15 +03:00
|
|
|
class FileTest extends \Test\TestCase {
|
2013-09-24 17:44:02 +04:00
|
|
|
|
2015-05-12 20:08:04 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2017-11-27 21:41:39 +03:00
|
|
|
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $config;
|
|
|
|
|
2015-05-12 20:08:04 +03:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2017-02-10 18:20:25 +03:00
|
|
|
unset($_SERVER['HTTP_OC_CHUNKED']);
|
|
|
|
unset($_SERVER['CONTENT_LENGTH']);
|
|
|
|
unset($_SERVER['REQUEST_METHOD']);
|
2015-05-12 20:08:04 +03:00
|
|
|
|
|
|
|
\OC_Hook::clear();
|
|
|
|
|
|
|
|
$this->user = $this->getUniqueID('user_');
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
$userManager->createUser($this->user, 'pass');
|
|
|
|
|
|
|
|
$this->loginAsUser($this->user);
|
2017-11-27 21:41:39 +03:00
|
|
|
|
|
|
|
$this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
|
2015-05-12 20:08:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
$userManager->get($this->user)->delete();
|
2015-06-26 11:38:59 +03:00
|
|
|
unset($_SERVER['HTTP_OC_CHUNKED']);
|
2015-05-12 20:08:04 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
private function getMockStorage() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$storage = $this->getMockBuilder(Storage::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-12-08 18:33:39 +03:00
|
|
|
$storage->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('home::someuser'));
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
2015-11-20 18:42:34 +03:00
|
|
|
/**
|
|
|
|
* @param string $string
|
|
|
|
*/
|
2015-04-14 16:25:52 +03:00
|
|
|
private function getStream($string) {
|
|
|
|
$stream = fopen('php://temp', 'r+');
|
|
|
|
fwrite($stream, $string);
|
|
|
|
fseek($stream, 0);
|
|
|
|
return $stream;
|
|
|
|
}
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
|
|
|
|
public function fopenFailuresProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
// return false
|
|
|
|
null,
|
|
|
|
'\Sabre\Dav\Exception',
|
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Files\NotPermittedException(),
|
|
|
|
'Sabre\DAV\Exception\Forbidden'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Files\EntityTooLargeException(),
|
2015-08-30 20:13:01 +03:00
|
|
|
'OCA\DAV\Connector\Sabre\Exception\EntityTooLarge'
|
2015-06-26 11:38:59 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Files\InvalidContentException(),
|
2015-08-30 20:13:01 +03:00
|
|
|
'OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType'
|
2015-06-26 11:38:59 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Files\InvalidPathException(),
|
|
|
|
'Sabre\DAV\Exception\Forbidden'
|
|
|
|
],
|
2015-11-13 16:13:16 +03:00
|
|
|
[
|
|
|
|
new \OCP\Files\ForbiddenException('', true),
|
|
|
|
'OCA\DAV\Connector\Sabre\Exception\Forbidden'
|
|
|
|
],
|
2015-06-26 11:38:59 +03:00
|
|
|
[
|
|
|
|
new \OCP\Files\LockNotAcquiredException('/test.txt', 1),
|
2015-08-30 20:13:01 +03:00
|
|
|
'OCA\DAV\Connector\Sabre\Exception\FileLocked'
|
2015-06-26 11:38:59 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Lock\LockedException('/test.txt'),
|
2015-08-30 20:13:01 +03:00
|
|
|
'OCA\DAV\Connector\Sabre\Exception\FileLocked'
|
2015-06-26 11:38:59 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Encryption\Exceptions\GenericEncryptionException(),
|
|
|
|
'Sabre\DAV\Exception\ServiceUnavailable'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new \OCP\Files\StorageNotAvailableException(),
|
|
|
|
'Sabre\DAV\Exception\ServiceUnavailable'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new \Sabre\DAV\Exception('Generic sabre exception'),
|
|
|
|
'Sabre\DAV\Exception',
|
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new \Exception('Generic exception'),
|
|
|
|
'Sabre\DAV\Exception'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2013-09-24 17:44:02 +04:00
|
|
|
/**
|
2015-06-26 11:38:59 +03:00
|
|
|
* @dataProvider fopenFailuresProvider
|
2013-09-24 17:44:02 +04:00
|
|
|
*/
|
2015-06-26 11:38:59 +03:00
|
|
|
public function testSimplePutFails($thrownException, $expectedException, $checkPreviousClass = true) {
|
2013-09-24 17:44:02 +04:00
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$storage = $this->getMockBuilder(Local::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['fopen'])
|
|
|
|
->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
|
|
|
|
->getMock();
|
2015-06-26 11:38:59 +03:00
|
|
|
\OC\Files\Filesystem::mount($storage, [], $this->user . '/');
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['getRelativePath', 'resolvePath'])
|
|
|
|
->getMock();
|
2015-06-26 11:38:59 +03:00
|
|
|
$view->expects($this->atLeastOnce())
|
2015-04-09 15:46:25 +03:00
|
|
|
->method('resolvePath')
|
2015-06-26 11:38:59 +03:00
|
|
|
->will($this->returnCallback(
|
2015-07-24 15:12:50 +03:00
|
|
|
function ($path) use ($storage) {
|
2015-06-26 11:38:59 +03:00
|
|
|
return [$storage, $path];
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($thrownException !== null) {
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('fopen')
|
|
|
|
->will($this->throwException($thrownException));
|
|
|
|
} else {
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('fopen')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
}
|
2014-03-03 17:27:24 +04:00
|
|
|
|
|
|
|
$view->expects($this->any())
|
|
|
|
->method('getRelativePath')
|
2015-06-26 11:38:59 +03:00
|
|
|
->will($this->returnArgument(0));
|
2014-03-03 17:27:24 +04:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2015-04-14 16:25:52 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2014-03-03 17:27:24 +04:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2013-09-24 17:44:02 +04:00
|
|
|
|
|
|
|
// action
|
2015-06-26 11:38:59 +03:00
|
|
|
$caughtException = null;
|
|
|
|
try {
|
|
|
|
$file->put('test data');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$caughtException = $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertInstanceOf($expectedException, $caughtException);
|
|
|
|
if ($checkPreviousClass) {
|
|
|
|
$this->assertInstanceOf(get_class($thrownException), $caughtException->getPrevious());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
2013-09-24 17:44:02 +04:00
|
|
|
}
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
/**
|
|
|
|
* Test putting a file using chunking
|
|
|
|
*
|
|
|
|
* @dataProvider fopenFailuresProvider
|
|
|
|
*/
|
|
|
|
public function testChunkedPutFails($thrownException, $expectedException, $checkPreviousClass = false) {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$storage = $this->getMockBuilder(Local::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['fopen'])
|
|
|
|
->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
|
|
|
|
->getMock();
|
2015-06-26 11:38:59 +03:00
|
|
|
\OC\Files\Filesystem::mount($storage, [], $this->user . '/');
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['getRelativePath', 'resolvePath'])
|
|
|
|
->getMock();
|
2015-06-26 11:38:59 +03:00
|
|
|
$view->expects($this->atLeastOnce())
|
|
|
|
->method('resolvePath')
|
|
|
|
->will($this->returnCallback(
|
2015-07-24 15:12:50 +03:00
|
|
|
function ($path) use ($storage) {
|
2015-06-26 11:38:59 +03:00
|
|
|
return [$storage, $path];
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($thrownException !== null) {
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('fopen')
|
|
|
|
->will($this->throwException($thrownException));
|
|
|
|
} else {
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('fopen')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
$view->expects($this->any())
|
|
|
|
->method('getRelativePath')
|
|
|
|
->will($this->returnArgument(0));
|
|
|
|
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt-chunking-12345-2-0', $this->getMockStorage(), null, [
|
2015-06-26 11:38:59 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
], null);
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-06-26 11:38:59 +03:00
|
|
|
|
|
|
|
// put first chunk
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->acquireLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
$this->assertNull($file->put('test data one'));
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->releaseLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt-chunking-12345-2-1', $this->getMockStorage(), null, [
|
2015-06-26 11:38:59 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
], null);
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-06-26 11:38:59 +03:00
|
|
|
|
|
|
|
// action
|
|
|
|
$caughtException = null;
|
|
|
|
try {
|
|
|
|
// last chunk
|
2015-08-12 17:44:43 +03:00
|
|
|
$file->acquireLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
$file->put('test data two');
|
2015-08-12 17:44:43 +03:00
|
|
|
$file->releaseLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$caughtException = $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertInstanceOf($expectedException, $caughtException);
|
|
|
|
if ($checkPreviousClass) {
|
|
|
|
$this->assertInstanceOf(get_class($thrownException), $caughtException->getPrevious());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulate putting a file to the given path.
|
|
|
|
*
|
|
|
|
* @param string $path path to put the file into
|
|
|
|
* @param string $viewRoot root to use for the view
|
2017-11-27 21:41:34 +03:00
|
|
|
* @param null|\OC\AppFramework\Http\Request $request the HTTP request
|
2015-06-26 11:38:59 +03:00
|
|
|
*
|
2015-11-20 18:42:34 +03:00
|
|
|
* @return null|string of the PUT operaiton which is usually the etag
|
2015-06-26 11:38:59 +03:00
|
|
|
*/
|
2017-11-27 21:41:34 +03:00
|
|
|
private function doPut($path, $viewRoot = null, \OC\AppFramework\Http\Request $request = null) {
|
2015-05-12 20:08:04 +03:00
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
if (!is_null($viewRoot)) {
|
|
|
|
$view = new \OC\Files\View($viewRoot);
|
|
|
|
} else {
|
|
|
|
$viewRoot = '/' . $this->user . '/files';
|
|
|
|
}
|
|
|
|
|
|
|
|
$info = new \OC\Files\FileInfo(
|
|
|
|
$viewRoot . '/' . ltrim($path, '/'),
|
2015-12-08 18:33:39 +03:00
|
|
|
$this->getMockStorage(),
|
2015-05-12 20:08:04 +03:00
|
|
|
null,
|
|
|
|
['permissions' => \OCP\Constants::PERMISSION_ALL],
|
|
|
|
null
|
|
|
|
);
|
2015-01-26 19:48:28 +03:00
|
|
|
|
2017-11-27 21:41:48 +03:00
|
|
|
/** @var \OCA\DAV\Connector\Sabre\File | \PHPUnit_Framework_MockObject_MockObject $file */
|
|
|
|
$file = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class)
|
|
|
|
->setConstructorArgs([$view, $info, null, $request])
|
|
|
|
->setMethods(['header'])
|
|
|
|
->getMock();
|
2015-01-26 19:48:28 +03:00
|
|
|
|
2015-07-24 15:12:50 +03:00
|
|
|
// beforeMethod locks
|
|
|
|
$view->lockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
$result = $file->put($this->getStream('test data'));
|
|
|
|
|
|
|
|
// afterMethod unlocks
|
|
|
|
$view->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
return $result;
|
2015-01-26 19:48:28 +03:00
|
|
|
}
|
|
|
|
|
2015-05-12 20:08:04 +03:00
|
|
|
/**
|
|
|
|
* Test putting a single file
|
|
|
|
*/
|
|
|
|
public function testPutSingleFile() {
|
2015-06-26 11:38:59 +03:00
|
|
|
$this->assertNotEmpty($this->doPut('/foo.txt'));
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:41:39 +03:00
|
|
|
public function legalMtimeProvider() {
|
|
|
|
return [
|
|
|
|
"string" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "string",
|
|
|
|
'expected result' => null
|
|
|
|
],
|
|
|
|
"castable string (int)" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "34",
|
|
|
|
'expected result' => 34
|
|
|
|
],
|
|
|
|
"castable string (float)" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "34.56",
|
|
|
|
'expected result' => 34
|
|
|
|
],
|
|
|
|
"float" => [
|
|
|
|
'HTTP_X_OC_MTIME' => 34.56,
|
|
|
|
'expected result' => 34
|
|
|
|
],
|
|
|
|
"zero" => [
|
|
|
|
'HTTP_X_OC_MTIME' => 0,
|
|
|
|
'expected result' => 0
|
|
|
|
],
|
|
|
|
"zero string" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "0",
|
|
|
|
'expected result' => 0
|
|
|
|
],
|
|
|
|
"negative zero string" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "-0",
|
|
|
|
'expected result' => 0
|
|
|
|
],
|
|
|
|
"string starting with number following by char" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "2345asdf",
|
|
|
|
'expected result' => null
|
|
|
|
],
|
|
|
|
"string castable hex int" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "0x45adf",
|
|
|
|
'expected result' => 0
|
|
|
|
],
|
|
|
|
"string that looks like invalid hex int" => [
|
|
|
|
'HTTP_X_OC_MTIME' => "0x123g",
|
|
|
|
'expected result' => null
|
|
|
|
],
|
|
|
|
"negative int" => [
|
|
|
|
'HTTP_X_OC_MTIME' => -34,
|
|
|
|
'expected result' => -34
|
|
|
|
],
|
|
|
|
"negative float" => [
|
|
|
|
'HTTP_X_OC_MTIME' => -34.43,
|
|
|
|
'expected result' => -34
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test putting a file with string Mtime
|
|
|
|
* @dataProvider legalMtimeProvider
|
|
|
|
*/
|
|
|
|
public function testPutSingleFileLegalMtime($requestMtime, $resultMtime) {
|
|
|
|
$request = new \OC\AppFramework\Http\Request([
|
|
|
|
'server' => [
|
|
|
|
'HTTP_X_OC_MTIME' => $requestMtime,
|
|
|
|
]
|
|
|
|
], null, $this->config, null);
|
|
|
|
$file = 'foo.txt';
|
|
|
|
|
|
|
|
if ($resultMtime === null) {
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage("X-OC-MTime header must be an integer (unix timestamp).");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->doPut($file, null, $request);
|
|
|
|
|
|
|
|
if ($resultMtime !== null) {
|
|
|
|
$this->assertEquals($resultMtime, $this->getFileInfos($file)['mtime']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test putting a file with string Mtime using chunking
|
|
|
|
* @dataProvider legalMtimeProvider
|
|
|
|
*/
|
|
|
|
public function testChunkedPutLegalMtime($requestMtime, $resultMtime) {
|
|
|
|
$request = new \OC\AppFramework\Http\Request([
|
|
|
|
'server' => [
|
|
|
|
'HTTP_X_OC_MTIME' => $requestMtime,
|
|
|
|
]
|
|
|
|
], null, $this->config, null);
|
|
|
|
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
$file = 'foo.txt';
|
|
|
|
|
|
|
|
if ($resultMtime === null) {
|
|
|
|
$this->expectException(\Sabre\DAV\Exception::class);
|
|
|
|
$this->expectExceptionMessage("X-OC-MTime header must be an integer (unix timestamp).");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->doPut($file.'-chunking-12345-2-0', null, $request);
|
|
|
|
$this->doPut($file.'-chunking-12345-2-1', null, $request);
|
|
|
|
|
|
|
|
if ($resultMtime !== null) {
|
|
|
|
$this->assertEquals($resultMtime, $this->getFileInfos($file)['mtime']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
/**
|
|
|
|
* Test putting a file using chunking
|
|
|
|
*/
|
|
|
|
public function testChunkedPut() {
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
$this->assertNull($this->doPut('/test.txt-chunking-12345-2-0'));
|
|
|
|
$this->assertNotEmpty($this->doPut('/test.txt-chunking-12345-2-1'));
|
2015-05-12 20:08:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that putting a file triggers create hooks
|
|
|
|
*/
|
|
|
|
public function testPutSingleFileTriggersHooks() {
|
|
|
|
HookHelper::setUpHooks();
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
$this->assertNotEmpty($this->doPut('/foo.txt'));
|
2015-05-12 20:08:04 +03:00
|
|
|
|
|
|
|
$this->assertCount(4, HookHelper::$hookCalls);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[0],
|
|
|
|
Filesystem::signal_create,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[1],
|
|
|
|
Filesystem::signal_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[2],
|
|
|
|
Filesystem::signal_post_create,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[3],
|
|
|
|
Filesystem::signal_post_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that putting a file triggers update hooks
|
|
|
|
*/
|
|
|
|
public function testPutOverwriteFileTriggersHooks() {
|
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
$view->file_put_contents('/foo.txt', 'some content that will be replaced');
|
|
|
|
|
|
|
|
HookHelper::setUpHooks();
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
$this->assertNotEmpty($this->doPut('/foo.txt'));
|
2015-05-12 20:08:04 +03:00
|
|
|
|
|
|
|
$this->assertCount(4, HookHelper::$hookCalls);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[0],
|
|
|
|
Filesystem::signal_update,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[1],
|
|
|
|
Filesystem::signal_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[2],
|
|
|
|
Filesystem::signal_post_update,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[3],
|
|
|
|
Filesystem::signal_post_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that putting a file triggers hooks with the correct path
|
|
|
|
* if the passed view was chrooted (can happen with public webdav
|
|
|
|
* where the root is the share root)
|
|
|
|
*/
|
|
|
|
public function testPutSingleFileTriggersHooksDifferentRoot() {
|
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
$view->mkdir('noderoot');
|
|
|
|
|
|
|
|
HookHelper::setUpHooks();
|
|
|
|
|
|
|
|
// happens with public webdav where the view root is the share root
|
2015-06-26 11:38:59 +03:00
|
|
|
$this->assertNotEmpty($this->doPut('/foo.txt', '/' . $this->user . '/files/noderoot'));
|
2015-05-12 20:08:04 +03:00
|
|
|
|
|
|
|
$this->assertCount(4, HookHelper::$hookCalls);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[0],
|
|
|
|
Filesystem::signal_create,
|
|
|
|
'/noderoot/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[1],
|
|
|
|
Filesystem::signal_write,
|
|
|
|
'/noderoot/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[2],
|
|
|
|
Filesystem::signal_post_create,
|
|
|
|
'/noderoot/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[3],
|
|
|
|
Filesystem::signal_post_write,
|
|
|
|
'/noderoot/foo.txt'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-16 12:15:41 +03:00
|
|
|
/**
|
|
|
|
* Test that putting a file with chunks triggers create hooks
|
|
|
|
*/
|
|
|
|
public function testPutChunkedFileTriggersHooks() {
|
|
|
|
HookHelper::setUpHooks();
|
|
|
|
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
$this->assertNull($this->doPut('/foo.txt-chunking-12345-2-0'));
|
|
|
|
$this->assertNotEmpty($this->doPut('/foo.txt-chunking-12345-2-1'));
|
|
|
|
|
|
|
|
$this->assertCount(4, HookHelper::$hookCalls);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[0],
|
|
|
|
Filesystem::signal_create,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[1],
|
|
|
|
Filesystem::signal_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[2],
|
|
|
|
Filesystem::signal_post_create,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[3],
|
|
|
|
Filesystem::signal_post_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that putting a chunked file triggers update hooks
|
|
|
|
*/
|
|
|
|
public function testPutOverwriteChunkedFileTriggersHooks() {
|
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
$view->file_put_contents('/foo.txt', 'some content that will be replaced');
|
|
|
|
|
|
|
|
HookHelper::setUpHooks();
|
|
|
|
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
$this->assertNull($this->doPut('/foo.txt-chunking-12345-2-0'));
|
|
|
|
$this->assertNotEmpty($this->doPut('/foo.txt-chunking-12345-2-1'));
|
|
|
|
|
|
|
|
$this->assertCount(4, HookHelper::$hookCalls);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[0],
|
|
|
|
Filesystem::signal_update,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[1],
|
|
|
|
Filesystem::signal_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[2],
|
|
|
|
Filesystem::signal_post_update,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
$this->assertHookCall(
|
|
|
|
HookHelper::$hookCalls[3],
|
|
|
|
Filesystem::signal_post_write,
|
|
|
|
'/foo.txt'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-05-12 20:08:04 +03:00
|
|
|
public static function cancellingHook($params) {
|
|
|
|
self::$hookCalls[] = array(
|
|
|
|
'signal' => Filesystem::signal_post_create,
|
|
|
|
'params' => $params
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test put file with cancelled hook
|
|
|
|
*/
|
|
|
|
public function testPutSingleFileCancelPreHook() {
|
|
|
|
\OCP\Util::connectHook(
|
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
Filesystem::signal_create,
|
|
|
|
'\Test\HookHelper',
|
|
|
|
'cancellingCallback'
|
|
|
|
);
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
// action
|
|
|
|
$thrown = false;
|
|
|
|
try {
|
|
|
|
$this->doPut('/foo.txt');
|
|
|
|
} catch (\Sabre\DAV\Exception $e) {
|
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
$this->assertEmpty($this->listPartFiles(), 'No stray part files');
|
2015-05-12 20:08:04 +03:00
|
|
|
}
|
|
|
|
|
2013-09-30 13:30:34 +04:00
|
|
|
/**
|
2015-06-26 11:38:59 +03:00
|
|
|
* Test exception when the uploaded size did not match
|
2013-09-30 13:30:34 +04:00
|
|
|
*/
|
2015-06-26 11:38:59 +03:00
|
|
|
public function testSimplePutFailsSizeCheck() {
|
2013-09-30 13:30:34 +04:00
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['rename', 'getRelativePath', 'filesize'])
|
|
|
|
->getMock();
|
2014-03-03 17:27:24 +04:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('rename')
|
|
|
|
->withAnyParameters()
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$view->expects($this->any())
|
|
|
|
->method('getRelativePath')
|
2015-06-26 11:38:59 +03:00
|
|
|
->will($this->returnArgument(0));
|
|
|
|
|
2014-07-08 11:44:46 +04:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('filesize')
|
|
|
|
->will($this->returnValue(123456));
|
|
|
|
|
|
|
|
$_SERVER['CONTENT_LENGTH'] = 123456;
|
2014-09-13 00:02:42 +04:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
2014-03-03 17:27:24 +04:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2014-11-25 18:28:41 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2014-03-03 17:27:24 +04:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2013-09-30 13:30:34 +04:00
|
|
|
|
|
|
|
// action
|
2015-06-26 11:38:59 +03:00
|
|
|
$thrown = false;
|
|
|
|
try {
|
2015-07-24 15:12:50 +03:00
|
|
|
// beforeMethod locks
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->acquireLock(ILockingProvider::LOCK_SHARED);
|
2015-07-24 15:12:50 +03:00
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
$file->put($this->getStream('test data'));
|
2015-07-24 15:12:50 +03:00
|
|
|
|
|
|
|
// afterMethod unlocks
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->releaseLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
} catch (\Sabre\DAV\Exception\BadRequest $e) {
|
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
2013-09-30 13:30:34 +04:00
|
|
|
}
|
|
|
|
|
2014-01-13 16:14:05 +04:00
|
|
|
/**
|
2015-06-26 11:38:59 +03:00
|
|
|
* Test exception during final rename in simple upload mode
|
|
|
|
*/
|
|
|
|
public function testSimplePutFailsMoveFromStorage() {
|
|
|
|
$view = new \OC\Files\View('/' . $this->user . '/files');
|
|
|
|
|
|
|
|
// simulate situation where the target file is locked
|
|
|
|
$view->lockFile('/test.txt', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/' . $this->user . '/files/test.txt', $this->getMockStorage(), null, array(
|
2015-06-26 11:38:59 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
), null);
|
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-06-26 11:38:59 +03:00
|
|
|
|
|
|
|
// action
|
|
|
|
$thrown = false;
|
|
|
|
try {
|
2015-07-24 15:12:50 +03:00
|
|
|
// beforeMethod locks
|
|
|
|
$view->lockFile($info->getPath(), ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
$file->put($this->getStream('test data'));
|
2015-07-24 15:12:50 +03:00
|
|
|
|
|
|
|
// afterMethod unlocks
|
|
|
|
$view->unlockFile($info->getPath(), ILockingProvider::LOCK_SHARED);
|
2015-08-30 20:13:01 +03:00
|
|
|
} catch (\OCA\DAV\Connector\Sabre\Exception\FileLocked $e) {
|
2015-06-26 11:38:59 +03:00
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test exception during final rename in chunk upload mode
|
|
|
|
*/
|
|
|
|
public function testChunkedPutFailsFinalRename() {
|
|
|
|
$view = new \OC\Files\View('/' . $this->user . '/files');
|
|
|
|
|
|
|
|
// simulate situation where the target file is locked
|
|
|
|
$view->lockFile('/test.txt', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/' . $this->user . '/files/test.txt-chunking-12345-2-0', $this->getMockStorage(), null, [
|
2015-06-26 11:38:59 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
], null);
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->acquireLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
$this->assertNull($file->put('test data one'));
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->releaseLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/' . $this->user . '/files/test.txt-chunking-12345-2-1', $this->getMockStorage(), null, [
|
2015-06-26 11:38:59 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
], null);
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-06-26 11:38:59 +03:00
|
|
|
|
|
|
|
// action
|
|
|
|
$thrown = false;
|
|
|
|
try {
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->acquireLock(ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
$file->put($this->getStream('test data'));
|
2015-10-05 16:41:41 +03:00
|
|
|
$file->releaseLock(ILockingProvider::LOCK_SHARED);
|
2015-08-30 20:13:01 +03:00
|
|
|
} catch (\OCA\DAV\Connector\Sabre\Exception\FileLocked $e) {
|
2015-06-26 11:38:59 +03:00
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test put file with invalid chars
|
2014-01-13 16:14:05 +04:00
|
|
|
*/
|
|
|
|
public function testSimplePutInvalidChars() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['getRelativePath'])
|
|
|
|
->getMock();
|
2014-03-03 17:27:24 +04:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('getRelativePath')
|
2015-06-26 11:38:59 +03:00
|
|
|
->will($this->returnArgument(0));
|
2014-03-03 17:27:24 +04:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/*', $this->getMockStorage(), null, array(
|
2014-11-25 18:28:41 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2014-01-13 16:14:05 +04:00
|
|
|
|
|
|
|
// action
|
2015-06-26 11:38:59 +03:00
|
|
|
$thrown = false;
|
|
|
|
try {
|
2015-07-24 15:12:50 +03:00
|
|
|
// beforeMethod locks
|
|
|
|
$view->lockFile($info->getPath(), ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
$file->put($this->getStream('test data'));
|
2015-07-24 15:12:50 +03:00
|
|
|
|
|
|
|
// afterMethod unlocks
|
|
|
|
$view->unlockFile($info->getPath(), ILockingProvider::LOCK_SHARED);
|
2015-08-30 20:13:01 +03:00
|
|
|
} catch (\OCA\DAV\Connector\Sabre\Exception\InvalidPath $e) {
|
2015-06-26 11:38:59 +03:00
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
2014-01-13 16:14:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting name with setName() with invalid chars
|
2015-04-14 16:25:52 +03:00
|
|
|
*
|
2015-08-30 20:13:01 +03:00
|
|
|
* @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath
|
2014-01-13 16:14:05 +04:00
|
|
|
*/
|
|
|
|
public function testSetNameInvalidChars() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['getRelativePath'])
|
|
|
|
->getMock();
|
2014-03-03 17:27:24 +04:00
|
|
|
|
|
|
|
$view->expects($this->any())
|
|
|
|
->method('getRelativePath')
|
2015-06-26 11:38:59 +03:00
|
|
|
->will($this->returnArgument(0));
|
2014-03-03 17:27:24 +04:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/*', $this->getMockStorage(), null, array(
|
2014-11-25 18:28:41 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2014-01-13 16:14:05 +04:00
|
|
|
$file->setName('/super*star.txt');
|
|
|
|
}
|
2014-07-08 11:44:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function testUploadAbort() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['rename', 'getRelativePath', 'filesize'])
|
|
|
|
->getMock();
|
2014-07-08 11:44:46 +04:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('rename')
|
|
|
|
->withAnyParameters()
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$view->expects($this->any())
|
|
|
|
->method('getRelativePath')
|
2015-06-26 11:38:59 +03:00
|
|
|
->will($this->returnArgument(0));
|
2014-07-08 11:44:46 +04:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('filesize')
|
|
|
|
->will($this->returnValue(123456));
|
|
|
|
|
|
|
|
$_SERVER['CONTENT_LENGTH'] = 12345;
|
2014-09-13 00:02:42 +04:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
2014-07-08 11:44:46 +04:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2014-11-25 18:28:41 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2014-07-08 11:44:46 +04:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2014-07-08 11:44:46 +04:00
|
|
|
|
|
|
|
// action
|
2015-06-26 11:38:59 +03:00
|
|
|
$thrown = false;
|
|
|
|
try {
|
2015-07-24 15:12:50 +03:00
|
|
|
// beforeMethod locks
|
|
|
|
$view->lockFile($info->getPath(), ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
$file->put($this->getStream('test data'));
|
2015-07-24 15:12:50 +03:00
|
|
|
|
|
|
|
// afterMethod unlocks
|
|
|
|
$view->unlockFile($info->getPath(), ILockingProvider::LOCK_SHARED);
|
2015-06-26 11:38:59 +03:00
|
|
|
} catch (\Sabre\DAV\Exception\BadRequest $e) {
|
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
$this->assertEmpty($this->listPartFiles($view, ''), 'No stray part files');
|
2014-07-08 11:44:46 +04:00
|
|
|
}
|
2014-09-22 14:19:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testDeleteWhenAllowed() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2014-09-22 14:19:34 +04:00
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('unlink')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2014-11-25 18:28:41 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2014-09-22 14:19:34 +04:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2014-09-22 14:19:34 +04:00
|
|
|
|
|
|
|
// action
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testDeleteThrowsWhenDeletionNotAllowed() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2014-09-22 14:19:34 +04:00
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2014-09-22 14:19:34 +04:00
|
|
|
'permissions' => 0
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2014-09-22 14:19:34 +04:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2014-09-22 14:19:34 +04:00
|
|
|
|
|
|
|
// action
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testDeleteThrowsWhenDeletionFailed() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2014-09-22 14:19:34 +04:00
|
|
|
|
|
|
|
// but fails
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('unlink')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2014-11-25 18:28:41 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
2014-12-16 16:24:48 +03:00
|
|
|
), null);
|
2014-09-22 14:19:34 +04:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2014-09-22 14:19:34 +04:00
|
|
|
|
|
|
|
// action
|
|
|
|
$file->delete();
|
|
|
|
}
|
2015-05-12 20:08:04 +03:00
|
|
|
|
2015-11-13 16:13:16 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCA\DAV\Connector\Sabre\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testDeleteThrowsWhenDeletionThrows() {
|
|
|
|
// setup
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2015-11-13 16:13:16 +03:00
|
|
|
|
|
|
|
// but fails
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('unlink')
|
|
|
|
->willThrowException(new ForbiddenException('', true));
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2015-11-13 16:13:16 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
), null);
|
|
|
|
|
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
|
|
|
|
|
|
|
// action
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
|
2015-05-12 20:08:04 +03:00
|
|
|
/**
|
|
|
|
* Asserts hook call
|
|
|
|
*
|
|
|
|
* @param array $callData hook call data to check
|
|
|
|
* @param string $signal signal name
|
|
|
|
* @param string $hookPath hook path
|
|
|
|
*/
|
|
|
|
protected function assertHookCall($callData, $signal, $hookPath) {
|
|
|
|
$this->assertEquals($signal, $callData['signal']);
|
|
|
|
$params = $callData['params'];
|
|
|
|
$this->assertEquals(
|
|
|
|
$hookPath,
|
|
|
|
$params[Filesystem::signal_param_path]
|
|
|
|
);
|
|
|
|
}
|
2015-06-12 19:50:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether locks are set before and after the operation
|
|
|
|
*/
|
|
|
|
public function testPutLocking() {
|
|
|
|
$view = new \OC\Files\View('/' . $this->user . '/files/');
|
|
|
|
|
|
|
|
$path = 'test-locking.txt';
|
|
|
|
$info = new \OC\Files\FileInfo(
|
|
|
|
'/' . $this->user . '/files/' . $path,
|
2015-12-08 18:33:39 +03:00
|
|
|
$this->getMockStorage(),
|
2015-06-12 19:50:49 +03:00
|
|
|
null,
|
|
|
|
['permissions' => \OCP\Constants::PERMISSION_ALL],
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-06-12 19:50:49 +03:00
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_SHARED),
|
|
|
|
'File unlocked before put'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE),
|
|
|
|
'File unlocked before put'
|
|
|
|
);
|
|
|
|
|
|
|
|
$wasLockedPre = false;
|
|
|
|
$wasLockedPost = false;
|
2017-10-25 01:03:28 +03:00
|
|
|
$eventHandler = $this->getMockBuilder(\stdclass::class)
|
2015-06-12 19:50:49 +03:00
|
|
|
->setMethods(['writeCallback', 'postWriteCallback'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// both pre and post hooks might need access to the file,
|
|
|
|
// so only shared lock is acceptable
|
|
|
|
$eventHandler->expects($this->once())
|
|
|
|
->method('writeCallback')
|
|
|
|
->will($this->returnCallback(
|
2015-07-24 15:12:50 +03:00
|
|
|
function () use ($view, $path, &$wasLockedPre) {
|
2015-06-12 19:50:49 +03:00
|
|
|
$wasLockedPre = $this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_SHARED);
|
|
|
|
$wasLockedPre = $wasLockedPre && !$this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$eventHandler->expects($this->once())
|
|
|
|
->method('postWriteCallback')
|
|
|
|
->will($this->returnCallback(
|
2015-07-24 15:12:50 +03:00
|
|
|
function () use ($view, $path, &$wasLockedPost) {
|
2015-06-12 19:50:49 +03:00
|
|
|
$wasLockedPost = $this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_SHARED);
|
|
|
|
$wasLockedPost = $wasLockedPost && !$this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
\OCP\Util::connectHook(
|
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
Filesystem::signal_write,
|
|
|
|
$eventHandler,
|
|
|
|
'writeCallback'
|
|
|
|
);
|
|
|
|
\OCP\Util::connectHook(
|
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
Filesystem::signal_post_write,
|
|
|
|
$eventHandler,
|
|
|
|
'postWriteCallback'
|
|
|
|
);
|
|
|
|
|
2015-07-24 15:12:50 +03:00
|
|
|
// beforeMethod locks
|
|
|
|
$view->lockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2015-06-12 19:50:49 +03:00
|
|
|
$this->assertNotEmpty($file->put($this->getStream('test data')));
|
|
|
|
|
2015-07-24 15:12:50 +03:00
|
|
|
// afterMethod unlocks
|
|
|
|
$view->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2015-06-12 19:50:49 +03:00
|
|
|
$this->assertTrue($wasLockedPre, 'File was locked during pre-hooks');
|
|
|
|
$this->assertTrue($wasLockedPost, 'File was locked during post-hooks');
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_SHARED),
|
|
|
|
'File unlocked after put'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->isFileLocked($view, $path, \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE),
|
|
|
|
'File unlocked after put'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-26 11:38:59 +03:00
|
|
|
/**
|
|
|
|
* Returns part files in the given path
|
|
|
|
*
|
|
|
|
* @param \OC\Files\View view which root is the current user's "files" folder
|
|
|
|
* @param string $path path for which to list part files
|
|
|
|
*
|
|
|
|
* @return array list of part files
|
|
|
|
*/
|
|
|
|
private function listPartFiles(\OC\Files\View $userView = null, $path = '') {
|
|
|
|
if ($userView === null) {
|
|
|
|
$userView = \OC\Files\Filesystem::getView();
|
|
|
|
}
|
|
|
|
$files = [];
|
|
|
|
list($storage, $internalPath) = $userView->resolvePath($path);
|
2015-10-12 18:21:54 +03:00
|
|
|
if($storage instanceof Local) {
|
|
|
|
$realPath = $storage->getSourcePath($internalPath);
|
|
|
|
$dh = opendir($realPath);
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if (substr($file, strlen($file) - 5, 5) === '.part') {
|
|
|
|
$files[] = $file;
|
|
|
|
}
|
2015-06-26 11:38:59 +03:00
|
|
|
}
|
2015-10-12 18:21:54 +03:00
|
|
|
closedir($dh);
|
2015-06-26 11:38:59 +03:00
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:41:39 +03:00
|
|
|
/**
|
|
|
|
* returns an array of file information filesize, mtime, filetype, mimetype
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param View $userView
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getFileInfos($path = '', View $userView = null) {
|
|
|
|
if ($userView === null) {
|
|
|
|
$userView = Filesystem::getView();
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
"filesize" => $userView->filesize($path),
|
|
|
|
"mtime" => $userView->filemtime($path),
|
|
|
|
"filetype" => $userView->filetype($path),
|
|
|
|
"mimetype" => $userView->getMimeType($path)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:40:12 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\ServiceUnavailable
|
|
|
|
*/
|
|
|
|
public function testGetFopenFails() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['fopen'])
|
|
|
|
->getMock();
|
2015-09-16 14:40:12 +03:00
|
|
|
$view->expects($this->atLeastOnce())
|
|
|
|
->method('fopen')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2015-09-16 14:40:12 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
), null);
|
|
|
|
|
2015-10-16 18:36:27 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2015-09-16 14:40:12 +03:00
|
|
|
|
|
|
|
$file->get();
|
|
|
|
}
|
2015-11-13 16:13:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCA\DAV\Connector\Sabre\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testGetFopenThrows() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['fopen'])
|
|
|
|
->getMock();
|
2015-11-13 16:13:16 +03:00
|
|
|
$view->expects($this->atLeastOnce())
|
|
|
|
->method('fopen')
|
|
|
|
->willThrowException(new ForbiddenException('', true));
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, array(
|
2015-11-13 16:13:16 +03:00
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
), null);
|
|
|
|
|
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
|
|
|
|
|
|
|
$file->get();
|
|
|
|
}
|
2017-02-24 13:56:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\NotFound
|
|
|
|
*/
|
|
|
|
public function testGetThrowsIfNoPermission() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2017-02-24 13:56:29 +03:00
|
|
|
->setMethods(['fopen'])
|
|
|
|
->getMock();
|
|
|
|
$view->expects($this->never())
|
|
|
|
->method('fopen');
|
|
|
|
|
2017-04-27 16:45:41 +03:00
|
|
|
$info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, [
|
|
|
|
'permissions' => \OCP\Constants::PERMISSION_CREATE // no read perm
|
2017-02-24 13:56:29 +03:00
|
|
|
], null);
|
|
|
|
|
2017-04-27 16:45:41 +03:00
|
|
|
$file = new \OCA\DAV\Connector\Sabre\File($view, $info);
|
2017-02-24 13:56:29 +03:00
|
|
|
|
|
|
|
$file->get();
|
|
|
|
}
|
2013-09-24 17:44:02 +04:00
|
|
|
}
|