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);
}
* 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;
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;
/**
* 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();
->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),
->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()
$this->mountManager->method('findByStorageId')
->willReturn([]);
- $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
+ $this->instance = $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
[
'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'])
->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'])
}
);
- $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
+ $this->instance = $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
[
'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'])
->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(
[
[
'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'])
$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(
[
[
'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'])
->willReturnCallback(
function () use ($failure, $expected) {
if ($failure) {
- throw new \Exception();
+ throw new Exception();
} else {
return $expected;
}
$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,
);
return ['encrypted' => true, 'path' => $path];
});
- $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
+ $instance = $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
[
'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'])
*
* @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),
$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();
return ['encrypted' => $isEncrypted, 'path' => $path];
});
- $instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
+ $instance = $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
[
'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'])
$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')
$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')
$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(
[
[
$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);
$util,
$this->logger,
$fileHelper,
- $uid,
+ null,
$keyStorage,
$update,
$mountManager,
->getMock();
if ($encryptionModule === true) {
- /** @var IEncryptionModule|\PHPUnit\Framework\MockObject\MockObject $encryptionModule */
+ /** @var IEncryptionModule|MockObject $encryptionModule */
$encryptionModule = $this->createMock(IEncryptionModule::class);
}