]> source.dussan.org Git - nextcloud-server.git/commitdiff
various fixes & start to unit test the encryption storage wrapper
authorThomas Müller <thomas.mueller@tmit.eu>
Mon, 30 Mar 2015 11:23:10 +0000 (13:23 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:28 +0000 (13:30 +0200)
apps/encryption_dummy/lib/dummymodule.php
lib/private/encryption/exceptions/encryptionheaderkeyexistsexception.php
lib/private/encryption/util.php
lib/private/files/storage/wrapper/encryption.php
tests/lib/files/storage/wrapper/encryption.php [new file with mode: 0644]
tests/lib/files/storage/wrapper/jail.php

index 8ca9cd4f9afc2a66525b715b834c5186be177c90..55e8f26af95245aecb602b56dc6df5e895e38481 100644 (file)
@@ -23,7 +23,9 @@
 
 namespace OCA\Encryption_Dummy;
 
-class DummyModule implements \OCP\Encryption\IEncryptionModule {
+use OCP\Encryption\IEncryptionModule;
+
+class DummyModule implements IEncryptionModule {
 
        /** @var boolean */
        protected $isWriteOperation;
@@ -103,17 +105,6 @@ class DummyModule implements \OCP\Encryption\IEncryptionModule {
                return $data;
        }
 
-       /**
-        * update encrypted file, e.g. give additional users access to the file
-        *
-        * @param string $path path to the file which should be updated
-        * @param array $accessList who has access to the file contains the key 'users' and 'public'
-        * @return boolean
-        */
-       public function update($path, $accessList) {
-               return true;
-       }
-
        /**
         * should the file be encrypted or not
         *
@@ -142,4 +133,15 @@ class DummyModule implements \OCP\Encryption\IEncryptionModule {
                return 6126;
        }
 
-}
\ No newline at end of file
+       /**
+        * update encrypted file, e.g. give additional users access to the file
+        *
+        * @param string $path path to the file which should be updated
+        * @param string $uid of the user who performs the operation
+        * @param array $accessList who has access to the file contains the key 'users' and 'public'
+        * @return boolean
+        */
+       public function update($path, $uid, $accessList) {
+               return true;
+       }
+}
index d401f0323ba482b8b544ab09cbe7a3b5cbb0ece6..23103b90c4f02fd7b024d4dee88381be9fe53552 100644 (file)
@@ -26,4 +26,8 @@ namespace OC\Encryption\Exceptions;
 
 class EncryptionHeaderKeyExistsException extends \Exception {
 
-}
\ No newline at end of file
+}
+
+class EncryptionHeaderToLargeException extends \Exception {
+
+}
index d983c92781c227416086aad90e35e443466fea60..1308d27c924b1b09a1e306062d04420d812c71be 100644 (file)
@@ -23,8 +23,9 @@
 
 namespace OC\Encryption;
 
-use OC\Encryption\Exceptions\EncryptionHeaderToLargeException;
 use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
+use OC\Encryption\Exceptions\EncryptionHeaderToLargeException;
+use OC\Files\View;
 use OCP\Encryption\IEncryptionModule;
 use OCP\IConfig;
 
@@ -50,13 +51,13 @@ class Util {
         */
        protected $blockSize = 8192;
 
-       /** @var \OC\Files\View */
+       /** @var View */
        protected $view;
 
        /** @var array */
        protected $ocHeaderKeys;
 
-       /** @var \OC\User\Manager */
+       /** @var Manager */
        protected $userManager;
 
        /** @var IConfig */
@@ -93,7 +94,7 @@ class Util {
         * @param array $header
         * @return string
         */
-       public function getEncryptionModuleId(array $header) {
+       public function getEncryptionModuleId(array $header = null) {
                $id = '';
                $encryptionModuleKey = self::HEADER_ENCRYPTION_MODULE_KEY;
 
@@ -153,7 +154,7 @@ class Util {
                $header .= self::HEADER_END;
 
                if (strlen($header) > $this->getHeaderSize()) {
-                       throw new EncryptionHeaderToLargeException('max header size exceeded', EncryptionException::ENCRYPTION_HEADER_TO_LARGE);
+                       throw new EncryptionHeaderToLargeException('max header size exceeded');
                }
 
                $paddedHeader = str_pad($header, $this->headerSize, self::HEADER_PADDING_CHAR, STR_PAD_RIGHT);
@@ -208,7 +209,7 @@ class Util {
         * go recursively through a dir and collect all files and sub files.
         *
         * @param string $dir relative to the users files folder
-        * @param strinf $mountPoint
+        * @param string $mountPoint
         * @return array with list of files relative to the users files folder
         */
        public function getAllFiles($dir, $mountPoint = '') {
@@ -285,19 +286,19 @@ class Util {
                        throw new \BadMethodCallException('path needs to be relative to the system wide data folder and point to a user specific file');
                }
 
-               $pathinfo = pathinfo($path);
-               $partfile = false;
+               $pathInfo = pathinfo($path);
+               $partFile = false;
                $parentFolder = false;
-               if (array_key_exists('extension', $pathinfo) && $pathinfo['extension'] === 'part') {
+               if (array_key_exists('extension', $pathInfo) && $pathInfo['extension'] === 'part') {
                        // if the real file exists we check this file
-                       $filePath = $pathinfo['dirname'] . '/' . $pathinfo['filename'];
+                       $filePath = $pathInfo['dirname'] . '/' . $pathInfo['filename'];
                        if ($this->view->file_exists($filePath)) {
-                               $pathToCheck = $pathinfo['dirname'] . '/' . $pathinfo['filename'];
+                               $pathToCheck = $pathInfo['dirname'] . '/' . $pathInfo['filename'];
                        } else { // otherwise we look for the parent
-                               $pathToCheck = $pathinfo['dirname'];
+                               $pathToCheck = $pathInfo['dirname'];
                                $parentFolder = true;
                        }
-                       $partfile = true;
+                       $partFile = true;
                } else {
                        $pathToCheck = $path;
                }
@@ -320,11 +321,11 @@ class Util {
                $this->view->chroot('/');
 
                if ($parentFolder) {
-                       $ownerPath = $ownerPath . '/'. $pathinfo['filename'];
+                       $ownerPath = $ownerPath . '/'. $pathInfo['filename'];
                }
 
-               if ($partfile) {
-                       $ownerPath = $ownerPath . '.' . $pathinfo['extension'];
+               if ($partFile) {
+                       $ownerPath = $ownerPath . '.' . $pathInfo['extension'];
                }
 
                return array(
index 0e70c99c8d73d3649357e87297c3578e7b4df981..a9e65a0014778b3b5db6420627dc2eda024dfd53 100644 (file)
@@ -295,7 +295,9 @@ class Encryption extends Wrapper {
         * read encryption module needed to read/write the file located at $path
         *
         * @param string $path
-        * @return \OCP\Encryption\IEncryptionModule|null
+        * @return null|\OCP\Encryption\IEncryptionModule
+        * @throws ModuleDoesNotExistsException
+        * @throws \Exception
         */
        protected function getEncryptionModule($path) {
                $encryptionModule = null;
@@ -305,7 +307,7 @@ class Encryption extends Wrapper {
                        try {
                                $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
                        } catch (ModuleDoesNotExistsException $e) {
-                               $this->logger->critical('Encryption module defined in "' . $path . '" mot loaded!');
+                               $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!');
                                throw $e;
                        }
                }
diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php
new file mode 100644 (file)
index 0000000..b8d4e96
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace Test\Files\Storage\Wrapper;
+
+use OC\Files\View;
+use OCA\Encryption_Dummy\DummyModule;
+
+class Encryption extends \Test\Files\Storage\Storage {
+
+       /**
+        * @var \OC\Files\Storage\Temporary
+        */
+       private $sourceStorage;
+
+       public function setUp() {
+
+               parent::setUp();
+
+               $encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
+                       ->disableOriginalConstructor()
+                       ->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule'])
+                       ->getMock();
+               $encryptionManager->expects($this->any())
+                       ->method('getDefaultEncryptionModule')
+                       ->willReturn(new DummyModule());
+
+               $util = new \OC\Encryption\Util(new View(), new \OC\User\Manager());
+
+               $logger = $this->getMock('\OC\Log');
+
+               $this->sourceStorage = new \OC\Files\Storage\Temporary(array());
+               $this->instance = new \OC\Files\Storage\Wrapper\Encryption([
+                       'storage' => $this->sourceStorage,
+                       'root' => 'foo',
+                       'mountPoint' => '/'
+               ],
+                       $encryptionManager, $util, $logger
+               );
+       }
+
+//     public function testMkDirRooted() {
+//             $this->instance->mkdir('bar');
+//             $this->assertTrue($this->sourceStorage->is_dir('foo/bar'));
+//     }
+//
+//     public function testFilePutContentsRooted() {
+//             $this->instance->file_put_contents('bar', 'asd');
+//             $this->assertEquals('asd', $this->sourceStorage->file_get_contents('foo/bar'));
+//     }
+}
index 270ce750ecfe16b76ee9a17f2f1abdbde160a72b..a7bd684df4403babee11c81890d7cfaf3c4c964a 100644 (file)
@@ -9,10 +9,6 @@
 namespace Test\Files\Storage\Wrapper;
 
 class Jail extends \Test\Files\Storage\Storage {
-       /**
-        * @var string tmpDir
-        */
-       private $tmpDir;
 
        /**
         * @var \OC\Files\Storage\Temporary