diff options
Diffstat (limited to 'tests/lib/Files/Stream')
-rw-r--r-- | tests/lib/Files/Stream/DummyEncryptionWrapper.php | 37 | ||||
-rw-r--r-- | tests/lib/Files/Stream/EncryptionTest.php | 342 | ||||
-rw-r--r-- | tests/lib/Files/Stream/QuotaTest.php | 155 | ||||
-rw-r--r-- | tests/lib/Files/Stream/StaticStreamTest.php | 70 |
4 files changed, 604 insertions, 0 deletions
diff --git a/tests/lib/Files/Stream/DummyEncryptionWrapper.php b/tests/lib/Files/Stream/DummyEncryptionWrapper.php new file mode 100644 index 00000000000..bb512d99c66 --- /dev/null +++ b/tests/lib/Files/Stream/DummyEncryptionWrapper.php @@ -0,0 +1,37 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace Test\Files\Stream; + +class DummyEncryptionWrapper extends \OC\Files\Stream\Encryption { + + /** + * simulate a non-seekable stream wrapper by always return false + * + * @param int $position + * @return bool + */ + protected function parentStreamSeek($position) { + return false; + } + +} diff --git a/tests/lib/Files/Stream/EncryptionTest.php b/tests/lib/Files/Stream/EncryptionTest.php new file mode 100644 index 00000000000..45204e48d09 --- /dev/null +++ b/tests/lib/Files/Stream/EncryptionTest.php @@ -0,0 +1,342 @@ +<?php + +namespace Test\Files\Stream; + +use OC\Files\View; + +class EncryptionTest extends \Test\TestCase { + + /** @var \OCP\Encryption\IEncryptionModule | \PHPUnit_Framework_MockObject_MockObject */ + private $encryptionModule; + + /** + * @param string $fileName + * @param string $mode + * @param integer $unencryptedSize + * @return resource + */ + protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC\Files\Stream\Encryption') { + clearstatcache(); + $size = filesize($fileName); + $source = fopen($fileName, $mode); + $internalPath = $fileName; + $fullPath = $fileName; + $header = []; + $uid = ''; + $this->encryptionModule = $this->buildMockModule(); + $storage = $this->getMockBuilder('\OC\Files\Storage\Storage') + ->disableOriginalConstructor()->getMock(); + $encStorage = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + ->disableOriginalConstructor()->getMock(); + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $arrayCache = $this->getMock('OC\Memcache\ArrayCache'); + $groupManager = $this->getMockBuilder('\OC\Group\Manager') + ->disableOriginalConstructor() + ->getMock(); + $file = $this->getMockBuilder('\OC\Encryption\File') + ->disableOriginalConstructor() + ->setMethods(['getAccessList']) + ->getMock(); + $file->expects($this->any())->method('getAccessList')->willReturn([]); + $util = $this->getMock( + '\OC\Encryption\Util', + ['getUidAndFilename'], + [new View(), new \OC\User\Manager(), $groupManager, $config, $arrayCache] + ); + $util->expects($this->any()) + ->method('getUidAndFilename') + ->willReturn(['user1', $internalPath]); + + + return $wrapper::wrap($source, $internalPath, + $fullPath, $header, $uid, $this->encryptionModule, $storage, $encStorage, + $util, $file, $mode, $size, $unencryptedSize, 8192, $wrapper); + } + + /** + * @dataProvider dataProviderStreamOpen() + */ + public function testStreamOpen($mode, + $fullPath, + $fileExists, + $expectedSharePath, + $expectedSize, + $expectedUnencryptedSize, + $expectedReadOnly) { + + // build mocks + $encryptionModuleMock = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor()->getMock(); + $encryptionModuleMock->expects($this->once()) + ->method('getUnencryptedBlockSize')->willReturn(99); + $encryptionModuleMock->expects($this->once()) + ->method('begin')->willReturn(true); + + $storageMock = $this->getMockBuilder('\OC\Files\Storage\Storage') + ->disableOriginalConstructor()->getMock(); + $storageMock->expects($this->once())->method('file_exists')->willReturn($fileExists); + + $fileMock = $this->getMockBuilder('\OC\Encryption\File') + ->disableOriginalConstructor()->getMock(); + $fileMock->expects($this->once())->method('getAccessList') + ->will($this->returnCallback(function($sharePath) use ($expectedSharePath) { + $this->assertSame($expectedSharePath, $sharePath); + 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', 'writeHeader', 'skipHeader'])->disableOriginalConstructor()->getMock(); + + // set internal properties of the stream wrapper + $stream = new \ReflectionClass('\OC\Files\Stream\Encryption'); + $encryptionModule = $stream->getProperty('encryptionModule'); + $encryptionModule->setAccessible(true); + $encryptionModule->setValue($streamWrapper, $encryptionModuleMock); + $encryptionModule->setAccessible(false); + $storage = $stream->getProperty('storage'); + $storage->setAccessible(true); + $storage->setValue($streamWrapper, $storageMock); + $storage->setAccessible(false); + $file = $stream->getProperty('file'); + $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); + $fullPathP->setAccessible(false); + $header = $stream->getProperty('header'); + $header->setAccessible(true); + $header->setValue($streamWrapper, array()); + $header->setAccessible(false); + $this->invokePrivate($streamWrapper, 'signed', [true]); + + // call stream_open, that's the method we want to test + $dummyVar = 'foo'; + $streamWrapper->stream_open('', $mode, '', $dummyVar); + + // check internal properties + $size = $stream->getProperty('size'); + $size->setAccessible(true); + $this->assertSame($expectedSize, + $size->getValue($streamWrapper) + ); + $size->setAccessible(false); + + $unencryptedSize = $stream->getProperty('unencryptedSize'); + $unencryptedSize->setAccessible(true); + $this->assertSame($expectedUnencryptedSize, + $unencryptedSize->getValue($streamWrapper) + ); + $unencryptedSize->setAccessible(false); + + $readOnly = $stream->getProperty('readOnly'); + $readOnly->setAccessible(true); + $this->assertSame($expectedReadOnly, + $readOnly->getValue($streamWrapper) + ); + $readOnly->setAccessible(false); + } + + public function dataProviderStreamOpen() { + return array( + 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), + ); + } + + public function testWriteRead() { + $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('foobar', fread($stream, 100)); + 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); + + unlink($fileName); + } + + 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); + + unlink($fileName); +} + + public function testSeek() { + $fileName = tempnam("/tmp", "FOO"); + $stream = $this->getStream($fileName, 'w+', 0); + $this->assertEquals(6, fwrite($stream, 'foobar')); + $this->assertEquals(0, fseek($stream, 3)); + $this->assertEquals(6, fwrite($stream, 'foobar')); + fclose($stream); + + $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); + } + + function dataFilesProvider() { + return [ + ['lorem-big.txt'], + ['block-aligned.txt'], + ['block-aligned-plus-one.txt'], + ]; + } + + /** + * @dataProvider dataFilesProvider + */ + public function testWriteReadBigFile($testFile) { + + $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile); + // write it + $fileName = tempnam("/tmp", "FOO"); + $stream = $this->getStream($fileName, 'w+', 0); + // 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 + $this->encryptionModule->expects($this->never())->method('decrypt'); + fwrite($stream, $expectedData); + fclose($stream); + + // read it all + $stream = $this->getStream($fileName, 'r', strlen($expectedData)); + $data = stream_get_contents($stream); + fclose($stream); + + $this->assertEquals($expectedData, $data); + + // another read test with a loop like we do in several places: + $stream = $this->getStream($fileName, 'r', strlen($expectedData)); + $data = ''; + while (!feof($stream)) { + $data .= fread($stream, 8192); + } + fclose($stream); + + $this->assertEquals($expectedData, $data); + + unlink($fileName); + } + + /** + * 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); + + $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'); + // 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 + $this->encryptionModule->expects($this->never())->method('decrypt'); + fwrite($stream, $expectedData); + fclose($stream); + + // read it all + $stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper'); + $data = stream_get_contents($stream); + fclose($stream); + + $this->assertEquals($expectedData, $data); + + // another read test with a loop like we do in several places: + $stream = $this->getStream($fileName, 'r', strlen($expectedData)); + $data = ''; + while (!feof($stream)) { + $data .= fread($stream, 8192); + } + fclose($stream); + + $this->assertEquals($expectedData, $data); + + unlink($fileName); + + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function buildMockModule() { + $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor() + ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser']) + ->getMock(); + + $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE'); + $encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module'); + $encryptionModule->expects($this->any())->method('begin')->willReturn([]); + $encryptionModule->expects($this->any())->method('end')->willReturn(''); + $encryptionModule->expects($this->any())->method('isReadable')->willReturn(true); + $encryptionModule->expects($this->any())->method('encrypt')->willReturnCallback(function($data) { + // simulate different block size by adding some padding to the data + if (isset($data[6125])) { + return str_pad($data, 8192, 'X'); + } + // last block + return $data; + }); + $encryptionModule->expects($this->any())->method('decrypt')->willReturnCallback(function($data) { + if (isset($data[8191])) { + return substr($data, 0, 6126); + } + // last block + return $data; + }); + $encryptionModule->expects($this->any())->method('update')->willReturn(true); + $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true); + $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(6126); + return $encryptionModule; + } +} diff --git a/tests/lib/Files/Stream/QuotaTest.php b/tests/lib/Files/Stream/QuotaTest.php new file mode 100644 index 00000000000..d084f0c769c --- /dev/null +++ b/tests/lib/Files/Stream/QuotaTest.php @@ -0,0 +1,155 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Stream; + +class QuotaTest extends \Test\TestCase { + protected function tearDown() { + \OC\Files\Stream\Quota::clear(); + parent::tearDown(); + } + + /** + * @param string $mode + * @param integer $limit + */ + protected function getStream($mode, $limit) { + $source = fopen('php://temp', $mode); + return \OC\Files\Stream\Quota::wrap($source, $limit); + } + + public function testWriteEnoughSpace() { + $stream = $this->getStream('w+', 100); + $this->assertEquals(6, fwrite($stream, 'foobar')); + rewind($stream); + $this->assertEquals('foobar', fread($stream, 100)); + } + + public function testWriteNotEnoughSpace() { + $stream = $this->getStream('w+', 3); + $this->assertEquals(3, fwrite($stream, 'foobar')); + rewind($stream); + $this->assertEquals('foo', fread($stream, 100)); + } + + public function testWriteNotEnoughSpaceSecondTime() { + $stream = $this->getStream('w+', 9); + $this->assertEquals(6, fwrite($stream, 'foobar')); + $this->assertEquals(3, fwrite($stream, 'qwerty')); + rewind($stream); + $this->assertEquals('foobarqwe', fread($stream, 100)); + } + + public function testWriteEnoughSpaceRewind() { + $stream = $this->getStream('w+', 6); + $this->assertEquals(6, fwrite($stream, 'foobar')); + rewind($stream); + $this->assertEquals(3, fwrite($stream, 'qwe')); + rewind($stream); + $this->assertEquals('qwebar', fread($stream, 100)); + } + + public function testWriteNotEnoughSpaceRead() { + $stream = $this->getStream('w+', 6); + $this->assertEquals(6, fwrite($stream, 'foobar')); + rewind($stream); + $this->assertEquals('foobar', fread($stream, 6)); + $this->assertEquals(0, fwrite($stream, 'qwe')); + } + + public function testWriteNotEnoughSpaceExistingStream() { + $source = fopen('php://temp', 'w+'); + fwrite($source, 'foobar'); + $stream = \OC\Files\Stream\Quota::wrap($source, 3); + $this->assertEquals(3, fwrite($stream, 'foobar')); + rewind($stream); + $this->assertEquals('foobarfoo', fread($stream, 100)); + } + + public function testWriteNotEnoughSpaceExistingStreamRewind() { + $source = fopen('php://temp', 'w+'); + fwrite($source, 'foobar'); + $stream = \OC\Files\Stream\Quota::wrap($source, 3); + rewind($stream); + $this->assertEquals(6, fwrite($stream, 'qwerty')); + rewind($stream); + $this->assertEquals('qwerty', fread($stream, 100)); + } + + public function testFseekReturnsSuccess() { + $stream = $this->getStream('w+', 100); + fwrite($stream, '0123456789'); + $this->assertEquals(0, fseek($stream, 3, SEEK_SET)); + $this->assertEquals(0, fseek($stream, -1, SEEK_CUR)); + $this->assertEquals(0, fseek($stream, -4, SEEK_END)); + } + + public function testWriteAfterSeekEndWithEnoughSpace() { + $stream = $this->getStream('w+', 100); + fwrite($stream, '0123456789'); + fseek($stream, -3, SEEK_END); + $this->assertEquals(11, fwrite($stream, 'abcdefghijk')); + rewind($stream); + $this->assertEquals('0123456abcdefghijk', fread($stream, 100)); + } + + public function testWriteAfterSeekEndWithNotEnoughSpace() { + $stream = $this->getStream('w+', 13); + fwrite($stream, '0123456789'); + // seek forward first to potentially week out + // potential limit calculation errors + fseek($stream, 4, SEEK_SET); + // seek to the end + fseek($stream, -3, SEEK_END); + $this->assertEquals(6, fwrite($stream, 'abcdefghijk')); + rewind($stream); + $this->assertEquals('0123456abcdef', fread($stream, 100)); + } + + public function testWriteAfterSeekSetWithEnoughSpace() { + $stream = $this->getStream('w+', 100); + fwrite($stream, '0123456789'); + fseek($stream, 7, SEEK_SET); + $this->assertEquals(11, fwrite($stream, 'abcdefghijk')); + rewind($stream); + $this->assertEquals('0123456abcdefghijk', fread($stream, 100)); + } + + public function testWriteAfterSeekSetWithNotEnoughSpace() { + $stream = $this->getStream('w+', 13); + fwrite($stream, '0123456789'); + fseek($stream, 7, SEEK_SET); + $this->assertEquals(6, fwrite($stream, 'abcdefghijk')); + rewind($stream); + $this->assertEquals('0123456abcdef', fread($stream, 100)); + } + + public function testWriteAfterSeekCurWithEnoughSpace() { + $stream = $this->getStream('w+', 100); + fwrite($stream, '0123456789'); + rewind($stream); + fseek($stream, 3, SEEK_CUR); + fseek($stream, 5, SEEK_CUR); + fseek($stream, -1, SEEK_CUR); + $this->assertEquals(11, fwrite($stream, 'abcdefghijk')); + rewind($stream); + $this->assertEquals('0123456abcdefghijk', fread($stream, 100)); + } + + public function testWriteAfterSeekCurWithNotEnoughSpace() { + $stream = $this->getStream('w+', 13); + fwrite($stream, '0123456789'); + rewind($stream); + fseek($stream, 3, SEEK_CUR); + fseek($stream, 5, SEEK_CUR); + fseek($stream, -1, SEEK_CUR); + $this->assertEquals(6, fwrite($stream, 'abcdefghijk')); + rewind($stream); + $this->assertEquals('0123456abcdef', fread($stream, 100)); + } +} diff --git a/tests/lib/Files/Stream/StaticStreamTest.php b/tests/lib/Files/Stream/StaticStreamTest.php new file mode 100644 index 00000000000..309291a3a5b --- /dev/null +++ b/tests/lib/Files/Stream/StaticStreamTest.php @@ -0,0 +1,70 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Stream; + +class StaticStreamTest extends \Test\TestCase { + + private $sourceFile; + private $sourceText; + + protected function setUp() { + parent::setUp(); + $this->sourceFile = \OC::$SERVERROOT . '/tests/data/lorem.txt'; + $this->sourceText = file_get_contents($this->sourceFile); + } + + protected function tearDown() { + \OC\Files\Stream\StaticStream::clear(); + parent::tearDown(); + } + + public function testContent() { + file_put_contents('static://foo', $this->sourceText); + $this->assertEquals($this->sourceText, file_get_contents('static://foo')); + } + + public function testMultipleFiles() { + file_put_contents('static://foo', $this->sourceText); + file_put_contents('static://bar', strrev($this->sourceText)); + $this->assertEquals($this->sourceText, file_get_contents('static://foo')); + $this->assertEquals(strrev($this->sourceText), file_get_contents('static://bar')); + } + + public function testOverwrite() { + file_put_contents('static://foo', $this->sourceText); + file_put_contents('static://foo', 'qwerty'); + $this->assertEquals('qwerty', file_get_contents('static://foo')); + } + + public function testIsFile() { + $this->assertFalse(is_file('static://foo')); + file_put_contents('static://foo', $this->sourceText); + $this->assertTrue(is_file('static://foo')); + } + + public function testIsDir() { + $this->assertFalse(is_dir('static://foo')); + file_put_contents('static://foo', $this->sourceText); + $this->assertFalse(is_dir('static://foo')); + } + + public function testFileType() { + file_put_contents('static://foo', $this->sourceText); + $this->assertEquals('file', filetype('static://foo')); + } + + public function testUnlink() { + $this->assertFalse(file_exists('static://foo')); + file_put_contents('static://foo', $this->sourceText); + $this->assertTrue(file_exists('static://foo')); + unlink('static://foo'); + clearstatcache(); + $this->assertFalse(file_exists('static://foo')); + } +} |