]> source.dussan.org Git - nextcloud-server.git/commitdiff
Also update the unencrypted size of files when creating them with empty content 35657/head
authorJulius Härtl <jus@bitgrid.net>
Wed, 7 Dec 2022 15:27:31 +0000 (16:27 +0100)
committerbackportbot-nextcloud[bot] <backportbot-nextcloud[bot]@users.noreply.github.com>
Wed, 7 Dec 2022 21:15:57 +0000 (21:15 +0000)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/Files/Stream/Encryption.php
tests/lib/Files/Stream/EncryptionTest.php

index 4a8d7b026a543e84977cbf91a8e6c8386e6db65d..d21a9ffc4fe1b9fe79417b43e26c03429c6496b5 100644 (file)
@@ -528,6 +528,7 @@ class Encryption extends Wrapper {
         */
        protected function writeHeader() {
                $header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
+               $this->fileUpdated = true;
                return parent::stream_write($header);
        }
 
index 5516c0bf6580d44880f74569b44bcf246be48991..79a845fe7a6b1fe586e97277a03d4df96ba402ce 100644 (file)
@@ -12,6 +12,7 @@ use OCP\IConfig;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 class EncryptionTest extends \Test\TestCase {
+       public const DEFAULT_WRAPPER = '\OC\Files\Stream\Encryption';
 
        /** @var  \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject  */
        private $encryptionModule;
@@ -22,7 +23,7 @@ class EncryptionTest extends \Test\TestCase {
         * @param integer $unencryptedSize
         * @return resource
         */
-       protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC\Files\Stream\Encryption') {
+       protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = self::DEFAULT_WRAPPER, $unencryptedSizeOnClose = 0) {
                clearstatcache();
                $size = filesize($fileName);
                $source = fopen($fileName, $mode);
@@ -64,9 +65,10 @@ class EncryptionTest extends \Test\TestCase {
                $entry = new CacheEntry([
                        'fileid' => 5,
                        'encryptedVersion' => 2,
+                       'unencrypted_size' => $unencryptedSizeOnClose,
                ]);
                $cache->expects($this->any())->method('get')->willReturn($entry);
-               $cache->expects($this->any())->method('update')->with(5, ['encrypted' => 3, 'encryptedVersion' => 3]);
+               $cache->expects($this->any())->method('update')->with(5, ['encrypted' => 3, 'encryptedVersion' => 3, 'unencrypted_size' => $unencryptedSizeOnClose]);
 
 
                return $wrapper::wrap($source, $internalPath,
@@ -188,7 +190,7 @@ class EncryptionTest extends \Test\TestCase {
 
        public function testWriteRead() {
                $fileName = tempnam("/tmp", "FOO");
-               $stream = $this->getStream($fileName, 'w+', 0);
+               $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6);
                $this->assertEquals(6, fwrite($stream, 'foobar'));
                fclose($stream);
 
@@ -196,7 +198,7 @@ class EncryptionTest extends \Test\TestCase {
                $this->assertEquals('foobar', fread($stream, 100));
                fclose($stream);
 
-               $stream = $this->getStream($fileName, 'r+', 6);
+               $stream = $this->getStream($fileName, 'r+', 6, self::DEFAULT_WRAPPER, 6);
                $this->assertEquals(3, fwrite($stream, 'bar'));
                fclose($stream);
 
@@ -209,7 +211,7 @@ class EncryptionTest extends \Test\TestCase {
 
        public function testRewind() {
                $fileName = tempnam("/tmp", "FOO");
-               $stream = $this->getStream($fileName, 'w+', 0);
+               $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6);
                $this->assertEquals(6, fwrite($stream, 'foobar'));
                $this->assertEquals(true, rewind($stream));
                $this->assertEquals('foobar', fread($stream, 100));
@@ -227,7 +229,7 @@ class EncryptionTest extends \Test\TestCase {
        public function testSeek() {
                $fileName = tempnam("/tmp", "FOO");
 
-               $stream = $this->getStream($fileName, 'w+', 0);
+               $stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 9);
                $this->assertEquals(6, fwrite($stream, 'foobar'));
                $this->assertEquals(0, fseek($stream, 3));
                $this->assertEquals(6, fwrite($stream, 'foobar'));
@@ -261,7 +263,7 @@ class EncryptionTest extends \Test\TestCase {
                $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
                // write it
                $fileName = tempnam("/tmp", "FOO");
-               $stream = $this->getStream($fileName, 'w+', 0);
+               $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
                // in the middle of a file
@@ -302,7 +304,7 @@ class EncryptionTest extends \Test\TestCase {
                $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
                // write it
                $fileName = tempnam("/tmp", "FOO");
-               $stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper');
+               $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
                // in the middle of a file
@@ -311,7 +313,7 @@ class EncryptionTest extends \Test\TestCase {
                fclose($stream);
 
                // read it all
-               $stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper');
+               $stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper', strlen($expectedData));
                $data = stream_get_contents($stream);
                fclose($stream);