Skip to content

Commit

Permalink
refactor(Wrapper\Encryption): Migrate to strong types
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Oct 1, 2024
1 parent 0420b58 commit 9b32a61
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 130 deletions.
57 changes: 16 additions & 41 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,23 @@
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;
private ?Util $util;
private ?IManager $encryptionManager;
private ?LoggerInterface $logger;
private ?string $uid;
protected array $unencryptedSize;
private ?IFile $fileHelper;
private IMountPoint $mount;
private ?IStorage $keyStorage;
private ?Update $update;
private ?Manager $mountManager;
/** for which path we execute the repair step to avoid recursions */
private array $fixUnencryptedSizeOf = [];
private ?ArrayCache $arrayCache;
/** @var CappedMemoryCache<bool> */
private CappedMemoryCache $encryptedPaths;

private $enabled = true;
private bool $enabled = true;

/**
* @param array $parameters
Expand All @@ -81,7 +56,7 @@ public function __construct(
?Util $util = null,
?LoggerInterface $logger = null,
?IFile $fileHelper = null,
$uid = null,
?string $uid = null,
?IStorage $keyStorage = null,
?Update $update = null,
?Manager $mountManager = null,
Expand Down
130 changes: 41 additions & 89 deletions tests/lib/Files/Storage/Wrapper/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
namespace Test\Files\Storage\Wrapper;

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;
Expand All @@ -23,94 +26,34 @@
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;

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();
Expand All @@ -133,7 +76,7 @@ protected function setUp(): void {
->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),
Expand All @@ -146,6 +89,11 @@ protected function setUp(): void {
->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()
Expand Down Expand Up @@ -219,10 +167,7 @@ protected function setUp(): void {
->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'])
Expand Down Expand Up @@ -498,7 +443,7 @@ public function testRmdir($path, $rmdirResult, $isExcluded, $encryptionEnabled):
$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',
Expand Down Expand Up @@ -649,22 +594,29 @@ public function dataTestGetHeader() {
*
* @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(IEventDispatcher::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();
Expand Down Expand Up @@ -840,7 +792,7 @@ public function testCopyBetweenStorageVersions($sourceInternalPath, $targetInter

$mountPoint = '/mountPoint';

/** @var \OC\Files\Storage\Wrapper\Encryption |\PHPUnit\Framework\MockObject\MockObject $instance */
/** @var Encryption |MockObject $instance */
$instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
->setConstructorArgs(
[
Expand Down Expand Up @@ -985,7 +937,7 @@ public function testShouldEncrypt(
->getMock();

if ($encryptionModule === true) {
/** @var IEncryptionModule|\PHPUnit\Framework\MockObject\MockObject $encryptionModule */
/** @var IEncryptionModule|MockObject $encryptionModule */
$encryptionModule = $this->createMock(IEncryptionModule::class);
}

Expand Down

0 comments on commit 9b32a61

Please sign in to comment.