1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Tests\Rdata; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Algorithms; |
17
|
|
|
use Badcow\DNS\Rdata\CERT; |
18
|
|
|
use Badcow\DNS\Rdata\Factory; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class CertTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $certificate; |
27
|
|
|
|
28
|
|
|
public function setUp(): void |
29
|
|
|
{ |
30
|
|
|
$certificate = file_get_contents(__DIR__.'/../Resources/google.com.cer'); |
31
|
|
|
$this->certificate = base64_decode(str_replace(['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----', "\r", "\n"], '', $certificate)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetType(): void |
35
|
|
|
{ |
36
|
|
|
$cert = new CERT(); |
37
|
|
|
$this->assertEquals('CERT', $cert->getType()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testGetTypeCode(): void |
41
|
|
|
{ |
42
|
|
|
$cert = new CERT(); |
43
|
|
|
$this->assertEquals(37, $cert->getTypeCode()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testToText(): void |
47
|
|
|
{ |
48
|
|
|
$cert = new CERT(); |
49
|
|
|
$cert->setCertificateType('PGP'); |
50
|
|
|
$cert->setKeyTag(65); |
51
|
|
|
$cert->setAlgorithm(Algorithms::ECC); |
52
|
|
|
$cert->setCertificate($this->certificate); |
53
|
|
|
|
54
|
|
|
$expectation = 'PGP 65 ECC '.base64_encode($this->certificate); |
55
|
|
|
|
56
|
|
|
$this->assertEquals($expectation, $cert->toText()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testWire(): void |
60
|
|
|
{ |
61
|
|
|
$cert = new CERT(); |
62
|
|
|
$cert->setCertificateType('PGP'); |
63
|
|
|
$cert->setKeyTag(65); |
64
|
|
|
$cert->setAlgorithm(Algorithms::ECC); |
65
|
|
|
$cert->setCertificate($this->certificate); |
66
|
|
|
|
67
|
|
|
$wireFormatted = $cert->toWire(); |
68
|
|
|
|
69
|
|
|
$fromWire = new CERT(); |
70
|
|
|
$fromWire->fromWire($wireFormatted); |
71
|
|
|
|
72
|
|
|
$this->assertEquals(3, $fromWire->getCertificateType()); |
73
|
|
|
$this->assertEquals(65, $fromWire->getKeyTag()); |
74
|
|
|
$this->assertEquals(4, $fromWire->getAlgorithm()); |
75
|
|
|
$this->assertEquals($this->certificate, $fromWire->getCertificate()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFromText(): void |
79
|
|
|
{ |
80
|
|
|
$cert = new CERT(); |
81
|
|
|
$cert->setCertificateType('PGP'); |
82
|
|
|
$cert->setKeyTag(65); |
83
|
|
|
$cert->setAlgorithm(Algorithms::ECC); |
84
|
|
|
$cert->setCertificate($this->certificate); |
85
|
|
|
|
86
|
|
|
$text = 'PGP 65 ECC '.base64_encode($this->certificate); |
87
|
|
|
|
88
|
|
|
$fromText = new CERT(); |
89
|
|
|
$fromText->fromText($text); |
90
|
|
|
$this->assertEquals($cert, $fromText); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testFactory(): void |
94
|
|
|
{ |
95
|
|
|
$cert = new CERT(); |
96
|
|
|
$cert->setCertificateType('PGP'); |
97
|
|
|
$cert->setKeyTag(65); |
98
|
|
|
$cert->setAlgorithm(Algorithms::ECC); |
99
|
|
|
$cert->setCertificate($this->certificate); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($cert, Factory::CERT('PGP', 65, Algorithms::ECC, $this->certificate)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testGetKeyTypeMnemonic(): void |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->assertEquals('IACPKIX', CERT::getKeyTypeMnemonic(8)); |
107
|
|
|
|
108
|
|
|
$this->expectException(\InvalidArgumentException::class); |
109
|
|
|
$this->expectExceptionMessage('"256" is not a valid key type.'); |
110
|
|
|
CERT::getKeyTypeMnemonic(256); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function testGetKeyTypeValue(): void |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->assertEquals(8, CERT::getKeyTypeValue('IACPKIX')); |
116
|
|
|
|
117
|
|
|
$this->expectException(\InvalidArgumentException::class); |
118
|
|
|
$this->expectExceptionMessage('"NOT_A_VALUE" is not a valid key type mnemonic.'); |
119
|
|
|
CERT::getKeyTypeValue('NOT_A_VALUE'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.