From f5415653fd975b41e0bc27fca8b0666288fc5eef Mon Sep 17 00:00:00 2001 From: jknockaert Date: Thu, 30 Apr 2015 17:10:18 +0200 Subject: fix #15973 Rework of stream_seek handling; there where basically two bugs: 1. seeking to the end of the current file would fail (with SEEK_SET); and 2. if seeking to an undefined position (outside 0,unencryptedSize) then newPosition was not defined. I used the opportunity to simplify the code. --- lib/private/files/stream/encryption.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/private/files/stream/encryption.php b/lib/private/files/stream/encryption.php index 0262405f367..e423868d82b 100644 --- a/lib/private/files/stream/encryption.php +++ b/lib/private/files/stream/encryption.php @@ -356,24 +356,22 @@ class Encryption extends Wrapper { switch ($whence) { case SEEK_SET: - if ($offset < $this->unencryptedSize && $offset >= 0) { - $newPosition = $offset; - } + $newPosition = $offset; break; case SEEK_CUR: - if ($offset >= 0) { - $newPosition = $offset + $this->position; - } + $newPosition = $this->position + $offset; break; case SEEK_END: - if ($this->unencryptedSize + $offset >= 0) { - $newPosition = $this->unencryptedSize + $offset; - } + $newPosition = $this->unencryptedSize + $offset; break; default: return $return; } + if ($newPosition > $this->unencryptedSize || $newPosition < 0) { + return $return; + } + $newFilePosition = floor($newPosition / $this->unencryptedBlockSize) * $this->util->getBlockSize() + $this->headerSize; -- cgit v1.2.3 From 9a71eddaf926f5379090384c7e8e86a295da26bf Mon Sep 17 00:00:00 2001 From: jknockaert Date: Mon, 18 May 2015 11:32:29 +0200 Subject: work on tests Tests reorganised and extended --- tests/lib/files/stream/encryption.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index 892491cbc32..7905d55530b 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -160,15 +160,6 @@ class Encryption extends \Test\TestCase { $this->assertEquals('foobar', fread($stream, 100)); fclose($stream); - unlink($fileName); - } - - public function testWriteWriteRead() { - $fileName = tempnam("/tmp", "FOO"); - $stream = $this->getStream($fileName, 'w+', 0); - $this->assertEquals(6, fwrite($stream, 'foobar')); - fclose($stream); - $stream = $this->getStream($fileName, 'r+', 6); $this->assertEquals(3, fwrite($stream, 'bar')); fclose($stream); @@ -176,6 +167,8 @@ class Encryption extends \Test\TestCase { $stream = $this->getStream($fileName, 'r', 6); $this->assertEquals('barbar', fread($stream, 100)); fclose($stream); + + unlink($fileName); } public function testRewind() { @@ -191,7 +184,9 @@ class Encryption extends \Test\TestCase { $stream = $this->getStream($fileName, 'r', 6); $this->assertEquals('barbar', fread($stream, 100)); fclose($stream); - } + + unlink($fileName); +} public function testSeek() { $fileName = tempnam("/tmp", "FOO"); @@ -203,6 +198,12 @@ class Encryption extends \Test\TestCase { $stream = $this->getStream($fileName, 'r', 9); $this->assertEquals('foofoobar', fread($stream, 100)); + $this->assertEquals(-1, fseek($stream, 10)); + $this->assertEquals(0, fseek($stream, 9)); + $this->assertEquals(-1, fseek($stream, -10, 'SEEK_CUR')); + $this->assertEquals(0, fseek($stream, -9, 'SEEK_CUR')); + $this->assertEquals(-1, fseek($stream, -10, 'SEEK_END')); + $this->assertEquals(0, fseek($stream, -9, 'SEEK_END')); fclose($stream); unlink($fileName); -- cgit v1.2.3 From 62e6c46216c5d019ec5f4f49089c4f13ec2c8f2a Mon Sep 17 00:00:00 2001 From: jknockaert Date: Mon, 18 May 2015 11:55:48 +0200 Subject: correct testSeek --- tests/lib/files/stream/encryption.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index 7905d55530b..e1cf60c2720 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -200,10 +200,10 @@ class Encryption extends \Test\TestCase { $this->assertEquals('foofoobar', fread($stream, 100)); $this->assertEquals(-1, fseek($stream, 10)); $this->assertEquals(0, fseek($stream, 9)); - $this->assertEquals(-1, fseek($stream, -10, 'SEEK_CUR')); - $this->assertEquals(0, fseek($stream, -9, 'SEEK_CUR')); - $this->assertEquals(-1, fseek($stream, -10, 'SEEK_END')); - $this->assertEquals(0, fseek($stream, -9, 'SEEK_END')); + $this->assertEquals(-1, fseek($stream, -10, SEEK_CUR)); + $this->assertEquals(0, fseek($stream, -9, SEEK_CUR)); + $this->assertEquals(-1, fseek($stream, -10, SEEK_END)); + $this->assertEquals(0, fseek($stream, -9, SEEK_END)); fclose($stream); unlink($fileName); -- cgit v1.2.3