aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Encryption/UtilTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Encryption/UtilTest.php')
-rw-r--r--tests/lib/Encryption/UtilTest.php151
1 files changed, 104 insertions, 47 deletions
diff --git a/tests/lib/Encryption/UtilTest.php b/tests/lib/Encryption/UtilTest.php
index 84d81dd1cbb..d1fefee872a 100644
--- a/tests/lib/Encryption/UtilTest.php
+++ b/tests/lib/Encryption/UtilTest.php
@@ -1,36 +1,41 @@
<?php
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
namespace Test\Encryption;
+use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
use OC\Encryption\Util;
use OC\Files\View;
use OCP\Encryption\IEncryptionModule;
use OCP\IConfig;
+use OCP\IGroupManager;
+use OCP\IUserManager;
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;
+ protected static int $headerSize = 8192;
/** @var \PHPUnit\Framework\MockObject\MockObject */
protected $view;
- /** @var \PHPUnit\Framework\MockObject\MockObject */
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
protected $userManager;
- /** @var \PHPUnit\Framework\MockObject\MockObject */
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
protected $groupManager;
- /** @var \PHPUnit\Framework\MockObject\MockObject */
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
private $config;
-
- /** @var \OC\Encryption\Util */
- private $util;
+ private Util $util;
protected function setUp(): void {
parent::setUp();
@@ -38,17 +43,9 @@ class UtilTest extends TestCase {
->disableOriginalConstructor()
->getMock();
- $this->userManager = $this->getMockBuilder('OC\User\Manager')
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->groupManager = $this->getMockBuilder('OC\Group\Manager')
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->config = $this->getMockBuilder(IConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->groupManager = $this->createMock(IGroupManager::class);
+ $this->config = $this->createMock(IConfig::class);
$this->util = new Util(
$this->view,
@@ -58,15 +55,13 @@ class UtilTest extends TestCase {
);
}
- /**
- * @dataProvider providesHeadersForEncryptionModule
- */
- public function testGetEncryptionModuleId($expected, $header) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('providesHeadersForEncryptionModule')]
+ public function testGetEncryptionModuleId($expected, $header): void {
$id = $this->util->getEncryptionModuleId($header);
$this->assertEquals($expected, $id);
}
- public function providesHeadersForEncryptionModule() {
+ public static function providesHeadersForEncryptionModule(): array {
return [
['', []],
['', ['1']],
@@ -74,10 +69,8 @@ class UtilTest extends TestCase {
];
}
- /**
- * @dataProvider providesHeaders
- */
- public function testCreateHeader($expected, $header, $moduleId) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('providesHeaders')]
+ public function testCreateHeader($expected, $header, $moduleId): void {
$em = $this->createMock(IEncryptionModule::class);
$em->expects($this->any())->method('getId')->willReturn($moduleId);
@@ -85,18 +78,18 @@ class UtilTest extends TestCase {
$this->assertEquals($expected, $result);
}
- public function providesHeaders() {
+ public static function providesHeaders(): array {
return [
- [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
+ [str_pad('HBEGIN:oc_encryption_module:0:HEND', self::$headerSize, '-', STR_PAD_RIGHT)
, [], '0'],
- [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
+ [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', self::$headerSize, '-', STR_PAD_RIGHT)
, ['custom_header' => 'foo'], '0'],
];
}
- public function testCreateHeaderFailed() {
- $this->expectException(\OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException::class);
+ public function testCreateHeaderFailed(): void {
+ $this->expectException(EncryptionHeaderKeyExistsException::class);
$header = ['header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo'];
@@ -107,10 +100,8 @@ class UtilTest extends TestCase {
$this->util->createHeader($header, $em);
}
- /**
- * @dataProvider providePathsForTestIsExcluded
- */
- public function testIsExcluded($path, $keyStorageRoot, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('providePathsForTestIsExcluded')]
+ public function testIsExcluded($path, $keyStorageRoot, $expected): void {
$this->config->expects($this->once())
->method('getAppValue')
->with('core', 'encryption_key_storage_root', '')
@@ -125,7 +116,7 @@ class UtilTest extends TestCase {
);
}
- public function providePathsForTestIsExcluded() {
+ public static function providePathsForTestIsExcluded(): array {
return [
['/files_encryption', '', true],
['files_encryption/foo.txt', '', true],
@@ -148,16 +139,14 @@ class UtilTest extends TestCase {
return false;
}
- /**
- * @dataProvider dataTestIsFile
- */
- public function testIsFile($path, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsFile')]
+ public function testIsFile($path, $expected): void {
$this->assertSame($expected,
$this->util->isFile($path)
);
}
- public function dataTestIsFile() {
+ public static function dataTestIsFile(): array {
return [
['/user/files/test.txt', true],
['/user/files', true],
@@ -170,17 +159,17 @@ class UtilTest extends TestCase {
}
/**
- * @dataProvider dataTestStripPartialFileExtension
*
* @param string $path
* @param string $expected
*/
- public function testStripPartialFileExtension($path, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestStripPartialFileExtension')]
+ public function testStripPartialFileExtension($path, $expected): void {
$this->assertSame($expected,
$this->util->stripPartialFileExtension($path));
}
- public function dataTestStripPartialFileExtension() {
+ public static function dataTestStripPartialFileExtension(): array {
return [
['/foo/test.txt', '/foo/test.txt'],
['/foo/test.txt.part', '/foo/test.txt'],
@@ -188,4 +177,72 @@ class UtilTest extends TestCase {
['/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'],
];
}
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestParseRawHeader')]
+ public function testParseRawHeader($rawHeader, $expected): void {
+ $result = $this->util->parseRawHeader($rawHeader);
+ $this->assertSameSize($expected, $result);
+ foreach ($result as $key => $value) {
+ $this->assertArrayHasKey($key, $expected);
+ $this->assertSame($expected[$key], $value);
+ }
+ }
+
+ public static function dataTestParseRawHeader(): array {
+ return [
+ [str_pad('HBEGIN:oc_encryption_module:0:HEND', self::$headerSize, '-', STR_PAD_RIGHT)
+ , [Util::HEADER_ENCRYPTION_MODULE_KEY => '0']],
+ [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', self::$headerSize, '-', STR_PAD_RIGHT)
+ , ['custom_header' => 'foo', Util::HEADER_ENCRYPTION_MODULE_KEY => '0']],
+ [str_pad('HelloWorld', self::$headerSize, '-', STR_PAD_RIGHT), []],
+ ['', []],
+ [str_pad('HBEGIN:oc_encryption_module:0', self::$headerSize, '-', STR_PAD_RIGHT)
+ , []],
+ [str_pad('oc_encryption_module:0:HEND', self::$headerSize, '-', STR_PAD_RIGHT)
+ , []],
+ ];
+ }
+
+ /**
+ *
+ * @param bool $isSystemWideMountPoint
+ * @param string $storageRoot
+ * @param string $expected
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetFileKeyDir')]
+ public function testGetFileKeyDir($isSystemWideMountPoint, $storageRoot, $expected): void {
+ $path = '/user1/files/foo/bar.txt';
+ $owner = 'user1';
+ $relativePath = '/foo/bar.txt';
+
+ $util = $this->getMockBuilder(Util::class)
+ ->onlyMethods(['isSystemWideMountPoint', 'getUidAndFilename', 'getKeyStorageRoot'])
+ ->setConstructorArgs([
+ $this->view,
+ $this->userManager,
+ $this->groupManager,
+ $this->config
+ ])
+ ->getMock();
+
+ $util->expects($this->once())->method('getKeyStorageRoot')
+ ->willReturn($storageRoot);
+ $util->expects($this->once())->method('isSystemWideMountPoint')
+ ->willReturn($isSystemWideMountPoint);
+ $util->expects($this->once())->method('getUidAndFilename')
+ ->with($path)->willReturn([$owner, $relativePath]);
+
+ $this->assertSame($expected,
+ $util->getFileKeyDir('OC_DEFAULT_MODULE', $path)
+ );
+ }
+
+ public static function dataTestGetFileKeyDir(): array {
+ return [
+ [false, '', '/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
+ [true, '', '/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
+ [false, 'newStorageRoot', '/newStorageRoot/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
+ [true, 'newStorageRoot', '/newStorageRoot/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
+ ];
+ }
}