From abb6e89c5d83102c2838bd6a48b5bf6e73e9660d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 10 Nov 2014 16:00:08 +0100 Subject: Add storage and cache wrappers to jail a storage to a subfolder --- tests/lib/files/cache/cache.php | 8 ++-- tests/lib/files/cache/wrapper/cachejail.php | 67 +++++++++++++++++++++++++++++ tests/lib/files/storage/wrapper/jail.php | 55 +++++++++++++++++++++++ 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 tests/lib/files/cache/wrapper/cachejail.php create mode 100644 tests/lib/files/storage/wrapper/jail.php (limited to 'tests/lib') diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php index 02c45a16571..7360e9885c1 100644 --- a/tests/lib/files/cache/cache.php +++ b/tests/lib/files/cache/cache.php @@ -20,20 +20,20 @@ class Cache extends \Test\TestCase { /** * @var \OC\Files\Storage\Temporary $storage ; */ - private $storage; + protected $storage; /** * @var \OC\Files\Storage\Temporary $storage2 ; */ - private $storage2; + protected $storage2; /** * @var \OC\Files\Cache\Cache $cache */ - private $cache; + protected $cache; /** * @var \OC\Files\Cache\Cache $cache2 */ - private $cache2; + protected $cache2; public function testSimple() { $file1 = 'foo'; diff --git a/tests/lib/files/cache/wrapper/cachejail.php b/tests/lib/files/cache/wrapper/cachejail.php new file mode 100644 index 00000000000..13f3dc8858e --- /dev/null +++ b/tests/lib/files/cache/wrapper/cachejail.php @@ -0,0 +1,67 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Cache\Wrapper; + +use Test\Files\Cache\Cache; + +class CacheJail extends Cache { + /** + * @var \OC\Files\Cache\Cache $sourceCache + */ + protected $sourceCache; + + public function setUp() { + parent::setUp(); + $this->storage->mkdir('foo'); + $this->sourceCache = $this->cache; + $this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'foo'); + } + + function testSearchOutsideJail() { + $file1 = 'foo/foobar'; + $file2 = 'folder/foobar'; + $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'); + + $this->sourceCache->put($file1, $data1); + $this->sourceCache->put($file2, $data1); + + $this->assertCount(2, $this->sourceCache->search('%foobar')); + + $result = $this->cache->search('%foobar%'); + $this->assertCount(1, $result); + $this->assertEquals('foobar', $result[0]['path']); + } + + function testClearKeepEntriesOutsideJail() { + $file1 = 'foo/foobar'; + $file2 = 'foo/foobar/asd'; + $file3 = 'folder/foobar'; + $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'); + + $this->sourceCache->put('foo', $data1); + $this->sourceCache->put($file1, $data1); + $this->sourceCache->put($file2, $data1); + $this->sourceCache->put($file3, $data1); + + $this->cache->clear(); + + $this->assertFalse($this->cache->inCache('foobar')); + $this->assertTrue($this->sourceCache->inCache('folder/foobar')); + } + + function testGetById() { + //not supported + $this->assertTrue(true); + } + + function testGetIncomplete() { + //not supported + $this->assertTrue(true); + } +} diff --git a/tests/lib/files/storage/wrapper/jail.php b/tests/lib/files/storage/wrapper/jail.php new file mode 100644 index 00000000000..270ce750ecf --- /dev/null +++ b/tests/lib/files/storage/wrapper/jail.php @@ -0,0 +1,55 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Storage\Wrapper; + +class Jail extends \Test\Files\Storage\Storage { + /** + * @var string tmpDir + */ + private $tmpDir; + + /** + * @var \OC\Files\Storage\Temporary + */ + private $sourceStorage; + + public function setUp() { + parent::setUp(); + $this->sourceStorage = new \OC\Files\Storage\Temporary(array()); + $this->sourceStorage->mkdir('foo'); + $this->instance = new \OC\Files\Storage\Wrapper\Jail(array( + 'storage' => $this->sourceStorage, + 'root' => 'foo' + )); + } + + public function tearDown() { + // test that nothing outside our jail is touched + $contents = array(); + $dh = $this->sourceStorage->opendir(''); + while ($file = readdir($dh)) { + if ($file !== '.' and $file !== '..') { + $contents[] = $file; + } + } + $this->assertEquals(array('foo'), $contents); + $this->sourceStorage->cleanUp(); + parent::tearDown(); + } + + public function testMkDirRooted() { + $this->instance->mkdir('bar'); + $this->assertTrue($this->sourceStorage->is_dir('foo/bar')); + } + + public function testFilePutContentsRooted() { + $this->instance->file_put_contents('bar', 'asd'); + $this->assertEquals('asd', $this->sourceStorage->file_get_contents('foo/bar')); + } +} -- cgit v1.2.3