2014-08-27 18:28:51 +04:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:02:58 +03:00
|
|
|
namespace Test\Security;
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
use OC\Files\View;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Security\CertificateManager;
|
2016-09-12 22:38:07 +03:00
|
|
|
use OCP\IConfig;
|
2017-01-09 16:26:31 +03:00
|
|
|
use OCP\ILogger;
|
2017-05-19 23:51:26 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2014-08-27 18:28:51 +04:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
|
|
|
* Class CertificateManagerTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*/
|
2014-11-07 11:46:37 +03:00
|
|
|
class CertificateManagerTest extends \Test\TestCase {
|
2015-12-22 19:42:28 +03:00
|
|
|
use \Test\Traits\UserTrait;
|
|
|
|
use \Test\Traits\MountProviderTrait;
|
2014-08-27 18:28:51 +04:00
|
|
|
|
|
|
|
/** @var CertificateManager */
|
|
|
|
private $certificateManager;
|
|
|
|
/** @var String */
|
|
|
|
private $username;
|
2017-05-19 23:51:26 +03:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $random;
|
2014-08-27 18:28:51 +04:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2014-11-07 11:46:37 +03:00
|
|
|
$this->username = $this->getUniqueID('', 20);
|
2015-12-22 19:42:28 +03:00
|
|
|
$this->createUser($this->username, '');
|
|
|
|
|
|
|
|
$storage = new \OC\Files\Storage\Temporary();
|
|
|
|
$this->registerMount($this->username, $storage, '/' . $this->username . '/');
|
2014-08-27 18:28:51 +04:00
|
|
|
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_Util::setupFS($this->username);
|
|
|
|
|
2016-09-12 22:38:07 +03:00
|
|
|
$config = $this->createMock(IConfig::class);
|
2015-08-06 17:51:31 +03:00
|
|
|
$config->expects($this->any())->method('getSystemValue')
|
|
|
|
->with('installed', false)->willReturn(true);
|
|
|
|
|
2017-05-19 23:51:26 +03:00
|
|
|
$this->random = $this->createMock(ISecureRandom::class);
|
|
|
|
$this->random->method('generate')
|
|
|
|
->willReturn('random');
|
|
|
|
|
|
|
|
$this->certificateManager = new CertificateManager(
|
|
|
|
new \OC\Files\View(),
|
|
|
|
$config,
|
|
|
|
$this->createMock(ILogger::class),
|
|
|
|
$this->random
|
|
|
|
);
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2015-12-17 17:59:23 +03:00
|
|
|
$user = \OC::$server->getUserManager()->get($this->username);
|
2015-12-22 19:42:28 +03:00
|
|
|
if ($user !== null) {
|
|
|
|
$user->delete();
|
|
|
|
}
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::tearDown();
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertEqualsArrays($expected, $actual) {
|
|
|
|
sort($expected);
|
|
|
|
sort($actual);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testListCertificates() {
|
2014-08-27 18:28:51 +04:00
|
|
|
// Test empty certificate bundle
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertSame([], $this->certificateManager->listCertificates());
|
2014-08-27 18:28:51 +04:00
|
|
|
|
|
|
|
// Add some certificates
|
2015-12-22 19:42:28 +03:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
|
2020-03-26 11:30:18 +03:00
|
|
|
$certificateStore = [];
|
2015-12-22 19:42:28 +03:00
|
|
|
$certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
|
|
|
|
|
|
|
|
// Add another certificates
|
2015-12-22 19:42:28 +03:00
|
|
|
$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');
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testAddInvalidCertificate() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Certificate could not get parsed.');
|
|
|
|
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
|
|
|
|
}
|
|
|
|
|
2015-04-16 11:48:25 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dangerousFileProvider() {
|
|
|
|
return [
|
|
|
|
['.htaccess'],
|
|
|
|
['../../foo.txt'],
|
|
|
|
['..\..\foo.txt'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-04-15 17:20:05 +03:00
|
|
|
/**
|
2015-04-16 11:48:25 +03:00
|
|
|
* @dataProvider dangerousFileProvider
|
|
|
|
* @param string $filename
|
2015-04-15 17:20:05 +03:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testAddDangerousFile($filename) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Filename is not valid');
|
|
|
|
|
2015-12-22 19:42:28 +03:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), $filename);
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testRemoveDangerousFile() {
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertFalse($this->certificateManager->removeCertificate('../../foo.txt'));
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testRemoveExistingFile() {
|
2015-12-22 19:42:28 +03:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertTrue($this->certificateManager->removeCertificate('GoodCertificate'));
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testGetCertificateBundle() {
|
2020-07-05 15:31:19 +03:00
|
|
|
$this->assertSame('/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2016-07-11 16:51:48 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestNeedRebundling
|
|
|
|
*
|
|
|
|
* @param int $CaBundleMtime
|
|
|
|
* @param int $targetBundleMtime
|
|
|
|
* @param int $targetBundleExists
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
2020-07-05 15:31:19 +03:00
|
|
|
public function testNeedRebundling($CaBundleMtime,
|
2016-07-11 16:51:48 +03:00
|
|
|
$targetBundleMtime,
|
|
|
|
$targetBundleExists,
|
|
|
|
$expected
|
|
|
|
) {
|
2017-10-26 14:46:16 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-11 16:51:48 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-12 22:38:07 +03:00
|
|
|
$config = $this->createMock(IConfig::class);
|
2016-07-11 16:51:48 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CertificateManager | \PHPUnit\Framework\MockObject\MockObject $certificateManager */
|
2016-07-11 16:51:48 +03:00
|
|
|
$certificateManager = $this->getMockBuilder('OC\Security\CertificateManager')
|
2020-07-05 15:31:19 +03:00
|
|
|
->setConstructorArgs([$view, $config, $this->createMock(ILogger::class), $this->random])
|
2016-07-11 16:51:48 +03:00
|
|
|
->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$certificateManager->expects($this->any())->method('getFilemtimeOfCaBundle')
|
|
|
|
->willReturn($CaBundleMtime);
|
|
|
|
|
2020-07-05 15:31:19 +03:00
|
|
|
$certificateManager->expects($this->at(0))->method('getCertificateBundle')
|
|
|
|
->willReturn('targetBundlePath');
|
2016-07-11 16:51:48 +03:00
|
|
|
|
|
|
|
$view->expects($this->any())->method('file_exists')
|
|
|
|
->with('targetBundlePath')
|
|
|
|
->willReturn($targetBundleExists);
|
|
|
|
|
|
|
|
|
|
|
|
$view->expects($this->any())->method('filemtime')
|
2020-07-05 15:31:19 +03:00
|
|
|
->willReturnCallback(function ($path) use ($targetBundleMtime) {
|
|
|
|
if ($path === 'targetBundlePath') {
|
2016-07-11 16:51:48 +03:00
|
|
|
return $targetBundleMtime;
|
|
|
|
}
|
|
|
|
throw new \Exception('unexpected path');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSame($expected,
|
2020-07-05 15:31:19 +03:00
|
|
|
$this->invokePrivate($certificateManager, 'needsRebundling')
|
2016-07-11 16:51:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function dataTestNeedRebundling() {
|
2016-07-11 16:51:48 +03:00
|
|
|
return [
|
2020-07-05 15:31:19 +03:00
|
|
|
//values: CaBundleMtime, targetBundleMtime, targetBundleExists, expected
|
2016-07-11 16:51:48 +03:00
|
|
|
|
2020-07-05 15:31:19 +03:00
|
|
|
[10, 30, true, false],
|
|
|
|
[10, 15, true, false],
|
|
|
|
[10, 8, true, true],
|
|
|
|
[10, 30, true, false],
|
|
|
|
[10, 8, true, true],
|
2016-07-11 16:51:48 +03:00
|
|
|
|
|
|
|
// if no target bundle exists we always build a new one
|
2020-07-05 15:31:19 +03:00
|
|
|
[10, 30, false, true],
|
|
|
|
[10, 15, false, true],
|
|
|
|
[10, 8, false, true],
|
|
|
|
[10, 30, false, true],
|
|
|
|
[10, 8, false, true],
|
2016-07-11 16:51:48 +03:00
|
|
|
];
|
|
|
|
}
|
2015-01-20 22:34:34 +03:00
|
|
|
}
|