2015-08-07 17:04:27 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +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>
|
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
2015-08-07 17:04:27 +03:00
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
|
2015-08-07 17:04:27 +03:00
|
|
|
|
2015-09-14 16:33:34 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\Lock\ILockingProvider;
|
2015-09-08 16:33:52 +03:00
|
|
|
|
2015-11-20 13:27:11 +03:00
|
|
|
/**
|
|
|
|
* Class UploadTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
2016-05-25 17:04:15 +03:00
|
|
|
* @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest
|
2015-11-20 13:27:11 +03:00
|
|
|
*/
|
2017-04-27 10:49:37 +03:00
|
|
|
class UploadTest extends RequestTestCase {
|
2015-08-07 17:04:27 +03:00
|
|
|
public function testBasicUpload() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
|
2015-09-08 16:33:52 +03:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 17:04:27 +03:00
|
|
|
$this->assertTrue($view->file_exists('foo.txt'));
|
|
|
|
$this->assertEquals('asd', $view->file_get_contents('foo.txt'));
|
2015-08-12 16:18:30 +03:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(3, $info->getSize());
|
2015-08-07 17:04:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUploadOverWrite() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
2015-08-12 16:18:30 +03:00
|
|
|
$view->file_put_contents('foo.txt', 'foobar');
|
2015-08-07 17:04:27 +03:00
|
|
|
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
|
2015-09-08 16:33:52 +03:00
|
|
|
$this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
|
2015-08-07 17:04:27 +03:00
|
|
|
$this->assertEquals('asd', $view->file_get_contents('foo.txt'));
|
2015-08-12 16:18:30 +03:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(3, $info->getSize());
|
2015-08-07 17:04:27 +03:00
|
|
|
}
|
|
|
|
|
2015-09-14 16:33:34 +03:00
|
|
|
public function testUploadOverWriteReadLocked() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$view->file_put_contents('foo.txt', 'bar');
|
|
|
|
|
|
|
|
$view->lockFile('/foo.txt', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2018-11-04 15:21:34 +03:00
|
|
|
$result = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
$this->assertEquals(Http::STATUS_LOCKED, $result->getStatus());
|
2015-09-14 16:33:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUploadOverWriteWriteLocked() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
2016-09-23 18:21:07 +03:00
|
|
|
$this->loginAsUser($user);
|
2015-09-14 16:33:34 +03:00
|
|
|
|
|
|
|
$view->file_put_contents('foo.txt', 'bar');
|
|
|
|
|
|
|
|
$view->lockFile('/foo.txt', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
2018-11-04 15:21:34 +03:00
|
|
|
$result = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
$this->assertEquals(Http::STATUS_LOCKED, $result->getStatus());
|
2015-09-14 16:33:34 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:04:27 +03:00
|
|
|
public function testChunkedUpload() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->getStatus());
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
|
|
|
|
|
2015-09-08 16:33:52 +03:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 17:04:27 +03:00
|
|
|
$this->assertTrue($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
|
2015-08-12 16:18:30 +03:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(6, $info->getSize());
|
2015-08-07 17:04:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChunkedUploadOverWrite() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$view->file_put_contents('foo.txt', 'bar');
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
|
2015-09-08 16:33:52 +03:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 17:04:27 +03:00
|
|
|
$this->assertEquals('bar', $view->file_get_contents('foo.txt'));
|
|
|
|
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
|
|
|
|
|
2015-09-08 16:33:52 +03:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 17:04:27 +03:00
|
|
|
|
|
|
|
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
|
2015-08-12 16:18:30 +03:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(6, $info->getSize());
|
2015-08-07 17:04:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChunkedUploadOutOfOrder() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
|
|
|
|
|
2015-09-08 16:33:52 +03:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 17:04:27 +03:00
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->getStatus());
|
|
|
|
$this->assertTrue($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
|
2015-08-12 16:18:30 +03:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(6, $info->getSize());
|
2015-08-07 17:04:27 +03:00
|
|
|
}
|
2015-09-14 16:33:34 +03:00
|
|
|
|
|
|
|
public function testChunkedUploadOutOfOrderReadLocked() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$view->lockFile('/foo.txt', ILockingProvider::LOCK_SHARED);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
|
2015-10-20 17:22:45 +03:00
|
|
|
} catch (\OCA\DAV\Connector\Sabre\Exception\FileLocked $e) {
|
2015-09-14 16:33:34 +03:00
|
|
|
$this->fail('Didn\'t expect locked error for the first chunk on read lock');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
// last chunk should trigger the locked error since it tries to assemble
|
2018-11-04 15:21:34 +03:00
|
|
|
$result = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
$this->assertEquals(Http::STATUS_LOCKED, $result->getStatus());
|
2015-09-14 16:33:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChunkedUploadOutOfOrderWriteLocked() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$view->lockFile('/foo.txt', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
|
2015-10-20 17:22:45 +03:00
|
|
|
} catch (\OCA\DAV\Connector\Sabre\Exception\FileLocked $e) {
|
2015-09-14 16:33:34 +03:00
|
|
|
$this->fail('Didn\'t expect locked error for the first chunk on write lock'); // maybe forbid this in the future for write locks only?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
|
|
|
$this->assertFalse($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
// last chunk should trigger the locked error since it tries to assemble
|
2018-11-04 15:21:34 +03:00
|
|
|
$result = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
$this->assertEquals(Http::STATUS_LOCKED, $result->getStatus());
|
2015-09-14 16:33:34 +03:00
|
|
|
}
|
2015-08-07 17:04:27 +03:00
|
|
|
}
|