|
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(); |
|
|
|
|
|
|
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
|
|
|
|
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:
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: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: