diff options
Diffstat (limited to 'tests/lib/Files/Stream/EncryptionTest.php')
-rw-r--r-- | tests/lib/Files/Stream/EncryptionTest.php | 147 |
1 files changed, 83 insertions, 64 deletions
diff --git a/tests/lib/Files/Stream/EncryptionTest.php b/tests/lib/Files/Stream/EncryptionTest.php index 3d45ff2fc3c..62eaab3cc7e 100644 --- a/tests/lib/Files/Stream/EncryptionTest.php +++ b/tests/lib/Files/Stream/EncryptionTest.php @@ -1,28 +1,40 @@ <?php +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ namespace Test\Files\Stream; +use OC\Encryption\File; +use OC\Encryption\Util; use OC\Files\Cache\CacheEntry; +use OC\Files\Storage\Storage; +use OC\Files\Storage\Wrapper\Wrapper; +use OC\Files\Stream\Encryption; 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); @@ -45,15 +57,16 @@ class EncryptionTest extends \Test\TestCase { ->getMock(); $file = $this->getMockBuilder('\OC\Encryption\File') ->disableOriginalConstructor() - ->setMethods(['getAccessList']) + ->onlyMethods(['getAccessList']) ->getMock(); $file->expects($this->any())->method('getAccessList')->willReturn([]); $util = $this->getMockBuilder('\OC\Encryption\Util') - ->setMethods(['getUidAndFilename']) + ->onlyMethods(['getUidAndFilename']) ->setConstructorArgs([new View(), new \OC\User\Manager( $config, $this->createMock(ICacheFactory::class), - $this->createMock(IEventDispatcher::class) + $this->createMock(IEventDispatcher::class), + $this->createMock(LoggerInterface::class), ), $groupManager, $config, $arrayCache]) ->getMock(); $util->expects($this->any()) @@ -68,38 +81,49 @@ 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, + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderStreamOpen')] + public function testStreamOpen( + $isMasterKeyUsed, $mode, $fullPath, $fileExists, $expectedSharePath, $expectedSize, $expectedUnencryptedSize, - $expectedReadOnly) { + $expectedReadOnly, + ): void { // build mocks - $encryptionModuleMock = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') - ->disableOriginalConstructor()->getMock(); + $encryptionModuleMock = $this->createMock(IEncryptionModule::class); $encryptionModuleMock->expects($this->any())->method('needDetailedAccessList')->willReturn(!$isMasterKeyUsed); $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(); + $storageMock = $this->createMock(Storage::class); $storageMock->expects($this->once())->method('file_exists')->willReturn($fileExists); - $fileMock = $this->getMockBuilder('\OC\Encryption\File') - ->disableOriginalConstructor()->getMock(); + $fileMock = $this->createMock(File::class); if ($isMasterKeyUsed) { $fileMock->expects($this->never())->method('getAccessList'); } else { @@ -109,18 +133,20 @@ class EncryptionTest extends \Test\TestCase { return []; }); } - $utilMock = $this->getMockBuilder('\OC\Encryption\Util') + $utilMock = $this->getMockBuilder(Util::class) ->disableOriginalConstructor()->getMock(); $utilMock->expects($this->any()) ->method('getHeaderSize') ->willReturn(8192); // get a instance of the stream wrapper - $streamWrapper = $this->getMockBuilder('\OC\Files\Stream\Encryption') - ->setMethods(['loadContext', 'writeHeader', 'skipHeader'])->disableOriginalConstructor()->getMock(); + $streamWrapper = $this->getMockBuilder(Encryption::class) + ->onlyMethods(['loadContext', 'writeHeader', 'skipHeader']) + ->disableOriginalConstructor() + ->getMock(); // set internal properties of the stream wrapper - $stream = new \ReflectionClass('\OC\Files\Stream\Encryption'); + $stream = new \ReflectionClass(Encryption::class); $encryptionModule = $stream->getProperty('encryptionModule'); $encryptionModule->setAccessible(true); $encryptionModule->setValue($streamWrapper, $encryptionModuleMock); @@ -146,6 +172,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'; @@ -154,27 +182,21 @@ 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); } - public function dataProviderStreamOpen() { + public static function dataProviderStreamOpen(): array { return [ [false, 'r', '/foo/bar/test.txt', true, '/foo/bar/test.txt', null, null, true], [false, 'r', '/foo/bar/test.txt', false, '/foo/bar', null, null, true], @@ -185,8 +207,8 @@ class EncryptionTest extends \Test\TestCase { ]; } - public function testWriteRead() { - $fileName = tempnam("/tmp", "FOO"); + public function testWriteRead(): void { + $fileName = tempnam('/tmp', 'FOO'); $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6); $this->assertEquals(6, fwrite($stream, 'foobar')); fclose($stream); @@ -206,8 +228,8 @@ class EncryptionTest extends \Test\TestCase { unlink($fileName); } - public function testRewind() { - $fileName = tempnam("/tmp", "FOO"); + public function testRewind(): void { + $fileName = tempnam('/tmp', 'FOO'); $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6); $this->assertEquals(6, fwrite($stream, 'foobar')); $this->assertEquals(true, rewind($stream)); @@ -223,8 +245,8 @@ class EncryptionTest extends \Test\TestCase { unlink($fileName); } - public function testSeek() { - $fileName = tempnam("/tmp", "FOO"); + public function testSeek(): void { + $fileName = tempnam('/tmp', 'FOO'); $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 9); $this->assertEquals(6, fwrite($stream, 'foobar')); @@ -245,7 +267,7 @@ class EncryptionTest extends \Test\TestCase { unlink($fileName); } - public function dataFilesProvider() { + public static function dataFilesProvider(): array { return [ ['lorem-big.txt'], ['block-aligned.txt'], @@ -253,13 +275,11 @@ class EncryptionTest extends \Test\TestCase { ]; } - /** - * @dataProvider dataFilesProvider - */ - public function testWriteReadBigFile($testFile) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilesProvider')] + public function testWriteReadBigFile($testFile): void { $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile); // write it - $fileName = tempnam("/tmp", "FOO"); + $fileName = tempnam('/tmp', 'FOO'); $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, strlen($expectedData)); // while writing the file from the beginning to the end we should never try // to read parts of the file. This should only happen for write operations @@ -290,17 +310,19 @@ class EncryptionTest extends \Test\TestCase { /** * simulate a non-seekable storage - * - * @dataProvider dataFilesProvider */ - public function testWriteToNonSeekableStorage($testFile) { - $wrapper = $this->getMockBuilder('\OC\Files\Stream\Encryption') - ->setMethods(['parentSeekStream'])->getMock(); - $wrapper->expects($this->any())->method('parentSeekStream')->willReturn(false); + #[\PHPUnit\Framework\Attributes\DataProvider('dataFilesProvider')] + public function testWriteToNonSeekableStorage($testFile): void { + $wrapper = $this->getMockBuilder(Encryption::class) + ->onlyMethods(['parentStreamSeek']) + ->getMock(); + $wrapper->expects($this->any()) + ->method('parentStreamSeek') + ->willReturn(false); $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile); // write it - $fileName = tempnam("/tmp", "FOO"); + $fileName = tempnam('/tmp', 'FOO'); $stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper', strlen($expectedData)); // while writing the file from the beginning to the end we should never try // to read parts of the file. This should only happen for write operations @@ -329,13 +351,10 @@ class EncryptionTest extends \Test\TestCase { unlink($fileName); } - /** - * @return \PHPUnit\Framework\MockObject\MockObject - */ - protected function buildMockModule() { - $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + protected function buildMockModule(): IEncryptionModule&MockObject { + $encryptionModule = $this->getMockBuilder(IEncryptionModule::class) ->disableOriginalConstructor() - ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser', 'needDetailedAccessList']) + ->onlyMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser', 'needDetailedAccessList']) ->getMock(); $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE'); |