2012-02-12 12:06:32 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2012-02-12 12:06:32 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2014-10-24 10:07:45 -04:00
|
|
|
*/
|
2012-02-12 12:06:32 -05:00
|
|
|
|
2012-09-22 08:51:15 -04:00
|
|
|
namespace Test\Files\Storage;
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\Files\Storage\Local;
|
2024-10-02 11:00:53 -04:00
|
|
|
use OC\Files\Storage\Wrapper\Jail;
|
2025-05-16 17:09:37 -04:00
|
|
|
use OCP\Files;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Files\ForbiddenException;
|
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
|
use OCP\ITempManager;
|
|
|
|
|
use OCP\Server;
|
2024-10-02 11:00:53 -04:00
|
|
|
|
2015-11-20 05:27:11 -05:00
|
|
|
/**
|
2016-05-20 09:38:20 -04:00
|
|
|
* Class LocalTest
|
2015-11-20 05:27:11 -05:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @package Test\Files\Storage
|
|
|
|
|
*/
|
2025-10-20 19:52:40 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\Group('DB')]
|
2016-05-20 09:38:20 -04:00
|
|
|
class LocalTest extends Storage {
|
2012-02-12 12:06:32 -05:00
|
|
|
/**
|
|
|
|
|
* @var string tmpDir
|
|
|
|
|
*/
|
|
|
|
|
private $tmpDir;
|
2014-10-24 10:07:45 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-07 09:23:15 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->tmpDir = Server::get(ITempManager::class)->getTemporaryFolder();
|
|
|
|
|
$this->instance = new Local(['datadir' => $this->tmpDir]);
|
2012-02-12 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function tearDown(): void {
|
2025-05-16 17:09:37 -04:00
|
|
|
Files::rmdirr($this->tmpDir);
|
2014-11-07 09:23:15 -05:00
|
|
|
parent::tearDown();
|
2012-02-12 12:06:32 -05:00
|
|
|
}
|
2014-10-24 10:07:45 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testStableEtag(): void {
|
2014-10-24 10:07:45 -04:00
|
|
|
$this->instance->file_put_contents('test.txt', 'foobar');
|
|
|
|
|
$etag1 = $this->instance->getETag('test.txt');
|
|
|
|
|
$etag2 = $this->instance->getETag('test.txt');
|
|
|
|
|
$this->assertEquals($etag1, $etag2);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEtagChange(): void {
|
2014-10-24 10:07:45 -04:00
|
|
|
$this->instance->file_put_contents('test.txt', 'foo');
|
|
|
|
|
$this->instance->touch('test.txt', time() - 2);
|
|
|
|
|
$etag1 = $this->instance->getETag('test.txt');
|
|
|
|
|
$this->instance->file_put_contents('test.txt', 'bar');
|
|
|
|
|
$etag2 = $this->instance->getETag('test.txt');
|
|
|
|
|
$this->assertNotEquals($etag1, $etag2);
|
|
|
|
|
}
|
2016-04-26 10:08:52 -04:00
|
|
|
|
2021-01-22 09:44:24 -05:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testInvalidArgumentsEmptyArray(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
new Local([]);
|
2016-04-26 10:08:52 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 09:44:24 -05:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testInvalidArgumentsNoArray(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
new Local([]);
|
2016-04-26 10:08:52 -04:00
|
|
|
}
|
2016-05-30 09:44:19 -04:00
|
|
|
|
2021-01-22 09:44:24 -05:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testDisallowSymlinksOutsideDatadir(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(ForbiddenException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2016-05-30 09:44:19 -04:00
|
|
|
$subDir1 = $this->tmpDir . 'sub1';
|
|
|
|
|
$subDir2 = $this->tmpDir . 'sub2';
|
|
|
|
|
$sym = $this->tmpDir . 'sub1/sym';
|
|
|
|
|
mkdir($subDir1);
|
|
|
|
|
mkdir($subDir2);
|
|
|
|
|
|
|
|
|
|
symlink($subDir2, $sym);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$storage = new Local(['datadir' => $subDir1]);
|
2016-05-30 09:44:19 -04:00
|
|
|
|
|
|
|
|
$storage->file_put_contents('sym/foo', 'bar');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testDisallowSymlinksInsideDatadir(): void {
|
2016-05-30 09:44:19 -04:00
|
|
|
$subDir1 = $this->tmpDir . 'sub1';
|
|
|
|
|
$subDir2 = $this->tmpDir . 'sub1/sub2';
|
|
|
|
|
$sym = $this->tmpDir . 'sub1/sym';
|
|
|
|
|
mkdir($subDir1);
|
|
|
|
|
mkdir($subDir2);
|
|
|
|
|
|
|
|
|
|
symlink($subDir2, $sym);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$storage = new Local(['datadir' => $subDir1]);
|
2016-05-30 09:44:19 -04:00
|
|
|
|
|
|
|
|
$storage->file_put_contents('sym/foo', 'bar');
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2016-05-30 09:44:19 -04:00
|
|
|
}
|
2021-01-22 09:44:24 -05:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteUmaskFilePutContents(): void {
|
2021-01-22 09:44:24 -05:00
|
|
|
$oldMask = umask(0333);
|
|
|
|
|
$this->instance->file_put_contents('test.txt', 'sad');
|
|
|
|
|
umask($oldMask);
|
|
|
|
|
$this->assertTrue($this->instance->isUpdatable('test.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteUmaskMkdir(): void {
|
2021-01-22 09:44:24 -05:00
|
|
|
$oldMask = umask(0333);
|
|
|
|
|
$this->instance->mkdir('test.txt');
|
|
|
|
|
umask($oldMask);
|
|
|
|
|
$this->assertTrue($this->instance->isUpdatable('test.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteUmaskFopen(): void {
|
2021-01-22 09:44:24 -05:00
|
|
|
$oldMask = umask(0333);
|
|
|
|
|
$handle = $this->instance->fopen('test.txt', 'w');
|
|
|
|
|
fwrite($handle, 'foo');
|
|
|
|
|
fclose($handle);
|
|
|
|
|
umask($oldMask);
|
|
|
|
|
$this->assertTrue($this->instance->isUpdatable('test.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testWriteUmaskCopy(): void {
|
2021-01-22 09:44:24 -05:00
|
|
|
$this->instance->file_put_contents('source.txt', 'sad');
|
|
|
|
|
$oldMask = umask(0333);
|
|
|
|
|
$this->instance->copy('source.txt', 'test.txt');
|
|
|
|
|
umask($oldMask);
|
|
|
|
|
$this->assertTrue($this->instance->isUpdatable('test.txt'));
|
|
|
|
|
}
|
2023-08-03 17:09:17 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testUnavailableExternal(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->expectException(StorageNotAvailableException::class);
|
|
|
|
|
$this->instance = new Local(['datadir' => $this->tmpDir . '/unexist', 'isExternal' => true]);
|
2023-08-03 17:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testUnavailableNonExternal(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->instance = new Local(['datadir' => $this->tmpDir . '/unexist']);
|
2023-08-03 17:09:17 -04:00
|
|
|
// no exception thrown
|
|
|
|
|
$this->assertNotNull($this->instance);
|
|
|
|
|
}
|
2024-10-02 11:00:53 -04:00
|
|
|
|
|
|
|
|
public function testMoveNestedJail(): void {
|
|
|
|
|
$this->instance->mkdir('foo');
|
|
|
|
|
$this->instance->mkdir('foo/bar');
|
|
|
|
|
$this->instance->mkdir('target');
|
|
|
|
|
$this->instance->file_put_contents('foo/bar/file.txt', 'foo');
|
|
|
|
|
$jail1 = new Jail([
|
|
|
|
|
'storage' => $this->instance,
|
|
|
|
|
'root' => 'foo'
|
|
|
|
|
]);
|
|
|
|
|
$jail2 = new Jail([
|
|
|
|
|
'storage' => $jail1,
|
|
|
|
|
'root' => 'bar'
|
|
|
|
|
]);
|
|
|
|
|
$jail3 = new Jail([
|
|
|
|
|
'storage' => $this->instance,
|
|
|
|
|
'root' => 'target'
|
|
|
|
|
]);
|
|
|
|
|
$jail3->moveFromStorage($jail2, 'file.txt', 'file.txt');
|
|
|
|
|
$this->assertTrue($this->instance->file_exists('target/file.txt'));
|
|
|
|
|
}
|
2012-02-12 12:06:32 -05:00
|
|
|
}
|