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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Federation\Tests;
use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\Events\TrustedServerRemovedEvent;
use OCP\HintException;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class TrustedServersTest extends TestCase {
private TrustedServers $trustedServers;
private DbHandler&MockObject $dbHandler;
private IClientService&MockObject $httpClientService;
private IClient&MockObject $httpClient;
private IResponse&MockObject $response;
private LoggerInterface&MockObject $logger;
private IJobList&MockObject $jobList;
private ISecureRandom&MockObject $secureRandom;
private IConfig&MockObject $config;
private IEventDispatcher&MockObject $dispatcher;
private ITimeFactory&MockObject $timeFactory;
protected function setUp(): void {
parent::setUp();
$this->dbHandler = $this->createMock(DbHandler::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->httpClientService = $this->createMock(IClientService::class);
$this->httpClient = $this->createMock(IClient::class);
$this->response = $this->createMock(IResponse::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->jobList = $this->createMock(IJobList::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->trustedServers = new TrustedServers(
$this->dbHandler,
$this->httpClientService,
$this->logger,
$this->jobList,
$this->secureRandom,
$this->config,
$this->dispatcher,
$this->timeFactory
);
}
public function testAddServer(): void {
/** @var TrustedServers&MockObject $trustedServers */
$trustedServers = $this->getMockBuilder(TrustedServers::class)
->setConstructorArgs(
[
$this->dbHandler,
$this->httpClientService,
$this->logger,
$this->jobList,
$this->secureRandom,
$this->config,
$this->dispatcher,
$this->timeFactory
]
)
->onlyMethods(['updateProtocol'])
->getMock();
$trustedServers->expects($this->once())->method('updateProtocol')
->with('url')->willReturn('https://url');
$this->timeFactory->method('getTime')
->willReturn(1234567);
$this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
->willReturn(1);
$this->secureRandom->expects($this->once())->method('generate')
->willReturn('token');
$this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
$this->jobList->expects($this->once())->method('add')
->with(RequestSharedSecret::class,
['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
$this->assertSame(
1,
$trustedServers->addServer('url')
);
}
public function testAddSharedSecret(): void {
$this->dbHandler->expects($this->once())->method('addSharedSecret')
->with('url', 'secret');
$this->trustedServers->addSharedSecret('url', 'secret');
}
public function testGetSharedSecret(): void {
$this->dbHandler->expects($this->once())
->method('getSharedSecret')
->with('url')
->willReturn('secret');
$this->assertSame(
$this->trustedServers->getSharedSecret('url'),
'secret'
);
}
public function testRemoveServer(): void {
$id = 42;
$server = ['url_hash' => 'url_hash'];
$this->dbHandler->expects($this->once())->method('removeServer')->with($id);
$this->dbHandler->expects($this->once())->method('getServerById')->with($id)
->willReturn($server);
$this->dispatcher->expects($this->once())->method('dispatchTyped')
->willReturnCallback(
function ($event): void {
$this->assertSame(get_class($event), TrustedServerRemovedEvent::class);
/** @var \OCP\Federated\Events\TrustedServerRemovedEvent $event */
$this->assertSame('url_hash', $event->getUrlHash());
}
);
$this->trustedServers->removeServer($id);
}
public function testGetServers(): void {
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
$this->assertEquals(
['servers'],
$this->trustedServers->getServers()
);
}
public function testIsTrustedServer(): void {
$this->dbHandler->expects($this->once())
->method('serverExists')->with('url')
->willReturn(true);
$this->assertTrue(
$this->trustedServers->isTrustedServer('url')
);
}
public function testSetServerStatus(): void {
$this->dbHandler->expects($this->once())->method('setServerStatus')
->with('url', 1);
$this->trustedServers->setServerStatus('url', 1);
}
public function testGetServerStatus(): void {
$this->dbHandler->expects($this->once())->method('getServerStatus')
->with('url')->willReturn(1);
$this->assertSame(
$this->trustedServers->getServerStatus('url'),
1
);
}
/**
* @dataProvider dataTestIsNextcloudServer
*/
public function testIsNextcloudServer(int $statusCode, bool $isValidNextcloudVersion, bool $expected): void {
$server = 'server1';
/** @var TrustedServers&MockObject $trustedServers */
$trustedServers = $this->getMockBuilder(TrustedServers::class)
->setConstructorArgs(
[
$this->dbHandler,
$this->httpClientService,
$this->logger,
$this->jobList,
$this->secureRandom,
$this->config,
$this->dispatcher,
$this->timeFactory
]
)
->onlyMethods(['checkNextcloudVersion'])
->getMock();
$this->httpClientService->expects($this->once())->method('newClient')
->willReturn($this->httpClient);
$this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
->willReturn($this->response);
$this->response->expects($this->once())->method('getStatusCode')
->willReturn($statusCode);
if ($statusCode === 200) {
$this->response->expects($this->once())->method('getBody')
->willReturn('');
$trustedServers->expects($this->once())->method('checkNextcloudVersion')
->willReturn($isValidNextcloudVersion);
} else {
$trustedServers->expects($this->never())->method('checkNextcloudVersion');
}
$this->assertSame($expected,
$trustedServers->isNextcloudServer($server)
);
}
public static function dataTestIsNextcloudServer(): array {
return [
[200, true, true],
[200, false, false],
[404, true, false],
];
}
public function testIsNextcloudServerFail(): void {
$server = 'server1';
$this->httpClientService->expects($this->once())
->method('newClient')
->willReturn($this->httpClient);
$this->httpClient->expects($this->once())
->method('get')
->with($server . '/status.php')
->willThrowException(new \Exception('simulated exception'));
$this->assertFalse($this->trustedServers->isNextcloudServer($server));
}
/**
* @dataProvider dataTestCheckNextcloudVersion
*/
public function testCheckNextcloudVersion(string $status): void {
$this->assertTrue(self::invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]));
}
public static function dataTestCheckNextcloudVersion(): array {
return [
['{"version":"9.0.0"}'],
['{"version":"9.1.0"}']
];
}
/**
* @dataProvider dataTestCheckNextcloudVersionTooLow
*/
public function testCheckNextcloudVersionTooLow(string $status): void {
$this->expectException(HintException::class);
$this->expectExceptionMessage('Remote server version is too low. 9.0 is required.');
self::invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]);
}
public static function dataTestCheckNextcloudVersionTooLow(): array {
return [
['{"version":"8.2.3"}'],
];
}
/**
* @dataProvider dataTestUpdateProtocol
*/
public function testUpdateProtocol(string $url, string $expected): void {
$this->assertSame($expected,
self::invokePrivate($this->trustedServers, 'updateProtocol', [$url])
);
}
public static function dataTestUpdateProtocol(): array {
return [
['http://owncloud.org', 'http://owncloud.org'],
['https://owncloud.org', 'https://owncloud.org'],
['owncloud.org', 'https://owncloud.org'],
['httpserver', 'https://httpserver'],
];
}
}
|