diff options
author | Kate <26026535+provokateurin@users.noreply.github.com> | 2024-10-07 14:37:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-07 14:37:25 +0200 |
commit | 414430980a9efa3ced924d092ef50d336b2b7dde (patch) | |
tree | 78635302621e721c0ffc890d81afe56df25b8cd8 | |
parent | bbb6cb2eb01937ca5346b30b44e1969eb1eb0807 (diff) | |
parent | 4412b2b8a8842718ad194d1b1715a6488f15af75 (diff) | |
download | nextcloud-server-414430980a9efa3ced924d092ef50d336b2b7dde.tar.gz nextcloud-server-414430980a9efa3ced924d092ef50d336b2b7dde.zip |
Merge pull request #48487 from nextcloud/refactor/stream-encryption/typings
-rw-r--r-- | lib/private/Files/Storage/Wrapper/Encryption.php | 74 | ||||
-rw-r--r-- | lib/private/Files/Stream/Encryption.php | 116 | ||||
-rw-r--r-- | tests/lib/Files/Storage/Wrapper/EncryptionTest.php | 228 | ||||
-rw-r--r-- | tests/lib/Files/Stream/EncryptionTest.php | 64 |
4 files changed, 214 insertions, 268 deletions
diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index f2f3cdb94f6..a4e8a571f42 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -29,76 +29,32 @@ use Psr\Log\LoggerInterface; class Encryption extends Wrapper { use LocalTempFileTrait; - /** @var string */ - private $mountPoint; - - /** @var \OC\Encryption\Util */ - private $util; - - /** @var \OCP\Encryption\IManager */ - private $encryptionManager; - - private LoggerInterface $logger; - - /** @var string */ - private $uid; - - /** @var array */ - protected $unencryptedSize; - - /** @var \OCP\Encryption\IFile */ - private $fileHelper; - - /** @var IMountPoint */ - private $mount; - - /** @var IStorage */ - private $keyStorage; - - /** @var Update */ - private $update; - - /** @var Manager */ - private $mountManager; - - /** @var array remember for which path we execute the repair step to avoid recursions */ - private $fixUnencryptedSizeOf = []; - - /** @var ArrayCache */ - private $arrayCache; - + private string $mountPoint; + protected array $unencryptedSize = []; + private IMountPoint $mount; + /** for which path we execute the repair step to avoid recursions */ + private array $fixUnencryptedSizeOf = []; /** @var CappedMemoryCache<bool> */ private CappedMemoryCache $encryptedPaths; - - private $enabled = true; + private bool $enabled = true; /** * @param array $parameters */ public function __construct( $parameters, - ?IManager $encryptionManager = null, - ?Util $util = null, - ?LoggerInterface $logger = null, - ?IFile $fileHelper = null, - $uid = null, - ?IStorage $keyStorage = null, - ?Update $update = null, - ?Manager $mountManager = null, - ?ArrayCache $arrayCache = null, + private IManager $encryptionManager, + private Util $util, + private LoggerInterface $logger, + private IFile $fileHelper, + private ?string $uid, + private IStorage $keyStorage, + private Update $update, + private Manager $mountManager, + private ArrayCache $arrayCache, ) { $this->mountPoint = $parameters['mountPoint']; $this->mount = $parameters['mount']; - $this->encryptionManager = $encryptionManager; - $this->util = $util; - $this->logger = $logger; - $this->uid = $uid; - $this->fileHelper = $fileHelper; - $this->keyStorage = $keyStorage; - $this->unencryptedSize = []; - $this->update = $update; - $this->mountManager = $mountManager; - $this->arrayCache = $arrayCache; $this->encryptedPaths = new CappedMemoryCache(); parent::__construct($parameters); } diff --git a/lib/private/Files/Stream/Encryption.php b/lib/private/Files/Stream/Encryption.php index 8f08f925da0..10df9ae98e2 100644 --- a/lib/private/Files/Stream/Encryption.php +++ b/lib/private/Files/Stream/Encryption.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -9,81 +11,42 @@ namespace OC\Files\Stream; use Icewind\Streams\Wrapper; use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException; +use OC\Encryption\File; +use OC\Encryption\Util; +use OC\Files\Storage\Storage; +use OCP\Encryption\IEncryptionModule; use function is_array; use function stream_context_create; class Encryption extends Wrapper { - /** @var \OC\Encryption\Util */ - protected $util; - - /** @var \OC\Encryption\File */ - protected $file; - - /** @var \OCP\Encryption\IEncryptionModule */ - protected $encryptionModule; - - /** @var \OC\Files\Storage\Storage */ - protected $storage; - - /** @var \OC\Files\Storage\Wrapper\Encryption */ - protected $encryptionStorage; - - /** @var string */ - protected $internalPath; - - /** @var string */ - protected $cache; - - /** @var integer */ - protected $size; - - /** @var integer */ - protected $position; - - /** @var integer */ - protected $unencryptedSize; - - /** @var integer */ - protected $headerSize; - - /** @var integer */ - protected $unencryptedBlockSize; - - /** @var array */ - protected $header; - - /** @var string */ - protected $fullPath; - - /** @var bool */ - protected $signed; - + protected Util $util; + protected File $file; + protected IEncryptionModule $encryptionModule; + protected Storage $storage; + protected \OC\Files\Storage\Wrapper\Encryption $encryptionStorage; + protected string $internalPath; + protected string $cache; + protected ?int $size = null; + protected int $position; + protected ?int $unencryptedSize = null; + protected int $headerSize; + protected int $unencryptedBlockSize; + protected array $header; + protected string $fullPath; + protected bool $signed; /** * header data returned by the encryption module, will be written to the file * in case of a write operation - * - * @var array */ - protected $newHeader; - + protected array $newHeader; /** * user who perform the read/write operation null for public access - * - * @var string */ - protected $uid; - - /** @var bool */ - protected $readOnly; - - /** @var bool */ - protected $writeFlag; - - /** @var array */ - protected $expectedContextProperties; - - /** @var bool */ - protected $fileUpdated; + protected string $uid; + protected bool $readOnly; + protected bool $writeFlag; + protected array $expectedContextProperties; + protected bool $fileUpdated; public function __construct() { $this->expectedContextProperties = [ @@ -113,11 +76,11 @@ class Encryption extends Wrapper { * @param string $fullPath relative to data/ * @param array $header * @param string $uid - * @param \OCP\Encryption\IEncryptionModule $encryptionModule - * @param \OC\Files\Storage\Storage $storage + * @param IEncryptionModule $encryptionModule + * @param Storage $storage * @param \OC\Files\Storage\Wrapper\Encryption $encStorage - * @param \OC\Encryption\Util $util - * @param \OC\Encryption\File $file + * @param Util $util + * @param File $file * @param string $mode * @param int|float $size * @param int|float $unencryptedSize @@ -128,19 +91,24 @@ class Encryption extends Wrapper { * * @throws \BadMethodCallException */ - public static function wrap($source, $internalPath, $fullPath, array $header, + public static function wrap( + $source, + $internalPath, + $fullPath, + array $header, $uid, - \OCP\Encryption\IEncryptionModule $encryptionModule, - \OC\Files\Storage\Storage $storage, + IEncryptionModule $encryptionModule, + Storage $storage, \OC\Files\Storage\Wrapper\Encryption $encStorage, - \OC\Encryption\Util $util, - \OC\Encryption\File $file, + Util $util, + File $file, $mode, $size, $unencryptedSize, $headerSize, $signed, - $wrapper = Encryption::class) { + $wrapper = Encryption::class, + ) { $context = stream_context_create([ 'ocencryption' => [ 'source' => $source, diff --git a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php index a2949938410..bb3df36dec2 100644 --- a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php +++ b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php @@ -4,12 +4,18 @@ * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only */ + 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; +use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; use OC\Files\Storage\Wrapper\Encryption; use OC\Files\View; @@ -23,6 +29,7 @@ use OCP\Files\Cache\ICache; use OCP\Files\Mount\IMountPoint; use OCP\ICacheFactory; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\Files\Storage\Storage; @@ -30,87 +37,26 @@ class EncryptionTest extends Storage { /** * block size will always be 8192 for a PHP stream * @see https://bugs.php.net/bug.php?id=21641 - * @var integer - */ - protected $headerSize = 8192; - - /** - * @var Temporary - */ - private $sourceStorage; - - /** - * @var \OC\Files\Storage\Wrapper\Encryption | \PHPUnit\Framework\MockObject\MockObject */ + protected int $headerSize = 8192; + private Temporary $sourceStorage; + /** @var Encryption&MockObject */ protected $instance; - - /** - * @var \OC\Encryption\Keys\Storage | \PHPUnit\Framework\MockObject\MockObject - */ - private $keyStore; - - /** - * @var \OC\Encryption\Util | \PHPUnit\Framework\MockObject\MockObject - */ - private $util; - - /** - * @var \OC\Encryption\Manager | \PHPUnit\Framework\MockObject\MockObject - */ - private $encryptionManager; - - /** - * @var \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject - */ - private $encryptionModule; - - /** - * @var \OC\Encryption\Update | \PHPUnit\Framework\MockObject\MockObject - */ - private $update; - - /** - * @var \OC\Files\Cache\Cache | \PHPUnit\Framework\MockObject\MockObject - */ - private $cache; - - /** - * @var \OC\Log | \PHPUnit\Framework\MockObject\MockObject - */ - private $logger; - - /** - * @var \OC\Encryption\File | \PHPUnit\Framework\MockObject\MockObject - */ - private $file; - - - /** - * @var \OC\Files\Mount\MountPoint | \PHPUnit\Framework\MockObject\MockObject - */ - private $mount; - - /** - * @var \OC\Files\Mount\Manager | \PHPUnit\Framework\MockObject\MockObject - */ - private $mountManager; - - /** - * @var \OC\Group\Manager | \PHPUnit\Framework\MockObject\MockObject - */ - private $groupManager; - - /** - * @var \OCP\IConfig | \PHPUnit\Framework\MockObject\MockObject - */ - private $config; - - /** @var \OC\Memcache\ArrayCache | \PHPUnit\Framework\MockObject\MockObject */ - private $arrayCache; - - - /** @var integer dummy unencrypted size */ - private $dummySize = -1; + private \OC\Encryption\Keys\Storage&MockObject $keyStore; + 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; + private MountPoint&MockObject $mount; + private \OC\Files\Mount\Manager&MockObject $mountManager; + private \OC\Group\Manager&MockObject $groupManager; + private IConfig&MockObject $config; + private ArrayCache&MockObject $arrayCache; + /** dummy unencrypted size */ + private int $dummySize = -1; protected function setUp(): void { parent::setUp(); @@ -133,7 +79,7 @@ class EncryptionTest extends Storage { ->getMock(); $this->util = $this->getMockBuilder('\OC\Encryption\Util') - ->setMethods(['getUidAndFilename', 'isFile', 'isExcluded']) + ->setMethods(['getUidAndFilename', 'isFile', 'isExcluded', 'stripPartialFileExtension']) ->setConstructorArgs([new View(), new Manager( $this->config, $this->createMock(ICacheFactory::class), @@ -146,6 +92,11 @@ class EncryptionTest extends Storage { ->willReturnCallback(function ($path) { return ['user1', $path]; }); + $this->util->expects($this->any()) + ->method('stripPartialFileExtension') + ->willReturnCallback(function ($path) { + return $path; + }); $this->file = $this->getMockBuilder('\OC\Encryption\File') ->disableOriginalConstructor() @@ -189,7 +140,7 @@ class EncryptionTest extends Storage { $this->mountManager->method('findByStorageId') ->willReturn([]); - $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + $this->instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -198,7 +149,15 @@ class EncryptionTest extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache + $this->encryptionManager, + $this->util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache ] ) ->setMethods(['getMetaData', 'getCache', 'getEncryptionModule']) @@ -219,10 +178,7 @@ class EncryptionTest extends Storage { ->willReturn($mockModule); } - /** - * @return \PHPUnit\Framework\MockObject\MockObject - */ - protected function buildMockModule() { + 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']) @@ -266,7 +222,7 @@ class EncryptionTest extends Storage { } ); - $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + $this->instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -275,7 +231,15 @@ class EncryptionTest extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache + $this->encryptionManager, + $this->util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache, ] ) ->setMethods(['getCache', 'verifyUnencryptedSize']) @@ -337,7 +301,7 @@ class EncryptionTest extends Storage { ->method('get') ->willReturn(new CacheEntry(['encrypted' => true, 'path' => '/test.txt', 'size' => 0, 'fileid' => 1])); - $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + $this->instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -346,7 +310,15 @@ class EncryptionTest extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache + $this->encryptionManager, + $this->util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache, ] ) ->setMethods(['getCache', 'verifyUnencryptedSize']) @@ -374,7 +346,7 @@ class EncryptionTest extends Storage { $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); - $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + $this->instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -383,7 +355,15 @@ class EncryptionTest extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache + $this->encryptionManager, + $this->util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache, ] ) ->setMethods(['fixUnencryptedSize']) @@ -396,7 +376,7 @@ class EncryptionTest extends Storage { ->willReturnCallback( function () use ($failure, $expected) { if ($failure) { - throw new \Exception(); + throw new Exception(); } else { return $expected; } @@ -495,17 +475,25 @@ class EncryptionTest extends Storage { $util = $this->getMockBuilder('\OC\Encryption\Util')->disableOriginalConstructor()->getMock(); $sourceStorage->expects($this->once())->method('rmdir')->willReturn($rmdirResult); - $util->expects($this->any())->method('isExcluded')-> willReturn($isExcluded); + $util->expects($this->any())->method('isExcluded')->willReturn($isExcluded); $this->encryptionManager->expects($this->any())->method('isEnabled')->willReturn($encryptionEnabled); - $encryptionStorage = new \OC\Files\Storage\Wrapper\Encryption( + $encryptionStorage = new Encryption( [ 'storage' => $sourceStorage, 'root' => 'foo', 'mountPoint' => '/mountPoint', 'mount' => $this->mount ], - $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update + $this->encryptionManager, + $util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache, ); @@ -595,7 +583,7 @@ class EncryptionTest extends Storage { return ['encrypted' => true, 'path' => $path]; }); - $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + $instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -604,7 +592,15 @@ class EncryptionTest extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache + $this->encryptionManager, + $util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache, ] ) ->setMethods(['getCache', 'readFirstBlock']) @@ -649,15 +645,17 @@ class EncryptionTest extends Storage { * * @dataProvider dataTestGetHeaderAddLegacyModule */ - public function testGetHeaderAddLegacyModule($header, $isEncrypted, $exists, $expected): void { + public function testGetHeaderAddLegacyModule($header, $isEncrypted, $strippedPathExists, $expected): void { $sourceStorage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); $sourceStorage->expects($this->once()) ->method('is_file') - ->willReturn($exists); + ->with('test.txt') + ->willReturn($strippedPathExists); $util = $this->getMockBuilder('\OC\Encryption\Util') + ->onlyMethods(['stripPartialFileExtension', 'parseRawHeader']) ->setConstructorArgs([new View(), new Manager( $this->config, $this->createMock(ICacheFactory::class), @@ -665,6 +663,11 @@ class EncryptionTest extends Storage { $this->createMock(LoggerInterface::class), ), $this->groupManager, $this->config, $this->arrayCache]) ->getMock(); + $util->expects($this->any()) + ->method('stripPartialFileExtension') + ->willReturnCallback(function ($path) { + return $path; + }); $cache = $this->getMockBuilder('\OC\Files\Cache\Cache') ->disableOriginalConstructor()->getMock(); @@ -674,7 +677,7 @@ class EncryptionTest extends Storage { return ['encrypted' => $isEncrypted, 'path' => $path]; }); - $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + $instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -683,7 +686,15 @@ class EncryptionTest extends Storage { 'mountPoint' => '/', 'mount' => $this->mount ], - $this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache + $this->encryptionManager, + $util, + $this->logger, + $this->file, + null, + $this->keyStore, + $this->update, + $this->mountManager, + $this->arrayCache, ] ) ->setMethods(['readFirstBlock', 'getCache']) @@ -729,7 +740,7 @@ class EncryptionTest extends Storage { $storage2->expects($this->any()) ->method('fopen') ->willReturnCallback(function ($path, $mode) { - $temp = \OC::$server->getTempManager(); + $temp = OC::$server->getTempManager(); return fopen($temp->getTemporaryFile(), $mode); }); $storage2->method('getId') @@ -778,7 +789,7 @@ class EncryptionTest extends Storage { $storage2->expects($this->any()) ->method('fopen') ->willReturnCallback(function ($path, $mode) { - $temp = \OC::$server->getTempManager(); + $temp = OC::$server->getTempManager(); return fopen($temp->getTemporaryFile(), $mode); }); $storage2->method('getId') @@ -840,8 +851,8 @@ class EncryptionTest extends Storage { $mountPoint = '/mountPoint'; - /** @var \OC\Files\Storage\Wrapper\Encryption |\PHPUnit\Framework\MockObject\MockObject $instance */ - $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + /** @var Encryption |MockObject $instance */ + $instance = $this->getMockBuilder(Encryption::class) ->setConstructorArgs( [ [ @@ -956,7 +967,6 @@ class EncryptionTest extends Storage { $encryptionManager = $this->createMock(\OC\Encryption\Manager::class); $util = $this->createMock(Util::class); $fileHelper = $this->createMock(IFile::class); - $uid = null; $keyStorage = $this->createMock(IStorage::class); $update = $this->createMock(Update::class); $mountManager = $this->createMock(\OC\Files\Mount\Manager::class); @@ -974,7 +984,7 @@ class EncryptionTest extends Storage { $util, $this->logger, $fileHelper, - $uid, + null, $keyStorage, $update, $mountManager, @@ -985,7 +995,7 @@ class EncryptionTest extends Storage { ->getMock(); if ($encryptionModule === true) { - /** @var IEncryptionModule|\PHPUnit\Framework\MockObject\MockObject $encryptionModule */ + /** @var IEncryptionModule|MockObject $encryptionModule */ $encryptionModule = $this->createMock(IEncryptionModule::class); } diff --git a/tests/lib/Files/Stream/EncryptionTest.php b/tests/lib/Files/Stream/EncryptionTest.php index 419c1a752d0..36e72ea764a 100644 --- a/tests/lib/Files/Stream/EncryptionTest.php +++ b/tests/lib/Files/Stream/EncryptionTest.php @@ -1,4 +1,7 @@ <?php + +declare(strict_types=1); + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,27 +10,27 @@ namespace Test\Files\Stream; use OC\Files\Cache\CacheEntry; +use OC\Files\Storage\Wrapper\Wrapper; use OC\Files\View; use OC\Memcache\ArrayCache; +use OCP\Encryption\IEncryptionModule; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICache; use OCP\ICacheFactory; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; class EncryptionTest extends \Test\TestCase { public const DEFAULT_WRAPPER = '\OC\Files\Stream\Encryption'; - /** @var \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */ - private $encryptionModule; + private IEncryptionModule&MockObject $encryptionModule; /** - * @param string $fileName - * @param string $mode - * @param integer $unencryptedSize + * @param class-string<Wrapper> $wrapper * @return resource */ - protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = self::DEFAULT_WRAPPER, $unencryptedSizeOnClose = 0) { + protected function getStream(string $fileName, string $mode, int $unencryptedSize, string $wrapper = self::DEFAULT_WRAPPER, int $unencryptedSizeOnClose = 0) { clearstatcache(); $size = filesize($fileName); $source = fopen($fileName, $mode); @@ -74,23 +77,39 @@ class EncryptionTest extends \Test\TestCase { $cache->expects($this->any())->method('get')->willReturn($entry); $cache->expects($this->any())->method('update')->with(5, ['encrypted' => 3, 'encryptedVersion' => 3, 'unencrypted_size' => $unencryptedSizeOnClose]); - - return $wrapper::wrap($source, $internalPath, - $fullPath, $header, $uid, $this->encryptionModule, $storage, $encStorage, - $util, $file, $mode, $size, $unencryptedSize, 8192, $wrapper); + return $wrapper::wrap( + $source, + $internalPath, + $fullPath, + $header, + $uid, + $this->encryptionModule, + $storage, + $encStorage, + $util, + $file, + $mode, + $size, + $unencryptedSize, + 8192, + true, + $wrapper, + ); } /** * @dataProvider dataProviderStreamOpen() */ - public function testStreamOpen($isMasterKeyUsed, + public function testStreamOpen( + $isMasterKeyUsed, $mode, $fullPath, $fileExists, $expectedSharePath, $expectedSize, $expectedUnencryptedSize, - $expectedReadOnly): void { + $expectedReadOnly, + ): void { // build mocks $encryptionModuleMock = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor()->getMock(); @@ -98,7 +117,7 @@ class EncryptionTest extends \Test\TestCase { $encryptionModuleMock->expects($this->once()) ->method('getUnencryptedBlockSize')->willReturn(99); $encryptionModuleMock->expects($this->once()) - ->method('begin')->willReturn(true); + ->method('begin')->willReturn([]); $storageMock = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor()->getMock(); @@ -152,6 +171,8 @@ class EncryptionTest extends \Test\TestCase { $header->setValue($streamWrapper, []); $header->setAccessible(false); $this->invokePrivate($streamWrapper, 'signed', [true]); + $this->invokePrivate($streamWrapper, 'internalPath', [$fullPath]); + $this->invokePrivate($streamWrapper, 'uid', ['test']); // call stream_open, that's the method we want to test $dummyVar = 'foo'; @@ -160,23 +181,17 @@ class EncryptionTest extends \Test\TestCase { // check internal properties $size = $stream->getProperty('size'); $size->setAccessible(true); - $this->assertSame($expectedSize, - $size->getValue($streamWrapper) - ); + $this->assertSame($expectedSize, $size->getValue($streamWrapper)); $size->setAccessible(false); $unencryptedSize = $stream->getProperty('unencryptedSize'); $unencryptedSize->setAccessible(true); - $this->assertSame($expectedUnencryptedSize, - $unencryptedSize->getValue($streamWrapper) - ); + $this->assertSame($expectedUnencryptedSize, $unencryptedSize->getValue($streamWrapper)); $unencryptedSize->setAccessible(false); $readOnly = $stream->getProperty('readOnly'); $readOnly->setAccessible(true); - $this->assertSame($expectedReadOnly, - $readOnly->getValue($streamWrapper) - ); + $this->assertSame($expectedReadOnly, $readOnly->getValue($streamWrapper)); $readOnly->setAccessible(false); } @@ -335,10 +350,7 @@ class EncryptionTest extends \Test\TestCase { unlink($fileName); } - /** - * @return \PHPUnit\Framework\MockObject\MockObject - */ - protected function buildMockModule() { + protected function buildMockModule(): IEncryptionModule&MockObject { $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor() ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser', 'needDetailedAccessList']) |