2014-11-10 15:00:25 +00:00
|
|
|
<?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;
|
|
|
|
|
2017-05-18 14:38:54 +00:00
|
|
|
use OC\Files\Storage\Wrapper\Wrapper;
|
2014-11-10 15:00:25 +00:00
|
|
|
use OCP\Constants;
|
2017-05-18 14:38:54 +00:00
|
|
|
use OCP\Files\Cache\IScanner;
|
2014-11-10 15:00:25 +00:00
|
|
|
|
2017-04-10 13:19:21 +00:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2016-05-20 13:38:20 +00:00
|
|
|
class PermissionsMaskTest extends \Test\Files\Storage\Storage {
|
2014-11-10 15:00:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
2016-12-02 17:04:21 +00:00
|
|
|
$this->assertEquals(3, $storage->file_put_contents('foo', 'bar'));
|
2014-11-10 15:00:25 +00:00
|
|
|
$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'));
|
|
|
|
}
|
2017-04-10 13:19:21 +00:00
|
|
|
|
|
|
|
public function testScanNewFiles() {
|
|
|
|
$storage = $this->getMaskedStorage(Constants::PERMISSION_READ + Constants::PERMISSION_CREATE);
|
|
|
|
$storage->file_put_contents('foo', 'bar');
|
|
|
|
$storage->getScanner()->scan('');
|
|
|
|
|
|
|
|
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->sourceStorage->getCache()->get('foo')->getPermissions());
|
|
|
|
$this->assertEquals(Constants::PERMISSION_READ, $storage->getCache()->get('foo')->getPermissions());
|
|
|
|
}
|
2017-05-18 14:38:54 +00:00
|
|
|
|
|
|
|
public function testScanNewWrappedFiles() {
|
|
|
|
$storage = $this->getMaskedStorage(Constants::PERMISSION_READ + Constants::PERMISSION_CREATE);
|
|
|
|
$wrappedStorage = new Wrapper(['storage' => $storage]);
|
|
|
|
$wrappedStorage->file_put_contents('foo', 'bar');
|
|
|
|
$wrappedStorage->getScanner()->scan('');
|
|
|
|
|
|
|
|
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->sourceStorage->getCache()->get('foo')->getPermissions());
|
|
|
|
$this->assertEquals(Constants::PERMISSION_READ, $storage->getCache()->get('foo')->getPermissions());
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:17:52 +00:00
|
|
|
public function testScanNewFilesNested() {
|
|
|
|
$storage = $this->getMaskedStorage(Constants::PERMISSION_READ + Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE);
|
|
|
|
$nestedStorage = new \OC\Files\Storage\Wrapper\PermissionsMask(array(
|
|
|
|
'storage' => $storage,
|
|
|
|
'mask' => Constants::PERMISSION_READ + Constants::PERMISSION_CREATE
|
|
|
|
));
|
|
|
|
$wrappedStorage = new Wrapper(['storage' => $nestedStorage]);
|
|
|
|
$wrappedStorage->file_put_contents('foo', 'bar');
|
|
|
|
$wrappedStorage->getScanner()->scan('');
|
|
|
|
|
|
|
|
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->sourceStorage->getCache()->get('foo')->getPermissions());
|
|
|
|
$this->assertEquals(Constants::PERMISSION_READ + Constants::PERMISSION_UPDATE, $storage->getCache()->get('foo')->getPermissions());
|
|
|
|
$this->assertEquals(Constants::PERMISSION_READ, $wrappedStorage->getCache()->get('foo')->getPermissions());
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:38:54 +00:00
|
|
|
public function testScanUnchanged() {
|
|
|
|
$this->sourceStorage->mkdir('foo');
|
|
|
|
$this->sourceStorage->file_put_contents('foo/bar.txt', 'bar');
|
|
|
|
|
|
|
|
$this->sourceStorage->getScanner()->scan('foo');
|
|
|
|
|
|
|
|
$storage = $this->getMaskedStorage(Constants::PERMISSION_READ);
|
|
|
|
$scanner = $storage->getScanner();
|
|
|
|
$called = false;
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function () use (&$called) {
|
|
|
|
$called = true;
|
|
|
|
});
|
|
|
|
$scanner->scan('foo', IScanner::SCAN_RECURSIVE, IScanner::REUSE_ETAG | IScanner::REUSE_SIZE);
|
|
|
|
|
|
|
|
$this->assertFalse($called);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testScanUnchangedWrapped() {
|
|
|
|
$this->sourceStorage->mkdir('foo');
|
|
|
|
$this->sourceStorage->file_put_contents('foo/bar.txt', 'bar');
|
|
|
|
|
|
|
|
$this->sourceStorage->getScanner()->scan('foo');
|
|
|
|
|
|
|
|
$storage = $this->getMaskedStorage(Constants::PERMISSION_READ);
|
|
|
|
$wrappedStorage = new Wrapper(['storage' => $storage]);
|
|
|
|
$scanner = $wrappedStorage->getScanner();
|
|
|
|
$called = false;
|
|
|
|
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function () use (&$called) {
|
|
|
|
$called = true;
|
|
|
|
});
|
|
|
|
$scanner->scan('foo', IScanner::SCAN_RECURSIVE, IScanner::REUSE_ETAG | IScanner::REUSE_SIZE);
|
|
|
|
|
|
|
|
$this->assertFalse($called);
|
|
|
|
}
|
2014-11-10 15:00:25 +00:00
|
|
|
}
|