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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
<?php
/**
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\Security;
use OC\Files\Storage\Temporary;
use OC\Files\View;
use OC\Security\CertificateManager;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Security\ISecureRandom;
/**
* Class CertificateManagerTest
*
* @group DB
*/
class CertificateManagerTest extends \Test\TestCase {
use \Test\Traits\UserTrait;
use \Test\Traits\MountProviderTrait;
/** @var CertificateManager */
private $certificateManager;
/** @var String */
private $username;
/** @var ISecureRandom */
private $random;
protected function setUp(): void {
parent::setUp();
$this->username = $this->getUniqueID('', 20);
$this->createUser($this->username, '');
$storage = new \OC\Files\Storage\Temporary();
$this->registerMount($this->username, $storage, '/' . $this->username . '/');
\OC_Util::tearDownFS();
\OC_User::setUserId('');
\OC\Files\Filesystem::tearDown();
\OC_Util::setupFS($this->username);
$config = $this->createMock(IConfig::class);
$config->expects($this->any())->method('getSystemValue')
->with('installed', false)->willReturn(true);
$this->random = $this->createMock(ISecureRandom::class);
$this->random->method('generate')
->willReturn('random');
$this->certificateManager = new CertificateManager(
$this->username,
new \OC\Files\View(),
$config,
$this->createMock(ILogger::class),
$this->random
);
}
protected function tearDown(): void {
$user = \OC::$server->getUserManager()->get($this->username);
if ($user !== null) {
$user->delete();
}
parent::tearDown();
}
protected function assertEqualsArrays($expected, $actual) {
sort($expected);
sort($actual);
$this->assertEquals($expected, $actual);
}
function testListCertificates() {
// Test empty certificate bundle
$this->assertSame(array(), $this->certificateManager->listCertificates());
// Add some certificates
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
$certificateStore = array();
$certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
// Add another certificates
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
$certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
}
function testAddInvalidCertificate() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Certificate could not get parsed.');
$this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
}
/**
* @return array
*/
public function dangerousFileProvider() {
return [
['.htaccess'],
['../../foo.txt'],
['..\..\foo.txt'],
];
}
/**
* @dataProvider dangerousFileProvider
* @param string $filename
*/
function testAddDangerousFile($filename) {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Filename is not valid');
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), $filename);
}
function testRemoveDangerousFile() {
$this->assertFalse($this->certificateManager->removeCertificate('../../foo.txt'));
}
function testRemoveExistingFile() {
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
$this->assertTrue($this->certificateManager->removeCertificate('GoodCertificate'));
}
function testGetCertificateBundle() {
$this->assertSame('/' . $this->username . '/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
}
/**
* @dataProvider dataTestNeedRebundling
*
* @param string $uid
* @param int $CaBundleMtime
* @param int $systemWideMtime
* @param int $targetBundleMtime
* @param int $targetBundleExists
* @param bool $expected
*/
function testNeedRebundling($uid,
$CaBundleMtime,
$systemWideMtime,
$targetBundleMtime,
$targetBundleExists,
$expected
) {
$view = $this->getMockBuilder(View::class)
->disableOriginalConstructor()->getMock();
$config = $this->createMock(IConfig::class);
/** @var CertificateManager | \PHPUnit_Framework_MockObject_MockObject $certificateManager */
$certificateManager = $this->getMockBuilder('OC\Security\CertificateManager')
->setConstructorArgs([$uid, $view, $config, $this->createMock(ILogger::class), $this->random])
->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
->getMock();
$certificateManager->expects($this->any())->method('getFilemtimeOfCaBundle')
->willReturn($CaBundleMtime);
$certificateManager->expects($this->at(1))->method('getCertificateBundle')
->with($uid)->willReturn('targetBundlePath');
$view->expects($this->any())->method('file_exists')
->with('targetBundlePath')
->willReturn($targetBundleExists);
if ($uid !== null && $targetBundleExists) {
$certificateManager->expects($this->at(2))->method('getCertificateBundle')
->with(null)->willReturn('SystemBundlePath');
}
$view->expects($this->any())->method('filemtime')
->willReturnCallback(function($path) use ($systemWideMtime, $targetBundleMtime) {
if ($path === 'SystemBundlePath') {
return $systemWideMtime;
} elseif ($path === 'targetBundlePath') {
return $targetBundleMtime;
}
throw new \Exception('unexpected path');
});
$this->assertSame($expected,
$this->invokePrivate($certificateManager, 'needsRebundling', [$uid])
);
}
function dataTestNeedRebundling() {
return [
//values: uid, CaBundleMtime, systemWideMtime, targetBundleMtime, targetBundleExists, expected
// compare minimum of CaBundleMtime and systemWideMtime with targetBundleMtime
['user1', 10, 20, 30, true, false],
['user1', 10, 20, 15, true, true],
['user1', 10, 5, 30, true, false],
['user1', 10, 5, 8, true, true],
// if no user exists we ignore 'systemWideMtime' because this is the bundle we re-build
[null, 10, 20, 30, true, false],
[null, 10, 20, 15, true, false],
[null, 10, 20, 8, true, true],
[null, 10, 5, 30, true, false],
[null, 10, 5, 8, true, true],
// if no target bundle exists we always build a new one
['user1', 10, 20, 30, false, true],
['user1', 10, 20, 15, false, true],
['user1', 10, 5, 30, false, true],
['user1', 10, 5, 8, false, true],
[null, 10, 20, 30, false, true],
[null, 10, 20, 15, false, true],
[null, 10, 20, 8, false, true],
[null, 10, 5, 30, false, true],
[null, 10, 5, 8, false, true],
];
}
}
|