Completed
Pull Request — stable8.2 (#24197)
by Joas
11:58
created

CertificateTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 86
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testBogusData() 0 4 1
A setUp() 0 10 1
A testCertificateStartingWithFileReference() 0 3 1
A testGetName() 0 4 1
A testGetCommonName() 0 4 1
A testGetOrganization() 0 4 1
A testGetIssueDate() 0 6 1
A testGetExpireDate() 0 8 1
A testIsExpired() 0 5 1
A testGetIssuerName() 0 5 1
A testGetIssuerOrganization() 0 5 1
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
use \OC\Security\Certificate;
23
24
class CertificateTest extends \Test\TestCase {
25
26
	/** @var Certificate That contains a valid certificate */
27
	protected $goodCertificate;
28
	/** @var Certificate That contains an invalid certificate */
29
	protected $invalidCertificate;
30
	/** @var Certificate That contains an expired certificate */
31
	protected $expiredCertificate;
32
33
	protected function setUp() {
34
		parent::setUp();
35
36
		$goodCertificate = file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt');
37
		$this->goodCertificate = new Certificate($goodCertificate, 'GoodCertificate');
38
		$badCertificate = file_get_contents(__DIR__ . '/../../data/certificates/badCertificate.crt');
39
		$this->invalidCertificate = new Certificate($badCertificate, 'BadCertificate');
40
		$expiredCertificate = file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt');
41
		$this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
42
	}
43
44
	/**
45
	 * @expectedException \Exception
46
	 * @expectedExceptionMessage Certificate could not get parsed.
47
	 */
48
	public function testBogusData() {
49
		$certificate = new Certificate('foo', 'bar');
50
		$certificate->getIssueDate();
0 ignored issues
show
Unused Code introduced by
The call to the method OC\Security\Certificate::getIssueDate() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
51
	}
52
53
	/**
54
	 * @expectedException \Exception
55
	 * @expectedExceptionMessage Certificate could not get parsed.
56
	 */
57
	function testCertificateStartingWithFileReference() {
58
		new Certificate('file://'.__DIR__ . '/../../data/certificates/goodCertificate.crt', 'bar');
59
	}
60
61
	public function testGetName() {
62
		$this->assertSame('GoodCertificate', $this->goodCertificate->getName());
63
		$this->assertSame('BadCertificate', $this->invalidCertificate->getName());
64
	}
65
66
	public function testGetCommonName() {
67
		$this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
68
		$this->assertSame(null, $this->invalidCertificate->getCommonName());
69
	}
70
71
	public function testGetOrganization() {
72
		$this->assertSame('ownCloud Security', $this->goodCertificate->getOrganization());
73
		$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
74
	}
75
76
	public function testGetIssueDate() {
77
		$expected = new DateTime('2015-08-27 20:03:42 GMT');
78
		$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
79
		$expected = new DateTime('2015-08-27 20:19:13 GMT');
80
		$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
81
	}
82
83
	public function testGetExpireDate() {
84
		$expected = new DateTime('2025-08-24 20:03:42 GMT');
85
		$this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
86
		$expected = new DateTime('2025-08-24 20:19:13 GMT');
87
		$this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
88
		$expected = new DateTime('2014-08-28 09:12:43 GMT');
89
		$this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
90
	}
91
92
	public function testIsExpired() {
93
		$this->assertSame(false, $this->goodCertificate->isExpired());
94
		$this->assertSame(false, $this->invalidCertificate->isExpired());
95
		$this->assertSame(true, $this->expiredCertificate->isExpired());
96
	}
97
98
	public function testGetIssuerName() {
99
		$this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
100
		$this->assertSame(null, $this->invalidCertificate->getIssuerName());
101
		$this->assertSame(null, $this->expiredCertificate->getIssuerName());
102
	}
103
104
	public function testGetIssuerOrganization() {
105
		$this->assertSame('ownCloud Security', $this->goodCertificate->getIssuerOrganization());
106
		$this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
107
		$this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
108
	}
109
}
110