diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2016-03-30 23:20:37 +0200 |
---|---|---|
committer | Bjoern Schiessle <schiessle@owncloud.com> | 2016-03-31 19:24:47 +0200 |
commit | 93ed965cbb1952faafc1ca5e09ee0de84d122c05 (patch) | |
tree | 2631e5302305b9b59b25b006edca19650ca2a1bc /tests | |
parent | 8ef6c6c7bcf0b58561aee6ec1a18ea7a8643a773 (diff) | |
download | nextcloud-server-93ed965cbb1952faafc1ca5e09ee0de84d122c05.tar.gz nextcloud-server-93ed965cbb1952faafc1ca5e09ee0de84d122c05.zip |
fix creation of versions of encrypted files on external storages
in order to create a 1:1 copy of a file if a version gets created
we need to store this information on copyBetweenStorage(). This
allows us to by-pass the encryption wrapper if we read the source file.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/encryption/encryptionwrappertest.php | 101 | ||||
-rw-r--r-- | tests/lib/encryption/managertest.php | 6 | ||||
-rw-r--r-- | tests/lib/encryption/utiltest.php | 45 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/encryption.php | 44 | ||||
-rw-r--r-- | tests/lib/files/stream/encryption.php | 7 | ||||
-rw-r--r-- | tests/lib/traits/encryptiontrait.php | 16 |
6 files changed, 151 insertions, 68 deletions
diff --git a/tests/lib/encryption/encryptionwrappertest.php b/tests/lib/encryption/encryptionwrappertest.php new file mode 100644 index 00000000000..909187ae43b --- /dev/null +++ b/tests/lib/encryption/encryptionwrappertest.php @@ -0,0 +1,101 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2016, 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 <http://www.gnu.org/licenses/> + * + */ + + +namespace Test\Encryption; + + +use OC\Encryption\EncryptionWrapper; +use Test\TestCase; + +class EncryptionWrapperTest extends TestCase { + + /** @var EncryptionWrapper */ + private $instance; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\ILogger */ + private $logger; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Encryption\Manager */ + private $manager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Memcache\ArrayCache */ + private $arrayCache; + + public function setUp() { + parent::setUp(); + + $this->arrayCache = $this->getMock('OC\Memcache\ArrayCache'); + $this->manager = $this->getMockBuilder('OC\Encryption\Manager') + ->disableOriginalConstructor()->getMock(); + $this->logger = $this->getMock('OCP\ILogger'); + + $this->instance = new EncryptionWrapper($this->arrayCache, $this->manager, $this->logger); + } + + + /** + * @dataProvider provideWrapStorage + */ + public function testWrapStorage($expectedWrapped, $wrappedStorages) { + $storage = $this->getMockBuilder('OC\Files\Storage\Storage') + ->disableOriginalConstructor() + ->getMock(); + + foreach ($wrappedStorages as $wrapper) { + $storage->expects($this->any()) + ->method('instanceOfStorage') + ->willReturnMap([ + [$wrapper, true], + ]); + } + + $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint') + ->disableOriginalConstructor() + ->getMock(); + + $returnedStorage = $this->instance->wrapStorage('mountPoint', $storage, $mount); + + $this->assertEquals( + $expectedWrapped, + $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'), + 'Asserted that the storage is (not) wrapped with encryption' + ); + } + + public function provideWrapStorage() { + return [ + // Wrap when not wrapped or not wrapped with storage + [true, []], + [true, ['OCA\Files_Trashbin\Storage']], + + // Do not wrap shared storages + [false, ['OC\Files\Storage\Shared']], + [false, ['OCA\Files_Sharing\External\Storage']], + [false, ['OC\Files\Storage\OwnCloud']], + [false, ['OC\Files\Storage\Shared', 'OCA\Files_Sharing\External\Storage']], + [false, ['OC\Files\Storage\Shared', 'OC\Files\Storage\OwnCloud']], + [false, ['OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']], + [false, ['OC\Files\Storage\Shared', 'OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']], + ]; + } + +} diff --git a/tests/lib/encryption/managertest.php b/tests/lib/encryption/managertest.php index 6355c706b61..3dcfc949a18 100644 --- a/tests/lib/encryption/managertest.php +++ b/tests/lib/encryption/managertest.php @@ -24,6 +24,9 @@ class ManagerTest extends TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject */ private $util; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Memcache\ArrayCache */ + private $arrayCache; public function setUp() { parent::setUp(); @@ -32,7 +35,8 @@ class ManagerTest extends TestCase { $this->l10n = $this->getMock('\OCP\Il10n'); $this->view = $this->getMock('\OC\Files\View'); $this->util = $this->getMockBuilder('\OC\Encryption\Util')->disableOriginalConstructor()->getMock(); - $this->manager = new Manager($this->config, $this->logger, $this->l10n, $this->view, $this->util); + $this->arrayCache = $this->getMock('OC\Memcache\ArrayCache'); + $this->manager = new Manager($this->config, $this->logger, $this->l10n, $this->view, $this->util, $this->arrayCache); } public function testManagerIsDisabled() { diff --git a/tests/lib/encryption/utiltest.php b/tests/lib/encryption/utiltest.php index 449326bb351..ec316a9f05a 100644 --- a/tests/lib/encryption/utiltest.php +++ b/tests/lib/encryption/utiltest.php @@ -188,49 +188,4 @@ class UtilTest extends TestCase { ); } - /** - * @dataProvider provideWrapStorage - */ - public function testWrapStorage($expectedWrapped, $wrappedStorages) { - $storage = $this->getMockBuilder('OC\Files\Storage\Storage') - ->disableOriginalConstructor() - ->getMock(); - - foreach ($wrappedStorages as $wrapper) { - $storage->expects($this->any()) - ->method('instanceOfStorage') - ->willReturnMap([ - [$wrapper, true], - ]); - } - - $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint') - ->disableOriginalConstructor() - ->getMock(); - - $returnedStorage = $this->util->wrapStorage('mountPoint', $storage, $mount); - - $this->assertEquals( - $expectedWrapped, - $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'), - 'Asserted that the storage is (not) wrapped with encryption' - ); - } - - public function provideWrapStorage() { - return [ - // Wrap when not wrapped or not wrapped with storage - [true, []], - [true, ['OCA\Files_Trashbin\Storage']], - - // Do not wrap shared storages - [false, ['OC\Files\Storage\Shared']], - [false, ['OCA\Files_Sharing\External\Storage']], - [false, ['OC\Files\Storage\OwnCloud']], - [false, ['OC\Files\Storage\Shared', 'OCA\Files_Sharing\External\Storage']], - [false, ['OC\Files\Storage\Shared', 'OC\Files\Storage\OwnCloud']], - [false, ['OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']], - [false, ['OC\Files\Storage\Shared', 'OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']], - ]; - } } diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php index bde920e440e..9d68f3b14b2 100644 --- a/tests/lib/files/storage/wrapper/encryption.php +++ b/tests/lib/files/storage/wrapper/encryption.php @@ -87,6 +87,9 @@ class Encryption extends Storage { */ private $config; + /** @var \OC\Memcache\ArrayCache | \PHPUnit_Framework_MockObject_MockObject */ + private $arrayCache; + /** @var integer dummy unencrypted size */ private $dummySize = -1; @@ -104,6 +107,7 @@ class Encryption extends Storage { ->method('getEncryptionModule') ->willReturn($mockModule); + $this->arrayCache = $this->getMock('OC\Memcache\ArrayCache'); $this->config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor() ->getMock(); @@ -111,9 +115,10 @@ class Encryption extends Storage { ->disableOriginalConstructor() ->getMock(); - $this->util = $this->getMock('\OC\Encryption\Util', + $this->util = $this->getMock( + '\OC\Encryption\Util', ['getUidAndFilename', 'isFile', 'isExcluded'], - [new View(), new \OC\User\Manager(), $this->groupManager, $this->config]); + [new View(), new \OC\User\Manager(), $this->groupManager, $this->config, $this->arrayCache]); $this->util->expects($this->any()) ->method('getUidAndFilename') ->willReturnCallback(function ($path) { @@ -168,7 +173,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ] ) ->setMethods(['getMetaData', 'getCache', 'getEncryptionModule']) @@ -245,7 +250,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ] ) ->setMethods(['getCache', 'verifyUnencryptedSize']) @@ -293,7 +298,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ] ) ->setMethods(['getCache', 'verifyUnencryptedSize']) @@ -331,7 +336,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ] ) ->setMethods(['fixUnencryptedSize']) @@ -521,8 +526,15 @@ class Encryption extends Storage { ->disableOriginalConstructor()->getMock(); $util = $this->getMockBuilder('\OC\Encryption\Util') - ->setConstructorArgs([new View(), new \OC\User\Manager(), $this->groupManager, $this->config]) - ->getMock(); + ->setConstructorArgs( + [ + new View(), + new \OC\User\Manager(), + $this->groupManager, + $this->config, + $this->arrayCache + ] + )->getMock(); $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') ->setConstructorArgs( @@ -533,7 +545,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ] ) ->setMethods(['readFirstBlock', 'parseRawHeader']) @@ -582,7 +594,7 @@ class Encryption extends Storage { ->disableOriginalConstructor()->getMock(); $util = $this->getMockBuilder('\OC\Encryption\Util') - ->setConstructorArgs([new View(), new \OC\User\Manager(), $this->groupManager, $this->config]) + ->setConstructorArgs([new View(), new \OC\User\Manager(), $this->groupManager, $this->config, $this->arrayCache]) ->getMock(); $cache = $this->getMockBuilder('\OC\Files\Cache\Cache') @@ -600,7 +612,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ] ) ->setMethods(['readFirstBlock', 'parseRawHeader', 'getCache']) @@ -636,7 +648,7 @@ class Encryption extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager + $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache ); @@ -763,6 +775,8 @@ class Encryption extends Storage { $expectedCachePut['encryptedVersion'] = 12345; } + $this->arrayCache->expects($this->never())->method('set'); + $this->cache->expects($this->once()) ->method('put') ->with($sourceInternalPath, $expectedCachePut); @@ -812,7 +826,8 @@ class Encryption extends Storage { null, $this->keyStore, $this->update, - $this->mountManager + $this->mountManager, + $this->arrayCache ] ) ->setMethods(['updateUnencryptedSize', 'getCache']) @@ -825,6 +840,9 @@ class Encryption extends Storage { $instance->expects($this->any())->method('getCache') ->willReturn($cache); + $this->arrayCache->expects($this->once())->method('set') + ->with('encryption_copy_version_' . $sourceInternalPath, true); + if ($copyResult) { $cache->expects($this->once())->method('get') ->with($sourceInternalPath) diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index f67dd09bc4d..afb31f2822d 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -31,6 +31,7 @@ class Encryption extends \Test\TestCase { $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor() ->getMock(); + $arrayCache = $this->getMock('OC\Memcache\ArrayCache'); $groupManager = $this->getMockBuilder('\OC\Group\Manager') ->disableOriginalConstructor() ->getMock(); @@ -39,7 +40,11 @@ class Encryption extends \Test\TestCase { ->setMethods(['getAccessList']) ->getMock(); $file->expects($this->any())->method('getAccessList')->willReturn([]); - $util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $groupManager, $config]); + $util = $this->getMock( + '\OC\Encryption\Util', + ['getUidAndFilename'], + [new View(), new \OC\User\Manager(), $groupManager, $config, $arrayCache] + ); $util->expects($this->any()) ->method('getUidAndFilename') ->willReturn(['user1', $internalPath]); diff --git a/tests/lib/traits/encryptiontrait.php b/tests/lib/traits/encryptiontrait.php index 92ba3734873..2c1c585d6d4 100644 --- a/tests/lib/traits/encryptiontrait.php +++ b/tests/lib/traits/encryptiontrait.php @@ -8,8 +8,8 @@ namespace Test\Traits; -use OC\Encryption\Util; -use OC\Files\View; +use OC\Encryption\EncryptionWrapper; +use OC\Memcache\ArrayCache; use OCA\Encryption\AppInfo\Application; use OCA\Encryption\KeyManager; use OCA\Encryption\Users\Setup; @@ -68,13 +68,13 @@ trait EncryptionTrait { } protected function postLogin() { - $util = new Util( - new View(), - \OC::$server->getUserManager(), - \OC::$server->getGroupManager(), - \OC::$server->getConfig() + $encryptionWrapper = new EncryptionWrapper( + new ArrayCache(), + \OC::$server->getEncryptionManager(), + \OC::$server->getLogger() ); - $this->registerStorageWrapper('oc_encryption', array($util, 'wrapStorage')); + + $this->registerStorageWrapper('oc_encryption', array($encryptionWrapper, 'wrapStorage')); } protected function setUpEncryptionTrait() { |