2015-03-30 14:23:10 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Test\Files\Storage\Wrapper;
|
|
|
|
|
2015-04-02 19:12:20 +03:00
|
|
|
use OC\Files\Storage\Temporary;
|
2015-03-30 14:23:10 +03:00
|
|
|
use OC\Files\View;
|
|
|
|
|
|
|
|
class Encryption extends \Test\Files\Storage\Storage {
|
|
|
|
|
|
|
|
/**
|
2015-04-02 19:12:20 +03:00
|
|
|
* @var Temporary
|
2015-03-30 14:23:10 +03:00
|
|
|
*/
|
|
|
|
private $sourceStorage;
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
2015-05-05 15:38:06 +03:00
|
|
|
* @var \OC\Files\Storage\Wrapper\Encryption | \PHPUnit_Framework_MockObject_MockObject
|
2015-04-23 17:48:11 +03:00
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
2015-04-22 13:12:27 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Keys\Storage | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $keyStore;
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Util | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $util;
|
|
|
|
|
2015-04-23 18:06:55 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Manager | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $encryptionManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Encryption\IEncryptionModule | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $encryptionModule;
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Update | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $update;
|
|
|
|
|
2015-05-05 15:38:06 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\Cache | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
2015-04-27 14:13:27 +03:00
|
|
|
/** @var integer dummy unencrypted size */
|
|
|
|
private $dummySize = -1;
|
|
|
|
|
2015-04-24 14:06:27 +03:00
|
|
|
protected function setUp() {
|
2015-03-30 14:23:10 +03:00
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-04-02 15:44:58 +03:00
|
|
|
$mockModule = $this->buildMockModule();
|
2015-04-23 18:06:55 +03:00
|
|
|
$this->encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
|
2015-03-30 14:23:10 +03:00
|
|
|
->disableOriginalConstructor()
|
2015-04-27 12:10:31 +03:00
|
|
|
->setMethods(['getEncryptionModule', 'isEnabled'])
|
2015-03-31 00:19:35 +03:00
|
|
|
->getMock();
|
2015-04-23 18:06:55 +03:00
|
|
|
$this->encryptionManager->expects($this->any())
|
2015-04-02 15:44:58 +03:00
|
|
|
->method('getEncryptionModule')
|
|
|
|
->willReturn($mockModule);
|
2015-04-02 19:12:20 +03:00
|
|
|
|
|
|
|
$config = $this->getMockBuilder('\OCP\IConfig')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-04-15 14:19:17 +03:00
|
|
|
$groupManager = $this->getMockBuilder('\OC\Group\Manager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-03-30 14:23:10 +03:00
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
$this->util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename', 'isFile'], [new View(), new \OC\User\Manager(), $groupManager, $config]);
|
|
|
|
$this->util->expects($this->any())
|
2015-04-02 15:44:58 +03:00
|
|
|
->method('getUidAndFilename')
|
|
|
|
->willReturnCallback(function ($path) {
|
|
|
|
return ['user1', $path];
|
|
|
|
});
|
2015-04-02 19:12:20 +03:00
|
|
|
|
2015-04-02 15:44:58 +03:00
|
|
|
$file = $this->getMockBuilder('\OC\Encryption\File')
|
|
|
|
->disableOriginalConstructor()
|
2015-04-09 19:30:45 +03:00
|
|
|
->setMethods(['getAccessList'])
|
2015-04-02 15:44:58 +03:00
|
|
|
->getMock();
|
2015-04-09 19:30:45 +03:00
|
|
|
$file->expects($this->any())->method('getAccessList')->willReturn([]);
|
2015-03-30 14:23:10 +03:00
|
|
|
|
|
|
|
$logger = $this->getMock('\OC\Log');
|
|
|
|
|
2015-04-02 19:12:20 +03:00
|
|
|
$this->sourceStorage = new Temporary(array());
|
2015-04-27 14:13:27 +03:00
|
|
|
|
2015-04-22 13:12:27 +03:00
|
|
|
$this->keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage')
|
2015-04-02 15:44:58 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-04-27 14:13:27 +03:00
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
$this->update = $this->getMockBuilder('\OC\Encryption\Update')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-04-27 14:13:27 +03:00
|
|
|
|
2015-04-07 11:03:44 +03:00
|
|
|
$mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['getOption'])
|
|
|
|
->getMock();
|
|
|
|
$mount->expects($this->any())->method('getOption')->willReturn(true);
|
2015-04-27 14:13:27 +03:00
|
|
|
|
2015-05-05 15:38:06 +03:00
|
|
|
$this->cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-04-27 14:13:27 +03:00
|
|
|
$this->cache->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->willReturn(['encrypted' => false]);
|
2015-05-05 15:38:06 +03:00
|
|
|
|
|
|
|
$this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
2015-04-15 10:49:50 +03:00
|
|
|
->setConstructorArgs(
|
2015-05-05 15:38:06 +03:00
|
|
|
[
|
2015-04-15 10:49:50 +03:00
|
|
|
[
|
|
|
|
'storage' => $this->sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $mount
|
|
|
|
],
|
|
|
|
$this->encryptionManager, $this->util, $logger, $file, null, $this->keyStore, $this->update
|
|
|
|
]
|
|
|
|
)
|
2015-05-05 15:38:06 +03:00
|
|
|
->setMethods(['getMetaData', 'getCache'])
|
|
|
|
->getMock();
|
|
|
|
|
2015-04-27 14:13:27 +03:00
|
|
|
$this->instance->expects($this->any())
|
|
|
|
->method('getMetaData')
|
|
|
|
->willReturn(['encrypted' => true, 'size' => $this->dummySize]);
|
|
|
|
|
|
|
|
$this->instance->expects($this->any())
|
|
|
|
->method('getCache')
|
|
|
|
->willReturn($this->cache);
|
2015-03-30 14:23:10 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 15:44:58 +03:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
protected function buildMockModule() {
|
2015-04-23 18:06:55 +03:00
|
|
|
$this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
|
2015-04-02 15:44:58 +03:00
|
|
|
->disableOriginalConstructor()
|
2015-04-14 13:53:13 +03:00
|
|
|
->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize'])
|
2015-04-02 15:44:58 +03:00
|
|
|
->getMock();
|
|
|
|
|
2015-04-23 18:06:55 +03:00
|
|
|
$this->encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
|
|
|
|
$this->encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
|
|
|
|
$this->encryptionModule->expects($this->any())->method('begin')->willReturn([]);
|
|
|
|
$this->encryptionModule->expects($this->any())->method('end')->willReturn('');
|
|
|
|
$this->encryptionModule->expects($this->any())->method('encrypt')->willReturnArgument(0);
|
|
|
|
$this->encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0);
|
|
|
|
$this->encryptionModule->expects($this->any())->method('update')->willReturn(true);
|
|
|
|
$this->encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
|
|
|
|
$this->encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(8192);
|
|
|
|
return $this->encryptionModule;
|
2015-04-02 15:44:58 +03:00
|
|
|
}
|
2015-04-22 13:12:27 +03:00
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
2015-04-24 14:06:27 +03:00
|
|
|
* @dataProvider dataTestCopyAndRename
|
2015-04-23 17:48:11 +03:00
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
2015-04-27 14:13:27 +03:00
|
|
|
* @param $encryptionEnabled
|
2015-04-24 15:27:23 +03:00
|
|
|
* @param boolean $renameKeysReturn
|
2015-04-23 17:48:11 +03:00
|
|
|
* @param boolean $shouldUpdate
|
|
|
|
*/
|
2015-04-27 14:13:27 +03:00
|
|
|
public function testRename($source,
|
|
|
|
$target,
|
|
|
|
$encryptionEnabled,
|
|
|
|
$renameKeysReturn,
|
|
|
|
$shouldUpdate) {
|
|
|
|
if ($encryptionEnabled) {
|
|
|
|
$this->keyStore
|
|
|
|
->expects($this->once())
|
|
|
|
->method('renameKeys')
|
|
|
|
->willReturn($renameKeysReturn);
|
|
|
|
} else {
|
|
|
|
$this->keyStore
|
|
|
|
->expects($this->never())->method('renameKeys');
|
|
|
|
}
|
2015-04-23 17:48:11 +03:00
|
|
|
$this->util->expects($this->any())
|
|
|
|
->method('isFile')->willReturn(true);
|
2015-04-27 14:13:27 +03:00
|
|
|
$this->encryptionManager->expects($this->once())
|
|
|
|
->method('isEnabled')->willReturn($encryptionEnabled);
|
2015-04-23 17:48:11 +03:00
|
|
|
if ($shouldUpdate) {
|
|
|
|
$this->update->expects($this->once())
|
|
|
|
->method('update');
|
|
|
|
} else {
|
|
|
|
$this->update->expects($this->never())
|
|
|
|
->method('update');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->instance->mkdir($source);
|
|
|
|
$this->instance->mkdir(dirname($target));
|
|
|
|
$this->instance->rename($source, $target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-24 14:06:27 +03:00
|
|
|
* @dataProvider dataTestCopyAndRename
|
2015-04-23 18:06:55 +03:00
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
2015-04-27 14:13:27 +03:00
|
|
|
* @param $encryptionEnabled
|
2015-04-24 15:27:23 +03:00
|
|
|
* @param boolean $copyKeysReturn
|
2015-04-23 18:06:55 +03:00
|
|
|
* @param boolean $shouldUpdate
|
|
|
|
*/
|
2015-04-27 14:13:27 +03:00
|
|
|
public function testCopy($source,
|
|
|
|
$target,
|
|
|
|
$encryptionEnabled,
|
|
|
|
$copyKeysReturn,
|
|
|
|
$shouldUpdate) {
|
|
|
|
|
|
|
|
if ($encryptionEnabled) {
|
|
|
|
$this->keyStore
|
|
|
|
->expects($this->once())
|
|
|
|
->method('copyKeys')
|
|
|
|
->willReturn($copyKeysReturn);
|
|
|
|
$this->cache->expects($this->once())
|
|
|
|
->method('put')
|
|
|
|
->with($this->anything(), ['encrypted' => true])
|
|
|
|
->willReturn(true);
|
|
|
|
} else {
|
|
|
|
$this->cache->expects($this->never())->method('put');
|
|
|
|
$this->keyStore->expects($this->never())->method('copyKeys');
|
|
|
|
}
|
2015-04-23 18:06:55 +03:00
|
|
|
$this->util->expects($this->any())
|
|
|
|
->method('isFile')->willReturn(true);
|
2015-04-27 14:13:27 +03:00
|
|
|
$this->encryptionManager->expects($this->once())
|
|
|
|
->method('isEnabled')->willReturn($encryptionEnabled);
|
2015-04-23 18:06:55 +03:00
|
|
|
if ($shouldUpdate) {
|
|
|
|
$this->update->expects($this->once())
|
|
|
|
->method('update');
|
|
|
|
} else {
|
|
|
|
$this->update->expects($this->never())
|
|
|
|
->method('update');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->instance->mkdir($source);
|
|
|
|
$this->instance->mkdir(dirname($target));
|
|
|
|
$this->instance->copy($source, $target);
|
2015-05-05 15:38:06 +03:00
|
|
|
|
2015-04-27 14:13:27 +03:00
|
|
|
if ($encryptionEnabled) {
|
|
|
|
$this->assertSame($this->dummySize,
|
|
|
|
$this->instance->filesize($target)
|
|
|
|
);
|
|
|
|
}
|
2015-04-24 14:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testCopyTesting() and dataTestCopyAndRename()
|
2015-04-23 18:06:55 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-04-24 14:06:27 +03:00
|
|
|
public function dataTestCopyAndRename() {
|
2015-04-23 18:06:55 +03:00
|
|
|
return array(
|
2015-04-27 14:13:27 +03:00
|
|
|
array('source', 'target', true, false, false),
|
|
|
|
array('source', 'target', true, true, false),
|
|
|
|
array('source', '/subFolder/target', true, false, false),
|
|
|
|
array('source', '/subFolder/target', true, true, true),
|
|
|
|
array('source', '/subFolder/target', false, true, false),
|
2015-04-23 18:06:55 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-27 15:26:05 +03:00
|
|
|
public function testIsLocal() {
|
2015-04-27 14:13:27 +03:00
|
|
|
$this->encryptionManager->expects($this->once())
|
|
|
|
->method('isEnabled')->willReturn(true);
|
2015-04-27 15:26:05 +03:00
|
|
|
$this->assertFalse($this->instance->isLocal());
|
|
|
|
}
|
2015-03-30 14:23:10 +03:00
|
|
|
}
|