Fixing encryption storage wrapper tests

This commit is contained in:
Thomas Müller 2015-04-02 14:44:58 +02:00
parent d9c41b00ab
commit 104d11ec4c
4 changed files with 133 additions and 9 deletions

View 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');
}

View File

@ -24,9 +24,12 @@
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;
}
}

View 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));

View 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;
}
}