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.
|
2014-08-31 15:16:28 +04:00
|
|
|
*/
|
2014-08-27 18:28:51 +04:00
|
|
|
|
|
|
|
use \OC\Security\Certificate;
|
|
|
|
|
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;
|
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
protected function setUp() {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage Certificate could not get parsed.
|
|
|
|
*/
|
|
|
|
function testBogusData() {
|
|
|
|
new Certificate('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetName() {
|
|
|
|
$this->assertSame('GoodCertificate', $this->goodCertificate->getName());
|
|
|
|
$this->assertSame('BadCertificate', $this->invalidCertificate->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetCommonName() {
|
|
|
|
$this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
|
|
|
|
$this->assertSame(null, $this->invalidCertificate->getCommonName());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetOrganization() {
|
|
|
|
$this->assertSame('ownCloud Inc.', $this->goodCertificate->getOrganization());
|
|
|
|
$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetIssueDate() {
|
2014-08-31 15:16:28 +04:00
|
|
|
$expected = new DateTime('2014-08-27 08:45:52 GMT');
|
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
|
|
|
|
$expected = new DateTime('2014-08-27 08:48:51 GMT');
|
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetExpireDate() {
|
2014-08-31 15:16:28 +04:00
|
|
|
$expected = new DateTime('2015-08-27 08:45:52 GMT');
|
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
|
|
|
|
$expected = new DateTime('2015-08-27 08:48:51 GMT');
|
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
|
|
|
|
$expected = new DateTime('2014-08-28 09:12:43 GMT');
|
|
|
|
$this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
|
2014-08-27 18:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obviously the following test case might fail after 2015-08-27, just create a new certificate with longer validity then
|
|
|
|
*/
|
|
|
|
function testIsExpired() {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
function testGetIssuerName() {
|
|
|
|
$this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
|
|
|
|
$this->assertSame(null, $this->invalidCertificate->getIssuerName());
|
|
|
|
$this->assertSame(null, $this->expiredCertificate->getIssuerName());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetIssuerOrganization() {
|
|
|
|
$this->assertSame('ownCloud Inc.', $this->goodCertificate->getIssuerOrganization());
|
|
|
|
$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
|
|
|
}
|