1
|
|
|
<?php |
2
|
|
|
use Sop\CryptoEncoding\PEM; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @group pem |
6
|
|
|
*/ |
7
|
|
|
class PEMTest extends PHPUnit_Framework_TestCase |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
*/ |
11
|
|
|
public function testFromString() |
12
|
|
|
{ |
13
|
|
|
$str = file_get_contents(TEST_ASSETS_DIR . "/public_key.pem"); |
14
|
|
|
$pem = PEM::fromString($str); |
15
|
|
|
$this->assertInstanceOf(PEM::class, $pem); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @return \Sop\CryptoEncoding\PEM |
21
|
|
|
*/ |
22
|
|
|
public function testFromFile() |
23
|
|
|
{ |
24
|
|
|
$pem = PEM::fromFile(TEST_ASSETS_DIR . "/public_key.pem"); |
25
|
|
|
$this->assertInstanceOf(PEM::class, $pem); |
26
|
|
|
return $pem; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @depends testFromFile |
31
|
|
|
* |
32
|
|
|
* @param PEM $pem |
33
|
|
|
*/ |
34
|
|
|
public function testType(PEM $pem) |
35
|
|
|
{ |
36
|
|
|
$this->assertEquals(PEM::TYPE_PUBLIC_KEY, $pem->type()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
*/ |
41
|
|
|
public function testData() |
42
|
|
|
{ |
43
|
|
|
$data = "payload"; |
44
|
|
|
$encoded = base64_encode($data); |
45
|
|
|
$str = <<<DATA |
46
|
|
|
-----BEGIN TEST----- |
47
|
|
|
$encoded |
48
|
|
|
-----END TEST----- |
49
|
|
|
DATA; |
50
|
|
|
$this->assertEquals($data, PEM::fromString($str)->data()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @expectedException UnexpectedValueException |
55
|
|
|
*/ |
56
|
|
|
public function testInvalidPEM() |
57
|
|
|
{ |
58
|
|
|
PEM::fromString("invalid"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @expectedException UnexpectedValueException |
63
|
|
|
*/ |
64
|
|
|
public function testInvalidPEMData() |
65
|
|
|
{ |
66
|
|
|
$str = <<<DATA |
67
|
|
|
-----BEGIN TEST----- |
68
|
|
|
%%% |
69
|
|
|
-----END TEST----- |
70
|
|
|
DATA; |
71
|
|
|
PEM::fromString($str); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @expectedException RuntimeException |
76
|
|
|
*/ |
77
|
|
|
public function testInvalidFile() |
78
|
|
|
{ |
79
|
|
|
PEM::fromFile(TEST_ASSETS_DIR . "/nonexistent"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @depends testFromFile |
84
|
|
|
* |
85
|
|
|
* @param PEM $pem |
86
|
|
|
*/ |
87
|
|
|
public function testString(PEM $pem) |
88
|
|
|
{ |
89
|
|
|
$this->assertInternalType("string", $pem->string()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @depends testFromFile |
94
|
|
|
* |
95
|
|
|
* @param PEM $pem |
96
|
|
|
*/ |
97
|
|
|
public function testToString(PEM $pem) |
98
|
|
|
{ |
99
|
|
|
$this->assertInternalType("string", strval($pem)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.