Skip to content

Commit

Permalink
Add unit tests for versioned storage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen authored and DeepDiver1975 committed Jul 27, 2023
1 parent 254e793 commit ff070d0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,10 @@ public function getContentOfVersion($internalPath, $versionId) {
$fileEncryptionVersion = null;
$info = $this->storage->getVersion($internalPath, $versionId);

if ($info['size']) {
if (isset($info['size'])) {
$unencryptedSize = $info['size'];
}
if ($info['encryptedVersion']) {
if (isset($info['encryptedVersion'])) {
$fileEncryptionVersion = $info['encryptedVersion'];
}
$headerSize = $this->getHeaderSize($stream);
Expand Down
72 changes: 71 additions & 1 deletion tests/lib/Files/Storage/Wrapper/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
use OC\Files\Storage\Wrapper\Encryption;
use OC\Files\View;
use OC\User\Manager;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IVersionedStorage;
use PHPUnit\Framework\MockObject\MockObject;
use Test\Files\Storage\Storage;

interface ITestVersionStorage extends IVersionedStorage, \OC\Files\Storage\Storage {
}

class EncryptionTest extends Storage {

/**
Expand Down Expand Up @@ -1047,4 +1050,71 @@ public function testCopyFromStorageWithMultipleMounts() {

$instance->copyFromStorage($sourceStorage, 'files/text.txt', '/files/groupshare/text.txt');
}

/**
* @param IVersionedStorage | MockObject
* @return Encryption | MockObject
*/
private function getVersionInstance($sourceStorage) {
return $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
[
'storage' => $sourceStorage,
'root' => 'foo',
'mountPoint' => '/',
'mount' => $this->mount
],
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
]
)
->setMethods(['getHeader', 'getFullPath', 'getHeaderSize'])
->getMock();
}

public function testGetVersions() {
$expectedVersions = ['version1'];
$sourceStorage = $this->createMock(ITestVersionStorage::class);
$sourceStorage->method('getVersions')->willReturn($expectedVersions);
$instance = $this->getVersionInstance($sourceStorage);
$this->assertSame($expectedVersions, $instance->getVersions('/test/test.txt'));
}

public function testGetVersion() {
$expectedVersion = ['version1'];
$sourceStorage = $this->createMock(ITestVersionStorage::class);
$sourceStorage->method('getVersion')->willReturn($expectedVersion);
$instance = $this->getVersionInstance($sourceStorage);
$this->assertSame($expectedVersion, $instance->getVersion('/test/test.txt', 'version1Id'));
}

public function testGetContentOfVersion() {
$this->encryptionManager->method('isEnabled')->willReturn(true);
$sourceStorage = $this->createMock(ITestVersionStorage::class);
$tmp = \tmpfile();
$sourceStorage->method('getContentOfVersion')->willReturn($tmp);
$sourceStorage->method('filesize')->willReturn(100);
$sourceStorage->method('getVersion')->willReturn(['version1']);

$instance = $this->getVersionInstance($sourceStorage);
$instance->method('getHeader')->willReturn(['signed' => true]);
$instance->method('getFullPath')->willReturn('/full/path');
$instance->method('getHeaderSize')->willReturn($this->headerSize);

$this->assertNotNull($instance->getContentOfVersion('/test/test.txt', 'version1Id'));
}

public function testResoreVersion() {
$sourceStorage = $this->createMock(ITestVersionStorage::class);
$sourceStorage->method('restoreVersion')->willReturn(true);
$instance = $this->getVersionInstance($sourceStorage);
$this->assertTrue($instance->restoreVersion('/test/test.txt', 'version1Id'));
}

public function testSaveVersion() {
$sourceStorage = $this->createMock(ITestVersionStorage::class);
$sourceStorage->method('saveVersion')->willReturn(true);
$instance = $this->getVersionInstance($sourceStorage);
$this->assertTrue($instance->saveVersion('/test/test.txt'));
}
}

0 comments on commit ff070d0

Please sign in to comment.