fixing unit tests for stream wrapper
This commit is contained in:
parent
391fab35f0
commit
8ffa6db110
|
@ -248,7 +248,7 @@ class Encryption extends Wrapper {
|
||||||
$result = '';
|
$result = '';
|
||||||
|
|
||||||
// skip the header if we read the file from the beginning
|
// skip the header if we read the file from the beginning
|
||||||
if ($this->position === 0 && !empty($this->header)) {
|
if ($this->position === 0) {
|
||||||
parent::stream_read($this->util->getBlockSize());
|
parent::stream_read($this->util->getBlockSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,14 +11,14 @@ class Encryption extends \Test\TestCase {
|
||||||
* @param string $mode
|
* @param string $mode
|
||||||
* @param integer $limit
|
* @param integer $limit
|
||||||
*/
|
*/
|
||||||
protected function getStream($mode) {
|
protected function getStream($fileName, $mode) {
|
||||||
|
|
||||||
$source = fopen('php://temp', $mode);
|
$source = fopen($fileName, $mode);
|
||||||
$internalPath = '';
|
$internalPath = $fileName;
|
||||||
$fullPath = '';
|
$fullPath = $fileName;
|
||||||
$header = [];
|
$header = [];
|
||||||
$uid = '';
|
$uid = '';
|
||||||
$encryptionModule = new DummyModule();
|
$encryptionModule = $this->buildMockModule();
|
||||||
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
||||||
->disableOriginalConstructor()->getMock();
|
->disableOriginalConstructor()->getMock();
|
||||||
$encStorage = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
$encStorage = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
|
||||||
|
@ -26,19 +26,51 @@ class Encryption extends \Test\TestCase {
|
||||||
$config = $this->getMockBuilder('\OCP\IConfig')
|
$config = $this->getMockBuilder('\OCP\IConfig')
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
->getMock();
|
->getMock();
|
||||||
$util = new \OC\Encryption\Util(new View(), new \OC\User\Manager(), $config);
|
$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]);
|
||||||
$size = 12;
|
$size = 12;
|
||||||
$unencryptedSize = 8000;
|
$unencryptedSize = 8000;
|
||||||
|
|
||||||
return \OC\Files\Stream\Encryption::wrap($source, $internalPath,
|
return \OC\Files\Stream\Encryption::wrap($source, $internalPath,
|
||||||
$fullPath, $header, $uid, $encryptionModule, $storage, $encStorage,
|
$fullPath, $header, $uid, $encryptionModule, $storage, $encStorage,
|
||||||
$util, $mode, $size, $unencryptedSize);
|
$util, $file, $mode, $size, $unencryptedSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteEnoughSpace() {
|
public function testWriteRead() {
|
||||||
$stream = $this->getStream('w+');
|
$fileName = tempnam("/tmp", "FOO");
|
||||||
|
$stream = $this->getStream($fileName, 'w+');
|
||||||
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
||||||
rewind($stream);
|
fclose($stream);
|
||||||
|
|
||||||
|
$stream = $this->getStream($fileName, 'r');
|
||||||
$this->assertEquals('foobar', fread($stream, 100));
|
$this->assertEquals('foobar', fread($stream, 100));
|
||||||
|
fclose($stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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(6126);
|
||||||
|
return $encryptionModule;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue