2015-08-07 14:04:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2015-10-14 12:57:23 +00:00
|
|
|
namespace OCA\DAV\Tests\Unit\Connector\Sabre\RequestTest;
|
2015-08-07 14:04:27 +00:00
|
|
|
|
2015-09-14 13:33:34 +00:00
|
|
|
use OC\Connector\Sabre\Exception\FileLocked;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\Lock\ILockingProvider;
|
2015-09-08 13:33:52 +00:00
|
|
|
|
2015-08-07 14:04:27 +00:00
|
|
|
class UploadTest extends RequestTest {
|
|
|
|
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 13:33:52 +00:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 14:04:27 +00:00
|
|
|
$this->assertTrue($view->file_exists('foo.txt'));
|
|
|
|
$this->assertEquals('asd', $view->file_get_contents('foo.txt'));
|
2015-08-12 13:18:30 +00:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(3, $info->getSize());
|
2015-08-07 14:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUploadOverWrite() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
2015-08-12 13:18:30 +00:00
|
|
|
$view->file_put_contents('foo.txt', 'foobar');
|
2015-08-07 14:04:27 +00:00
|
|
|
|
|
|
|
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
|
2015-09-08 13:33:52 +00:00
|
|
|
$this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
|
2015-08-07 14:04:27 +00:00
|
|
|
$this->assertEquals('asd', $view->file_get_contents('foo.txt'));
|
2015-08-12 13:18:30 +00:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(3, $info->getSize());
|
2015-08-07 14:04:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 13:33:34 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \OC\Connector\Sabre\Exception\FileLocked
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
$this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OC\Connector\Sabre\Exception\FileLocked
|
|
|
|
*/
|
|
|
|
public function testUploadOverWriteWriteLocked() {
|
|
|
|
$user = $this->getUniqueID();
|
|
|
|
$view = $this->setupUser($user, 'pass');
|
|
|
|
|
|
|
|
$view->file_put_contents('foo.txt', 'bar');
|
|
|
|
|
|
|
|
$view->lockFile('/foo.txt', ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
$this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:04:27 +00: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 13:33:52 +00:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 14:04:27 +00:00
|
|
|
$this->assertTrue($view->file_exists('foo.txt'));
|
|
|
|
|
|
|
|
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
|
2015-08-12 13:18:30 +00:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(6, $info->getSize());
|
2015-08-07 14:04:27 +00: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 13:33:52 +00:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 14:04:27 +00: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 13:33:52 +00:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 14:04:27 +00:00
|
|
|
|
|
|
|
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
|
2015-08-12 13:18:30 +00:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(6, $info->getSize());
|
2015-08-07 14:04:27 +00: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 13:33:52 +00:00
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
2015-08-07 14:04:27 +00: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 13:18:30 +00:00
|
|
|
|
|
|
|
$info = $view->getFileInfo('foo.txt');
|
|
|
|
$this->assertInstanceOf('\OC\Files\FileInfo', $info);
|
|
|
|
$this->assertEquals(6, $info->getSize());
|
2015-08-07 14:04:27 +00:00
|
|
|
}
|
2015-09-14 13:33:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OC\Connector\Sabre\Exception\FileLocked
|
|
|
|
*/
|
|
|
|
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']);
|
|
|
|
} catch (FileLocked $e) {
|
|
|
|
$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
|
|
|
|
$this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OC\Connector\Sabre\Exception\FileLocked
|
|
|
|
*/
|
|
|
|
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']);
|
|
|
|
} catch (FileLocked $e) {
|
|
|
|
$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
|
|
|
|
$this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
|
|
|
|
}
|
2015-08-07 14:04:27 +00:00
|
|
|
}
|