2015-03-30 11:23:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Test\Files\Storage\Wrapper;
|
|
|
|
|
2015-04-02 16:12:20 +00:00
|
|
|
use OC\Files\Storage\Temporary;
|
2015-03-30 11:23:10 +00:00
|
|
|
use OC\Files\View;
|
|
|
|
|
|
|
|
class Encryption extends \Test\Files\Storage\Storage {
|
|
|
|
|
|
|
|
/**
|
2015-04-02 16:12:20 +00:00
|
|
|
* @var Temporary
|
2015-03-30 11:23:10 +00:00
|
|
|
*/
|
|
|
|
private $sourceStorage;
|
|
|
|
|
2015-04-23 14:48:11 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Wrapper\Encryption
|
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
2015-04-22 10:12:27 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Keys\Storage | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $keyStore;
|
|
|
|
|
2015-04-23 14:48:11 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Util | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $util;
|
|
|
|
|
2015-04-23 15:06:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Manager | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $encryptionManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Encryption\IEncryptionModule | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $encryptionModule;
|
|
|
|
|
|
|
|
|
2015-04-23 14:48:11 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\Update | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $update;
|
|
|
|
|
2015-03-30 11:23:10 +00:00
|
|
|
public function setUp() {
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-04-02 12:44:58 +00:00
|
|
|
$mockModule = $this->buildMockModule();
|
2015-04-23 15:06:55 +00:00
|
|
|
$this->encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
|
2015-03-30 11:23:10 +00:00
|
|
|
->disableOriginalConstructor()
|
2015-04-02 16:12:20 +00:00
|
|
|
->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule', 'isEnabled'])
|
2015-03-30 21:19:35 +00:00
|
|
|
->getMock();
|
2015-04-23 15:06:55 +00:00
|
|
|
$this->encryptionManager->expects($this->any())
|
2015-03-30 11:23:10 +00:00
|
|
|
->method('getDefaultEncryptionModule')
|
2015-04-02 12:44:58 +00:00
|
|
|
->willReturn($mockModule);
|
2015-04-23 15:06:55 +00:00
|
|
|
$this->encryptionManager->expects($this->any())
|
2015-04-02 12:44:58 +00:00
|
|
|
->method('getEncryptionModule')
|
|
|
|
->willReturn($mockModule);
|
2015-04-23 15:06:55 +00:00
|
|
|
$this->encryptionManager->expects($this->any())
|
2015-04-02 16:12:20 +00:00
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$config = $this->getMockBuilder('\OCP\IConfig')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-04-15 11:19:17 +00:00
|
|
|
$groupManager = $this->getMockBuilder('\OC\Group\Manager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-03-30 11:23:10 +00:00
|
|
|
|
2015-04-23 14:48:11 +00: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 12:44:58 +00:00
|
|
|
->method('getUidAndFilename')
|
|
|
|
->willReturnCallback(function ($path) {
|
|
|
|
return ['user1', $path];
|
|
|
|
});
|
2015-04-02 16:12:20 +00:00
|
|
|
|
2015-04-02 12:44:58 +00:00
|
|
|
$file = $this->getMockBuilder('\OC\Encryption\File')
|
|
|
|
->disableOriginalConstructor()
|
2015-04-09 16:30:45 +00:00
|
|
|
->setMethods(['getAccessList'])
|
2015-04-02 12:44:58 +00:00
|
|
|
->getMock();
|
2015-04-09 16:30:45 +00:00
|
|
|
$file->expects($this->any())->method('getAccessList')->willReturn([]);
|
2015-03-30 11:23:10 +00:00
|
|
|
|
|
|
|
$logger = $this->getMock('\OC\Log');
|
|
|
|
|
2015-04-02 16:12:20 +00:00
|
|
|
$this->sourceStorage = new Temporary(array());
|
2015-04-22 10:12:27 +00:00
|
|
|
$this->keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage')
|
2015-04-02 12:44:58 +00:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-04-23 14:48:11 +00:00
|
|
|
$this->update = $this->getMockBuilder('\OC\Encryption\Update')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-04-07 08:03:44 +00:00
|
|
|
$mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['getOption'])
|
|
|
|
->getMock();
|
|
|
|
$mount->expects($this->any())->method('getOption')->willReturn(true);
|
2015-04-22 11:09:42 +00:00
|
|
|
$this->instance = new \OC\Files\Storage\Wrapper\Encryption([
|
2015-03-30 11:23:10 +00:00
|
|
|
'storage' => $this->sourceStorage,
|
|
|
|
'root' => 'foo',
|
2015-04-07 08:03:44 +00:00
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $mount
|
2015-03-30 11:23:10 +00:00
|
|
|
],
|
2015-04-23 15:06:55 +00:00
|
|
|
$this->encryptionManager, $this->util, $logger, $file, null, $this->keyStore, $this->update
|
2015-03-30 11:23:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:44:58 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
protected function buildMockModule() {
|
2015-04-23 15:06:55 +00:00
|
|
|
$this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
|
2015-04-02 12:44:58 +00:00
|
|
|
->disableOriginalConstructor()
|
2015-04-14 10:53:13 +00:00
|
|
|
->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize'])
|
2015-04-02 12:44:58 +00:00
|
|
|
->getMock();
|
|
|
|
|
2015-04-23 15:06:55 +00: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 12:44:58 +00:00
|
|
|
}
|
2015-04-22 10:12:27 +00:00
|
|
|
|
2015-04-23 14:48:11 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestRename
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
|
|
|
* @param boolean $shouldUpdate
|
|
|
|
*/
|
|
|
|
public function testRename($source, $target, $shouldUpdate) {
|
2015-04-22 10:12:27 +00:00
|
|
|
$this->keyStore
|
|
|
|
->expects($this->once())
|
|
|
|
->method('renameKeys');
|
2015-04-23 14:48:11 +00:00
|
|
|
$this->util->expects($this->any())
|
|
|
|
->method('isFile')->willReturn(true);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testRename()
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataTestRename() {
|
|
|
|
return array(
|
|
|
|
array('source', 'target', false),
|
|
|
|
array('source', '/subFolder/target', true),
|
|
|
|
);
|
2015-04-22 10:12:27 +00:00
|
|
|
}
|
2015-04-23 15:06:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestCopy
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
|
|
|
* @param boolean $shouldUpdate
|
|
|
|
*/
|
|
|
|
public function testCopy($source, $target, $shouldUpdate) {
|
|
|
|
$this->keyStore
|
|
|
|
->expects($this->once())
|
|
|
|
->method('copyKeys');
|
|
|
|
$this->util->expects($this->any())
|
|
|
|
->method('isFile')->willReturn(true);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testRename()
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataTestCopy() {
|
|
|
|
return array(
|
|
|
|
array('source', 'target', false),
|
|
|
|
array('source', '/subFolder/target', true),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-30 11:23:10 +00:00
|
|
|
}
|