2015-01-14 22:39:23 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Test\Encryption;
|
|
|
|
|
|
|
|
use OC\Encryption\Util;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OC\Files\View;
|
2016-09-07 20:51:45 +03:00
|
|
|
use OCP\Encryption\IEncryptionModule;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IConfig;
|
2015-01-14 22:39:23 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class UtilTest extends TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* block size will always be 8192 for a PHP stream
|
|
|
|
* @see https://bugs.php.net/bug.php?id=21641
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $headerSize = 8192;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-01-14 22:39:23 +03:00
|
|
|
protected $view;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-01-14 22:39:23 +03:00
|
|
|
protected $userManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-04-15 14:19:17 +03:00
|
|
|
protected $groupManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-03-30 12:01:06 +03:00
|
|
|
private $config;
|
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
/** @var \OC\Encryption\Util */
|
|
|
|
private $util;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-01-14 22:39:23 +03:00
|
|
|
parent::setUp();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->view = $this->getMockBuilder(View::class)
|
2015-01-14 22:39:23 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->userManager = $this->getMockBuilder('OC\User\Manager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-03-30 12:01:06 +03:00
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
$this->groupManager = $this->getMockBuilder('OC\Group\Manager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)
|
2015-03-30 12:01:06 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
$this->util = new Util(
|
|
|
|
$this->view,
|
|
|
|
$this->userManager,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->config
|
|
|
|
);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providesHeadersForEncryptionModule
|
|
|
|
*/
|
|
|
|
public function testGetEncryptionModuleId($expected, $header) {
|
2015-04-15 14:19:17 +03:00
|
|
|
$id = $this->util->getEncryptionModuleId($header);
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->assertEquals($expected, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesHeadersForEncryptionModule() {
|
|
|
|
return [
|
|
|
|
['', []],
|
|
|
|
['', ['1']],
|
|
|
|
[2, ['oc_encryption_module' => 2]],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providesHeaders
|
|
|
|
*/
|
|
|
|
public function testCreateHeader($expected, $header, $moduleId) {
|
2016-09-07 20:51:45 +03:00
|
|
|
$em = $this->createMock(IEncryptionModule::class);
|
2015-01-14 22:39:23 +03:00
|
|
|
$em->expects($this->any())->method('getId')->willReturn($moduleId);
|
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
$result = $this->util->createHeader($header, $em);
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesHeaders() {
|
|
|
|
return [
|
|
|
|
[str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
|
|
|
|
, [], '0'],
|
|
|
|
[str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
|
|
|
|
, ['custom_header' => 'foo'], '0'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
public function testCreateHeaderFailed() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException::class);
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$header = ['header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo'];
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2016-09-07 20:51:45 +03:00
|
|
|
$em = $this->createMock(IEncryptionModule::class);
|
2015-01-14 22:39:23 +03:00
|
|
|
$em->expects($this->any())->method('getId')->willReturn('moduleId');
|
|
|
|
|
2015-04-15 14:19:17 +03:00
|
|
|
$this->util->createHeader($header, $em);
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 13:46:07 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider providePathsForTestIsExcluded
|
|
|
|
*/
|
2015-07-24 13:24:18 +03:00
|
|
|
public function testIsExcluded($path, $keyStorageRoot, $expected) {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('core', 'encryption_key_storage_root', '')
|
|
|
|
->willReturn($keyStorageRoot);
|
2015-03-27 13:46:07 +03:00
|
|
|
$this->userManager
|
|
|
|
->expects($this->any())
|
|
|
|
->method('userExists')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturnCallback([$this, 'isExcludedCallback']);
|
2015-03-27 13:46:07 +03:00
|
|
|
|
|
|
|
$this->assertSame($expected,
|
2015-04-15 14:19:17 +03:00
|
|
|
$this->util->isExcluded($path)
|
2015-03-27 13:46:07 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providePathsForTestIsExcluded() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['/files_encryption', '', true],
|
|
|
|
['files_encryption/foo.txt', '', true],
|
|
|
|
['test/foo.txt', '', false],
|
|
|
|
['/user1/files_encryption/foo.txt', '', true],
|
|
|
|
['/user1/files/foo.txt', '', false],
|
|
|
|
['/keyStorage/user1/files/foo.txt', 'keyStorage', true],
|
|
|
|
['/keyStorage/files_encryption', '/keyStorage', true],
|
|
|
|
['keyStorage/user1/files_encryption', '/keyStorage/', true],
|
2015-03-27 13:46:07 +03:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2015-03-27 13:46:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isExcludedCallback() {
|
|
|
|
$args = func_get_args();
|
|
|
|
if ($args[0] === 'user1') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestIsFile
|
|
|
|
*/
|
|
|
|
public function testIsFile($path, $expected) {
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->util->isFile($path)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestIsFile() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['/user/files/test.txt', true],
|
|
|
|
['/user/files', true],
|
|
|
|
['/user/files_versions/test.txt', false],
|
|
|
|
['/user/foo/files/test.txt', false],
|
|
|
|
['/files/foo/files/test.txt', false],
|
|
|
|
['/user', false],
|
|
|
|
['/user/test.txt', false],
|
|
|
|
];
|
2015-04-23 17:48:11 +03:00
|
|
|
}
|
|
|
|
|
2015-05-22 13:12:31 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestStripPartialFileExtension
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testStripPartialFileExtension($path, $expected) {
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->util->stripPartialFileExtension($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestStripPartialFileExtension() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
['/foo/test.txt', '/foo/test.txt'],
|
|
|
|
['/foo/test.txt.part', '/foo/test.txt'],
|
|
|
|
['/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'],
|
|
|
|
['/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'],
|
|
|
|
];
|
2015-05-22 13:12:31 +03:00
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|