1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
<?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) {
clearstatcache();
$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();
$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]);
$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);
}
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);
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);
}
/**
* @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'])
->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')->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;
}
}
|