diff options
Diffstat (limited to 'tests/lib/Files/Storage/Wrapper/EncryptionTest.php')
-rw-r--r-- | tests/lib/Files/Storage/Wrapper/EncryptionTest.php | 112 |
1 files changed, 48 insertions, 64 deletions
diff --git a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php index bb3df36dec2..3e643714300 100644 --- a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php +++ b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,10 +9,8 @@ namespace Test\Files\Storage\Wrapper; use Exception; -use OC; use OC\Encryption\Exceptions\ModuleDoesNotExistsException; use OC\Encryption\File; -use OC\Encryption\Update; use OC\Encryption\Util; use OC\Files\Cache\Cache; use OC\Files\Cache\CacheEntry; @@ -29,6 +28,8 @@ use OCP\Files\Cache\ICache; use OCP\Files\Mount\IMountPoint; use OCP\ICacheFactory; use OCP\IConfig; +use OCP\ITempManager; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\Files\Storage\Storage; @@ -46,7 +47,6 @@ class EncryptionTest extends Storage { private Util&MockObject $util; private \OC\Encryption\Manager&MockObject $encryptionManager; private IEncryptionModule&MockObject $encryptionModule; - private Update&MockObject $update; private Cache&MockObject $cache; private LoggerInterface&MockObject $logger; private File&MockObject $file; @@ -62,9 +62,9 @@ class EncryptionTest extends Storage { parent::setUp(); $mockModule = $this->buildMockModule(); - $this->encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager') + $this->encryptionManager = $this->getMockBuilder(\OC\Encryption\Manager::class) ->disableOriginalConstructor() - ->setMethods(['getEncryptionModule', 'isEnabled']) + ->onlyMethods(['getEncryptionModule', 'isEnabled']) ->getMock(); $this->encryptionManager->expects($this->any()) ->method('getEncryptionModule') @@ -78,8 +78,8 @@ class EncryptionTest extends Storage { ->disableOriginalConstructor() ->getMock(); - $this->util = $this->getMockBuilder('\OC\Encryption\Util') - ->setMethods(['getUidAndFilename', 'isFile', 'isExcluded', 'stripPartialFileExtension']) + $this->util = $this->getMockBuilder(Util::class) + ->onlyMethods(['getUidAndFilename', 'isFile', 'isExcluded', 'stripPartialFileExtension']) ->setConstructorArgs([new View(), new Manager( $this->config, $this->createMock(ICacheFactory::class), @@ -98,9 +98,9 @@ class EncryptionTest extends Storage { return $path; }); - $this->file = $this->getMockBuilder('\OC\Encryption\File') + $this->file = $this->getMockBuilder(File::class) ->disableOriginalConstructor() - ->setMethods(['getAccessList']) + ->onlyMethods(['getAccessList']) ->getMock(); $this->file->expects($this->any())->method('getAccessList')->willReturn([]); @@ -108,15 +108,11 @@ class EncryptionTest extends Storage { $this->sourceStorage = new Temporary([]); - $this->keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage') - ->disableOriginalConstructor()->getMock(); + $this->keyStore = $this->createMock(\OC\Encryption\Keys\Storage::class); - $this->update = $this->getMockBuilder('\OC\Encryption\Update') - ->disableOriginalConstructor()->getMock(); - - $this->mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint') + $this->mount = $this->getMockBuilder(MountPoint::class) ->disableOriginalConstructor() - ->setMethods(['getOption']) + ->onlyMethods(['getOption']) ->getMock(); $this->mount->expects($this->any())->method('getOption')->willReturnCallback(function ($option, $default) { if ($option === 'encrypt' && $default === true) { @@ -155,12 +151,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache ] ) - ->setMethods(['getMetaData', 'getCache', 'getEncryptionModule']) + ->onlyMethods(['getMetaData', 'getCache', 'getEncryptionModule']) ->getMock(); $this->instance->expects($this->any()) @@ -181,7 +176,7 @@ class EncryptionTest extends Storage { protected function buildMockModule(): IEncryptionModule&MockObject { $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor() - ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser', 'needDetailedAccessList']) + ->onlyMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser', 'needDetailedAccessList']) ->getMock(); $this->encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE'); @@ -199,7 +194,6 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestGetMetaData * * @param string $path * @param array $metaData @@ -208,6 +202,7 @@ class EncryptionTest extends Storage { * @param int $storedUnencryptedSize * @param array $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetMetaData')] public function testGetMetaData($path, $metaData, $encrypted, $unencryptedSizeSet, $storedUnencryptedSize, $expected): void { $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); @@ -237,12 +232,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache, ] ) - ->setMethods(['getCache', 'verifyUnencryptedSize']) + ->onlyMethods(['getCache', 'verifyUnencryptedSize']) ->getMock(); if ($unencryptedSizeSet) { @@ -285,7 +279,7 @@ class EncryptionTest extends Storage { } } - public function dataTestGetMetaData() { + public static function dataTestGetMetaData(): array { return [ ['/test.txt', ['size' => 42, 'encrypted' => 2, 'encryptedVersion' => 2, 'fileid' => 1], true, true, 12, ['size' => 12, 'encrypted' => true, 'encryptedVersion' => 2]], ['/test.txt', null, true, true, 12, null], @@ -316,12 +310,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache, ] ) - ->setMethods(['getCache', 'verifyUnencryptedSize']) + ->onlyMethods(['getCache', 'verifyUnencryptedSize']) ->getMock(); $this->instance->expects($this->any())->method('getCache')->willReturn($cache); @@ -335,13 +328,13 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestVerifyUnencryptedSize * * @param int $encryptedSize * @param int $unencryptedSize * @param bool $failure * @param int $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestVerifyUnencryptedSize')] public function testVerifyUnencryptedSize($encryptedSize, $unencryptedSize, $failure, $expected): void { $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); @@ -361,12 +354,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache, ] ) - ->setMethods(['fixUnencryptedSize']) + ->onlyMethods(['fixUnencryptedSize']) ->getMock(); $sourceStorage->expects($this->once())->method('filesize')->willReturn($encryptedSize); @@ -389,7 +381,7 @@ class EncryptionTest extends Storage { ); } - public function dataTestVerifyUnencryptedSize() { + public static function dataTestVerifyUnencryptedSize(): array { return [ [120, 80, false, 80], [120, 120, false, 80], @@ -399,13 +391,13 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestCopyAndRename * * @param string $source * @param string $target * @param $encryptionEnabled * @param boolean $renameKeysReturn */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestCopyAndRename')] public function testRename($source, $target, $encryptionEnabled, @@ -444,7 +436,7 @@ class EncryptionTest extends Storage { * * @return array */ - public function dataTestCopyAndRename() { + public static function dataTestCopyAndRename(): array { return [ ['source', 'target', true, false, false], ['source', 'target', true, true, false], @@ -461,13 +453,13 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestRmdir * * @param string $path * @param boolean $rmdirResult * @param boolean $isExcluded * @param boolean $encryptionEnabled */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRmdir')] public function testRmdir($path, $rmdirResult, $isExcluded, $encryptionEnabled): void { $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); @@ -491,7 +483,6 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache, ); @@ -506,7 +497,7 @@ class EncryptionTest extends Storage { $encryptionStorage->rmdir($path); } - public function dataTestRmdir() { + public static function dataTestRmdir(): array { return [ ['/file.txt', true, true, true], ['/file.txt', false, true, true], @@ -520,11 +511,11 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestCopyKeys * * @param boolean $excluded * @param boolean $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestCopyKeys')] public function testCopyKeys($excluded, $expected): void { $this->util->expects($this->once()) ->method('isExcluded') @@ -541,7 +532,7 @@ class EncryptionTest extends Storage { ); } - public function dataTestCopyKeys() { + public static function dataTestCopyKeys(): array { return [ [true, false], [false, true], @@ -549,12 +540,12 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestGetHeader * * @param string $path * @param bool $strippedPathExists * @param string $strippedPath */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetHeader')] public function testGetHeader($path, $strippedPathExists, $strippedPath): void { $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); @@ -598,12 +589,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache, ] ) - ->setMethods(['getCache', 'readFirstBlock']) + ->onlyMethods(['getCache', 'readFirstBlock']) ->getMock(); $instance->method('getCache')->willReturn($cache); @@ -629,7 +619,7 @@ class EncryptionTest extends Storage { $this->invokePrivate($instance, 'getHeader', [$path]); } - public function dataTestGetHeader() { + public static function dataTestGetHeader(): array { return [ ['/foo/bar.txt', false, '/foo/bar.txt'], ['/foo/bar.txt.part', false, '/foo/bar.txt'], @@ -642,11 +632,10 @@ class EncryptionTest extends Storage { /** * test if getHeader adds the default module correctly to the header for * legacy files - * - * @dataProvider dataTestGetHeaderAddLegacyModule */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetHeaderAddLegacyModule')] public function testGetHeaderAddLegacyModule($header, $isEncrypted, $strippedPathExists, $expected): void { - $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') + $sourceStorage = $this->getMockBuilder(\OC\Files\Storage\Storage::class) ->disableOriginalConstructor()->getMock(); $sourceStorage->expects($this->once()) @@ -654,7 +643,7 @@ class EncryptionTest extends Storage { ->with('test.txt') ->willReturn($strippedPathExists); - $util = $this->getMockBuilder('\OC\Encryption\Util') + $util = $this->getMockBuilder(Util::class) ->onlyMethods(['stripPartialFileExtension', 'parseRawHeader']) ->setConstructorArgs([new View(), new Manager( $this->config, @@ -669,8 +658,7 @@ class EncryptionTest extends Storage { return $path; }); - $cache = $this->getMockBuilder('\OC\Files\Cache\Cache') - ->disableOriginalConstructor()->getMock(); + $cache = $this->createMock(Cache::class); $cache->expects($this->any()) ->method('get') ->willReturnCallback(function ($path) use ($isEncrypted) { @@ -692,12 +680,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache, ] ) - ->setMethods(['readFirstBlock', 'getCache']) + ->onlyMethods(['readFirstBlock', 'getCache']) ->getMock(); $instance->method('readFirstBlock')->willReturn(''); @@ -713,7 +700,7 @@ class EncryptionTest extends Storage { } } - public function dataTestGetHeaderAddLegacyModule() { + public static function dataTestGetHeaderAddLegacyModule(): array { return [ [['cipher' => 'AES-128'], true, true, ['cipher' => 'AES-128', Util::HEADER_ENCRYPTION_MODULE_KEY => 'OC_DEFAULT_MODULE']], [[], true, false, []], @@ -722,7 +709,7 @@ class EncryptionTest extends Storage { ]; } - public function dataCopyBetweenStorage() { + public static function dataCopyBetweenStorage(): array { return [ [true, true, true], [true, false, false], @@ -740,7 +727,7 @@ class EncryptionTest extends Storage { $storage2->expects($this->any()) ->method('fopen') ->willReturnCallback(function ($path, $mode) { - $temp = OC::$server->getTempManager(); + $temp = Server::get(ITempManager::class); return fopen($temp->getTemporaryFile(), $mode); }); $storage2->method('getId') @@ -774,12 +761,12 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataCopyBetweenStorage * * @param bool $encryptionEnabled * @param bool $mountPointEncryptionEnabled * @param bool $expectedEncrypted */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataCopyBetweenStorage')] public function testCopyBetweenStorage($encryptionEnabled, $mountPointEncryptionEnabled, $expectedEncrypted): void { $storage2 = $this->createMock(\OC\Files\Storage\Storage::class); @@ -789,7 +776,7 @@ class EncryptionTest extends Storage { $storage2->expects($this->any()) ->method('fopen') ->willReturnCallback(function ($path, $mode) { - $temp = OC::$server->getTempManager(); + $temp = Server::get(ITempManager::class); return fopen($temp->getTemporaryFile(), $mode); }); $storage2->method('getId') @@ -834,13 +821,13 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestCopyBetweenStorageVersions * * @param string $sourceInternalPath * @param string $targetInternalPath * @param bool $copyResult * @param bool $encrypted */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestCopyBetweenStorageVersions')] public function testCopyBetweenStorageVersions($sourceInternalPath, $targetInternalPath, $copyResult, $encrypted): void { $sourceStorage = $this->createMock(\OC\Files\Storage\Storage::class); @@ -867,12 +854,11 @@ class EncryptionTest extends Storage { $this->file, null, $this->keyStore, - $this->update, $this->mountManager, $this->arrayCache ] ) - ->setMethods(['updateUnencryptedSize', 'getCache']) + ->onlyMethods(['updateUnencryptedSize', 'getCache']) ->getMock(); $targetStorage->expects($this->once())->method('copyFromStorage') @@ -914,7 +900,7 @@ class EncryptionTest extends Storage { $this->assertSame($copyResult, $result); } - public function dataTestCopyBetweenStorageVersions() { + public static function dataTestCopyBetweenStorageVersions(): array { return [ ['/files/foo.txt', '/files_versions/foo.txt.768743', true, true], ['/files/foo.txt', '/files_versions/foo.txt.768743', true, false], @@ -929,17 +915,17 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestIsVersion * @param string $path * @param bool $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsVersion')] public function testIsVersion($path, $expected): void { $this->assertSame($expected, $this->invokePrivate($this->instance, 'isVersion', [$path]) ); } - public function dataTestIsVersion() { + public static function dataTestIsVersion(): array { return [ ['files_versions/foo', true], ['/files_versions/foo', true], @@ -951,13 +937,13 @@ class EncryptionTest extends Storage { } /** - * @dataProvider dataTestShouldEncrypt * * @param bool $encryptMountPoint * @param mixed $encryptionModule * @param bool $encryptionModuleShouldEncrypt * @param bool $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldEncrypt')] public function testShouldEncrypt( $encryptMountPoint, $encryptionModule, @@ -968,7 +954,6 @@ class EncryptionTest extends Storage { $util = $this->createMock(Util::class); $fileHelper = $this->createMock(IFile::class); $keyStorage = $this->createMock(IStorage::class); - $update = $this->createMock(Update::class); $mountManager = $this->createMock(\OC\Files\Mount\Manager::class); $mount = $this->createMock(IMountPoint::class); $arrayCache = $this->createMock(ArrayCache::class); @@ -986,12 +971,11 @@ class EncryptionTest extends Storage { $fileHelper, null, $keyStorage, - $update, $mountManager, $arrayCache ] ) - ->setMethods(['getFullPath', 'getEncryptionModule']) + ->onlyMethods(['getFullPath', 'getEncryptionModule']) ->getMock(); if ($encryptionModule === true) { @@ -1033,7 +1017,7 @@ class EncryptionTest extends Storage { $this->assertSame($expected, $result); } - public function dataTestShouldEncrypt() { + public static function dataTestShouldEncrypt(): array { return [ [false, false, false, false], [true, false, false, false], |