nextcloud/tests/lib/files/stream/encryption.php

105 lines
3.8 KiB
PHP
Raw Normal View History

2015-03-30 14:59:48 +03:00
<?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) {
2015-03-30 14:59:48 +03:00
$size = filesize($fileName);
2015-04-02 12:07:07 +03:00
$source = fopen($fileName, $mode);
$internalPath = $fileName;
$fullPath = $fileName;
2015-03-30 14:59:48 +03:00
$header = [];
$uid = '';
2015-04-02 12:07:07 +03:00
$encryptionModule = $this->buildMockModule();
2015-03-30 14:59:48 +03:00
$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();
2015-04-02 12:07:07 +03:00
$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]);
2015-03-30 14:59:48 +03:00
return \OC\Files\Stream\Encryption::wrap($source, $internalPath,
$fullPath, $header, $uid, $encryptionModule, $storage, $encStorage,
2015-04-02 12:07:07 +03:00
$util, $file, $mode, $size, $unencryptedSize);
2015-03-30 14:59:48 +03:00
}
2015-04-02 12:07:07 +03:00
public function testWriteRead() {
$fileName = tempnam("/tmp", "FOO");
$stream = $this->getStream($fileName, 'w+', 0);
2015-03-30 14:59:48 +03:00
$this->assertEquals(6, fwrite($stream, 'foobar'));
2015-04-02 12:07:07 +03:00
fclose($stream);
$stream = $this->getStream($fileName, 'r', 6);
2015-03-30 14:59:48 +03:00
$this->assertEquals('foobar', fread($stream, 100));
2015-04-02 12:07:07 +03:00
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);
}
2015-04-02 12:07:07 +03:00
/**
* @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);
2015-04-02 12:07:07 +03:00
return $encryptionModule;
2015-03-30 14:59:48 +03:00
}
}