2013-08-08 05:04:40 -04:00
|
|
|
<?php
|
2024-05-28 06:34:11 -04:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
declare(strict_types=1);
|
2016-01-12 09:02:16 -05:00
|
|
|
/**
|
2024-05-28 06:34:11 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2013-2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-01-12 09:02:16 -05:00
|
|
|
*/
|
2016-05-25 10:04:15 -04:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2020-04-09 05:48:10 -04:00
|
|
|
|
2017-09-13 07:12:16 -04:00
|
|
|
use OC\Files\View;
|
|
|
|
|
use OCA\DAV\Connector\Sabre\QuotaPlugin;
|
2017-04-12 06:27:47 -04:00
|
|
|
use OCP\Files\FileInfo;
|
2016-10-13 06:59:10 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class QuotaPluginTest extends TestCase {
|
2025-05-27 17:36:08 -04:00
|
|
|
private \Sabre\DAV\Server $server;
|
2013-08-08 05:04:40 -04:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
private QuotaPlugin $plugin;
|
2013-08-08 05:04:40 -04:00
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
private function init(int $quota, string $checkedPath = ''): void {
|
2025-09-24 20:53:57 -04:00
|
|
|
$view = $this->buildFileViewMock($quota, $checkedPath);
|
2014-01-09 08:25:48 -05:00
|
|
|
$this->server = new \Sabre\DAV\Server();
|
2025-05-27 17:36:08 -04:00
|
|
|
$this->plugin = new QuotaPlugin($view);
|
2013-08-08 05:04:40 -04:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'lengthProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testLength(?int $expected, array $headers): void {
|
2014-03-03 08:27:24 -05:00
|
|
|
$this->init(0);
|
2025-05-27 17:36:08 -04:00
|
|
|
|
2020-03-10 08:43:18 -04:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2013-08-08 05:04:40 -04:00
|
|
|
$length = $this->plugin->getLength();
|
|
|
|
|
$this->assertEquals($expected, $length);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'quotaOkayProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testCheckQuota(int $quota, array $headers): void {
|
2014-03-03 08:27:24 -05:00
|
|
|
$this->init($quota);
|
2013-08-08 07:33:00 -04:00
|
|
|
|
2020-03-10 08:43:18 -04:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2013-08-08 07:33:00 -04:00
|
|
|
$result = $this->plugin->checkQuota('');
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'quotaExceededProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testCheckExceededQuota(int $quota, array $headers): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\InsufficientStorage::class);
|
|
|
|
|
|
2014-03-03 08:27:24 -05:00
|
|
|
$this->init($quota);
|
2013-08-08 07:33:00 -04:00
|
|
|
|
2020-03-10 08:43:18 -04:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2013-08-08 07:33:00 -04:00
|
|
|
$this->plugin->checkQuota('');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'quotaOkayProvider')]
|
2025-05-27 17:36:08 -04:00
|
|
|
public function testCheckQuotaOnPath(int $quota, array $headers): void {
|
2016-01-11 06:38:13 -05:00
|
|
|
$this->init($quota, 'sub/test.txt');
|
|
|
|
|
|
2020-03-10 08:43:18 -04:00
|
|
|
$this->server->httpRequest = new \Sabre\HTTP\Request('POST', 'dummy.file', $headers);
|
2016-01-11 06:38:13 -05:00
|
|
|
$result = $this->plugin->checkQuota('/sub/test.txt');
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function quotaOkayProvider(): array {
|
2020-03-26 04:30:18 -04: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 07:33:00 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function quotaExceededProvider(): array {
|
2020-03-26 04:30:18 -04:00
|
|
|
return [
|
|
|
|
|
[1023, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
|
[511, ['CONTENT-LENGTH' => '512']],
|
|
|
|
|
[2047, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024']],
|
|
|
|
|
];
|
2013-08-08 07:33:00 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function lengthProvider(): array {
|
2016-10-13 06:59:10 -04: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 03:09:21 -04: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']],
|
2024-03-21 07:11:44 -04:00
|
|
|
[2048, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => 'A']],
|
|
|
|
|
[2048, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => 'A']],
|
2016-10-13 06:59:10 -04:00
|
|
|
];
|
2013-08-08 05:04:40 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function quotaChunkedOkProvider(): array {
|
2023-02-21 01:36:43 -05:00
|
|
|
return [
|
|
|
|
|
[1024, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
|
[1024, 0, ['CONTENT-LENGTH' => '512']],
|
|
|
|
|
[1024, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
|
// with existing chunks (allowed size = total length - chunk total size)
|
|
|
|
|
[400, 128, ['X-EXPECTED-ENTITY-LENGTH' => '512']],
|
|
|
|
|
[400, 128, ['CONTENT-LENGTH' => '512']],
|
|
|
|
|
[400, 128, ['OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500']],
|
|
|
|
|
// \OCP\Files\FileInfo::SPACE-UNKNOWN = -2
|
|
|
|
|
[-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']],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 17:36:08 -04:00
|
|
|
public static function quotaChunkedFailProvider(): array {
|
2023-02-21 01:36:43 -05:00
|
|
|
return [
|
|
|
|
|
[400, 0, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
|
|
|
|
|
[400, 0, ['CONTENT-LENGTH' => '512']],
|
|
|
|
|
[400, 0, ['OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512']],
|
|
|
|
|
// with existing chunks (allowed size = total length - chunk total size)
|
|
|
|
|
[380, 128, ['X-EXPECTED-ENTITY-LENGTH' => '512']],
|
|
|
|
|
[380, 128, ['CONTENT-LENGTH' => '512']],
|
|
|
|
|
[380, 128, ['OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500']],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 20:53:57 -04:00
|
|
|
/**
|
|
|
|
|
* Build a mock for the View class with a controlled free_space() response.
|
|
|
|
|
*
|
|
|
|
|
* @param int|float|false $quota The quota value to return from free_space().
|
|
|
|
|
* @param string $checkedPath The path expected as a parameter to free_space().
|
|
|
|
|
* @return View&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
|
*/
|
2025-09-24 20:46:39 -04:00
|
|
|
private function buildFileViewMock(int|float|false $quota, string $checkedPath): View {
|
2025-05-27 17:36:08 -04:00
|
|
|
// mock filesystem
|
2017-09-13 07:12:16 -04:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2025-05-27 17:36:08 -04:00
|
|
|
->onlyMethods(['free_space'])
|
2016-07-15 03:52:46 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2015-05-13 07:58:01 -04:00
|
|
|
$view->expects($this->any())
|
|
|
|
|
->method('free_space')
|
2025-05-27 17:36:08 -04:00
|
|
|
->with($checkedPath)
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($quota);
|
2013-08-08 07:33:00 -04:00
|
|
|
|
|
|
|
|
return $view;
|
|
|
|
|
}
|
2013-08-08 05:04:40 -04:00
|
|
|
}
|