2015-03-30 14:23:10 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Test\Files\Storage\Wrapper;
|
|
|
|
|
2015-07-09 19:04:35 +03:00
|
|
|
use OC\Encryption\Util;
|
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;
|
2016-09-07 21:01:13 +03:00
|
|
|
use OC\Log;
|
|
|
|
use OC\Memcache\ArrayCache;
|
2016-04-14 18:32:25 +03:00
|
|
|
use OC\User\Manager;
|
2016-09-07 21:01:13 +03:00
|
|
|
use OCP\Files\Cache\ICache;
|
2016-02-22 19:28:53 +03:00
|
|
|
use Test\Files\Storage\Storage;
|
2015-03-30 14:23:10 +03:00
|
|
|
|
2016-05-20 16:38:20 +03:00
|
|
|
class EncryptionTest extends Storage {
|
2015-03-30 14:23:10 +03:00
|
|
|
|
2015-07-09 19:04:35 +03:00
|
|
|
/**
|
|
|
|
* block size will always be 8192 for a PHP stream
|
|
|
|
* @see https://bugs.php.net/bug.php?id=21641
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $headerSize = 8192;
|
|
|
|
|
2015-03-30 14:23:10 +03:00
|
|
|
/**
|
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-05-18 12:54:51 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Log | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Encryption\File | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $file;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Mount\MountPoint | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $mount;
|
|
|
|
|
2015-05-21 15:07:42 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Mount\Manager | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $mountManager;
|
|
|
|
|
2015-06-23 11:43:28 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Group\Manager | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2016-03-31 00:20:37 +03:00
|
|
|
/** @var \OC\Memcache\ArrayCache | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $arrayCache;
|
|
|
|
|
2015-06-23 11:43:28 +03:00
|
|
|
|
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
|
|
|
|
2016-09-07 21:01:13 +03:00
|
|
|
$this->arrayCache = $this->createMock(ArrayCache::class);
|
2015-06-23 11:43:28 +03:00
|
|
|
$this->config = $this->getMockBuilder('\OCP\IConfig')
|
2015-04-02 19:12:20 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-06-23 11:43:28 +03:00
|
|
|
$this->groupManager = $this->getMockBuilder('\OC\Group\Manager')
|
2015-04-15 14:19:17 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-03-30 14:23:10 +03:00
|
|
|
|
2016-09-07 21:01:13 +03:00
|
|
|
$this->util = $this->getMockBuilder('\OC\Encryption\Util')
|
|
|
|
->setMethods(['getUidAndFilename', 'isFile', 'isExcluded'])
|
2016-10-28 10:07:40 +03:00
|
|
|
->setConstructorArgs([new View(), new Manager($this->config), $this->groupManager, $this->config, $this->arrayCache])
|
2016-09-07 21:01:13 +03:00
|
|
|
->getMock();
|
2015-04-23 17:48:11 +03:00
|
|
|
$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-05-18 12:54:51 +03:00
|
|
|
$this->file = $this->getMockBuilder('\OC\Encryption\File')
|
2015-04-02 15:44:58 +03:00
|
|
|
->disableOriginalConstructor()
|
2015-04-09 19:30:45 +03:00
|
|
|
->setMethods(['getAccessList'])
|
2015-04-02 15:44:58 +03:00
|
|
|
->getMock();
|
2015-05-18 12:54:51 +03:00
|
|
|
$this->file->expects($this->any())->method('getAccessList')->willReturn([]);
|
2015-03-30 14:23:10 +03:00
|
|
|
|
2016-09-07 21:01:13 +03:00
|
|
|
$this->logger = $this->createMock(Log::class);
|
2015-03-30 14:23:10 +03:00
|
|
|
|
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-05-18 12:54:51 +03:00
|
|
|
$this->mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
|
2015-04-07 11:03:44 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['getOption'])
|
|
|
|
->getMock();
|
2015-07-23 18:01:44 +03:00
|
|
|
$this->mount->expects($this->any())->method('getOption')->willReturnCallback(function ($option, $default) {
|
|
|
|
if ($option === 'encrypt' && $default === true) {
|
|
|
|
global $mockedMountPointEncryptionEnabled;
|
|
|
|
if ($mockedMountPointEncryptionEnabled !== null) {
|
|
|
|
return $mockedMountPointEncryptionEnabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 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')
|
2015-05-21 15:07:42 +03:00
|
|
|
->willReturnCallback(function($path) {return ['encrypted' => false, 'path' => $path];});
|
|
|
|
|
|
|
|
$this->mountManager = $this->getMockBuilder('\OC\Files\Mount\Manager')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
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' => '/',
|
2015-05-18 12:54:51 +03:00
|
|
|
'mount' => $this->mount
|
2015-04-15 10:49:50 +03:00
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2015-04-15 10:49:50 +03:00
|
|
|
]
|
|
|
|
)
|
2015-05-12 19:49:25 +03:00
|
|
|
->setMethods(['getMetaData', 'getCache', 'getEncryptionModule'])
|
2015-05-05 15:38:06 +03:00
|
|
|
->getMock();
|
|
|
|
|
2015-04-27 14:13:27 +03:00
|
|
|
$this->instance->expects($this->any())
|
|
|
|
->method('getMetaData')
|
2015-05-21 15:07:42 +03:00
|
|
|
->willReturnCallback(function ($path) {
|
|
|
|
return ['encrypted' => true, 'size' => $this->dummySize, 'path' => $path];
|
|
|
|
});
|
2015-04-27 14:13:27 +03:00
|
|
|
|
|
|
|
$this->instance->expects($this->any())
|
|
|
|
->method('getCache')
|
|
|
|
->willReturn($this->cache);
|
2015-05-12 19:49:25 +03:00
|
|
|
|
|
|
|
$this->instance->expects($this->any())
|
|
|
|
->method('getEncryptionModule')
|
|
|
|
->willReturn($mockModule);
|
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()
|
2016-04-14 18:32:25 +03:00
|
|
|
->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser'])
|
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);
|
2015-05-12 19:49:25 +03:00
|
|
|
$this->encryptionModule->expects($this->any())->method('isReadable')->willReturn(true);
|
2015-04-23 18:06:55 +03:00
|
|
|
return $this->encryptionModule;
|
2015-04-02 15:44:58 +03:00
|
|
|
}
|
2015-04-22 13:12:27 +03:00
|
|
|
|
2016-02-22 19:28:53 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestGetMetaData
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $metaData
|
|
|
|
* @param bool $encrypted
|
|
|
|
* @param bool $unencryptedSizeSet
|
|
|
|
* @param int $storedUnencryptedSize
|
|
|
|
* @param array $expected
|
|
|
|
*/
|
|
|
|
public function testGetMetaData($path, $metaData, $encrypted, $unencryptedSizeSet, $storedUnencryptedSize, $expected) {
|
|
|
|
|
|
|
|
$sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$cache->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->willReturnCallback(
|
|
|
|
function($path) use ($encrypted) {
|
|
|
|
return ['encrypted' => $encrypted, 'path' => $path, 'size' => 0, 'fileid' => 1];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'storage' => $sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2016-02-22 19:28:53 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['getCache', 'verifyUnencryptedSize'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
if($unencryptedSizeSet) {
|
|
|
|
$this->invokePrivate($this->instance, 'unencryptedSize', [[$path => $storedUnencryptedSize]]);
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:06:37 +03:00
|
|
|
$fileEntry = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-02-22 19:28:53 +03:00
|
|
|
$sourceStorage->expects($this->once())->method('getMetaData')->with($path)
|
|
|
|
->willReturn($metaData);
|
2016-03-31 19:06:37 +03:00
|
|
|
$sourceStorage->expects($this->any())
|
|
|
|
->method('getCache')
|
|
|
|
->with($path)
|
|
|
|
->willReturn($fileEntry);
|
|
|
|
$fileEntry->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with($metaData['fileid']);
|
2016-02-22 19:28:53 +03:00
|
|
|
|
|
|
|
$this->instance->expects($this->any())->method('getCache')->willReturn($cache);
|
|
|
|
$this->instance->expects($this->any())->method('verifyUnencryptedSize')
|
|
|
|
->with($path, 0)->willReturn($expected['size']);
|
|
|
|
|
|
|
|
$result = $this->instance->getMetaData($path);
|
2016-03-31 19:06:37 +03:00
|
|
|
if(isset($expected['encrypted'])) {
|
|
|
|
$this->assertSame($expected['encrypted'], (bool)$result['encrypted']);
|
|
|
|
|
|
|
|
if(isset($expected['encryptedVersion'])) {
|
|
|
|
$this->assertSame($expected['encryptedVersion'], $result['encryptedVersion']);
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 19:28:53 +03:00
|
|
|
$this->assertSame($expected['size'], $result['size']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestGetMetaData() {
|
|
|
|
return [
|
2016-03-31 19:06:37 +03:00
|
|
|
['/test.txt', ['size' => 42, 'encrypted' => 2, 'encryptedVersion' => 2, 'fileid' => 1], true, true, 12, ['size' => 12, 'encrypted' => true, 'encryptedVersion' => 2]],
|
2016-02-22 19:28:53 +03:00
|
|
|
['/test.txt', null, true, true, 12, null],
|
2016-03-31 19:06:37 +03:00
|
|
|
['/test.txt', ['size' => 42, 'encrypted' => 0, 'fileid' => 1], false, false, 12, ['size' => 42, 'encrypted' => false]],
|
|
|
|
['/test.txt', ['size' => 42, 'encrypted' => false, 'fileid' => 1], true, false, 12, ['size' => 12, 'encrypted' => true]]
|
2016-02-22 19:28:53 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilesize() {
|
|
|
|
$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$cache->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->willReturn(['encrypted' => true, 'path' => '/test.txt', 'size' => 0, 'fileid' => 1]);
|
|
|
|
|
|
|
|
$this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'storage' => $this->sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2016-02-22 19:28:53 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['getCache', 'verifyUnencryptedSize'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->instance->expects($this->any())->method('getCache')->willReturn($cache);
|
|
|
|
$this->instance->expects($this->any())->method('verifyUnencryptedSize')
|
|
|
|
->willReturn(42);
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSame(42,
|
|
|
|
$this->instance->filesize('/test.txt')
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestVerifyUnencryptedSize
|
|
|
|
*
|
|
|
|
* @param int $encryptedSize
|
|
|
|
* @param int $unencryptedSize
|
|
|
|
* @param bool $failure
|
|
|
|
* @param int $expected
|
|
|
|
*/
|
|
|
|
public function testVerifyUnencryptedSize($encryptedSize, $unencryptedSize, $failure, $expected) {
|
|
|
|
$sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'storage' => $sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2016-02-22 19:28:53 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['fixUnencryptedSize'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$sourceStorage->expects($this->once())->method('filesize')->willReturn($encryptedSize);
|
|
|
|
|
|
|
|
$this->instance->expects($this->any())->method('fixUnencryptedSize')
|
|
|
|
->with('/test.txt', $encryptedSize, $unencryptedSize)
|
|
|
|
->willReturnCallback(
|
|
|
|
function() use ($failure, $expected) {
|
|
|
|
if ($failure) {
|
|
|
|
throw new \Exception();
|
|
|
|
} else {
|
|
|
|
return $expected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
|
|
|
$this->invokePrivate($this->instance, 'verifyUnencryptedSize', ['/test.txt', $unencryptedSize])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestVerifyUnencryptedSize() {
|
|
|
|
return [
|
|
|
|
[120, 80, false, 80],
|
|
|
|
[120, 120, false, 80],
|
|
|
|
[120, -1, false, 80],
|
|
|
|
[120, -1, true, -1]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2015-04-27 14:13:27 +03:00
|
|
|
public function testRename($source,
|
|
|
|
$target,
|
|
|
|
$encryptionEnabled,
|
2015-05-11 11:35:42 +03:00
|
|
|
$renameKeysReturn) {
|
2015-04-27 14:13:27 +03:00
|
|
|
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
|
|
|
|
|
|
|
$this->instance->mkdir($source);
|
|
|
|
$this->instance->mkdir(dirname($target));
|
|
|
|
$this->instance->rename($source, $target);
|
|
|
|
}
|
|
|
|
|
2015-08-24 16:57:03 +03:00
|
|
|
public function testCopyEncryption() {
|
|
|
|
$this->instance->file_put_contents('source.txt', 'bar');
|
|
|
|
$this->instance->copy('source.txt', 'target.txt');
|
|
|
|
$this->assertSame('bar', $this->instance->file_get_contents('target.txt'));
|
|
|
|
$targetMeta = $this->instance->getMetaData('target.txt');
|
|
|
|
$sourceMeta = $this->instance->getMetaData('source.txt');
|
|
|
|
$this->assertSame($sourceMeta['encrypted'], $targetMeta['encrypted']);
|
|
|
|
$this->assertSame($sourceMeta['size'], $targetMeta['size']);
|
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-05-18 12:54:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestRmdir
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param boolean $rmdirResult
|
|
|
|
* @param boolean $isExcluded
|
|
|
|
* @param boolean $encryptionEnabled
|
|
|
|
*/
|
|
|
|
public function testRmdir($path, $rmdirResult, $isExcluded, $encryptionEnabled) {
|
|
|
|
$sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$util = $this->getMockBuilder('\OC\Encryption\Util')->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$sourceStorage->expects($this->once())->method('rmdir')->willReturn($rmdirResult);
|
|
|
|
$util->expects($this->any())->method('isExcluded')-> willReturn($isExcluded);
|
|
|
|
$this->encryptionManager->expects($this->any())->method('isEnabled')->willReturn($encryptionEnabled);
|
|
|
|
|
|
|
|
$encryptionStorage = new \OC\Files\Storage\Wrapper\Encryption(
|
|
|
|
[
|
|
|
|
'storage' => $sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/mountPoint',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
|
|
|
$this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if ($rmdirResult === true && $isExcluded === false && $encryptionEnabled === true) {
|
|
|
|
$this->keyStore->expects($this->once())->method('deleteAllFileKeys')->with('/mountPoint' . $path);
|
|
|
|
} else {
|
|
|
|
$this->keyStore->expects($this->never())->method('deleteAllFileKeys');
|
|
|
|
}
|
|
|
|
|
|
|
|
$encryptionStorage->rmdir($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestRmdir() {
|
|
|
|
return array(
|
|
|
|
array('/file.txt', true, true, true),
|
|
|
|
array('/file.txt', false, true, true),
|
|
|
|
array('/file.txt', true, false, true),
|
|
|
|
array('/file.txt', false, false, true),
|
|
|
|
array('/file.txt', true, true, false),
|
|
|
|
array('/file.txt', false, true, false),
|
|
|
|
array('/file.txt', true, false, false),
|
|
|
|
array('/file.txt', false, false, false),
|
|
|
|
);
|
|
|
|
}
|
2015-05-21 15:07:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestCopyKeys
|
|
|
|
*
|
|
|
|
* @param boolean $excluded
|
|
|
|
* @param boolean $expected
|
|
|
|
*/
|
|
|
|
public function testCopyKeys($excluded, $expected) {
|
|
|
|
$this->util->expects($this->once())
|
|
|
|
->method('isExcluded')
|
|
|
|
->willReturn($excluded);
|
|
|
|
|
|
|
|
if ($excluded) {
|
|
|
|
$this->keyStore->expects($this->never())->method('copyKeys');
|
|
|
|
} else {
|
|
|
|
$this->keyStore->expects($this->once())->method('copyKeys')->willReturn(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($expected,
|
2015-06-03 13:03:02 +03:00
|
|
|
self::invokePrivate($this->instance, 'copyKeys', ['/source', '/target'])
|
2015-05-21 15:07:42 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestCopyKeys() {
|
|
|
|
return array(
|
|
|
|
array(true, false),
|
|
|
|
array(false, true),
|
|
|
|
);
|
|
|
|
}
|
2015-06-23 11:43:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestGetHeader
|
2015-06-26 12:26:40 +03:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param bool $strippedPathExists
|
|
|
|
* @param string $strippedPath
|
2015-06-23 11:43:28 +03:00
|
|
|
*/
|
2015-06-26 12:26:40 +03:00
|
|
|
public function testGetHeader($path, $strippedPathExists, $strippedPath) {
|
2015-06-23 11:43:28 +03:00
|
|
|
|
|
|
|
$sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$util = $this->getMockBuilder('\OC\Encryption\Util')
|
2016-03-31 00:20:37 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
new View(),
|
2016-10-28 10:07:40 +03:00
|
|
|
new Manager($this->config),
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->groupManager,
|
|
|
|
$this->config,
|
|
|
|
$this->arrayCache
|
|
|
|
]
|
|
|
|
)->getMock();
|
2015-06-23 11:43:28 +03:00
|
|
|
|
|
|
|
$instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'storage' => $sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2015-06-23 11:43:28 +03:00
|
|
|
]
|
|
|
|
)
|
2015-07-09 19:04:35 +03:00
|
|
|
->setMethods(['readFirstBlock', 'parseRawHeader'])
|
2015-06-23 11:43:28 +03:00
|
|
|
->getMock();
|
|
|
|
|
2015-07-09 19:04:35 +03:00
|
|
|
$instance->expects($this->once())->method(('parseRawHeader'))
|
|
|
|
->willReturn([Util::HEADER_ENCRYPTION_MODULE_KEY => 'OC_DEFAULT_MODULE']);
|
|
|
|
|
|
|
|
if ($strippedPathExists) {
|
|
|
|
$instance->expects($this->once())->method('readFirstBlock')
|
|
|
|
->with($strippedPath)->willReturn('');
|
|
|
|
} else {
|
|
|
|
$instance->expects($this->once())->method('readFirstBlock')
|
|
|
|
->with($path)->willReturn('');
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:43:28 +03:00
|
|
|
$util->expects($this->once())->method('stripPartialFileExtension')
|
|
|
|
->with($path)->willReturn($strippedPath);
|
2015-07-09 19:04:35 +03:00
|
|
|
$sourceStorage->expects($this->once())
|
2015-06-26 12:26:40 +03:00
|
|
|
->method('file_exists')
|
|
|
|
->with($strippedPath)
|
|
|
|
->willReturn($strippedPathExists);
|
2015-06-23 11:43:28 +03:00
|
|
|
|
|
|
|
$this->invokePrivate($instance, 'getHeader', [$path]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestGetHeader() {
|
|
|
|
return array(
|
2015-06-26 12:26:40 +03:00
|
|
|
array('/foo/bar.txt', false, '/foo/bar.txt'),
|
|
|
|
array('/foo/bar.txt.part', false, '/foo/bar.txt'),
|
|
|
|
array('/foo/bar.txt.ocTransferId7437493.part', false, '/foo/bar.txt'),
|
|
|
|
array('/foo/bar.txt.part', true, '/foo/bar.txt'),
|
|
|
|
array('/foo/bar.txt.ocTransferId7437493.part', true, '/foo/bar.txt'),
|
2015-06-23 11:43:28 +03:00
|
|
|
);
|
|
|
|
}
|
2015-07-09 19:04:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test if getHeader adds the default module correctly to the header for
|
|
|
|
* legacy files
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestGetHeaderAddLegacyModule
|
|
|
|
*/
|
|
|
|
public function testGetHeaderAddLegacyModule($header, $isEncrypted, $expected) {
|
|
|
|
|
|
|
|
$sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$util = $this->getMockBuilder('\OC\Encryption\Util')
|
2016-10-28 10:07:40 +03:00
|
|
|
->setConstructorArgs([new View(), new Manager($this->config), $this->groupManager, $this->config, $this->arrayCache])
|
2015-07-09 19:04:35 +03:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$cache->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->willReturnCallback(function($path) use ($isEncrypted) {return ['encrypted' => $isEncrypted, 'path' => $path];});
|
|
|
|
|
|
|
|
$instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'storage' => $sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2015-07-09 19:04:35 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['readFirstBlock', 'parseRawHeader', 'getCache'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$instance->expects($this->once())->method(('parseRawHeader'))->willReturn($header);
|
|
|
|
$instance->expects($this->any())->method('getCache')->willReturn($cache);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($instance, 'getHeader', ['test.txt']);
|
|
|
|
$this->assertSameSize($expected, $result);
|
|
|
|
foreach ($result as $key => $value) {
|
|
|
|
$this->assertArrayHasKey($key, $expected);
|
|
|
|
$this->assertSame($expected[$key], $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestGetHeaderAddLegacyModule() {
|
|
|
|
return [
|
|
|
|
[['cipher' => 'AES-128'], true, ['cipher' => 'AES-128', Util::HEADER_ENCRYPTION_MODULE_KEY => 'OC_DEFAULT_MODULE']],
|
|
|
|
[[], true, [Util::HEADER_ENCRYPTION_MODULE_KEY => 'OC_DEFAULT_MODULE']],
|
|
|
|
[[], false, []],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestParseRawHeader
|
|
|
|
*/
|
|
|
|
public function testParseRawHeader($rawHeader, $expected) {
|
|
|
|
$instance = new \OC\Files\Storage\Wrapper\Encryption(
|
|
|
|
[
|
|
|
|
'storage' => $this->sourceStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => '/',
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
|
2015-07-09 19:04:35 +03:00
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->invokePrivate($instance, 'parseRawHeader', [$rawHeader]);
|
|
|
|
$this->assertSameSize($expected, $result);
|
|
|
|
foreach ($result as $key => $value) {
|
|
|
|
$this->assertArrayHasKey($key, $expected);
|
|
|
|
$this->assertSame($expected[$key], $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestParseRawHeader() {
|
|
|
|
return [
|
|
|
|
[str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
|
|
|
|
, [Util::HEADER_ENCRYPTION_MODULE_KEY => '0']],
|
|
|
|
[str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
|
|
|
|
, ['custom_header' => 'foo', Util::HEADER_ENCRYPTION_MODULE_KEY => '0']],
|
|
|
|
[str_pad('HelloWorld', $this->headerSize, '-', STR_PAD_RIGHT), []],
|
|
|
|
['', []],
|
|
|
|
[str_pad('HBEGIN:oc_encryption_module:0', $this->headerSize, '-', STR_PAD_RIGHT)
|
|
|
|
, []],
|
|
|
|
[str_pad('oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
|
|
|
|
, []],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:01:44 +03:00
|
|
|
public function dataCopyBetweenStorage() {
|
|
|
|
return [
|
|
|
|
[true, true, true],
|
|
|
|
[true, false, false],
|
|
|
|
[false, true, false],
|
|
|
|
[false, false, false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-10 17:58:24 +03:00
|
|
|
public function testCopyBetweenStorageMinimumEncryptedVersion() {
|
|
|
|
$storage2 = $this->getMockBuilder('OCP\Files\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$sourceInternalPath = $targetInternalPath = 'file.txt';
|
|
|
|
$preserveMtime = $isRename = false;
|
|
|
|
|
|
|
|
$storage2->expects($this->any())
|
|
|
|
->method('fopen')
|
|
|
|
->willReturnCallback(function($path, $mode) {
|
|
|
|
$temp = \OC::$server->getTempManager();
|
|
|
|
return fopen($temp->getTemporaryFile(), $mode);
|
|
|
|
});
|
2016-09-07 21:01:13 +03:00
|
|
|
$cache = $this->createMock(ICache::class);
|
2016-03-10 17:58:24 +03:00
|
|
|
$cache->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($sourceInternalPath)
|
|
|
|
->willReturn(['encryptedVersion' => 0]);
|
|
|
|
$storage2->expects($this->once())
|
|
|
|
->method('getCache')
|
|
|
|
->willReturn($cache);
|
|
|
|
$this->encryptionManager->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
|
|
|
global $mockedMountPointEncryptionEnabled;
|
|
|
|
$mockedMountPointEncryptionEnabled = true;
|
|
|
|
|
|
|
|
$expectedCachePut = [
|
|
|
|
'encrypted' => true,
|
|
|
|
];
|
|
|
|
$expectedCachePut['encryptedVersion'] = 1;
|
|
|
|
|
|
|
|
$this->cache->expects($this->once())
|
|
|
|
->method('put')
|
|
|
|
->with($sourceInternalPath, $expectedCachePut);
|
|
|
|
|
|
|
|
$this->invokePrivate($this->instance, 'copyBetweenStorage', [$storage2, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename]);
|
|
|
|
|
|
|
|
$this->assertFalse(false);
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:01:44 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataCopyBetweenStorage
|
|
|
|
*
|
|
|
|
* @param bool $encryptionEnabled
|
|
|
|
* @param bool $mountPointEncryptionEnabled
|
|
|
|
* @param bool $expectedEncrypted
|
|
|
|
*/
|
|
|
|
public function testCopyBetweenStorage($encryptionEnabled, $mountPointEncryptionEnabled, $expectedEncrypted) {
|
|
|
|
$storage2 = $this->getMockBuilder('OCP\Files\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$sourceInternalPath = $targetInternalPath = 'file.txt';
|
|
|
|
$preserveMtime = $isRename = false;
|
|
|
|
|
|
|
|
$storage2->expects($this->any())
|
|
|
|
->method('fopen')
|
|
|
|
->willReturnCallback(function($path, $mode) {
|
|
|
|
$temp = \OC::$server->getTempManager();
|
|
|
|
return fopen($temp->getTemporaryFile(), $mode);
|
|
|
|
});
|
2016-03-02 22:37:13 +03:00
|
|
|
if($expectedEncrypted) {
|
2016-09-07 21:01:13 +03:00
|
|
|
$cache = $this->createMock(ICache::class);
|
2016-03-02 22:37:13 +03:00
|
|
|
$cache->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($sourceInternalPath)
|
|
|
|
->willReturn(['encryptedVersion' => 12345]);
|
|
|
|
$storage2->expects($this->once())
|
|
|
|
->method('getCache')
|
|
|
|
->willReturn($cache);
|
|
|
|
}
|
2015-07-23 18:01:44 +03:00
|
|
|
$this->encryptionManager->expects($this->any())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn($encryptionEnabled);
|
|
|
|
// FIXME can not overwrite the return after definition
|
|
|
|
// $this->mount->expects($this->at(0))
|
|
|
|
// ->method('getOption')
|
|
|
|
// ->with('encrypt', true)
|
|
|
|
// ->willReturn($mountPointEncryptionEnabled);
|
|
|
|
global $mockedMountPointEncryptionEnabled;
|
|
|
|
$mockedMountPointEncryptionEnabled = $mountPointEncryptionEnabled;
|
|
|
|
|
2016-03-02 22:37:13 +03:00
|
|
|
$expectedCachePut = [
|
|
|
|
'encrypted' => $expectedEncrypted,
|
|
|
|
];
|
|
|
|
if($expectedEncrypted === true) {
|
|
|
|
$expectedCachePut['encryptedVersion'] = 12345;
|
|
|
|
}
|
|
|
|
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->arrayCache->expects($this->never())->method('set');
|
|
|
|
|
2015-07-23 18:01:44 +03:00
|
|
|
$this->cache->expects($this->once())
|
|
|
|
->method('put')
|
2016-03-02 22:37:13 +03:00
|
|
|
->with($sourceInternalPath, $expectedCachePut);
|
2015-07-23 18:01:44 +03:00
|
|
|
|
|
|
|
$this->invokePrivate($this->instance, 'copyBetweenStorage', [$storage2, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename]);
|
|
|
|
|
|
|
|
$this->assertFalse(false);
|
|
|
|
}
|
2015-09-29 14:17:39 +03:00
|
|
|
|
2015-11-04 12:15:28 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestCopyBetweenStorageVersions
|
|
|
|
*
|
|
|
|
* @param string $sourceInternalPath
|
|
|
|
* @param string $targetInternalPath
|
|
|
|
* @param bool $copyResult
|
|
|
|
* @param bool $encrypted
|
|
|
|
*/
|
|
|
|
public function testCopyBetweenStorageVersions($sourceInternalPath, $targetInternalPath, $copyResult, $encrypted) {
|
|
|
|
|
|
|
|
$sourceStorage = $this->getMockBuilder('OCP\Files\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$targetStorage = $this->getMockBuilder('OCP\Files\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$mountPoint = '/mountPoint';
|
|
|
|
|
|
|
|
/** @var \OC\Files\Storage\Wrapper\Encryption |\PHPUnit_Framework_MockObject_MockObject $instance */
|
|
|
|
$instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'storage' => $targetStorage,
|
|
|
|
'root' => 'foo',
|
|
|
|
'mountPoint' => $mountPoint,
|
|
|
|
'mount' => $this->mount
|
|
|
|
],
|
|
|
|
$this->encryptionManager,
|
|
|
|
$this->util,
|
|
|
|
$this->logger,
|
|
|
|
$this->file,
|
|
|
|
null,
|
|
|
|
$this->keyStore,
|
|
|
|
$this->update,
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->mountManager,
|
|
|
|
$this->arrayCache
|
2015-11-04 12:15:28 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['updateUnencryptedSize', 'getCache'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$targetStorage->expects($this->once())->method('copyFromStorage')
|
|
|
|
->with($sourceStorage, $sourceInternalPath, $targetInternalPath)
|
|
|
|
->willReturn($copyResult);
|
|
|
|
|
2016-03-02 22:37:13 +03:00
|
|
|
$instance->expects($this->any())->method('getCache')
|
|
|
|
->willReturn($cache);
|
|
|
|
|
2016-03-31 00:20:37 +03:00
|
|
|
$this->arrayCache->expects($this->once())->method('set')
|
|
|
|
->with('encryption_copy_version_' . $sourceInternalPath, true);
|
|
|
|
|
2015-11-04 12:15:28 +03:00
|
|
|
if ($copyResult) {
|
|
|
|
$cache->expects($this->once())->method('get')
|
|
|
|
->with($sourceInternalPath)
|
|
|
|
->willReturn(['encrypted' => $encrypted, 'size' => 42]);
|
|
|
|
if ($encrypted) {
|
|
|
|
$instance->expects($this->once())->method('updateUnencryptedSize')
|
|
|
|
->with($mountPoint . $targetInternalPath, 42);
|
|
|
|
} else {
|
|
|
|
$instance->expects($this->never())->method('updateUnencryptedSize');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$instance->expects($this->never())->method('updateUnencryptedSize');
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->invokePrivate(
|
|
|
|
$instance,
|
|
|
|
'copyBetweenStorage',
|
|
|
|
[
|
|
|
|
$sourceStorage,
|
|
|
|
$sourceInternalPath,
|
|
|
|
$targetInternalPath,
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame($copyResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestCopyBetweenStorageVersions() {
|
|
|
|
return [
|
|
|
|
['/files/foo.txt', '/files_versions/foo.txt.768743', true, true],
|
|
|
|
['/files/foo.txt', '/files_versions/foo.txt.768743', true, false],
|
|
|
|
['/files/foo.txt', '/files_versions/foo.txt.768743', false, true],
|
|
|
|
['/files/foo.txt', '/files_versions/foo.txt.768743', false, false],
|
|
|
|
['/files_versions/foo.txt.6487634', '/files/foo.txt', true, true],
|
|
|
|
['/files_versions/foo.txt.6487634', '/files/foo.txt', true, false],
|
|
|
|
['/files_versions/foo.txt.6487634', '/files/foo.txt', false, true],
|
|
|
|
['/files_versions/foo.txt.6487634', '/files/foo.txt', false, false],
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-09-29 14:17:39 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestIsVersion
|
|
|
|
* @param string $path
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testIsVersion($path, $expected) {
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->invokePrivate($this->instance, 'isVersion', [$path])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestIsVersion() {
|
|
|
|
return [
|
|
|
|
['files_versions/foo', true],
|
|
|
|
['/files_versions/foo', true],
|
|
|
|
['//files_versions/foo', true],
|
|
|
|
['files/versions/foo', false],
|
|
|
|
['files/files_versions/foo', false],
|
|
|
|
['files_versions_test/foo', false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-03-30 14:23:10 +03:00
|
|
|
}
|