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\DNSKEY; |
18
|
|
|
use Badcow\DNS\Rdata\DS; |
19
|
|
|
use Badcow\DNS\Rdata\Factory; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
|
22
|
|
|
class DsTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
private static $digest = '2BB183AF5F22588179A53B0A98631FAD1A292118'; |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testOutput(): void |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$expectation = '60485 5 1 '.self::$digest; |
29
|
|
|
|
30
|
|
|
$ds = new DS(); |
31
|
|
|
$ds->setKeyTag(60485); |
32
|
|
|
$ds->setAlgorithm(Algorithms::RSASHA1); |
33
|
|
|
$ds->setDigestType(DS::DIGEST_SHA1); |
34
|
|
|
$ds->setDigest(hex2bin(self::$digest)); |
35
|
|
|
|
36
|
|
|
$this->assertEquals($expectation, $ds->toText()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testFactory(): void |
40
|
|
|
{ |
41
|
|
|
$keyTag = 60485; |
42
|
|
|
$ds = Factory::DS($keyTag, Algorithms::RSASHA1, hex2bin(self::$digest), DS::DIGEST_SHA1); |
43
|
|
|
|
44
|
|
|
$this->assertEquals($keyTag, $ds->getKeyTag()); |
45
|
|
|
$this->assertEquals(Algorithms::RSASHA1, $ds->getAlgorithm()); |
46
|
|
|
$this->assertEquals(hex2bin(self::$digest), $ds->getDigest()); |
47
|
|
|
$this->assertEquals(DS::DIGEST_SHA1, $ds->getDigestType()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testFromText(): void |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$expectation = new DS(); |
53
|
|
|
$expectation->setKeyTag(60485); |
54
|
|
|
$expectation->setAlgorithm(Algorithms::RSASHA1); |
55
|
|
|
$expectation->setDigestType(DS::DIGEST_SHA1); |
56
|
|
|
$expectation->setDigest(hex2bin(self::$digest)); |
57
|
|
|
|
58
|
|
|
$fromText = new DS(); |
59
|
|
|
$fromText->fromText('60485 5 1 '.self::$digest); |
60
|
|
|
$this->assertEquals($expectation, $fromText); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testWire(): void |
64
|
|
|
{ |
65
|
|
|
$ds = new DS(); |
66
|
|
|
$ds->setKeyTag(60485); |
67
|
|
|
$ds->setAlgorithm(Algorithms::RSASHA1); |
68
|
|
|
$ds->setDigestType(DS::DIGEST_SHA1); |
69
|
|
|
$ds->setDigest(hex2bin(self::$digest)); |
70
|
|
|
$wireFormat = $ds->toWire(); |
71
|
|
|
|
72
|
|
|
$fromWire = new DS(); |
73
|
|
|
$fromWire->fromWire($wireFormat); |
74
|
|
|
|
75
|
|
|
$this->assertEquals($ds, $fromWire); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testCalculateDigest(): void |
79
|
|
|
{ |
80
|
|
|
$algorithm = Algorithms::RSASHA1; |
81
|
|
|
$dnskey = new DNSKEY(); |
82
|
|
|
$dnskey->setPublicKey(base64_decode('AQOeiiR0GOMYkDshWoSKz9XzfwJr1AYtsmx3TGkJaNXVbfi/2pHm822aJ5iI9BMzNXxeYCmZDRD99WYwYqUSdjMmmAphXdvxegXd/M5+X7OrzKBaMbCVdFLUUh6DhweJBjEVv5f2wwjM9XzcnOf+EPbtG9DMBmADjFDc2w/rljwvFw==')); |
83
|
|
|
$dnskey->setAlgorithm($algorithm); |
84
|
|
|
$dnskey->setFlags(256); |
85
|
|
|
|
86
|
|
|
$ds = new DS(); |
87
|
|
|
$ds->setAlgorithm($algorithm); |
88
|
|
|
$ds->setKeyTag(60485); |
89
|
|
|
$ds->calculateDigest('DSKEY.example.com.', $dnskey); |
90
|
|
|
|
91
|
|
|
$this->assertEquals('60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292118', $ds->toText()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.