diff options
Diffstat (limited to 'tests/lib/Encryption/Keys/StorageTest.php')
-rw-r--r-- | tests/lib/Encryption/Keys/StorageTest.php | 159 |
1 files changed, 55 insertions, 104 deletions
diff --git a/tests/lib/Encryption/Keys/StorageTest.php b/tests/lib/Encryption/Keys/StorageTest.php index d1c0257cc6e..333d8d8ce21 100644 --- a/tests/lib/Encryption/Keys/StorageTest.php +++ b/tests/lib/Encryption/Keys/StorageTest.php @@ -1,29 +1,15 @@ <?php /** - * ownCloud - * - * @copyright (C) 2015 ownCloud, Inc. - * - * @author Bjoern Schiessle <schiessle@owncloud.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU AFFERO GENERAL PUBLIC LICENSE for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Encryption\Keys; use OC\Encryption\Keys\Storage; +use OC\Encryption\Util; use OC\Files\View; use OCP\IConfig; use OCP\Security\ICrypto; @@ -51,8 +37,9 @@ class StorageTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->util = $this->getMockBuilder('OC\Encryption\Util') + $this->util = $this->getMockBuilder(Util::class) ->disableOriginalConstructor() + ->onlyMethods(array_diff(get_class_methods(Util::class), ['getFileKeyDir'])) ->getMock(); $this->view = $this->getMockBuilder(View::class) @@ -76,8 +63,8 @@ class StorageTest extends TestCase { $this->storage = new Storage($this->view, $this->util, $this->crypto, $this->config); } - public function testSetFileKey() { - $this->config->method('getSystemValue') + public function testSetFileKey(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); $this->util->expects($this->any()) @@ -102,8 +89,8 @@ class StorageTest extends TestCase { ); } - public function testSetFileOld() { - $this->config->method('getSystemValue') + public function testSetFileOld(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.0'); $this->util->expects($this->any()) @@ -128,7 +115,7 @@ class StorageTest extends TestCase { ); } - public function dataTestGetFileKey() { + public static function dataTestGetFileKey() { return [ ['/files/foo.txt', '/files/foo.txt', true, 'key'], ['/files/foo.txt.ocTransferId2111130212.part', '/files/foo.txt', true, 'key'], @@ -137,15 +124,15 @@ class StorageTest extends TestCase { } /** - * @dataProvider dataTestGetFileKey * * @param string $path * @param string $strippedPartialName * @param bool $originalKeyExists * @param string $expectedKeyContent */ - public function testGetFileKey($path, $strippedPartialName, $originalKeyExists, $expectedKeyContent) { - $this->config->method('getSystemValue') + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetFileKey')] + public function testGetFileKey($path, $strippedPartialName, $originalKeyExists, $expectedKeyContent): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); $this->util->expects($this->any()) @@ -171,13 +158,10 @@ class StorageTest extends TestCase { if (!$originalKeyExists) { $this->view->expects($this->exactly(2)) ->method('file_exists') - ->withConsecutive( - [$this->equalTo('/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey')], - [$this->equalTo('/user1/files_encryption/keys' . $path . '/encModule/fileKey')], - )->willReturnOnConsecutiveCalls( - $originalKeyExists, - true, - ); + ->willReturnMap([ + ['/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey', $originalKeyExists], + ['/user1/files_encryption/keys' . $path . '/encModule/fileKey', true], + ]); $this->view->expects($this->once()) ->method('file_get_contents') @@ -200,8 +184,8 @@ class StorageTest extends TestCase { ); } - public function testSetFileKeySystemWide() { - $this->config->method('getSystemValue') + public function testSetFileKeySystemWide(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); @@ -232,8 +216,8 @@ class StorageTest extends TestCase { ); } - public function testGetFileKeySystemWide() { - $this->config->method('getSystemValue') + public function testGetFileKeySystemWide(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); @@ -260,8 +244,8 @@ class StorageTest extends TestCase { ); } - public function testSetSystemUserKey() { - $this->config->method('getSystemValue') + public function testSetSystemUserKey(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); @@ -280,8 +264,8 @@ class StorageTest extends TestCase { ); } - public function testSetUserKey() { - $this->config->method('getSystemValue') + public function testSetUserKey(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); @@ -300,8 +284,8 @@ class StorageTest extends TestCase { ); } - public function testGetSystemUserKey() { - $this->config->method('getSystemValue') + public function testGetSystemUserKey(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); @@ -323,8 +307,8 @@ class StorageTest extends TestCase { ); } - public function testGetUserKey() { - $this->config->method('getSystemValue') + public function testGetUserKey(): void { + $this->config->method('getSystemValueString') ->with('version') ->willReturn('20.0.0.2'); @@ -346,7 +330,7 @@ class StorageTest extends TestCase { ); } - public function testDeleteUserKey() { + public function testDeleteUserKey(): void { $this->view->expects($this->once()) ->method('file_exists') ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey')) @@ -361,7 +345,7 @@ class StorageTest extends TestCase { ); } - public function testDeleteSystemUserKey() { + public function testDeleteSystemUserKey(): void { $this->view->expects($this->once()) ->method('file_exists') ->with($this->equalTo('/files_encryption/encModule/shareKey_56884')) @@ -376,7 +360,7 @@ class StorageTest extends TestCase { ); } - public function testDeleteFileKeySystemWide() { + public function testDeleteFileKeySystemWide(): void { $this->util->expects($this->any()) ->method('getUidAndFilename') ->willReturn(['user1', '/files/foo.txt']); @@ -400,7 +384,7 @@ class StorageTest extends TestCase { ); } - public function testDeleteFileKey() { + public function testDeleteFileKey(): void { $this->util->expects($this->any()) ->method('getUidAndFilename') ->willReturn(['user1', '/files/foo.txt']); @@ -424,10 +408,8 @@ class StorageTest extends TestCase { ); } - /** - * @dataProvider dataProviderCopyRename - */ - public function testRenameKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderCopyRename')] + public function testRenameKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget): void { $this->view->expects($this->any()) ->method('file_exists') ->willReturn(true); @@ -455,10 +437,8 @@ class StorageTest extends TestCase { $this->storage->renameKeys($source, $target); } - /** - * @dataProvider dataProviderCopyRename - */ - public function testCopyKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderCopyRename')] + public function testCopyKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget): void { $this->view->expects($this->any()) ->method('file_exists') ->willReturn(true); @@ -495,7 +475,7 @@ class StorageTest extends TestCase { return [$parts[1], '/' . implode('/', array_slice($parts, 2))]; } - public function dataProviderCopyRename() { + public static function dataProviderCopyRename() { return [ ['/user1/files/source.txt', '/user1/files/target.txt', false, false, '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'], @@ -526,14 +506,14 @@ class StorageTest extends TestCase { } /** - * @dataProvider dataTestGetPathToKeys * * @param string $path * @param boolean $systemWideMountPoint * @param string $storageRoot * @param string $expected */ - public function testGetPathToKeys($path, $systemWideMountPoint, $storageRoot, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetPathToKeys')] + public function testGetPathToKeys($path, $systemWideMountPoint, $storageRoot, $expected): void { $this->invokePrivate($this->storage, 'root_dir', [$storageRoot]); $this->util->expects($this->any()) @@ -548,7 +528,7 @@ class StorageTest extends TestCase { ); } - public function dataTestGetPathToKeys() { + public static function dataTestGetPathToKeys() { return [ ['/user1/files/source.txt', false, '', '/user1/files_encryption/keys/files/source.txt/'], ['/user1/files/source.txt', true, '', '/files_encryption/keys/files/source.txt/'], @@ -557,7 +537,7 @@ class StorageTest extends TestCase { ]; } - public function testKeySetPreparation() { + public function testKeySetPreparation(): void { $this->view->expects($this->any()) ->method('file_exists') ->willReturn(false); @@ -583,48 +563,15 @@ class StorageTest extends TestCase { $this->assertSame($expected, $args[0]); } - /** - * @dataProvider dataTestGetFileKeyDir - * - * @param bool $isSystemWideMountPoint - * @param string $storageRoot - * @param string $expected - */ - public function testGetFileKeyDir($isSystemWideMountPoint, $storageRoot, $expected) { - $path = '/user1/files/foo/bar.txt'; - $owner = 'user1'; - $relativePath = '/foo/bar.txt'; - - $this->invokePrivate($this->storage, 'root_dir', [$storageRoot]); - - $this->util->expects($this->once())->method('isSystemWideMountPoint') - ->willReturn($isSystemWideMountPoint); - $this->util->expects($this->once())->method('getUidAndFilename') - ->with($path)->willReturn([$owner, $relativePath]); - - $this->assertSame($expected, - $this->invokePrivate($this->storage, 'getFileKeyDir', ['OC_DEFAULT_MODULE', $path]) - ); - } - - public function dataTestGetFileKeyDir() { - 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/'], - ]; - } - /** - * @dataProvider dataTestBackupUserKeys * @param bool $createBackupDir */ - public function testBackupUserKeys($createBackupDir) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestBackupUserKeys')] + public function testBackupUserKeys($createBackupDir): void { $storage = $this->getMockBuilder('OC\Encryption\Keys\Storage') ->setConstructorArgs([$this->view, $this->util, $this->crypto, $this->config]) - ->setMethods(['getTimestamp']) + ->onlyMethods(['getTimestamp']) ->getMock(); $storage->expects($this->any())->method('getTimestamp')->willReturn('1234567'); @@ -633,11 +580,15 @@ class StorageTest extends TestCase { ->with('user1/files_encryption/backup')->willReturn(!$createBackupDir); if ($createBackupDir) { + $calls = [ + 'user1/files_encryption/backup', + 'user1/files_encryption/backup/test.encryptionModule.1234567', + ]; $this->view->expects($this->exactly(2))->method('mkdir') - ->withConsecutive( - ['user1/files_encryption/backup'], - ['user1/files_encryption/backup/test.encryptionModule.1234567'], - ); + ->willReturnCallback(function ($path) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $path); + }); } else { $this->view->expects($this->once())->method('mkdir') ->with('user1/files_encryption/backup/test.encryptionModule.1234567'); @@ -652,7 +603,7 @@ class StorageTest extends TestCase { $this->assertTrue($storage->backupUserKeys('encryptionModule', 'test', 'user1')); } - public function dataTestBackupUserKeys() { + public static function dataTestBackupUserKeys() { return [ [true], [false] ]; |