diff options
author | Robin Appelman <icewind@owncloud.com> | 2014-11-10 18:00:25 +0300 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2014-11-27 17:25:57 +0300 |
commit | 33b64868d7b65e751bd8d729ce69d6f46e6c3d8d (patch) | |
tree | 33f1e7cf8514425ec1a3c747f9ad5f627a9d92a7 /tests | |
parent | abb6e89c5d83102c2838bd6a48b5bf6e73e9660d (diff) |
Add storage and cache wrappers to apply a permissions mask to a storage
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/files/cache/wrapper/cachepermissionsmask.php | 94 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/permissionsmask.php | 105 |
2 files changed, 199 insertions, 0 deletions
diff --git a/tests/lib/files/cache/wrapper/cachepermissionsmask.php b/tests/lib/files/cache/wrapper/cachepermissionsmask.php new file mode 100644 index 00000000000..72fd22741d3 --- /dev/null +++ b/tests/lib/files/cache/wrapper/cachepermissionsmask.php @@ -0,0 +1,94 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * 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 OCP\Constants; +use Test\Files\Cache\Cache; + +class CachePermissionsMask 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 = $this->getMaskedCached(Constants::PERMISSION_ALL); + } + + protected function getMaskedCached($mask) { + return new \OC\Files\Cache\Wrapper\CachePermissionsMask($this->sourceCache, $mask); + } + + public function maskProvider() { + return array( + array(Constants::PERMISSION_ALL), + array(Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE), + array(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE), + array(Constants::PERMISSION_READ) + ); + } + + /** + * @dataProvider maskProvider + * @param int $mask + */ + public function testGetMasked($mask) { + $cache = $this->getMaskedCached($mask); + $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'permissions' => Constants::PERMISSION_ALL); + $this->sourceCache->put('foo', $data); + $result = $cache->get('foo'); + $this->assertEquals($mask, $result['permissions']); + + $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE); + $this->sourceCache->put('bar', $data); + $result = $cache->get('bar'); + $this->assertEquals($mask & ~Constants::PERMISSION_DELETE, $result['permissions']); + } + + /** + * @dataProvider maskProvider + * @param int $mask + */ + public function testGetFolderContentMasked($mask) { + $this->storage->mkdir('foo'); + $this->storage->file_put_contents('foo/bar', 'asd'); + $this->storage->file_put_contents('foo/asd', 'bar'); + $this->storage->getScanner()->scan(''); + + $cache = $this->getMaskedCached($mask); + $files = $cache->getFolderContents('foo'); + $this->assertCount(2, $files); + + foreach ($files as $file) { + $this->assertEquals($mask & ~Constants::PERMISSION_CREATE, $file['permissions']); + } + } + + /** + * @dataProvider maskProvider + * @param int $mask + */ + public function testSearchMasked($mask) { + $this->storage->mkdir('foo'); + $this->storage->file_put_contents('foo/bar', 'asd'); + $this->storage->file_put_contents('foo/foobar', 'bar'); + $this->storage->getScanner()->scan(''); + + $cache = $this->getMaskedCached($mask); + $files = $cache->search('%bar'); + $this->assertCount(2, $files); + + foreach ($files as $file) { + $this->assertEquals($mask & ~Constants::PERMISSION_CREATE, $file['permissions']); + } + } +} diff --git a/tests/lib/files/storage/wrapper/permissionsmask.php b/tests/lib/files/storage/wrapper/permissionsmask.php new file mode 100644 index 00000000000..7e8c387677c --- /dev/null +++ b/tests/lib/files/storage/wrapper/permissionsmask.php @@ -0,0 +1,105 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Storage\Wrapper; + +use OCP\Constants; + +class PermissionsMask extends \Test\Files\Storage\Storage { + + /** + * @var \OC\Files\Storage\Temporary + */ + private $sourceStorage; + + public function setUp() { + parent::setUp(); + $this->sourceStorage = new \OC\Files\Storage\Temporary(array()); + $this->instance = $this->getMaskedStorage(Constants::PERMISSION_ALL); + } + + public function tearDown() { + $this->sourceStorage->cleanUp(); + parent::tearDown(); + } + + protected function getMaskedStorage($mask) { + return new \OC\Files\Storage\Wrapper\PermissionsMask(array( + 'storage' => $this->sourceStorage, + 'mask' => $mask + )); + } + + public function testMkdirNoCreate() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE); + $this->assertFalse($storage->mkdir('foo')); + $this->assertFalse($storage->file_exists('foo')); + } + + public function testRmdirNoDelete() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE); + $this->assertTrue($storage->mkdir('foo')); + $this->assertTrue($storage->file_exists('foo')); + $this->assertFalse($storage->rmdir('foo')); + $this->assertTrue($storage->file_exists('foo')); + } + + public function testTouchNewFileNoCreate() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE); + $this->assertFalse($storage->touch('foo')); + $this->assertFalse($storage->file_exists('foo')); + } + + public function testTouchNewFileNoUpdate() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE); + $this->assertTrue($storage->touch('foo')); + $this->assertTrue($storage->file_exists('foo')); + } + + public function testTouchExistingFileNoUpdate() { + $this->sourceStorage->touch('foo'); + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE); + $this->assertFalse($storage->touch('foo')); + } + + public function testUnlinkNoDelete() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE); + $this->assertTrue($storage->touch('foo')); + $this->assertTrue($storage->file_exists('foo')); + $this->assertFalse($storage->unlink('foo')); + $this->assertTrue($storage->file_exists('foo')); + } + + public function testPutContentsNewFileNoUpdate() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE); + $this->assertTrue($storage->file_put_contents('foo', 'bar')); + $this->assertEquals('bar', $storage->file_get_contents('foo')); + } + + public function testPutContentsNewFileNoCreate() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE); + $this->assertFalse($storage->file_put_contents('foo', 'bar')); + } + + public function testPutContentsExistingFileNoUpdate() { + $this->sourceStorage->touch('foo'); + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE); + $this->assertFalse($storage->file_put_contents('foo', 'bar')); + } + + public function testFopenExistingFileNoUpdate() { + $this->sourceStorage->touch('foo'); + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE); + $this->assertFalse($storage->fopen('foo', 'w')); + } + + public function testFopenNewFileNoCreate() { + $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE); + $this->assertFalse($storage->fopen('foo', 'w')); + } +} |