diff options
Diffstat (limited to 'tests/lib/Files/Stream')
-rw-r--r-- | tests/lib/Files/Stream/DummyEncryptionWrapper.php | 5 | ||||
-rw-r--r-- | tests/lib/Files/Stream/EncryptionTest.php | 143 | ||||
-rw-r--r-- | tests/lib/Files/Stream/HashWrapperTest.php | 10 | ||||
-rw-r--r-- | tests/lib/Files/Stream/QuotaTest.php | 37 |
4 files changed, 107 insertions, 88 deletions
diff --git a/tests/lib/Files/Stream/DummyEncryptionWrapper.php b/tests/lib/Files/Stream/DummyEncryptionWrapper.php index 211050905bd..89904e6de73 100644 --- a/tests/lib/Files/Stream/DummyEncryptionWrapper.php +++ b/tests/lib/Files/Stream/DummyEncryptionWrapper.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,7 +8,9 @@ namespace Test\Files\Stream; -class DummyEncryptionWrapper extends \OC\Files\Stream\Encryption { +use OC\Files\Stream\Encryption; + +class DummyEncryptionWrapper extends Encryption { /** * simulate a non-seekable stream wrapper by always return false * diff --git a/tests/lib/Files/Stream/EncryptionTest.php b/tests/lib/Files/Stream/EncryptionTest.php index b6650b56a85..62eaab3cc7e 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. @@ -6,27 +9,32 @@ */ 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); @@ -49,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()) @@ -72,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 { @@ -113,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); @@ -150,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'; @@ -158,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], @@ -189,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); @@ -210,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)); @@ -227,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')); @@ -249,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'], @@ -257,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 @@ -294,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 @@ -333,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'); diff --git a/tests/lib/Files/Stream/HashWrapperTest.php b/tests/lib/Files/Stream/HashWrapperTest.php index 686fcbec82a..459bc5c4318 100644 --- a/tests/lib/Files/Stream/HashWrapperTest.php +++ b/tests/lib/Files/Stream/HashWrapperTest.php @@ -12,10 +12,8 @@ use OC\Files\Stream\HashWrapper; use Test\TestCase; class HashWrapperTest extends TestCase { - /** - * @dataProvider hashProvider - */ - public function testHashStream($data, string $algo, string $hash) { + #[\PHPUnit\Framework\Attributes\DataProvider('hashProvider')] + public function testHashStream($data, string $algo, string $hash): void { if (!is_resource($data)) { $tmpData = fopen('php://temp', 'r+'); if ($data !== null) { @@ -25,13 +23,13 @@ class HashWrapperTest extends TestCase { $data = $tmpData; } - $wrapper = HashWrapper::wrap($data, $algo, function ($result) use ($hash) { + $wrapper = HashWrapper::wrap($data, $algo, function ($result) use ($hash): void { $this->assertEquals($hash, $result); }); stream_get_contents($wrapper); } - public function hashProvider() { + public static function hashProvider(): array { return [ ['foo', 'md5', 'acbd18db4cc2f85cedef654fccc4a4d8'], ['foo', 'sha1', '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'], diff --git a/tests/lib/Files/Stream/QuotaTest.php b/tests/lib/Files/Stream/QuotaTest.php index 17ad14b7e44..4248d14f5a1 100644 --- a/tests/lib/Files/Stream/QuotaTest.php +++ b/tests/lib/Files/Stream/QuotaTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,6 +8,8 @@ namespace Test\Files\Stream; +use OC\Files\Stream\Quota; + class QuotaTest extends \Test\TestCase { /** * @param string $mode @@ -15,24 +18,24 @@ class QuotaTest extends \Test\TestCase { */ protected function getStream($mode, $limit) { $source = fopen('php://temp', $mode); - return \OC\Files\Stream\Quota::wrap($source, $limit); + return Quota::wrap($source, $limit); } - public function testWriteEnoughSpace() { + public function testWriteEnoughSpace(): void { $stream = $this->getStream('w+', 100); $this->assertEquals(6, fwrite($stream, 'foobar')); rewind($stream); $this->assertEquals('foobar', fread($stream, 100)); } - public function testWriteNotEnoughSpace() { + public function testWriteNotEnoughSpace(): void { $stream = $this->getStream('w+', 3); $this->assertEquals(3, fwrite($stream, 'foobar')); rewind($stream); $this->assertEquals('foo', fread($stream, 100)); } - public function testWriteNotEnoughSpaceSecondTime() { + public function testWriteNotEnoughSpaceSecondTime(): void { $stream = $this->getStream('w+', 9); $this->assertEquals(6, fwrite($stream, 'foobar')); $this->assertEquals(3, fwrite($stream, 'qwerty')); @@ -40,7 +43,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('foobarqwe', fread($stream, 100)); } - public function testWriteEnoughSpaceRewind() { + public function testWriteEnoughSpaceRewind(): void { $stream = $this->getStream('w+', 6); $this->assertEquals(6, fwrite($stream, 'foobar')); rewind($stream); @@ -49,7 +52,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('qwebar', fread($stream, 100)); } - public function testWriteNotEnoughSpaceRead() { + public function testWriteNotEnoughSpaceRead(): void { $stream = $this->getStream('w+', 6); $this->assertEquals(6, fwrite($stream, 'foobar')); rewind($stream); @@ -57,26 +60,26 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals(0, fwrite($stream, 'qwe')); } - public function testWriteNotEnoughSpaceExistingStream() { + public function testWriteNotEnoughSpaceExistingStream(): void { $source = fopen('php://temp', 'w+'); fwrite($source, 'foobar'); - $stream = \OC\Files\Stream\Quota::wrap($source, 3); + $stream = Quota::wrap($source, 3); $this->assertEquals(3, fwrite($stream, 'foobar')); rewind($stream); $this->assertEquals('foobarfoo', fread($stream, 100)); } - public function testWriteNotEnoughSpaceExistingStreamRewind() { + public function testWriteNotEnoughSpaceExistingStreamRewind(): void { $source = fopen('php://temp', 'w+'); fwrite($source, 'foobar'); - $stream = \OC\Files\Stream\Quota::wrap($source, 3); + $stream = Quota::wrap($source, 3); rewind($stream); $this->assertEquals(6, fwrite($stream, 'qwerty')); rewind($stream); $this->assertEquals('qwerty', fread($stream, 100)); } - public function testFseekReturnsSuccess() { + public function testFseekReturnsSuccess(): void { $stream = $this->getStream('w+', 100); fwrite($stream, '0123456789'); $this->assertEquals(0, fseek($stream, 3, SEEK_SET)); @@ -84,7 +87,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals(0, fseek($stream, -4, SEEK_END)); } - public function testWriteAfterSeekEndWithEnoughSpace() { + public function testWriteAfterSeekEndWithEnoughSpace(): void { $stream = $this->getStream('w+', 100); fwrite($stream, '0123456789'); fseek($stream, -3, SEEK_END); @@ -93,7 +96,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('0123456abcdefghijk', fread($stream, 100)); } - public function testWriteAfterSeekEndWithNotEnoughSpace() { + public function testWriteAfterSeekEndWithNotEnoughSpace(): void { $stream = $this->getStream('w+', 13); fwrite($stream, '0123456789'); // seek forward first to potentially week out @@ -106,7 +109,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('0123456abcdef', fread($stream, 100)); } - public function testWriteAfterSeekSetWithEnoughSpace() { + public function testWriteAfterSeekSetWithEnoughSpace(): void { $stream = $this->getStream('w+', 100); fwrite($stream, '0123456789'); fseek($stream, 7, SEEK_SET); @@ -115,7 +118,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('0123456abcdefghijk', fread($stream, 100)); } - public function testWriteAfterSeekSetWithNotEnoughSpace() { + public function testWriteAfterSeekSetWithNotEnoughSpace(): void { $stream = $this->getStream('w+', 13); fwrite($stream, '0123456789'); fseek($stream, 7, SEEK_SET); @@ -124,7 +127,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('0123456abcdef', fread($stream, 100)); } - public function testWriteAfterSeekCurWithEnoughSpace() { + public function testWriteAfterSeekCurWithEnoughSpace(): void { $stream = $this->getStream('w+', 100); fwrite($stream, '0123456789'); rewind($stream); @@ -136,7 +139,7 @@ class QuotaTest extends \Test\TestCase { $this->assertEquals('0123456abcdefghijk', fread($stream, 100)); } - public function testWriteAfterSeekCurWithNotEnoughSpace() { + public function testWriteAfterSeekCurWithNotEnoughSpace(): void { $stream = $this->getStream('w+', 13); fwrite($stream, '0123456789'); rewind($stream); |