From fcc1b9b3cc314e4b9032fbebfbce0333eae44998 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Sat, 18 Apr 2015 12:33:43 +0200 Subject: [PATCH 1/3] Thou shalt not corrupt the answer of a cache request --- apps/files_sharing/lib/readonlycache.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/files_sharing/lib/readonlycache.php b/apps/files_sharing/lib/readonlycache.php index ebef163475..c7640f896f 100644 --- a/apps/files_sharing/lib/readonlycache.php +++ b/apps/files_sharing/lib/readonlycache.php @@ -28,7 +28,9 @@ use OC\Files\Cache\Cache; class ReadOnlyCache extends Cache { public function get($path) { $data = parent::get($path); - $data['permissions'] &= (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE); + if ($data !== false) { + $data['permissions'] &= (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE); + } return $data; } From 80a1f1858e92ca7fcf5dabd9640cab76dd23925d Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Sat, 18 Apr 2015 19:43:20 +0200 Subject: [PATCH 2/3] Tests for read-only cache --- apps/files_sharing/tests/readonlycache.php | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 apps/files_sharing/tests/readonlycache.php diff --git a/apps/files_sharing/tests/readonlycache.php b/apps/files_sharing/tests/readonlycache.php new file mode 100644 index 0000000000..9a90867167 --- /dev/null +++ b/apps/files_sharing/tests/readonlycache.php @@ -0,0 +1,105 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +use OCA\Files_sharing\Tests\TestCase; + +class Test_Files_Sharing_ReadOnly_Cache extends TestCase { + + /** + * @type \OC\Files\Storage\Storage + */ + protected $storage; + + /** + * @type OC\Files\Storage\StorageFactory + */ + protected $loader; + + /** + * @type OC\Files\Mount\MountPoint + */ + protected $readOnlyMount; + + /** + * @type \OCA\Files_Sharing\ReadOnlyWrapper + */ + protected $readOnlyStorage; + + /** + * @type \OC\Files\Cache\Cache + */ + protected $readOnlyCache; + + protected function setUp() { + parent::setUp(); + + //error_reporting(E_ERROR | E_PARSE); + + $this->view->mkdir('readonly'); + $this->view->file_put_contents('readonly/foo.txt', 'foo'); + $this->view->file_put_contents('readonly/bar.txt', 'bar'); + + list($this->storage) = $this->view->resolvePath(''); + $this->loader = new \OC\Files\Storage\StorageFactory(); + $this->readOnlyMount = new \OC\Files\Mount\MountPoint($this->storage, + '/readonly', [[]], $this->loader); + $this->readOnlyStorage = $this->loader->getInstance($this->readOnlyMount, + '\OCA\Files_Sharing\ReadOnlyWrapper', ['storage' => $this->storage]); + + $this->readOnlyCache = $this->readOnlyStorage->getCache(); + } + + protected function tearDown() { + parent::tearDown(); + } + + public function testSetup() { + $this->assertTrue($this->view->file_exists('/readonly/foo.txt')); + + $perms = $this->readOnlyStorage->getPermissions('files/readonly/foo.txt'); + $this->assertEquals(17, $perms); + + $this->assertFalse($this->readOnlyStorage->unlink('files/readonly/foo.txt')); + $this->assertTrue($this->readOnlyStorage->file_exists('files/readonly/foo.txt')); + + $this->assertInstanceOf('\OCA\Files_Sharing\ReadOnlyCache', $this->readOnlyCache); + } + + public function testGet() { + $result = $this->readOnlyCache->get('files/readonly/foo.txt'); + $this->assertNotEmpty($result); + + $result = $this->readOnlyCache->get('files/readonly/proof does not exist.md'); + $this->assertFalse($result); + } + + public function testGetFolderContents() { + $results = $this->readOnlyCache->getFolderContents('files/readonly'); + $this->assertNotEmpty($results); + + foreach ($results as $result) { + $this->assertNotEmpty($result); + } + + $results = $this->readOnlyCache->getFolderContents('files/iamaghost'); + $this->assertEmpty($results); + } + +} From 9695e33e340158ead3d54811c8b99deabd49d50b Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Tue, 21 Apr 2015 14:40:11 +0200 Subject: [PATCH 3/3] Renamed class + split methods --- apps/files_sharing/tests/readonlycache.php | 38 ++++++++-------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/apps/files_sharing/tests/readonlycache.php b/apps/files_sharing/tests/readonlycache.php index 9a90867167..5da200fa78 100644 --- a/apps/files_sharing/tests/readonlycache.php +++ b/apps/files_sharing/tests/readonlycache.php @@ -18,40 +18,28 @@ * along with this program. If not, see * */ -use OCA\Files_sharing\Tests\TestCase; +namespace OCA\Files_Sharing\Tests; -class Test_Files_Sharing_ReadOnly_Cache extends TestCase { +class ReadOnlyCache extends TestCase { - /** - * @type \OC\Files\Storage\Storage - */ + /** @var \OC\Files\Storage\Storage */ protected $storage; - /** - * @type OC\Files\Storage\StorageFactory - */ + /** @var \OC\Files\Storage\StorageFactory */ protected $loader; - /** - * @type OC\Files\Mount\MountPoint - */ + /** @var \OC\Files\Mount\MountPoint */ protected $readOnlyMount; - /** - * @type \OCA\Files_Sharing\ReadOnlyWrapper - */ + /** @var \OCA\Files_Sharing\ReadOnlyWrapper */ protected $readOnlyStorage; - /** - * @type \OC\Files\Cache\Cache - */ + /** @var \OC\Files\Cache\Cache */ protected $readOnlyCache; protected function setUp() { parent::setUp(); - //error_reporting(E_ERROR | E_PARSE); - $this->view->mkdir('readonly'); $this->view->file_put_contents('readonly/foo.txt', 'foo'); $this->view->file_put_contents('readonly/bar.txt', 'bar'); @@ -66,10 +54,6 @@ class Test_Files_Sharing_ReadOnly_Cache extends TestCase { $this->readOnlyCache = $this->readOnlyStorage->getCache(); } - protected function tearDown() { - parent::tearDown(); - } - public function testSetup() { $this->assertTrue($this->view->file_exists('/readonly/foo.txt')); @@ -82,22 +66,26 @@ class Test_Files_Sharing_ReadOnly_Cache extends TestCase { $this->assertInstanceOf('\OCA\Files_Sharing\ReadOnlyCache', $this->readOnlyCache); } - public function testGet() { + public function testGetWhenFileExists() { $result = $this->readOnlyCache->get('files/readonly/foo.txt'); $this->assertNotEmpty($result); + } + public function testGetWhenFileDoesNotExist() { $result = $this->readOnlyCache->get('files/readonly/proof does not exist.md'); $this->assertFalse($result); } - public function testGetFolderContents() { + public function testGetFolderContentsWhenFolderExists() { $results = $this->readOnlyCache->getFolderContents('files/readonly'); $this->assertNotEmpty($results); foreach ($results as $result) { $this->assertNotEmpty($result); } + } + public function testGetFolderContentsWhenFolderDoesNotExist() { $results = $this->readOnlyCache->getFolderContents('files/iamaghost'); $this->assertEmpty($results); }