filenameValidator = $this->createMock(IFilenameValidator::class); $this->overwriteService(IFilenameValidator::class, $this->filenameValidator); $this->tmpDir = \OCP\Server::get(ITempManager::class)->getTemporaryFolder(); $this->instance = new \OC\Files\Storage\CommonTest(['datadir' => $this->tmpDir]); } protected function tearDown(): void { \OC_Helper::rmdirr($this->tmpDir); $this->restoreService(IFilenameValidator::class); parent::tearDown(); } public function testVerifyPath(): void { $this->filenameValidator ->expects($this->once()) ->method('validateFilename') ->with('invalid:char.txt') ->willThrowException(new InvalidCharacterInPathException()); $this->expectException(InvalidPathException::class); $this->instance->verifyPath('/', 'invalid:char.txt'); } public function testVerifyPathSucceed(): void { $this->filenameValidator ->expects($this->once()) ->method('validateFilename') ->with('valid-char.txt'); $this->instance->verifyPath('/', 'valid-char.txt'); } public function testMoveFromStorageWrapped(): void { /** @var \OC\Files\Storage\CommonTest|MockObject $instance */ $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class) ->onlyMethods(['copyFromStorage', 'rmdir', 'unlink']) ->setConstructorArgs([['datadir' => $this->tmpDir]]) ->getMock(); $instance->method('copyFromStorage') ->willThrowException(new \Exception('copy')); $source = new Wrapper([ 'storage' => $instance, ]); $instance->file_put_contents('foo.txt', 'bar'); $instance->moveFromStorage($source, 'foo.txt', 'bar.txt'); $this->assertTrue($instance->file_exists('bar.txt')); } public function testMoveFromStorageJailed(): void { /** @var \OC\Files\Storage\CommonTest|MockObject $instance */ $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class) ->onlyMethods(['copyFromStorage', 'rmdir', 'unlink']) ->setConstructorArgs([['datadir' => $this->tmpDir]]) ->getMock(); $instance->method('copyFromStorage') ->willThrowException(new \Exception('copy')); $source = new Jail([ 'storage' => $instance, 'root' => 'foo' ]); $source = new Wrapper([ 'storage' => $source ]); $instance->mkdir('foo'); $instance->file_put_contents('foo/foo.txt', 'bar'); $instance->moveFromStorage($source, 'foo.txt', 'bar.txt'); $this->assertTrue($instance->file_exists('bar.txt')); } public function testMoveFromStorageNestedJail(): void { /** @var \OC\Files\Storage\CommonTest|MockObject $instance */ $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class) ->onlyMethods(['copyFromStorage', 'rmdir', 'unlink']) ->setConstructorArgs([['datadir' => $this->tmpDir]]) ->getMock(); $instance->method('copyFromStorage') ->willThrowException(new \Exception('copy')); $source = new Jail([ 'storage' => $instance, 'root' => 'foo' ]); $source = new Wrapper([ 'storage' => $source ]); $source = new Jail([ 'storage' => $source, 'root' => 'bar' ]); $source = new Wrapper([ 'storage' => $source ]); $instance->mkdir('foo'); $instance->mkdir('foo/bar'); $instance->file_put_contents('foo/bar/foo.txt', 'bar'); $instance->moveFromStorage($source, 'foo.txt', 'bar.txt'); $this->assertTrue($instance->file_exists('bar.txt')); } }