2014-08-27 18:28:51 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-04-15 14:33:37 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-08-31 15:16:28 +04:00
|
|
|
*/
|
2014-08-27 18:28:51 +04:00
|
|
|
|
2016-05-19 10:02:58 +03:00
|
|
|
namespace Test\Security;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Security\Certificate;
|
2014-08-27 18:28:51 +04:00
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
class CertificateTest extends \Test\TestCase {
|
2014-08-27 18:28:51 +04:00
|
|
|
|
|
|
|
/** @var Certificate That contains a valid certificate */
|
|
|
|
protected $goodCertificate;
|
|
|
|
/** @var Certificate That contains an invalid certificate */
|
|
|
|
protected $invalidCertificate;
|
|
|
|
/** @var Certificate That contains an expired certificate */
|
|
|
|
protected $expiredCertificate;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2014-08-31 15:16:28 +04:00
|
|
|
$goodCertificate = file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt');
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->goodCertificate = new Certificate($goodCertificate, 'GoodCertificate');
|
2014-08-31 15:16:28 +04:00
|
|
|
$badCertificate = file_get_contents(__DIR__ . '/../../data/certificates/badCertificate.crt');
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->invalidCertificate = new Certificate($badCertificate, 'BadCertificate');
|
2014-08-31 15:16:28 +04:00
|
|
|
$expiredCertificate = file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt');
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testBogusData() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Certificate could not get parsed.');
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
$certificate = new Certificate('foo', 'bar');
|
|
|
|
$certificate->getIssueDate();
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-04-21 19:55:33 +03:00
|
|
|
function testCertificateStartingWithFileReference() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Certificate could not get parsed.');
|
|
|
|
|
2016-04-21 19:55:33 +03:00
|
|
|
new Certificate('file://'.__DIR__ . '/../../data/certificates/goodCertificate.crt', 'bar');
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetName() {
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertSame('GoodCertificate', $this->goodCertificate->getName());
|
|
|
|
$this->assertSame('BadCertificate', $this->invalidCertificate->getName());
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetCommonName() {
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
|
|
|
|
$this->assertSame(null, $this->invalidCertificate->getCommonName());
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetOrganization() {
|
2015-08-27 23:23:08 +03:00
|
|
|
$this->assertSame('ownCloud Security', $this->goodCertificate->getOrganization());
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetIssueDate() {
|
2016-05-19 10:40:34 +03:00
|
|
|
$expected = new \DateTime('2015-08-27 20:03:42 GMT');
|
2014-08-31 15:16:28 +04:00
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
|
2016-05-19 10:40:34 +03:00
|
|
|
$expected = new \DateTime('2015-08-27 20:19:13 GMT');
|
2014-08-31 15:16:28 +04:00
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetExpireDate() {
|
2016-05-19 10:40:34 +03:00
|
|
|
$expected = new \DateTime('2025-08-24 20:03:42 GMT');
|
2014-08-31 15:16:28 +04:00
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
|
2016-05-19 10:40:34 +03:00
|
|
|
$expected = new \DateTime('2025-08-24 20:19:13 GMT');
|
2014-08-31 15:16:28 +04:00
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
|
2016-05-19 10:40:34 +03:00
|
|
|
$expected = new \DateTime('2014-08-28 09:12:43 GMT');
|
2014-08-31 15:16:28 +04:00
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testIsExpired() {
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertSame(false, $this->goodCertificate->isExpired());
|
|
|
|
$this->assertSame(false, $this->invalidCertificate->isExpired());
|
2014-08-27 18:29:42 +04:00
|
|
|
$this->assertSame(true, $this->expiredCertificate->isExpired());
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetIssuerName() {
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
|
|
|
|
$this->assertSame(null, $this->invalidCertificate->getIssuerName());
|
|
|
|
$this->assertSame(null, $this->expiredCertificate->getIssuerName());
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:33:37 +03:00
|
|
|
public function testGetIssuerOrganization() {
|
2015-08-27 23:23:08 +03:00
|
|
|
$this->assertSame('ownCloud Security', $this->goodCertificate->getIssuerOrganization());
|
2014-08-27 18:28:51 +04:00
|
|
|
$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
|
|
|
|
$this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
|
|
|
|
}
|
2014-08-31 13:06:18 +04:00
|
|
|
}
|