Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception if filename contains slash #188

Merged
merged 2 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* raised minimum required PHP version to 7.2.0
* all methods of `org\bovigo\vfs\visitor\vfsStreamVisitor` are now declared with `self` as return type
* `vfsStreamWrapper::setRoot()` and `vsfStreamWrapper::getRoot()` method signatures now require and return `org\bovigo\vfs\vfsStreamDirectory` vice `org\bovigo\vfs\vfsStreamContainer`.
* `vfsStream::newFile()`, `vfsStream::newBlock()`, `org\bovigo\vfs\vfsStreamFile`, and `org\bovigo\vfs\vfsStreamBlock` will throw an exception if the filename contains a forward slash (`/`).


1.6.6 (2019-04-08)
Expand Down
8 changes: 8 additions & 0 deletions src/main/php/org/bovigo/vfs/vfsStreamAbstractContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ abstract class vfsStreamAbstractContent implements vfsStreamContent
*/
public function __construct(string $name, int $permissions = null)
{
if (strstr($name, '/') !== false) {
throw new vfsStreamException('Name can not contain /.');
}

$this->name = "{$name}";
$time = time();
if (null === $permissions) {
Expand Down Expand Up @@ -116,6 +120,10 @@ public function getName(): string
*/
public function rename(string $newName)
{
if (strstr($newName, '/') !== false) {
throw new vfsStreamException('Name can not contain /.');
}

$this->name = "{$newName}";
}

Expand Down
20 changes: 0 additions & 20 deletions src/main/php/org/bovigo/vfs/vfsStreamDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class vfsStreamDirectory extends vfsStreamAbstractContent implements vfsStreamCo
*/
public function __construct(string $name, int $permissions = null)
{
if (strstr($name, '/') !== false) {
throw new vfsStreamException('Directory name can not contain /.');
}

$this->type = vfsStreamContent::TYPE_DIR;
parent::__construct($name, $permissions);
}
Expand Down Expand Up @@ -83,22 +79,6 @@ public function sizeSummarized(): int
return $size;
}

/**
* renames the content
*
* @param string $newName
* @throws vfsStreamException
*/
public function rename(string $newName)
{
if (strstr($newName, '/') !== false) {
throw new vfsStreamException('Directory name can not contain /.');
}

parent::rename($newName);
}


/**
* sets parent path
*
Expand Down
37 changes: 37 additions & 0 deletions src/test/php/org/bovigo/vfs/vfsStreamAbstractContentTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
use PHPUnit\Framework\TestCase;

use function bovigo\assert\assertFalse;
use function bovigo\assert\assertThat;
use function bovigo\assert\assertTrue;
use function bovigo\assert\expect;
use function bovigo\assert\predicate\equals;
/**
* Test for org\bovigo\vfs\vfsStreamAbstractContent.
*/
Expand All @@ -30,6 +33,16 @@ private function createContent($permissions): vfsStreamContent
]);
}

/**
* @test
*/
public function invalidCharacterInNameThrowsException()
{
expect(function () {
NewInstance::of(vfsStreamAbstractContent::class, ['foo/bar']);
})->throws(vfsStreamException::class);
}

/**
* @test
* @group permissions
Expand Down Expand Up @@ -689,4 +702,28 @@ public function allPermissionsForOther()
assertFalse($content->isExecutable(self::OTHER, vfsStream::getCurrentGroup()));
assertTrue($content->isExecutable(self::OTHER, self::OTHER));
}

/**
* @test
*/
public function canBeRenamed()
{
$content = $this->createContent(0600);
$content->rename('bar');
assertThat($content->getName(), equals('bar'));
assertFalse($content->appliesTo('foo'));
assertFalse($content->appliesTo('foo/bar'));
assertTrue($content->appliesTo('bar'));
}

/**
* @test
*/
public function renameToInvalidNameThrowsException()
{
$content = $this->createContent(0600);
expect(function () use ($content) {
$content->rename('foo/baz');
})->throws(vfsStreamException::class);
}
}
18 changes: 18 additions & 0 deletions src/test/php/org/bovigo/vfs/vfsStreamFileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ protected function setUp(): void
$this->file = vfsStream::newFile('foo');
}

/**
* @test
*/
public function invalidCharacterInNameThrowsException()
{
expect(function() { new vfsStreamFile('foo/bar'); })
->throws(vfsStreamException::class);
}

/**
* @test
*/
Expand Down Expand Up @@ -91,6 +100,15 @@ public function canBeRenamed()
assertTrue($this->file->appliesTo('bar'));
}

/**
* @test
*/
public function renameToInvalidNameThrowsException()
{
expect(function() { $this->file->rename('foo/baz'); })
->throws(vfsStreamException::class);
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ protected function setUp(): void
*/
public function fileCanBeAccessedUsingWinDirSeparator()
{
vfsStream::newFile('foo/bar/baz.txt')
->at($this->root)
$structure = ['foo' => ['bar' => []]];
vfsStream::create($structure, $this->root);
vfsStream::newFile('baz.txt')
->at($this->root->getChild('foo')->getChild('bar'))
->withContent('test');
assertThat(file_get_contents('vfs://root/foo\bar\baz.txt'), equals('test'));
}
Expand Down