From 7a34f75da6081dfc6d85c2498b1a68838c28fb18 Mon Sep 17 00:00:00 2001 From: jknockaert Date: Tue, 21 Apr 2015 17:36:25 +0200 Subject: add two tests testRewind tests reading and writing after rewind on an encrypted stream; testWriteWriteRead tests r+ mode --- tests/lib/files/stream/encryption.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'tests/lib/files/stream') diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index 1da208282e6..024185b34c8 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -152,6 +152,36 @@ class Encryption extends \Test\TestCase { 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); + + $stream = $this->getStream($fileName, 'r', 6); + $this->assertEquals('barbar', fread($stream, 100)); + fclose($stream); + } + + public function testRewind() { + $fileName = tempnam("/tmp", "FOO"); + $stream = $this->getStream($fileName, 'w+', 0); + $this->assertEquals(6, fwrite($stream, 'foobar')); + $this->assertEquals(TRUE, rewind($stream)); + $this->assertEquals('foobar', fread($stream, 100)); + $this->assertEquals(TRUE, rewind($stream)); + $this->assertEquals(3, fwrite($stream, 'bar')); + fclose($stream); + + $stream = $this->getStream($fileName, 'r', 6); + $this->assertEquals('barbar', fread($stream, 100)); + fclose($stream); + } + public function testSeek() { $fileName = tempnam("/tmp", "FOO"); $stream = $this->getStream($fileName, 'w+', 0); -- cgit v1.2.3 From d6841aa70696d8f44a2656293bf7efe860073c06 Mon Sep 17 00:00:00 2001 From: jknockaert Date: Tue, 21 Apr 2015 18:01:27 +0200 Subject: disable r+ test --- tests/lib/files/stream/encryption.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests/lib/files/stream') diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index 024185b34c8..fdd710e62fa 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -152,20 +152,20 @@ class Encryption extends \Test\TestCase { 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); - - $stream = $this->getStream($fileName, 'r', 6); - $this->assertEquals('barbar', fread($stream, 100)); - fclose($stream); - } +// 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); +// +// $stream = $this->getStream($fileName, 'r', 6); +// $this->assertEquals('barbar', fread($stream, 100)); +// fclose($stream); +// } public function testRewind() { $fileName = tempnam("/tmp", "FOO"); -- cgit v1.2.3 From 27ea23ea6b88ee73ec2826212336d20e9c209029 Mon Sep 17 00:00:00 2001 From: jknockaert Date: Tue, 21 Apr 2015 18:09:21 +0200 Subject: Update encryption.php --- tests/lib/files/stream/encryption.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests/lib/files/stream') diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index fdd710e62fa..36e668a218e 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -167,20 +167,20 @@ class Encryption extends \Test\TestCase { // fclose($stream); // } - public function testRewind() { - $fileName = tempnam("/tmp", "FOO"); - $stream = $this->getStream($fileName, 'w+', 0); - $this->assertEquals(6, fwrite($stream, 'foobar')); - $this->assertEquals(TRUE, rewind($stream)); - $this->assertEquals('foobar', fread($stream, 100)); - $this->assertEquals(TRUE, rewind($stream)); - $this->assertEquals(3, fwrite($stream, 'bar')); - fclose($stream); - - $stream = $this->getStream($fileName, 'r', 6); - $this->assertEquals('barbar', fread($stream, 100)); - fclose($stream); - } +// public function testRewind() { +// $fileName = tempnam("/tmp", "FOO"); +// $stream = $this->getStream($fileName, 'w+', 0); +// $this->assertEquals(6, fwrite($stream, 'foobar')); +// $this->assertEquals(TRUE, rewind($stream)); +// $this->assertEquals('foobar', fread($stream, 100)); +// $this->assertEquals(TRUE, rewind($stream)); +// $this->assertEquals(3, fwrite($stream, 'bar')); +// fclose($stream); +// +// $stream = $this->getStream($fileName, 'r', 6); +// $this->assertEquals('barbar', fread($stream, 100)); +// fclose($stream); +// } public function testSeek() { $fileName = tempnam("/tmp", "FOO"); -- cgit v1.2.3 From 18a1225b0c0a8c44353a7f262b33aee2b5896f46 Mon Sep 17 00:00:00 2001 From: jknockaert Date: Wed, 22 Apr 2015 12:52:01 +0200 Subject: enable testRewind --- tests/lib/files/stream/encryption.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests/lib/files/stream') diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index 36e668a218e..fdd710e62fa 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -167,20 +167,20 @@ class Encryption extends \Test\TestCase { // fclose($stream); // } -// public function testRewind() { -// $fileName = tempnam("/tmp", "FOO"); -// $stream = $this->getStream($fileName, 'w+', 0); -// $this->assertEquals(6, fwrite($stream, 'foobar')); -// $this->assertEquals(TRUE, rewind($stream)); -// $this->assertEquals('foobar', fread($stream, 100)); -// $this->assertEquals(TRUE, rewind($stream)); -// $this->assertEquals(3, fwrite($stream, 'bar')); -// fclose($stream); -// -// $stream = $this->getStream($fileName, 'r', 6); -// $this->assertEquals('barbar', fread($stream, 100)); -// fclose($stream); -// } + public function testRewind() { + $fileName = tempnam("/tmp", "FOO"); + $stream = $this->getStream($fileName, 'w+', 0); + $this->assertEquals(6, fwrite($stream, 'foobar')); + $this->assertEquals(TRUE, rewind($stream)); + $this->assertEquals('foobar', fread($stream, 100)); + $this->assertEquals(TRUE, rewind($stream)); + $this->assertEquals(3, fwrite($stream, 'bar')); + fclose($stream); + + $stream = $this->getStream($fileName, 'r', 6); + $this->assertEquals('barbar', fread($stream, 100)); + fclose($stream); + } public function testSeek() { $fileName = tempnam("/tmp", "FOO"); -- cgit v1.2.3 From 4554df2512aadbbed22650515c2fb5c85651ef42 Mon Sep 17 00:00:00 2001 From: jknockaert Date: Wed, 22 Apr 2015 13:06:02 +0200 Subject: enable testWriteWriteRead --- tests/lib/files/stream/encryption.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests/lib/files/stream') diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index fdd710e62fa..024185b34c8 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -152,20 +152,20 @@ class Encryption extends \Test\TestCase { 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); -// -// $stream = $this->getStream($fileName, 'r', 6); -// $this->assertEquals('barbar', fread($stream, 100)); -// fclose($stream); -// } + 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); + + $stream = $this->getStream($fileName, 'r', 6); + $this->assertEquals('barbar', fread($stream, 100)); + fclose($stream); + } public function testRewind() { $fileName = tempnam("/tmp", "FOO"); -- cgit v1.2.3 From 9a5783b28434762aeb05ce62627a5adb675e5560 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 24 Apr 2015 16:47:27 +0200 Subject: fix unit tests --- lib/private/files/stream/encryption.php | 14 ++++++++++---- tests/lib/files/stream/encryption.php | 21 ++++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) (limited to 'tests/lib/files/stream') diff --git a/lib/private/files/stream/encryption.php b/lib/private/files/stream/encryption.php index 52660270763..5f39207db87 100644 --- a/lib/private/files/stream/encryption.php +++ b/lib/private/files/stream/encryption.php @@ -237,7 +237,6 @@ class Encryption extends Wrapper { $accessList = $this->file->getAccessList($sharePath); $this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $this->header, $accessList); - if (!($path==='')){ if ( $mode === 'w' || $mode === 'w+' @@ -249,9 +248,9 @@ class Encryption extends Wrapper { $this->writeHeader(); $this->size = $this->util->getHeaderSize(); } else { - parent::stream_read($this->util->getHeaderSize()); - } + $this->skipHeader(); } + return true; } @@ -432,9 +431,16 @@ class Encryption extends Wrapper { * @return integer * @throws EncryptionHeaderKeyExistsException if header key is already in use */ - private function writeHeader() { + protected function writeHeader() { $header = $this->util->createHeader($this->newHeader, $this->encryptionModule); return parent::stream_write($header); } + /** + * read first block to skip the header + */ + protected function skipHeader() { + parent::stream_read($this->util->getHeaderSize()); + } + } diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index 024185b34c8..0b34de8ae12 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -55,6 +55,7 @@ class Encryption extends \Test\TestCase { $fileExists, $expectedSharePath, $expectedSize, + $expectedUnencryptedSize, $expectedReadOnly) { // build mocks @@ -77,9 +78,15 @@ class Encryption extends \Test\TestCase { return array(); })); + $utilMock = $this->getMockBuilder('\OC\Encryption\Util') + ->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'])->disableOriginalConstructor()->getMock(); + ->setMethods(['loadContext', 'writeHeader', 'skipHeader'])->disableOriginalConstructor()->getMock(); // set internal properties of the stream wrapper $stream = new \ReflectionClass('\OC\Files\Stream\Encryption'); @@ -95,6 +102,10 @@ class Encryption extends \Test\TestCase { $file->setAccessible(true); $file->setValue($streamWrapper, $fileMock); $file->setAccessible(false); + $util = $stream->getProperty('util'); + $util->setAccessible(true); + $util->setValue($streamWrapper, $utilMock); + $util->setAccessible(false); $fullPathP = $stream->getProperty('fullPath'); $fullPathP->setAccessible(true); $fullPathP->setValue($streamWrapper, $fullPath); @@ -118,7 +129,7 @@ class Encryption extends \Test\TestCase { $unencryptedSize = $stream->getProperty('unencryptedSize'); $unencryptedSize->setAccessible(true); - $this->assertSame($expectedSize, + $this->assertSame($expectedUnencryptedSize, $unencryptedSize->getValue($streamWrapper) ); $unencryptedSize->setAccessible(false); @@ -133,9 +144,9 @@ class Encryption extends \Test\TestCase { public function dataProviderStreamOpen() { return array( - array('r', '/foo/bar/test.txt', true, '/foo/bar/test.txt', null, true), - array('r', '/foo/bar/test.txt', false, '/foo/bar', null, true), - array('w', '/foo/bar/test.txt', true, '/foo/bar/test.txt', 0, false), + array('r', '/foo/bar/test.txt', true, '/foo/bar/test.txt', null, null, true), + array('r', '/foo/bar/test.txt', false, '/foo/bar', null, null, true), + array('w', '/foo/bar/test.txt', true, '/foo/bar/test.txt', 8192, 0, false), ); } -- cgit v1.2.3