]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fixing encryption storage wrapper tests
authorThomas Müller <thomas.mueller@tmit.eu>
Thu, 2 Apr 2015 12:44:58 +0000 (14:44 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:30 +0000 (13:30 +0200)
lib/private/encryption/exceptions/encryptionheadertolargeexception.php
lib/private/files/storage/wrapper/encryption.php
tests/lib/files/storage/storage.php
tests/lib/files/storage/wrapper/encryption.php

index 94a130d792d6a03c073265c5e6d450a1eeda9903..cdb5f94080011e0967fc176a9f8871f8910adc63 100644 (file)
@@ -26,7 +26,7 @@ use OCP\Encryption\Exceptions\GenericEncryptionException;
 
 class EncryptionHeaderToLargeException extends GenericEncryptionException {
 
-       public function __construct($key) {
+       public function __construct() {
                parent::__construct('max header size exceeded');
        }
 
index 7cc488aa914f73912eea2bae68ce22ad0889922a..58d963613aa54749b331d6671d3cf178d7c5892b 100644 (file)
 namespace OC\Files\Storage\Wrapper;
 
 use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
+use OC\Files\Storage\LocalTempFileTrait;
 
 class Encryption extends Wrapper {
 
+       use LocalTempFileTrait;
+
        /** @var string */
        private $mountPoint;
 
@@ -156,8 +159,8 @@ class Encryption extends Wrapper {
 
                $encryptionModule = $this->getEncryptionModule($path);
                if ($encryptionModule) {
-                               $keyStorage = \OC::$server->getEncryptionKeyStorage($encryptionModule->getId());
-                               $keyStorage->deleteAllFileKeys($this->getFullPath($path));
+                       $keyStorage = $this->getKeyStorage($encryptionModule->getId());
+                       $keyStorage->deleteAllFileKeys($this->getFullPath($path));
                }
 
                return $this->storage->unlink($path);
@@ -184,7 +187,7 @@ class Encryption extends Wrapper {
                        list(, $target) = $this->util->getUidAndFilename($fullPath2);
                        $encryptionModule = $this->getEncryptionModule($path2);
                        if ($encryptionModule) {
-                               $keyStorage = \OC::$server->getEncryptionKeyStorage($encryptionModule->getId());
+                               $keyStorage = $this->getKeyStorage($encryptionModule->getId());
                                $keyStorage->renameKeys($source, $target, $owner, $systemWide);
                        }
                }
@@ -269,6 +272,57 @@ class Encryption extends Wrapper {
                }
        }
 
+       /**
+        * get the path to a local version of the file.
+        * The local version of the file can be temporary and doesn't have to be persistent across requests
+        *
+        * @param string $path
+        * @return string
+        */
+       public function getLocalFile($path) {
+               return $this->getCachedFile($path);
+       }
+
+       /**
+        * Returns the wrapped storage's value for isLocal()
+        *
+        * @return bool wrapped storage's isLocal() value
+        */
+       public function isLocal() {
+               return false;
+       }
+
+       /**
+        * see http://php.net/manual/en/function.stat.php
+        * only the following keys are required in the result: size and mtime
+        *
+        * @param string $path
+        * @return array
+        */
+       public function stat($path) {
+               $stat = $this->storage->stat($path);
+               $fileSize = $this->filesize($path);
+               $stat['size'] = $fileSize;
+               $stat[7] = $fileSize;
+               return $stat;
+       }
+
+       /**
+        * see http://php.net/manual/en/function.hash.php
+        *
+        * @param string $type
+        * @param string $path
+        * @param bool $raw
+        * @return string
+        */
+       public function hash($type, $path, $raw = false) {
+               $fh = $this->fopen($path, 'rb');
+               $ctx = hash_init($type);
+               hash_update_stream($ctx, $fh);
+               fclose($fh);
+               return hash_final($ctx, $raw);
+       }
+
        /**
         * return full path, including mount point
         *
@@ -322,4 +376,13 @@ class Encryption extends Wrapper {
                $this->unencryptedSize[$path] = $unencryptedSize;
        }
 
+       /**
+        * @param string $encryptionModule
+        * @return \OCP\Encryption\Keys\IStorage
+        */
+       protected function getKeyStorage($encryptionModuleId) {
+               $keyStorage = \OC::$server->getEncryptionKeyStorage($encryptionModuleId);
+               return $keyStorage;
+       }
+
 }
index ad7522f1ea871ac049c407546ad63759a4d8da2f..938fecb5bf3e36c2b0c7742604da05e8101161a3 100644 (file)
@@ -253,7 +253,7 @@ abstract class Storage extends \Test\TestCase {
                $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
                $localFile = $this->instance->getLocalFile('/lorem.txt');
                $this->assertTrue(file_exists($localFile));
-               $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
+               $this->assertEquals(file_get_contents($textFile), file_get_contents($localFile));
 
                $this->instance->mkdir('/folder');
                $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
index eea6a53e043691709c16c03ed79334487904ea10..9f681a3429842a15881d1f219674eaf960a29e93 100644 (file)
@@ -16,6 +16,7 @@ class Encryption extends \Test\Files\Storage\Storage {
 
                parent::setUp();
 
+               $mockModule = $this->buildMockModule();
                $encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
                        ->disableOriginalConstructor()
                        ->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule'])
@@ -25,22 +26,57 @@ class Encryption extends \Test\Files\Storage\Storage {
                        ->getMock();
                $encryptionManager->expects($this->any())
                        ->method('getDefaultEncryptionModule')
-                       ->willReturn(new DummyModule());
+                       ->willReturn($mockModule);
+               $encryptionManager->expects($this->any())
+                       ->method('getEncryptionModule')
+                       ->willReturn($mockModule);
 
-               $util = new \OC\Encryption\Util(new View(), new \OC\User\Manager(), $config);
+               $util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $config]);
+               $util->expects($this->any())
+                       ->method('getUidAndFilename')
+                       ->willReturnCallback(function ($path) {
+                               return ['user1', $path];
+                       });
+               $file = $this->getMockBuilder('\OC\Encryption\File')
+                       ->disableOriginalConstructor()
+                       ->getMock();
 
                $logger = $this->getMock('\OC\Log');
 
                $this->sourceStorage = new \OC\Files\Storage\Temporary(array());
-               $this->instance = new \OC\Files\Storage\Wrapper\Encryption([
+               $keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage')
+                       ->disableOriginalConstructor()->getMock();
+               $this->instance = new EncryptionWrapper([
                        'storage' => $this->sourceStorage,
                        'root' => 'foo',
                        'mountPoint' => '/'
                ],
-                       $encryptionManager, $util, $logger
+                       $encryptionManager, $util, $logger, $file, null, $keyStore
                );
        }
 
+       /**
+        * @return \PHPUnit_Framework_MockObject_MockObject
+        */
+       protected function buildMockModule() {
+               $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
+                       ->disableOriginalConstructor()
+                       ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'calculateUnencryptedSize', 'getUnencryptedBlockSize'])
+                       ->getMock();
+
+               $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
+               $encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
+               $encryptionModule->expects($this->any())->method('begin')->willReturn([]);
+               $encryptionModule->expects($this->any())->method('end')->willReturn('');
+               $encryptionModule->expects($this->any())->method('encrypt')->willReturnArgument(0);
+               $encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0);
+               $encryptionModule->expects($this->any())->method('update')->willReturn(true);
+               $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
+               $encryptionModule->expects($this->any())->method('calculateUnencryptedSize')->willReturn(42);
+               $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(6126);
+               return $encryptionModule;
+       }
+
 //     public function testMkDirRooted() {
 //             $this->instance->mkdir('bar');
 //             $this->assertTrue($this->sourceStorage->is_dir('foo/bar'));
@@ -51,3 +87,28 @@ class Encryption extends \Test\Files\Storage\Storage {
 //             $this->assertEquals('asd', $this->sourceStorage->file_get_contents('foo/bar'));
 //     }
 }
+
+//
+// FIXME: this is too bad and needs adjustment
+//
+class EncryptionWrapper extends \OC\Files\Storage\Wrapper\Encryption {
+       private $keyStore;
+
+       public function __construct(
+               $parameters,
+               \OC\Encryption\Manager $encryptionManager = null,
+               \OC\Encryption\Util $util = null,
+               \OC\Log $logger = null,
+               \OC\Encryption\File $fileHelper = null,
+               $uid = null,
+               $keyStore = null
+       ) {
+               $this->keyStore = $keyStore;
+               parent::__construct($parameters, $encryptionManager, $util, $logger, $fileHelper, $uid);
+       }
+
+       protected function getKeyStorage($encryptionModuleId) {
+               return $this->keyStore;
+       }
+
+}