diff options
Diffstat (limited to 'tests/lib/files')
-rw-r--r-- | tests/lib/files/cache/cache.php | 9 | ||||
-rw-r--r-- | tests/lib/files/cache/updaterlegacy.php | 11 | ||||
-rw-r--r-- | tests/lib/files/storage/storage.php | 2 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/encryption.php | 125 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/jail.php | 4 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/quota.php | 8 | ||||
-rw-r--r-- | tests/lib/files/stream/encryption.php | 104 |
7 files changed, 237 insertions, 26 deletions
diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php index f0ad6cf3ab1..9a64375f4e3 100644 --- a/tests/lib/files/cache/cache.php +++ b/tests/lib/files/cache/cache.php @@ -183,8 +183,8 @@ class Cache extends \Test\TestCase { $file3 = 'folder/foo'; $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'); $fileData = array(); - $fileData['bar'] = array('size' => 1000, 'unencrypted_size' => 900, 'encrypted' => 1, 'mtime' => 20, 'mimetype' => 'foo/file'); - $fileData['foo'] = array('size' => 20, 'unencrypted_size' => 16, 'encrypted' => 1, 'mtime' => 25, 'mimetype' => 'foo/file'); + $fileData['bar'] = array('size' => 1000, 'encrypted' => 1, 'mtime' => 20, 'mimetype' => 'foo/file'); + $fileData['foo'] = array('size' => 20, 'encrypted' => 1, 'mtime' => 25, 'mimetype' => 'foo/file'); $this->cache->put($file1, $data1); $this->cache->put($file2, $fileData['bar']); @@ -194,8 +194,6 @@ class Cache extends \Test\TestCase { $this->assertEquals(count($content), 2); foreach ($content as $cachedData) { $data = $fileData[$cachedData['name']]; - // indirect retrieval swaps unencrypted_size and size - $this->assertEquals($data['unencrypted_size'], $cachedData['size']); } $file4 = 'folder/unkownSize'; @@ -207,11 +205,10 @@ class Cache extends \Test\TestCase { $fileData['unkownSize'] = array('size' => 5, 'mtime' => 25, 'mimetype' => 'foo/file'); $this->cache->put($file4, $fileData['unkownSize']); - $this->assertEquals(916, $this->cache->calculateFolderSize($file1)); + $this->assertEquals(1025, $this->cache->calculateFolderSize($file1)); // direct cache entry retrieval returns the original values $entry = $this->cache->get($file1); $this->assertEquals(1025, $entry['size']); - $this->assertEquals(916, $entry['unencrypted_size']); $this->cache->remove($file2); $this->cache->remove($file3); diff --git a/tests/lib/files/cache/updaterlegacy.php b/tests/lib/files/cache/updaterlegacy.php index 99cacca8e95..6bdacbe34fe 100644 --- a/tests/lib/files/cache/updaterlegacy.php +++ b/tests/lib/files/cache/updaterlegacy.php @@ -22,8 +22,6 @@ class UpdaterLegacy extends \Test\TestCase { */ private $scanner; - private $stateFilesEncryption; - /** * @var \OC\Files\Cache\Cache $cache */ @@ -37,11 +35,6 @@ class UpdaterLegacy extends \Test\TestCase { protected function setUp() { parent::setUp(); - // remember files_encryption state - $this->stateFilesEncryption = \OC_App::isEnabled('files_encryption'); - // we want to tests with the encryption app disabled - \OC_App::disable('files_encryption'); - $this->storage = new \OC\Files\Storage\Temporary(array()); $textData = "dummy file data\n"; $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png'); @@ -80,10 +73,6 @@ class UpdaterLegacy extends \Test\TestCase { $this->assertTrue($result); Filesystem::tearDown(); Filesystem::mount($this->originalStorage, array(), '/'); - // reset app files_encryption - if ($this->stateFilesEncryption) { - \OC_App::enable('files_encryption'); - } parent::tearDown(); } diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index ad7522f1ea8..938fecb5bf3 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -253,7 +253,7 @@ abstract class Storage extends \Test\TestCase { $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile)); $localFile = $this->instance->getLocalFile('/lorem.txt'); $this->assertTrue(file_exists($localFile)); - $this->assertEquals(file_get_contents($localFile), file_get_contents($textFile)); + $this->assertEquals(file_get_contents($textFile), file_get_contents($localFile)); $this->instance->mkdir('/folder'); $this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile)); diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php new file mode 100644 index 00000000000..bf4464f0eb9 --- /dev/null +++ b/tests/lib/files/storage/wrapper/encryption.php @@ -0,0 +1,125 @@ +<?php + +namespace Test\Files\Storage\Wrapper; + +use OC\Files\Storage\Temporary; +use OC\Files\View; + +class Encryption extends \Test\Files\Storage\Storage { + + /** + * @var Temporary + */ + private $sourceStorage; + + public function setUp() { + + parent::setUp(); + + $mockModule = $this->buildMockModule(); + $encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager') + ->disableOriginalConstructor() + ->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule', 'isEnabled']) + ->getMock(); + $encryptionManager->expects($this->any()) + ->method('getDefaultEncryptionModule') + ->willReturn($mockModule); + $encryptionManager->expects($this->any()) + ->method('getEncryptionModule') + ->willReturn($mockModule); + $encryptionManager->expects($this->any()) + ->method('isEnabled') + ->willReturn(true); + + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + + $util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $config]); + $util->expects($this->any()) + ->method('getUidAndFilename') + ->willReturnCallback(function ($path) { + return ['user1', $path]; + }); + + $file = $this->getMockBuilder('\OC\Encryption\File') + ->disableOriginalConstructor() + ->getMock(); + + $logger = $this->getMock('\OC\Log'); + + $this->sourceStorage = new Temporary(array()); + $keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage') + ->disableOriginalConstructor()->getMock(); + $mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint') + ->disableOriginalConstructor() + ->setMethods(['getOption']) + ->getMock(); + $mount->expects($this->any())->method('getOption')->willReturn(true); + $this->instance = new EncryptionWrapper([ + 'storage' => $this->sourceStorage, + 'root' => 'foo', + 'mountPoint' => '/', + 'mount' => $mount + ], + $encryptionManager, $util, $logger, $file, null, $keyStore + ); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function buildMockModule() { + $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor() + ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'calculateUnencryptedSize', 'getUnencryptedBlockSize']) + ->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('encrypt')->willReturnArgument(0); + $encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0); + $encryptionModule->expects($this->any())->method('update')->willReturn(true); + $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true); + $encryptionModule->expects($this->any())->method('calculateUnencryptedSize')->willReturn(42); + $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(8192); + return $encryptionModule; + } + +// public function testMkDirRooted() { +// $this->instance->mkdir('bar'); +// $this->assertTrue($this->sourceStorage->is_dir('foo/bar')); +// } +// +// public function testFilePutContentsRooted() { +// $this->instance->file_put_contents('bar', 'asd'); +// $this->assertEquals('asd', $this->sourceStorage->file_get_contents('foo/bar')); +// } +} + +// +// FIXME: this is too bad and needs adjustment +// +class EncryptionWrapper extends \OC\Files\Storage\Wrapper\Encryption { + private $keyStore; + + public function __construct( + $parameters, + \OC\Encryption\Manager $encryptionManager = null, + \OC\Encryption\Util $util = null, + \OC\Log $logger = null, + \OC\Encryption\File $fileHelper = null, + $uid = null, + $keyStore = null + ) { + $this->keyStore = $keyStore; + parent::__construct($parameters, $encryptionManager, $util, $logger, $fileHelper, $uid); + } + + protected function getKeyStorage($encryptionModuleId) { + return $this->keyStore; + } + +} diff --git a/tests/lib/files/storage/wrapper/jail.php b/tests/lib/files/storage/wrapper/jail.php index 270ce750ecf..a7bd684df44 100644 --- a/tests/lib/files/storage/wrapper/jail.php +++ b/tests/lib/files/storage/wrapper/jail.php @@ -9,10 +9,6 @@ namespace Test\Files\Storage\Wrapper; class Jail extends \Test\Files\Storage\Storage { - /** - * @var string tmpDir - */ - private $tmpDir; /** * @var \OC\Files\Storage\Temporary diff --git a/tests/lib/files/storage/wrapper/quota.php b/tests/lib/files/storage/wrapper/quota.php index 8ca8f308b71..a5828296be9 100644 --- a/tests/lib/files/storage/wrapper/quota.php +++ b/tests/lib/files/storage/wrapper/quota.php @@ -59,7 +59,7 @@ class Quota extends \Test\Files\Storage\Storage { public function testFreeSpaceWithUsedSpace() { $instance = $this->getLimitedStorage(9); $instance->getCache()->put( - '', array('size' => 3, 'unencrypted_size' => 0) + '', array('size' => 3) ); $this->assertEquals(6, $instance->free_space('')); } @@ -77,7 +77,7 @@ class Quota extends \Test\Files\Storage\Storage { $instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 9)); $instance->getCache()->put( - '', array('size' => 3, 'unencrypted_size' => 0) + '', array('size' => 3) ); $this->assertEquals(6, $instance->free_space('')); } @@ -85,9 +85,9 @@ class Quota extends \Test\Files\Storage\Storage { public function testFreeSpaceWithUsedSpaceAndEncryption() { $instance = $this->getLimitedStorage(9); $instance->getCache()->put( - '', array('size' => 7, 'unencrypted_size' => 3) + '', array('size' => 7) ); - $this->assertEquals(6, $instance->free_space('')); + $this->assertEquals(2, $instance->free_space('')); } public function testFWriteNotEnoughSpace() { diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php new file mode 100644 index 00000000000..84156337ad7 --- /dev/null +++ b/tests/lib/files/stream/encryption.php @@ -0,0 +1,104 @@ +<?php + +namespace Test\Files\Stream; + +use OC\Files\View; +use OCA\Encryption_Dummy\DummyModule; + +class Encryption extends \Test\TestCase { + + /** + * @param string $mode + * @param integer $limit + */ + protected function getStream($fileName, $mode, $unencryptedSize) { + + $size = filesize($fileName); + $source = fopen($fileName, $mode); + $internalPath = $fileName; + $fullPath = $fileName; + $header = []; + $uid = ''; + $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(); + $file = $this->getMockBuilder('\OC\Encryption\File') + ->disableOriginalConstructor() + ->getMock(); + $util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $config]); + $util->expects($this->any()) + ->method('getUidAndFilename') + ->willReturn(['user1', $internalPath]); + + return \OC\Files\Stream\Encryption::wrap($source, $internalPath, + $fullPath, $header, $uid, $encryptionModule, $storage, $encStorage, + $util, $file, $mode, $size, $unencryptedSize); + } + + 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); + } + + 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)); + fclose($stream); + } + + public function testWriteReadBigFile() { + $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/lorem-big.txt'); + // write it + $fileName = tempnam("/tmp", "FOO"); + $stream = $this->getStream($fileName, 'w+', 0); + 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); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function buildMockModule() { + $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor() + ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'calculateUnencryptedSize', 'getUnencryptedBlockSize']) + ->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('encrypt')->willReturnArgument(0); + $encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0); + $encryptionModule->expects($this->any())->method('update')->willReturn(true); + $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true); + $encryptionModule->expects($this->any())->method('calculateUnencryptedSize')->willReturn(42); + $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(8192); + return $encryptionModule; + } +} |