Completed
Pull Request — stable8 (#24168)
by Lukas
09:47
created

testCertificateStartingWithFileReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2014 Lukas Reschke <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
use \OC\Security\Certificate;
10
11
class CertificateTest extends \Test\TestCase {
12
13
	/** @var Certificate That contains a valid certificate */
14
	protected $goodCertificate;
15
	/** @var Certificate That contains an invalid certificate */
16
	protected $invalidCertificate;
17
	/** @var Certificate That contains an expired certificate */
18
	protected $expiredCertificate;
19
20
	protected function setUp() {
21
		parent::setUp();
22
23
		$goodCertificate = file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt');
24
		$this->goodCertificate = new Certificate($goodCertificate, 'GoodCertificate');
25
		$badCertificate = file_get_contents(__DIR__ . '/../../data/certificates/badCertificate.crt');
26
		$this->invalidCertificate = new Certificate($badCertificate, 'BadCertificate');
27
		$expiredCertificate = file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt');
28
		$this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
29
	}
30
31
	/**
32
	 * @expectedException \Exception
33
	 * @expectedExceptionMessage Certificate could not get parsed.
34
	 */
35
	function testBogusData() {
36
		new Certificate('foo', 'bar');
37
	}
38
39
	/**
40
	 * @expectedException \Exception
41
	 * @expectedExceptionMessage Certificate could not get parsed.
42
	 */
43
	function testCertificateStartingWithFileReference() {
44
		new Certificate('file://'.__DIR__ . '/../../data/certificates/goodCertificate.crt', 'bar');
45
	}
46
47
	function testGetName() {
48
		$this->assertSame('GoodCertificate', $this->goodCertificate->getName());
49
		$this->assertSame('BadCertificate', $this->invalidCertificate->getName());
50
	}
51
52
	function testGetCommonName() {
53
		$this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
54
		$this->assertSame(null, $this->invalidCertificate->getCommonName());
55
	}
56
57
	public function testGetOrganization() {
58
		$this->assertSame('ownCloud Security', $this->goodCertificate->getOrganization());
59
		$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
60
	}
61
62
	public function testGetIssueDate() {
63
		$expected = new DateTime('2015-08-27 20:03:42 GMT');
64
		$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
65
		$expected = new DateTime('2015-08-27 20:19:13 GMT');
66
		$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
67
	}
68
69
	public function testGetExpireDate() {
70
		$expected = new DateTime('2025-08-24 20:03:42 GMT');
71
		$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
72
		$expected = new DateTime('2025-08-24 20:19:13 GMT');
73
		$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
74
		$expected = new DateTime('2014-08-28 09:12:43 GMT');
75
		$this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
76
	}
77
78
	public function testIsExpired() {
79
		$this->assertSame(false, $this->goodCertificate->isExpired());
80
		$this->assertSame(false, $this->invalidCertificate->isExpired());
81
		$this->assertSame(true, $this->expiredCertificate->isExpired());
82
	}
83
84
	function testGetIssuerName() {
85
		$this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
86
		$this->assertSame(null, $this->invalidCertificate->getIssuerName());
87
		$this->assertSame(null, $this->expiredCertificate->getIssuerName());
88
	}
89
90
	public function testGetIssuerOrganization() {
91
		$this->assertSame('ownCloud Security', $this->goodCertificate->getIssuerOrganization());
92
		$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
93
		$this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
94
	}
95
}
96