diff options
Diffstat (limited to 'tests/lib/Encryption')
-rw-r--r-- | tests/lib/Encryption/EncryptionWrapperTest.php | 11 | ||||
-rw-r--r-- | tests/lib/Encryption/UpdateTest.php | 195 | ||||
-rw-r--r-- | tests/lib/Encryption/UtilTest.php | 30 |
3 files changed, 105 insertions, 131 deletions
diff --git a/tests/lib/Encryption/EncryptionWrapperTest.php b/tests/lib/Encryption/EncryptionWrapperTest.php index 1ac7342a3d8..342a7214e44 100644 --- a/tests/lib/Encryption/EncryptionWrapperTest.php +++ b/tests/lib/Encryption/EncryptionWrapperTest.php @@ -9,7 +9,10 @@ namespace Test\Encryption; use OC\Encryption\EncryptionWrapper; use OC\Encryption\Manager; +use OC\Files\Storage\Wrapper\Encryption; use OC\Memcache\ArrayCache; +use OCA\Files_Trashbin\Storage; +use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IDisableEncryptionStorage; use OCP\Files\Storage\IStorage; use Psr\Log\LoggerInterface; @@ -55,7 +58,7 @@ class EncryptionWrapperTest extends TestCase { ]); } - $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint') + $mount = $this->getMockBuilder(IMountPoint::class) ->disableOriginalConstructor() ->getMock(); @@ -63,16 +66,16 @@ class EncryptionWrapperTest extends TestCase { $this->assertEquals( $expectedWrapped, - $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'), + $returnedStorage->instanceOfStorage(Encryption::class), 'Asserted that the storage is (not) wrapped with encryption' ); } - public function provideWrapStorage() { + public static function provideWrapStorage(): array { return [ // Wrap when not wrapped or not wrapped with storage [true, []], - [true, ['OCA\Files_Trashbin\Storage']], + [true, [Storage::class]], // Do not wrap shared storages [false, [IDisableEncryptionStorage::class]], diff --git a/tests/lib/Encryption/UpdateTest.php b/tests/lib/Encryption/UpdateTest.php index 2627e18601d..4f3de2b495d 100644 --- a/tests/lib/Encryption/UpdateTest.php +++ b/tests/lib/Encryption/UpdateTest.php @@ -1,4 +1,7 @@ <?php + +declare(strict_types=1); + /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -10,61 +13,67 @@ namespace Test\Encryption; use OC\Encryption\File; use OC\Encryption\Update; use OC\Encryption\Util; -use OC\Files\Mount\Manager; use OC\Files\View; use OCP\Encryption\IEncryptionModule; +use OCP\Files\File as OCPFile; +use OCP\Files\Folder; +use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; class UpdateTest extends TestCase { - /** @var \OC\Encryption\Update */ - private $update; - - /** @var string */ - private $uid; - - /** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */ - private $view; - - /** @var Util | \PHPUnit\Framework\MockObject\MockObject */ - private $util; - - /** @var \OC\Files\Mount\Manager | \PHPUnit\Framework\MockObject\MockObject */ - private $mountManager; - - /** @var \OC\Encryption\Manager | \PHPUnit\Framework\MockObject\MockObject */ - private $encryptionManager; - - /** @var \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */ - private $encryptionModule; - - /** @var \OC\Encryption\File | \PHPUnit\Framework\MockObject\MockObject */ - private $fileHelper; - - /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */ - private $logger; + private string $uid; + private View&MockObject $view; + private Util&MockObject $util; + private \OC\Encryption\Manager&MockObject $encryptionManager; + private IEncryptionModule&MockObject $encryptionModule; + private File&MockObject $fileHelper; + private LoggerInterface&MockObject $logger; protected function setUp(): void { parent::setUp(); $this->view = $this->createMock(View::class); $this->util = $this->createMock(Util::class); - $this->mountManager = $this->createMock(Manager::class); $this->encryptionManager = $this->createMock(\OC\Encryption\Manager::class); $this->fileHelper = $this->createMock(File::class); $this->encryptionModule = $this->createMock(IEncryptionModule::class); $this->logger = $this->createMock(LoggerInterface::class); $this->uid = 'testUser1'; + } - $this->update = new Update( - $this->view, - $this->util, - $this->mountManager, - $this->encryptionManager, - $this->fileHelper, - $this->logger, - $this->uid); + private function getUserMock(string $uid): IUser&MockObject { + $user = $this->createMock(IUser::class); + $user->expects(self::any()) + ->method('getUID') + ->willReturn($uid); + return $user; + } + + private function getFileMock(string $path, string $owner): OCPFile&MockObject { + $node = $this->createMock(OCPFile::class); + $node->expects(self::atLeastOnce()) + ->method('getPath') + ->willReturn($path); + $node->expects(self::any()) + ->method('getOwner') + ->willReturn($this->getUserMock($owner)); + + return $node; + } + + private function getFolderMock(string $path, string $owner): Folder&MockObject { + $node = $this->createMock(Folder::class); + $node->expects(self::atLeastOnce()) + ->method('getPath') + ->willReturn($path); + $node->expects(self::any()) + ->method('getOwner') + ->willReturn($this->getUserMock($owner)); + + return $node; } /** @@ -76,18 +85,21 @@ class UpdateTest extends TestCase { * @param integer $numberOfFiles */ public function testUpdate($path, $isDir, $allFiles, $numberOfFiles): void { + $updateMock = $this->getUpdateMock(['getOwnerPath']); + $updateMock->expects($this->once())->method('getOwnerPath') + ->willReturnCallback(fn (OCPFile|Folder $node) => '/user/' . $node->getPath()); + $this->encryptionManager->expects($this->once()) ->method('getEncryptionModule') ->willReturn($this->encryptionModule); - $this->view->expects($this->once()) - ->method('is_dir') - ->willReturn($isDir); - if ($isDir) { $this->util->expects($this->once()) ->method('getAllFiles') ->willReturn($allFiles); + $node = $this->getFolderMock($path, 'user'); + } else { + $node = $this->getFileMock($path, 'user'); } $this->fileHelper->expects($this->exactly($numberOfFiles)) @@ -98,15 +110,10 @@ class UpdateTest extends TestCase { ->method('update') ->willReturn(true); - $this->update->update($path); + $updateMock->update($node); } - /** - * data provider for testUpdate() - * - * @return array - */ - public function dataTestUpdate() { + public static function dataTestUpdate(): array { return [ ['/user/files/foo', true, ['/user/files/foo/file1.txt', '/user/files/foo/file1.txt'], 2], ['/user/files/test.txt', false, [], 1], @@ -118,102 +125,66 @@ class UpdateTest extends TestCase { * * @param string $source * @param string $target - * @param boolean $encryptionEnabled */ - public function testPostRename($source, $target, $encryptionEnabled): void { - $updateMock = $this->getUpdateMock(['update', 'getOwnerPath']); + public function testPostRename($source, $target): void { + $updateMock = $this->getUpdateMock(['update','getOwnerPath']); - $this->encryptionManager->expects($this->once()) - ->method('isEnabled') - ->willReturn($encryptionEnabled); + $sourceNode = $this->getFileMock($source, 'user'); + $targetNode = $this->getFileMock($target, 'user'); - if (dirname($source) === dirname($target) || $encryptionEnabled === false) { + if (dirname($source) === dirname($target)) { $updateMock->expects($this->never())->method('getOwnerPath'); $updateMock->expects($this->never())->method('update'); } else { - $updateMock->expects($this->once()) - ->method('getOwnerPath') - ->willReturnCallback(function ($path) use ($target) { - $this->assertSame( - $target, - $path, - 'update needs to be executed for the target destination'); - return ['owner', $path]; - }); - $updateMock->expects($this->once())->method('update'); + $updateMock->expects($this->once())->method('update') + ->willReturnCallback(fn (OCPFile|Folder $node) => $this->assertSame( + $target, + $node->getPath(), + 'update needs to be executed for the target destination' + )); } - $updateMock->postRename(['oldpath' => $source, 'newpath' => $target]); + $updateMock->postRename($sourceNode, $targetNode); } - /** - * test data for testPostRename() - * - * @return array - */ - public function dataTestPostRename() { + public static function dataTestPostRename(): array { return [ - ['/test.txt', '/testNew.txt', true], - ['/test.txt', '/testNew.txt', false], - ['/folder/test.txt', '/testNew.txt', true], - ['/folder/test.txt', '/testNew.txt', false], - ['/folder/test.txt', '/testNew.txt', true], - ['/test.txt', '/folder/testNew.txt', false], + ['/test.txt', '/testNew.txt'], + ['/folder/test.txt', '/testNew.txt'], + ['/test.txt', '/folder/testNew.txt'], ]; } - - /** - * @dataProvider dataTestPostRestore - * - * @param boolean $encryptionEnabled - */ - public function testPostRestore($encryptionEnabled): void { + public function testPostRestore(): void { $updateMock = $this->getUpdateMock(['update']); - $this->encryptionManager->expects($this->once()) - ->method('isEnabled') - ->willReturn($encryptionEnabled); + $updateMock->expects($this->once())->method('update') + ->willReturnCallback(fn (OCPFile|Folder $node) => $this->assertSame( + '/folder/test.txt', + $node->getPath(), + 'update needs to be executed for the target destination' + )); - if ($encryptionEnabled) { - $updateMock->expects($this->once())->method('update'); - } else { - $updateMock->expects($this->never())->method('update'); - } - - $updateMock->postRestore(['filePath' => '/folder/test.txt']); - } - - /** - * test data for testPostRestore() - * - * @return array - */ - public function dataTestPostRestore() { - return [ - [true], - [false], - ]; + $updateMock->postRestore($this->getFileMock('/folder/test.txt', 'user')); } /** * create mock of the update method * * @param array $methods methods which should be set - * @return \OC\Encryption\Update | \PHPUnit\Framework\MockObject\MockObject */ - protected function getUpdateMock($methods) { - return $this->getMockBuilder('\OC\Encryption\Update') + protected function getUpdateMock(array $methods): Update&MockObject { + return $this->getMockBuilder(Update::class) ->setConstructorArgs( [ - $this->view, $this->util, - $this->mountManager, $this->encryptionManager, $this->fileHelper, $this->logger, $this->uid ] - )->setMethods($methods)->getMock(); + ) + ->onlyMethods($methods) + ->getMock(); } } diff --git a/tests/lib/Encryption/UtilTest.php b/tests/lib/Encryption/UtilTest.php index 62ef809a867..68989c55fb0 100644 --- a/tests/lib/Encryption/UtilTest.php +++ b/tests/lib/Encryption/UtilTest.php @@ -20,7 +20,7 @@ class UtilTest extends TestCase { * * @see https://bugs.php.net/bug.php?id=21641 */ - protected int $headerSize = 8192; + protected static int $headerSize = 8192; /** @var \PHPUnit\Framework\MockObject\MockObject */ protected $view; @@ -61,7 +61,7 @@ class UtilTest extends TestCase { $this->assertEquals($expected, $id); } - public function providesHeadersForEncryptionModule() { + public static function providesHeadersForEncryptionModule(): array { return [ ['', []], ['', ['1']], @@ -80,11 +80,11 @@ 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'], ]; } @@ -120,7 +120,7 @@ class UtilTest extends TestCase { ); } - public function providePathsForTestIsExcluded() { + public static function providePathsForTestIsExcluded(): array { return [ ['/files_encryption', '', true], ['files_encryption/foo.txt', '', true], @@ -152,7 +152,7 @@ class UtilTest extends TestCase { ); } - public function dataTestIsFile() { + public static function dataTestIsFile(): array { return [ ['/user/files/test.txt', true], ['/user/files', true], @@ -175,7 +175,7 @@ class UtilTest extends TestCase { $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'], @@ -196,17 +196,17 @@ class UtilTest extends TestCase { } } - public function dataTestParseRawHeader() { + public static function dataTestParseRawHeader(): 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) , [Util::HEADER_ENCRYPTION_MODULE_KEY => '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', Util::HEADER_ENCRYPTION_MODULE_KEY => '0']], - [str_pad('HelloWorld', $this->headerSize, '-', STR_PAD_RIGHT), []], + [str_pad('HelloWorld', self::$headerSize, '-', STR_PAD_RIGHT), []], ['', []], - [str_pad('HBEGIN:oc_encryption_module:0', $this->headerSize, '-', STR_PAD_RIGHT) + [str_pad('HBEGIN:oc_encryption_module:0', self::$headerSize, '-', STR_PAD_RIGHT) , []], - [str_pad('oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT) + [str_pad('oc_encryption_module:0:HEND', self::$headerSize, '-', STR_PAD_RIGHT) , []], ]; } @@ -245,7 +245,7 @@ class UtilTest extends TestCase { ); } - public function dataTestGetFileKeyDir() { + 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/'], |