|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\CryptoEncoding; |
|
6
|
|
|
|
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use PHPUnit\Framework\Attributes\Depends; |
|
9
|
|
|
use PHPUnit\Framework\Attributes\Group; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM; |
|
12
|
|
|
use SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle; |
|
13
|
|
|
use SimpleSAML\XMLSecurity\Exception\IOException; |
|
14
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
use function dirname; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @internal |
|
20
|
|
|
*/ |
|
21
|
|
|
#[Group('pem')] |
|
22
|
|
|
class PEMBundleTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
private static string $baseDir; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function setUpBeforeClass(): void |
|
30
|
|
|
{ |
|
31
|
|
|
self::$baseDir = dirname(__FILE__, 3); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testBundle(): PEMBundle |
|
39
|
|
|
{ |
|
40
|
|
|
$bundle = PEMBundle::fromFile(self::$baseDir . '/resources/certificates/cacert.pem'); |
|
41
|
|
|
$this->assertInstanceOf(PEMBundle::class, $bundle); |
|
42
|
|
|
return $bundle; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
48
|
|
|
*/ |
|
49
|
|
|
#[Depends('testBundle')] |
|
50
|
|
|
public function testAll(PEMBundle $bundle): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->assertContainsOnlyInstancesOf(PEM::class, $bundle->all()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
58
|
|
|
*/ |
|
59
|
|
|
#[Depends('testBundle')] |
|
60
|
|
|
public function testFirst(PEMBundle $bundle): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->assertInstanceOf(PEM::class, $bundle->first()); |
|
63
|
|
|
$this->assertEquals($bundle->all()[0], $bundle->first()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
69
|
|
|
*/ |
|
70
|
|
|
#[Depends('testBundle')] |
|
71
|
|
|
public function testLast(PEMBundle $bundle): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->assertInstanceOf(PEM::class, $bundle->last()); |
|
74
|
|
|
$this->assertEquals($bundle->all()[149], $bundle->last()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
80
|
|
|
*/ |
|
81
|
|
|
#[Depends('testBundle')] |
|
82
|
|
|
public function testCount(PEMBundle $bundle): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertCount(150, $bundle); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
90
|
|
|
*/ |
|
91
|
|
|
#[Depends('testBundle')] |
|
92
|
|
|
public function testIterator(PEMBundle $bundle): void |
|
93
|
|
|
{ |
|
94
|
|
|
$values = []; |
|
95
|
|
|
foreach ($bundle as $pem) { |
|
96
|
|
|
$values[] = $pem; |
|
97
|
|
|
} |
|
98
|
|
|
$this->assertContainsOnlyInstancesOf(PEM::class, $values); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
104
|
|
|
*/ |
|
105
|
|
|
#[Depends('testBundle')] |
|
106
|
|
|
public function testString(PEMBundle $bundle): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->assertIsString($bundle->string()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
114
|
|
|
*/ |
|
115
|
|
|
#[Depends('testBundle')] |
|
116
|
|
|
public function testToString(PEMBundle $bundle): void |
|
117
|
|
|
{ |
|
118
|
|
|
$this->assertIsString(strval($bundle)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
public function testInvalidPEM(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->expectException(UnexpectedValueException::class); |
|
125
|
|
|
PEMBundle::fromString('invalid'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
public function testInvalidPEMData(): void |
|
130
|
|
|
{ |
|
131
|
|
|
$str = <<<'DATA' |
|
132
|
|
|
-----BEGIN TEST----- |
|
133
|
|
|
%%% |
|
134
|
|
|
-----END TEST----- |
|
135
|
|
|
DATA; |
|
136
|
|
|
$this->expectException(UnexpectedValueException::class); |
|
137
|
|
|
PEMBundle::fromString($str); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
public function testInvalidFile(): void |
|
142
|
|
|
{ |
|
143
|
|
|
$this->expectException(IOException::class); |
|
144
|
|
|
PEMBundle::fromFile('/phpunit/some/nonexistent'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
public function testFirstEmptyFail(): void |
|
149
|
|
|
{ |
|
150
|
|
|
$bundle = new PEMBundle(); |
|
151
|
|
|
$this->expectException(LogicException::class); |
|
152
|
|
|
$bundle->first(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
public function testLastEmptyFail(): void |
|
157
|
|
|
{ |
|
158
|
|
|
$bundle = new PEMBundle(); |
|
159
|
|
|
$this->expectException(LogicException::class); |
|
160
|
|
|
$bundle->last(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle |
|
166
|
|
|
*/ |
|
167
|
|
|
#[Depends('testBundle')] |
|
168
|
|
|
public function testWithPEMs(PEMBundle $bundle): void |
|
169
|
|
|
{ |
|
170
|
|
|
$bundle = $bundle->withPEMs(new PEM('TEST', 'data')); |
|
171
|
|
|
$this->assertCount(151, $bundle); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|