Commit ccd0c593 authored by Ilja Neumann's avatar Ilja Neumann

Added jailing unit-tests

parent 939a551d
<?php
namespace Test\Files\Storage\Wrapper;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\DirMask;
use OCP\Constants;
class DirMaskTest extends \PHPUnit_Framework_TestCase {
/** @var Temporary */
private $sourceStorage;
/**
* @param $mask
* @return DirMask
*/
private function getStorage($mask) {
$this->sourceStorage->mkdir('masked');
$this->sourceStorage->mkdir('masked/dir');
$this->sourceStorage->file_put_contents('masked/test.txt', 'something');
return new DirMask(
[
'storage' => $this->sourceStorage,
'path' => 'masked',
'mask' => $mask
]
);
}
public function setUp() {
parent::setUp();
$this->sourceStorage = new Temporary([]);
}
public function testIsUpdatable() {
$readOnlyStorage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse(
$readOnlyStorage->isUpdatable('masked/test.txt')
);
$writableStorage = $this->getStorage(Constants::PERMISSION_ALL);
$this->assertTrue(
$writableStorage->isUpdatable('masked/test.txt')
);
}
public function testIsDeletable() {
$readOnlyStorage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse(
$readOnlyStorage->isDeletable('masked/test.txt')
);
$writeOnlyStorage = $this->getStorage(Constants::PERMISSION_ALL);
$this->assertTrue(
$writeOnlyStorage->isDeletable('masked/test.txt')
);
}
public function testIsSharable() {
$readOnlyStorage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse(
$readOnlyStorage->isSharable('masked/test.txt')
);
$sharableStorage = $this->getStorage(Constants::PERMISSION_SHARE);
$this->assertTrue(
$sharableStorage->isSharable('masked/test.txt')
);
}
public function testMkdir() {
$storage = $this->getStorage(Constants::PERMISSION_CREATE);
$this->assertTrue($storage->mkdir('masked/dir2'));
$storage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse($storage->mkdir('masked/dir2'));
}
public function testRmdir() {
$storage = $this->getStorage(Constants::PERMISSION_ALL);
$this->assertTrue($storage->rmdir('masked/dir'));
$this->sourceStorage->mkdir('masked/dir2');
$storage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse($storage->rmdir('masked/dir2'));
}
public function testUnlink() {
$storage = $this->getStorage(Constants::PERMISSION_ALL);
$this->assertTrue($storage->unlink('masked/test.txt'));
$this->assertFalse($this->sourceStorage->file_exists('masked/test.txt'));
$storage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse($storage->unlink('masked/test.txt'));
$this->assertTrue($this->sourceStorage->file_exists('masked/test.txt'));
}
public function testFilePutContents() {
$storage = $this->getStorage(Constants::PERMISSION_ALL);
$this->assertTrue($storage->file_put_contents(
'masked/barbaz.txt', 'something'
));
$content = $this->sourceStorage->file_get_contents('masked/barbaz.txt');
$this->assertTrue($content == 'something');
$storage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse($storage->file_put_contents(
'masked/barbaz.txt', 'something'
));
}
public function testFopen() {
$storage = $this->getStorage(Constants::PERMISSION_ALL);
$this->assertTrue(
is_resource($storage->fopen('masked/test.txt', 'r+'))
);
$storage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertTrue(
is_resource($storage->fopen('masked/test.txt', 'r'))
);
$storage = $this->getStorage(Constants::PERMISSION_READ);
$this->assertFalse(
$storage->fopen('masked/test.txt', 'r+')
);
}
}
<?php
namespace Test\Files\Storage\Wrapper;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\ReadOnlyJail;
use OCP\Constants;
class ReadOnlyJailTest extends \PHPUnit_Framework_TestCase {
/** @var ReadOnlyJail */
private $sut;
/** @var Temporary */
private $sourceStorage;
public function setUp() {
parent::setUp();
$this->sourceStorage = new Temporary([]);
$this->sut = new ReadOnlyJail(
[
'storage' => $this->sourceStorage,
'path' => 'files',
'mask' => Constants::PERMISSION_READ
]
);
}
public function testMkdirCanOnlyCreateFilesDirectoryInsideRoot() {
$this->assertTrue($this->sut->mkdir('files'));
$this->assertFalse($this->sut->mkdir('files_other'));
}
public function testOnlyPartFilesAreDeletable() {
$this->assertTrue($this->sut->isDeletable('foo.part'));
$this->assertFalse($this->sut->isDeletable('cant_delete_me.txt'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment