2013-08-08 13:04:40 +04:00
|
|
|
<?php
|
2016-01-12 17:02:16 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @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>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2020-04-09 12:48:10 +03:00
|
|
|
|
2017-09-13 14:12:16 +03:00
|
|
|
use OC\Files\View;
|
|
|
|
use OCA\DAV\Connector\Sabre\QuotaPlugin;
|
2017-04-12 13:27:47 +03:00
|
|
|
use OCP\Files\FileInfo;
|
2016-10-13 13:59:10 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2013-08-08 13:04:40 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
2016-10-13 13:59:10 +03:00
|
|
|
class QuotaPluginTest extends TestCase {
|
2013-08-08 13:04:40 +04:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject */
|
2013-08-08 13:04:40 +04:00
|
|
|
private $server;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\DAV\Connector\Sabre\QuotaPlugin | \PHPUnit\Framework\MockObject\MockObject */
|
2013-08-08 13:04:40 +04:00
|
|
|
private $plugin;
|
|
|
|
|
2016-01-11 14:38:13 +03:00
|
|
|
private function init($quota, $checkedPath = '') {
|
|
|
|
$view = $this->buildFileViewMock($quota, $checkedPath);
|
2014-01-09 17:25:48 +04:00
|
|
|
$this->server = new \Sabre\DAV\Server();
|
2017-09-13 14:12:16 +03:00
|
|
|
$this->plugin = $this->getMockBuilder(QuotaPlugin::class)
|
2016-01-11 14:38:13 +03:00
|
|
|
->setConstructorArgs([$view])
|
|
|
|
->setMethods(['getFileChunking'])
|
|
|
|
->getMock();
|
2013-08-08 13:04:40 +04:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider lengthProvider
|
|
|
|
*/
|
2014-03-03 17:27:24 +04:00
|
|
|
public function testLength($expected, $headers) {
|
|
|
|
$this->init(0);
|
2016-01-11 14:38:13 +03:00
|
|
|
$this->plugin->expects($this->never())
|
|
|
|
->method('getFileChunking');
|
2020-03-10 15:43:18 +03:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2013-08-08 13:04:40 +04:00
|
|
|
$length = $this->plugin->getLength();
|
|
|
|
$this->assertEquals($expected, $length);
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:33:00 +04:00
|
|
|
/**
|
|
|
|
* @dataProvider quotaOkayProvider
|
|
|
|
*/
|
2014-03-03 17:27:24 +04:00
|
|
|
public function testCheckQuota($quota, $headers) {
|
|
|
|
$this->init($quota);
|
2016-01-11 14:38:13 +03:00
|
|
|
$this->plugin->expects($this->never())
|
|
|
|
->method('getFileChunking');
|
2013-08-08 15:33:00 +04:00
|
|
|
|
2020-03-10 15:43:18 +03:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2013-08-08 15:33:00 +04:00
|
|
|
$result = $this->plugin->checkQuota('');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider quotaExceededProvider
|
|
|
|
*/
|
2014-03-03 17:27:24 +04:00
|
|
|
public function testCheckExceededQuota($quota, $headers) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\InsufficientStorage::class);
|
|
|
|
|
2014-03-03 17:27:24 +04:00
|
|
|
$this->init($quota);
|
2016-01-11 14:38:13 +03:00
|
|
|
$this->plugin->expects($this->never())
|
|
|
|
->method('getFileChunking');
|
2013-08-08 15:33:00 +04:00
|
|
|
|
2020-03-10 15:43:18 +03:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2013-08-08 15:33:00 +04:00
|
|
|
$this->plugin->checkQuota('');
|
|
|
|
}
|
|
|
|
|
2016-01-11 14:38:13 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider quotaOkayProvider
|
|
|
|
*/
|
|
|
|
public function testCheckQuotaOnPath($quota, $headers) {
|
|
|
|
$this->init($quota, 'sub/test.txt');
|
|
|
|
$this->plugin->expects($this->never())
|
|
|
|
->method('getFileChunking');
|
|
|
|
|
2020-03-10 15:43:18 +03:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2016-01-11 14:38:13 +03:00
|
|
|
$result = $this->plugin->checkQuota('/sub/test.txt');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:33:00 +04:00
|
|
|
public function quotaOkayProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[1024, []],
|
|
|
|
[1024, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[1024, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[1024, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
|
|
|
|
[FileInfo::SPACE_UNKNOWN, []],
|
|
|
|
[FileInfo::SPACE_UNKNOWN, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[FileInfo::SPACE_UNKNOWN, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[FileInfo::SPACE_UNKNOWN, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
|
|
|
|
[FileInfo::SPACE_UNLIMITED, []],
|
|
|
|
[FileInfo::SPACE_UNLIMITED, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[FileInfo::SPACE_UNLIMITED, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[FileInfo::SPACE_UNLIMITED, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
];
|
2013-08-08 15:33:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function quotaExceededProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[1023, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[511, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[2047, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024']],
|
|
|
|
];
|
2013-08-08 15:33:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function lengthProvider() {
|
2016-10-13 13:59:10 +03:00
|
|
|
return [
|
|
|
|
[null, []],
|
|
|
|
[1024, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[512, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[2048, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024']],
|
|
|
|
[4096, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => '4096']],
|
2016-10-14 10:09:21 +03:00
|
|
|
[null, ['X-EXPECTED-ENTITY-LENGTH' => 'A']],
|
|
|
|
[null, ['CONTENT-LENGTH' => 'A']],
|
|
|
|
[1024, ['OC-TOTAL-LENGTH' => 'A', 'CONTENT-LENGTH' => '1024']],
|
|
|
|
[1024, ['OC-TOTAL-LENGTH' => 'A', 'X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[null, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => 'A']],
|
|
|
|
[null, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => 'A']],
|
2016-10-13 13:59:10 +03:00
|
|
|
];
|
2013-08-08 13:04:40 +04:00
|
|
|
}
|
|
|
|
|
2016-01-11 14:38:13 +03:00
|
|
|
public function quotaChunkedOkProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[1024, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[1024, 0, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[1024, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
2016-01-11 14:38:13 +03:00
|
|
|
// with existing chunks (allowed size = total length - chunk total size)
|
2020-03-26 11:30:18 +03:00
|
|
|
[400, 128, ['X-EXPECTED-ENTITY-LENGTH' => '512']],
|
|
|
|
[400, 128, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[400, 128, ['OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500']],
|
2016-01-11 14:38:13 +03:00
|
|
|
// \OCP\Files\FileInfo::SPACE-UNKNOWN = -2
|
2020-03-26 11:30:18 +03:00
|
|
|
[-2, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[-2, 0, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[-2, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
[-2, 128, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[-2, 128, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[-2, 128, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
];
|
2016-01-11 14:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider quotaChunkedOkProvider
|
|
|
|
*/
|
|
|
|
public function testCheckQuotaChunkedOk($quota, $chunkTotalSize, $headers) {
|
|
|
|
$this->init($quota, 'sub/test.txt');
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$mockChunking = $this->getMockBuilder(\OC_FileChunking::class)
|
2016-01-11 14:38:13 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$mockChunking->expects($this->once())
|
|
|
|
->method('getCurrentSize')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($chunkTotalSize);
|
2016-01-11 14:38:13 +03:00
|
|
|
|
|
|
|
$this->plugin->expects($this->once())
|
|
|
|
->method('getFileChunking')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($mockChunking);
|
2016-01-11 14:38:13 +03:00
|
|
|
|
|
|
|
$headers['OC-CHUNKED'] = 1;
|
2020-03-10 15:43:18 +03:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2016-01-11 14:38:13 +03:00
|
|
|
$result = $this->plugin->checkQuota('/sub/test.txt-chunking-12345-3-1');
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quotaChunkedFailProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[400, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
[400, 0, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[400, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
2016-01-11 14:38:13 +03:00
|
|
|
// with existing chunks (allowed size = total length - chunk total size)
|
2020-03-26 11:30:18 +03:00
|
|
|
[380, 128, ['X-EXPECTED-ENTITY-LENGTH' => '512']],
|
|
|
|
[380, 128, ['CONTENT-LENGTH' => '512']],
|
|
|
|
[380, 128, ['OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500']],
|
|
|
|
];
|
2016-01-11 14:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider quotaChunkedFailProvider
|
|
|
|
*/
|
|
|
|
public function testCheckQuotaChunkedFail($quota, $chunkTotalSize, $headers) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\InsufficientStorage::class);
|
|
|
|
|
2016-01-11 14:38:13 +03:00
|
|
|
$this->init($quota, 'sub/test.txt');
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$mockChunking = $this->getMockBuilder(\OC_FileChunking::class)
|
2016-01-11 14:38:13 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$mockChunking->expects($this->once())
|
|
|
|
->method('getCurrentSize')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($chunkTotalSize);
|
2016-01-11 14:38:13 +03:00
|
|
|
|
|
|
|
$this->plugin->expects($this->once())
|
|
|
|
->method('getFileChunking')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($mockChunking);
|
2016-01-11 14:38:13 +03:00
|
|
|
|
|
|
|
$headers['OC-CHUNKED'] = 1;
|
2020-03-10 15:43:18 +03:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2016-01-11 14:38:13 +03:00
|
|
|
$this->plugin->checkQuota('/sub/test.txt-chunking-12345-3-1');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildFileViewMock($quota, $checkedPath) {
|
2013-08-08 15:33:00 +04:00
|
|
|
// mock filesysten
|
2017-09-13 14:12:16 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['free_space'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-05-13 14:58:01 +03:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('free_space')
|
2016-01-11 14:38:13 +03:00
|
|
|
->with($this->identicalTo($checkedPath))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($quota);
|
2013-08-08 15:33:00 +04:00
|
|
|
|
|
|
|
return $view;
|
|
|
|
}
|
2013-08-08 13:04:40 +04:00
|
|
|
}
|