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;
$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);
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);
}
}
}
}
+ /**
+ * 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
*
$this->unencryptedSize[$path] = $unencryptedSize;
}
+ /**
+ * @param string $encryptionModule
+ * @return \OCP\Encryption\Keys\IStorage
+ */
+ protected function getKeyStorage($encryptionModuleId) {
+ $keyStorage = \OC::$server->getEncryptionKeyStorage($encryptionModuleId);
+ return $keyStorage;
+ }
+
}
parent::setUp();
+ $mockModule = $this->buildMockModule();
$encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
->disableOriginalConstructor()
->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule'])
->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'));
// $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;
+ }
+
+}