]> source.dussan.org Git - nextcloud-server.git/commitdiff
add unit tests to the keystorage
authorBjoern Schiessle <schiessle@owncloud.com>
Thu, 2 Apr 2015 14:42:28 +0000 (16:42 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:31 +0000 (13:30 +0200)
lib/private/encryption/keys/storage.php
tests/lib/encryption/keys/storage.php

index 7d0612ae30e1d49ae8978b44aa222df01a1e205a..42610bd0b41c9001daebd951d1e5f8a2df71b70a 100644 (file)
@@ -339,7 +339,7 @@ class Storage implements \OCP\Encryption\Keys\IStorage {
        protected function keySetPreparation($path) {
                // If the file resides within a subdirectory, create it
                if (!$this->view->file_exists($path)) {
-                       $sub_dirs = explode('/', $path);
+                       $sub_dirs = explode('/', ltrim($path, '/'));
                        $dir = '';
                        foreach ($sub_dirs as $sub_dir) {
                                $dir .= '/' . $sub_dir;
index be32f0e44a1337884750097542928e914b5d7fe7..8ab46987f8c5796d2304e0886773dfec9c033aed 100644 (file)
@@ -28,6 +28,9 @@ use Test\TestCase;
 
 class StorageTest extends TestCase {
 
+       /** @var Storage */
+       protected $storage;
+
        /** @var \PHPUnit_Framework_MockObject_MockObject */
        protected $util;
 
@@ -45,6 +48,8 @@ class StorageTest extends TestCase {
                        ->disableOriginalConstructor()
                        ->getMock();
 
+               $this->storage = new Storage('encModule', $this->view, $this->util);
+
        }
 
        public function testSetFileKey() {
@@ -63,10 +68,8 @@ class StorageTest extends TestCase {
                                $this->equalTo('key'))
                        ->willReturn(strlen('key'));
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
+                       $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
                );
        }
 
@@ -89,10 +92,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertSame('key',
-                       $storage->getFileKey('user1/files/foo.txt', 'fileKey')
+                       $this->storage->getFileKey('user1/files/foo.txt', 'fileKey')
                );
        }
 
@@ -112,10 +113,8 @@ class StorageTest extends TestCase {
                                $this->equalTo('key'))
                        ->willReturn(strlen('key'));
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
+                       $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
                );
        }
 
@@ -138,10 +137,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertSame('key',
-                       $storage->getFileKey('user1/files/foo.txt', 'fileKey')
+                       $this->storage->getFileKey('user1/files/foo.txt', 'fileKey')
                );
        }
 
@@ -152,10 +149,8 @@ class StorageTest extends TestCase {
                                $this->equalTo('key'))
                        ->willReturn(strlen('key'));
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->setSystemUserKey('shareKey_56884', 'key')
+                       $this->storage->setSystemUserKey('shareKey_56884', 'key')
                );
        }
 
@@ -166,10 +161,8 @@ class StorageTest extends TestCase {
                                $this->equalTo('key'))
                        ->willReturn(strlen('key'));
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->setUserKey('user1', 'publicKey', 'key')
+                       $this->storage->setUserKey('user1', 'publicKey', 'key')
                );
        }
 
@@ -183,10 +176,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertSame('key',
-                       $storage->getSystemUserKey('shareKey_56884')
+                       $this->storage->getSystemUserKey('shareKey_56884')
                );
        }
 
@@ -200,10 +191,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertSame('key',
-                       $storage->getUserKey('user1', 'publicKey')
+                       $this->storage->getUserKey('user1', 'publicKey')
                );
        }
 
@@ -213,10 +202,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->deleteUserKey('user1', 'publicKey')
+                       $this->storage->deleteUserKey('user1', 'publicKey')
                );
        }
 
@@ -226,10 +213,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->deleteSystemUserKey('shareKey_56884')
+                       $this->storage->deleteSystemUserKey('shareKey_56884')
                );
        }
 
@@ -248,10 +233,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
+                       $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
                );
        }
 
@@ -270,10 +253,8 @@ class StorageTest extends TestCase {
                        ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
                        ->willReturn(true);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-
                $this->assertTrue(
-                       $storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
+                       $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
                );
        }
 
@@ -300,8 +281,7 @@ class StorageTest extends TestCase {
                        ->method('isSystemWideMountPoint')
                        ->willReturn($systemWideMount);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-               $storage->renameKeys($source, $target);
+               $this->storage->renameKeys($source, $target);
        }
 
        /**
@@ -327,8 +307,7 @@ class StorageTest extends TestCase {
                        ->method('isSystemWideMountPoint')
                        ->willReturn($systemWideMount);
 
-               $storage = new Storage('encModule', $this->view, $this->util);
-               $storage->copyKeys($source, $target);
+               $this->storage->copyKeys($source, $target);
        }
 
        public function getUidAndFilenameCallback() {
@@ -353,4 +332,30 @@ class StorageTest extends TestCase {
                );
        }
 
+       public function testKeySetPreparation() {
+               $this->view->expects($this->any())
+                       ->method('file_exists')
+                       ->willReturn(false);
+               $this->view->expects($this->any())
+                       ->method('is_dir')
+                       ->willReturn(false);
+               $this->view->expects($this->any())
+                       ->method('mkdir')
+                       ->will($this->returnCallback(array($this, 'mkdirCallback')));
+
+               $this->mkdirStack = array(
+                       '/user1/files_encryption/keys/foo',
+                       '/user1/files_encryption/keys',
+                       '/user1/files_encryption',
+                       '/user1');
+
+               \Test_Helper::invokePrivate($this->storage, 'keySetPreparation', array('/user1/files_encryption/keys/foo'));
+       }
+
+       public function mkdirCallback() {
+               $args = func_get_args();
+               $expected = array_pop($this->mkdirStack);
+               $this->assertSame($expected, $args[0]);
+       }
+
 }