Completed
Pull Request — stable8.2 (#24508)
by Joas
11:24
created

CheckSetupControllerTest.php ➔ version_compare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @author Lukas Reschke <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Settings\Controller;
23
24
use OCP\AppFramework\Http\DataResponse;
25
use OCP\Http\Client\IClientService;
26
use OCP\IConfig;
27
use OCP\IL10N;
28
use OCP\IRequest;
29
use OCP\IURLGenerator;
30
use OC_Util;
31
use Test\TestCase;
32
33
/**
34
 * Mock version_compare
35
 * @param string $version1
36
 * @param string $version2
37
 * @return int
38
 */
39
function version_compare($version1, $version2) {
0 ignored issues
show
Unused Code introduced by
The parameter $version1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $version2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
	return CheckSetupControllerTest::$version_compare;
41
}
42
43
/**
44
 * Class CheckSetupControllerTest
45
 *
46
 * @package OC\Settings\Controller
47
 */
48
class CheckSetupControllerTest extends TestCase {
49
	/** @var int */
50
	public static $version_compare;
51
52
	/** @var CheckSetupController */
53
	private $checkSetupController;
54
	/** @var IRequest */
55
	private $request;
56
	/** @var IConfig */
57
	private $config;
58
	/** @var IClientService */
59
	private $clientService;
60
	/** @var IURLGenerator */
61
	private $urlGenerator;
62
	/** @var OC_Util */
63
	private $util;
64
	/** @var IL10N */
65
	private $l10n;
66
67 View Code Duplication
	public function setUp() {
68
		parent::setUp();
69
70
		$this->request = $this->getMockBuilder('\OCP\IRequest')
71
			->disableOriginalConstructor()->getMock();
72
		$this->config = $this->getMockBuilder('\OCP\IConfig')
73
			->disableOriginalConstructor()->getMock();
74
		$this->config = $this->getMockBuilder('\OCP\IConfig')
75
			->disableOriginalConstructor()->getMock();
76
		$this->clientService = $this->getMockBuilder('\OCP\Http\Client\IClientService')
77
			->disableOriginalConstructor()->getMock();
78
		$this->util = $this->getMockBuilder('\OC_Util')
79
			->disableOriginalConstructor()->getMock();
80
		$this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
81
			->disableOriginalConstructor()->getMock();
82
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
83
			->disableOriginalConstructor()->getMock();
84
		$this->l10n->expects($this->any())
85
			->method('t')
86
			->will($this->returnCallback(function($message, array $replace) {
87
				return vsprintf($message, $replace);
88
			}));
89
		$this->checkSetupController = $this->getMockBuilder('\OC\Settings\Controller\CheckSetupController')
90
			->setConstructorArgs([
91
				'settings',
92
				$this->request,
93
				$this->config,
94
				$this->clientService,
95
				$this->urlGenerator,
96
				$this->util,
97
				$this->l10n,
98
				])
99
			->setMethods(['getCurlVersion'])->getMock();
100
	}
101
102 View Code Duplication
	public function testIsInternetConnectionWorkingDisabledViaConfig() {
103
		$this->config->expects($this->once())
104
			->method('getSystemValue')
105
			->with('has_internet_connection', true)
106
			->will($this->returnValue(false));
107
108
		$this->assertFalse(
109
			self::invokePrivate(
110
				$this->checkSetupController,
111
				'isInternetConnectionWorking'
112
			)
113
		);
114
	}
115
116 View Code Duplication
	public function testIsInternetConnectionWorkingCorrectly() {
117
		$this->config->expects($this->once())
118
			->method('getSystemValue')
119
			->with('has_internet_connection', true)
120
			->will($this->returnValue(true));
121
122
		$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
123
			->disableOriginalConstructor()->getMock();
124
		$client->expects($this->at(0))
125
			->method('get')
126
			->with('https://www.owncloud.org/', []);
127
		$client->expects($this->at(1))
128
			->method('get')
129
			->with('http://www.owncloud.org/', []);
130
131
		$this->clientService->expects($this->once())
132
			->method('newClient')
133
			->will($this->returnValue($client));
134
135
136
		$this->assertTrue(
137
			self::invokePrivate(
138
				$this->checkSetupController,
139
				'isInternetConnectionWorking'
140
			)
141
		);
142
	}
143
144
	public function testIsInternetConnectionHttpsFail() {
145
		$this->config->expects($this->once())
146
			->method('getSystemValue')
147
			->with('has_internet_connection', true)
148
			->will($this->returnValue(true));
149
150
		$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
151
			->disableOriginalConstructor()->getMock();
152
		$client->expects($this->at(0))
153
			->method('get')
154
			->with('https://www.owncloud.org/', [])
155
			->will($this->throwException(new \Exception()));
156
157
		$this->clientService->expects($this->once())
158
			->method('newClient')
159
			->will($this->returnValue($client));
160
161
		$this->assertFalse(
162
			self::invokePrivate(
163
				$this->checkSetupController,
164
				'isInternetConnectionWorking'
165
			)
166
		);
167
	}
168
169 View Code Duplication
	public function testIsInternetConnectionHttpFail() {
170
		$this->config->expects($this->once())
171
			->method('getSystemValue')
172
			->with('has_internet_connection', true)
173
			->will($this->returnValue(true));
174
175
		$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
176
			->disableOriginalConstructor()->getMock();
177
		$client->expects($this->at(0))
178
			->method('get')
179
			->with('https://www.owncloud.org/', []);
180
		$client->expects($this->at(1))
181
			->method('get')
182
			->with('http://www.owncloud.org/', [])
183
			->will($this->throwException(new \Exception()));
184
185
		$this->clientService->expects($this->once())
186
			->method('newClient')
187
			->will($this->returnValue($client));
188
189
		$this->assertFalse(
190
			self::invokePrivate(
191
				$this->checkSetupController,
192
				'isInternetConnectionWorking'
193
			)
194
		);
195
	}
196
197 View Code Duplication
	public function testIsMemcacheConfiguredFalse() {
198
		$this->config->expects($this->once())
199
			->method('getSystemValue')
200
			->with('memcache.local', null)
201
			->will($this->returnValue(null));
202
203
		$this->assertFalse(
204
			self::invokePrivate(
205
				$this->checkSetupController,
206
				'isMemcacheConfigured'
207
			)
208
		);
209
	}
210
211 View Code Duplication
	public function testIsMemcacheConfiguredTrue() {
212
		$this->config->expects($this->once())
213
			->method('getSystemValue')
214
			->with('memcache.local', null)
215
			->will($this->returnValue('SomeProvider'));
216
217
		$this->assertTrue(
218
			self::invokePrivate(
219
				$this->checkSetupController,
220
				'isMemcacheConfigured'
221
			)
222
		);
223
	}
224
225
	public function testIsPhpSupportedFalse() {
226
		self::$version_compare = -1;
227
228
		$this->assertEquals(
229
			['eol' => true, 'version' => PHP_VERSION],
230
			self::invokePrivate($this->checkSetupController, 'isPhpSupported')
231
		);
232
	}
233
234
	public function testIsPhpSupportedTrue() {
235
		self::$version_compare = 0;
236
237
		$this->assertEquals(
238
			['eol' => false, 'version' => PHP_VERSION],
239
			self::invokePrivate($this->checkSetupController, 'isPhpSupported')
240
		);
241
242
243
		self::$version_compare = 1;
244
245
		$this->assertEquals(
246
			['eol' => false, 'version' => PHP_VERSION],
247
			self::invokePrivate($this->checkSetupController, 'isPhpSupported')
248
		);
249
	}
250
251 View Code Duplication
	public function testForwardedForHeadersWorkingFalse() {
252
		$this->config->expects($this->once())
253
			->method('getSystemValue')
254
			->with('trusted_proxies', [])
255
			->willReturn(['1.2.3.4']);
256
		$this->request->expects($this->once())
257
			->method('getRemoteAddress')
258
			->willReturn('1.2.3.4');
259
260
		$this->assertFalse(
261
			self::invokePrivate(
262
				$this->checkSetupController,
263
				'forwardedForHeadersWorking'
264
			)
265
		);
266
	}
267
268 View Code Duplication
	public function testForwardedForHeadersWorkingTrue() {
269
		$this->config->expects($this->once())
270
			->method('getSystemValue')
271
			->with('trusted_proxies', [])
272
			->willReturn(['1.2.3.4']);
273
		$this->request->expects($this->once())
274
			->method('getRemoteAddress')
275
			->willReturn('4.3.2.1');
276
277
		$this->assertTrue(
278
			self::invokePrivate(
279
				$this->checkSetupController,
280
				'forwardedForHeadersWorking'
281
			)
282
		);
283
	}
284
285
	public function testCheck() {
286
		$this->config->expects($this->at(0))
287
			->method('getSystemValue')
288
			->with('has_internet_connection', true)
289
			->will($this->returnValue(true));
290
		$this->config->expects($this->at(1))
291
			->method('getSystemValue')
292
			->with('memcache.local', null)
293
			->will($this->returnValue('SomeProvider'));
294
		$this->config->expects($this->at(2))
295
			->method('getSystemValue')
296
			->with('has_internet_connection', true)
297
			->will($this->returnValue(false));
298
		$this->config->expects($this->at(3))
299
			->method('getSystemValue')
300
			->with('trusted_proxies', [])
301
			->willReturn(['1.2.3.4']);
302
303
		$this->request->expects($this->once())
304
			->method('getRemoteAddress')
305
			->willReturn('4.3.2.1');
306
307
		$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
308
			->disableOriginalConstructor()->getMock();
309
		$client->expects($this->at(0))
310
			->method('get')
311
			->with('https://www.owncloud.org/', []);
312
		$client->expects($this->at(1))
313
			->method('get')
314
			->with('http://www.owncloud.org/', [])
315
			->will($this->throwException(new \Exception()));
316
317
		$this->clientService->expects($this->once())
318
			->method('newClient')
319
			->will($this->returnValue($client));
320
321
		$this->util->expects($this->once())
322
			->method('isHtaccessWorking')
323
			->will($this->returnValue(true));
324
		$this->urlGenerator->expects($this->at(0))
325
			->method('linkToDocs')
326
			->with('admin-performance')
327
			->willReturn('http://doc.owncloud.org/server/go.php?to=admin-performance');
328
		$this->urlGenerator->expects($this->at(1))
329
			->method('linkToDocs')
330
			->with('admin-security')
331
			->willReturn('https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/hardening.html');
332
		self::$version_compare = -1;
333
		$this->urlGenerator->expects($this->at(2))
334
			->method('linkToDocs')
335
			->with('admin-reverse-proxy')
336
			->willReturn('reverse-proxy-doc-link');
337
338
		$expected = new DataResponse(
339
			[
340
				'serverHasInternetConnection' => false,
341
				'dataDirectoryProtected' => true,
342
				'isMemcacheConfigured' => true,
343
				'memcacheDocs' => 'http://doc.owncloud.org/server/go.php?to=admin-performance',
344
				'isUrandomAvailable' => self::invokePrivate($this->checkSetupController, 'isUrandomAvailable'),
345
				'securityDocs' => 'https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/hardening.html',
346
				'isUsedTlsLibOutdated' => '',
347
				'phpSupported' => [
348
					'eol' => true,
349
					'version' => PHP_VERSION
350
				],
351
				'forwardedForHeadersWorking' => true,
352
				'reverseProxyDocs' => 'reverse-proxy-doc-link',
353
				'isCorrectMemcachedPHPModuleInstalled' => true,
354
			]
355
		);
356
		$this->assertEquals($expected, $this->checkSetupController->check());
357
	}
358
359
	public function testGetCurlVersion() {
360
		$checkSetupController = $this->getMockBuilder('\OC\Settings\Controller\CheckSetupController')
361
			->setConstructorArgs([
362
				'settings',
363
				$this->request,
364
				$this->config,
365
				$this->clientService,
366
				$this->urlGenerator,
367
				$this->util,
368
				$this->l10n,
369
			])
370
			->setMethods(null)->getMock();
371
372
		$this->assertArrayHasKey('ssl_version', $this->invokePrivate($checkSetupController, 'getCurlVersion'));
373
	}
374
375 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithAnotherLibrary() {
376
		$this->config->expects($this->any())
377
			->method('getSystemValue')
378
			->will($this->returnValue(true));
379
		$this->checkSetupController
380
			->expects($this->once())
381
			->method('getCurlVersion')
382
			->will($this->returnValue(['ssl_version' => 'SSLlib']));
383
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
384
	}
385
386 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithMisbehavingCurl() {
387
		$this->config->expects($this->any())
388
			->method('getSystemValue')
389
			->will($this->returnValue(true));
390
		$this->checkSetupController
391
			->expects($this->once())
392
			->method('getCurlVersion')
393
			->will($this->returnValue([]));
394
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
395
	}
396
397 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithOlderOpenSsl() {
398
		$this->config->expects($this->any())
399
			->method('getSystemValue')
400
			->will($this->returnValue(true));
401
		$this->checkSetupController
402
			->expects($this->once())
403
			->method('getCurlVersion')
404
			->will($this->returnValue(['ssl_version' => 'OpenSSL/1.0.1c']));
405
		$this->assertSame('cURL is using an outdated OpenSSL version (OpenSSL/1.0.1c). Please update your operating system or features such as installing and updating apps via the app store or Federated Cloud Sharing will not work reliably.', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
406
	}
407
408 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithOlderOpenSslAndWithoutAppstore() {
409
		$this->config
410
			->expects($this->at(0))
411
			->method('getSystemValue')
412
			->with('has_internet_connection', true)
413
			->will($this->returnValue(true));
414
		$this->checkSetupController
415
			->expects($this->once())
416
			->method('getCurlVersion')
417
			->will($this->returnValue(['ssl_version' => 'OpenSSL/1.0.1c']));
418
		$this->assertSame('cURL is using an outdated OpenSSL version (OpenSSL/1.0.1c). Please update your operating system or features such as Federated Cloud Sharing will not work reliably.', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
419
	}
420
421 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithOlderOpenSsl1() {
422
		$this->config->expects($this->any())
423
			->method('getSystemValue')
424
			->will($this->returnValue(true));
425
		$this->checkSetupController
426
			->expects($this->once())
427
			->method('getCurlVersion')
428
			->will($this->returnValue(['ssl_version' => 'OpenSSL/1.0.2a']));
429
		$this->assertSame('cURL is using an outdated OpenSSL version (OpenSSL/1.0.2a). Please update your operating system or features such as installing and updating apps via the app store or Federated Cloud Sharing will not work reliably.', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
430
	}
431
432 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithMatchingOpenSslVersion() {
433
		$this->config->expects($this->any())
434
			->method('getSystemValue')
435
			->will($this->returnValue(true));
436
		$this->checkSetupController
437
			->expects($this->once())
438
			->method('getCurlVersion')
439
			->will($this->returnValue(['ssl_version' => 'OpenSSL/1.0.1d']));
440
			$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
441
	}
442
443 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithMatchingOpenSslVersion1() {
444
		$this->config->expects($this->any())
445
			->method('getSystemValue')
446
			->will($this->returnValue(true));
447
		$this->checkSetupController
448
			->expects($this->once())
449
			->method('getCurlVersion')
450
			->will($this->returnValue(['ssl_version' => 'OpenSSL/1.0.2b']));
451
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
452
	}
453
454 View Code Duplication
	public function testIsBuggyNss400() {
455
		$this->config->expects($this->any())
456
			->method('getSystemValue')
457
			->will($this->returnValue(true));
458
		$this->checkSetupController
459
			->expects($this->once())
460
			->method('getCurlVersion')
461
			->will($this->returnValue(['ssl_version' => 'NSS/1.0.2b']));
462
		$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
463
			->disableOriginalConstructor()->getMock();
464
		$exception = $this->getMockBuilder('\GuzzleHttp\Exception\ClientException')
465
			->disableOriginalConstructor()->getMock();
466
		$response = $this->getMockBuilder('\GuzzleHttp\Message\ResponseInterface')
467
			->disableOriginalConstructor()->getMock();
468
		$response->expects($this->once())
469
			->method('getStatusCode')
470
			->will($this->returnValue(400));
471
		$exception->expects($this->once())
472
			->method('getResponse')
473
			->will($this->returnValue($response));
474
475
		$client->expects($this->at(0))
476
			->method('get')
477
			->with('https://www.owncloud.org/', [])
478
			->will($this->throwException($exception));
479
480
		$this->clientService->expects($this->once())
481
			->method('newClient')
482
			->will($this->returnValue($client));
483
484
		$this->assertSame('cURL is using an outdated NSS version (NSS/1.0.2b). Please update your operating system or features such as installing and updating apps via the app store or Federated Cloud Sharing will not work reliably.', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
485
	}
486
487
488 View Code Duplication
	public function testIsBuggyNss200() {
489
		$this->config->expects($this->any())
490
			->method('getSystemValue')
491
			->will($this->returnValue(true));
492
		$this->checkSetupController
493
			->expects($this->once())
494
			->method('getCurlVersion')
495
			->will($this->returnValue(['ssl_version' => 'NSS/1.0.2b']));
496
		$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
497
			->disableOriginalConstructor()->getMock();
498
		$exception = $this->getMockBuilder('\GuzzleHttp\Exception\ClientException')
499
			->disableOriginalConstructor()->getMock();
500
		$response = $this->getMockBuilder('\GuzzleHttp\Message\ResponseInterface')
501
			->disableOriginalConstructor()->getMock();
502
		$response->expects($this->once())
503
			->method('getStatusCode')
504
			->will($this->returnValue(200));
505
		$exception->expects($this->once())
506
			->method('getResponse')
507
			->will($this->returnValue($response));
508
509
		$client->expects($this->at(0))
510
			->method('get')
511
			->with('https://www.owncloud.org/', [])
512
			->will($this->throwException($exception));
513
514
		$this->clientService->expects($this->once())
515
			->method('newClient')
516
			->will($this->returnValue($client));
517
518
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
519
	}
520
521 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithInternetDisabled() {
522
		$this->config
523
			->expects($this->at(0))
524
			->method('getSystemValue')
525
			->with('has_internet_connection', true)
526
			->will($this->returnValue(false));
527
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
528
	}
529
530 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithAppstoreDisabledAndServerToServerSharingEnabled() {
531
		// Appstore is disabled by default in EE
532
		$appStoreDefault = false;
533
		if (\OC_Util::getEditionString() === '') {
534
			$appStoreDefault = true;
535
		}
536
537
		$this->config
538
			->expects($this->at(0))
539
			->method('getSystemValue')
540
			->with('has_internet_connection', true)
541
			->will($this->returnValue(true));
542
		$this->config
543
			->expects($this->at(1))
544
			->method('getSystemValue')
545
			->with('appstoreenabled', $appStoreDefault)
546
			->will($this->returnValue(false));
547
		$this->config
548
			->expects($this->at(2))
549
			->method('getAppValue')
550
			->with('files_sharing', 'outgoing_server2server_share_enabled', 'yes')
551
			->will($this->returnValue('no'));
552
		$this->config
553
			->expects($this->at(3))
554
			->method('getAppValue')
555
			->with('files_sharing', 'incoming_server2server_share_enabled', 'yes')
556
			->will($this->returnValue('yes'));
557
558
		$this->checkSetupController
559
			->expects($this->once())
560
			->method('getCurlVersion')
561
			->will($this->returnValue([]));
562
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
563
	}
564
565 View Code Duplication
	public function testIsUsedTlsLibOutdatedWithAppstoreDisabledAndServerToServerSharingDisabled() {
566
		// Appstore is disabled by default in EE
567
		$appStoreDefault = false;
568
		if (\OC_Util::getEditionString() === '') {
569
			$appStoreDefault = true;
570
		}
571
572
		$this->config
573
			->expects($this->at(0))
574
			->method('getSystemValue')
575
			->with('has_internet_connection', true)
576
			->will($this->returnValue(true));
577
		$this->config
578
			->expects($this->at(1))
579
			->method('getSystemValue')
580
			->with('appstoreenabled', $appStoreDefault)
581
			->will($this->returnValue(false));
582
		$this->config
583
			->expects($this->at(2))
584
			->method('getAppValue')
585
			->with('files_sharing', 'outgoing_server2server_share_enabled', 'yes')
586
			->will($this->returnValue('no'));
587
		$this->config
588
			->expects($this->at(3))
589
			->method('getAppValue')
590
			->with('files_sharing', 'incoming_server2server_share_enabled', 'yes')
591
			->will($this->returnValue('no'));
592
593
		$this->checkSetupController
594
			->expects($this->never())
595
			->method('getCurlVersion')
596
			->will($this->returnValue([]));
597
		$this->assertSame('', $this->invokePrivate($this->checkSetupController, 'isUsedTlsLibOutdated'));
598
	}
599
}
600