Completed
Pull Request — master (#97)
by Sam
04:21
created

TsigTest::testGetTypeCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Parser\ParseException;
17
use Badcow\DNS\Rcode;
18
use Badcow\DNS\Rdata\DecodeException;
19
use Badcow\DNS\Rdata\Factory;
20
use Badcow\DNS\Rdata\TSIG;
21
use PHPUnit\Framework\TestCase;
22
23
class TsigTest extends TestCase
24
{
25
    private $sampleMac = <<<TXT
26
VGhpcyBwcm90b2NvbCBhbGxvd3MgZm9yIHRyYW5zYWN0aW9uIGxldmVsIGF1dGhlbnRpY2F0aW9uIHV
27
zaW5nIHNoYXJlZCBzZWNyZXRzIGFuZCBvbmUgd2F5IGhhc2hpbmcuICBJdCBjYW4gYmUgdXNlZCB0by
28
BhdXRoZW50aWNhdGUgZHluYW1pYyB1cGRhdGVzIGFzIGNvbWluZyBmcm9tIGFuIGFwcHJvdmVkIGNsa
29
WVudCwgb3IgdG8gYXV0aGVudGljYXRlIHJlc3BvbnNlcyBhcyBjb21pbmcgZnJvbSBhbiBhcHByb3Zl
30
ZCByZWN1cnNpdmUgbmFtZSBzZXJ2ZXIu
31
TXT;
32
33
    private $sampleOtherData = <<<TXT
34
Tm8gcHJvdmlzaW9uIGhhcyBiZWVuIG1hZGUgaGVyZSBmb3IgZGlzdHJpYnV0aW5nIHRoZSBzaGFyZWQ
35
gc2VjcmV0czsKICAgaXQgaXMgZXhwZWN0ZWQgdGhhdCBhIG5ldHdvcmsgYWRtaW5pc3RyYXRvciB3aW
36
xsIHN0YXRpY2FsbHkgY29uZmlndXJlCiAgIG5hbWUgc2VydmVycyBhbmQgY2xpZW50cyB1c2luZyBzb
37
21lIG91dCBvZiBiYW5kIG1lY2hhbmlzbSBzdWNoIGFzCiAgIHNuZWFrZXItbmV0IHVudGlsIGEgc2Vj
38
dXJlIGF1dG9tYXRlZCBtZWNoYW5pc20gZm9yIGtleSBkaXN0cmlidXRpb24KICAgaXMgYXZhaWxhYmx
39
lLg==
40
TXT;
41
42
    public function testGetType(): void
43
    {
44
        $tsig = new TSIG();
45
        $this->assertEquals('TSIG', $tsig->getType());
46
    }
47
48
    public function testGetTypeCode(): void
49
    {
50
        $tsig = new TSIG();
51
        $this->assertEquals(250, $tsig->getTypeCode());
52
    }
53
54
    public function testToText(): void
55
    {
56
        $tsig = new TSIG();
57
        $tsig->setAlgorithmName('SAMPLE-ALG.EXAMPLE.');
58
        $tsig->setTimeSigned(new \DateTime('Tue Jan 21 00:00:00 1997'));
59
        $tsig->setFudge(300);
60
        $tsig->setMac(base64_decode($this->sampleMac));
61
        $tsig->setOriginalId(54321);
62
        $tsig->setError(Rcode::BADALG);
63
        $tsig->setOtherData(base64_decode($this->sampleOtherData));
64
65
        $mac = str_replace(["\r", "\n"], '', $this->sampleMac);
66
        $otherData = str_replace(["\r", "\n"], '', $this->sampleOtherData);
67
        $expectation = 'SAMPLE-ALG.EXAMPLE. 853804800 300 '.$mac.' 54321 21 '.$otherData;
68
69
        $this->assertEquals($expectation, $tsig->toText());
70
    }
71
72
    /**
73
     * @throws ParseException
74
     */
75
    public function testFromText(): void
76
    {
77
        $mac = str_replace(["\r", "\n"], '', $this->sampleMac);
78
        $otherData = str_replace(["\r", "\n"], '', $this->sampleOtherData);
79
        $text = 'SAMPLE-ALG.EXAMPLE. 853804800 300 '.$mac.' 54321 21 '.$otherData;
80
        $expectedTimeSigned = new \DateTime('Tue Jan 21 00:00:00 1997');
81
82
        $tsig = new TSIG();
83
        $tsig->fromText($text);
84
85
        $this->assertEquals('SAMPLE-ALG.EXAMPLE.', $tsig->getAlgorithmName());
86
        $this->assertEquals($expectedTimeSigned, $tsig->getTimeSigned());
87
        $this->assertEquals(300, $tsig->getFudge());
88
        $this->assertEquals(base64_decode($mac), $tsig->getMac());
89
        $this->assertEquals(54321, $tsig->getOriginalId());
90
        $this->assertEquals(Rcode::BADALG, $tsig->getError());
91
        $this->assertEquals(base64_decode($otherData), $tsig->getOtherData());
92
    }
93
94
    /**
95
     * @throws DecodeException
96
     */
97
    public function testWire(): void
98
    {
99
        $tsig = new TSIG();
100
        $tsig->setAlgorithmName('SAMPLE-ALG.EXAMPLE.');
101
        $tsig->setTimeSigned(new \DateTime('Tue Jan 21 00:00:00 1997'));
102
        $tsig->setFudge(300);
103
        $tsig->setMac(base64_decode($this->sampleMac));
104
        $tsig->setOriginalId(54321);
105
        $tsig->setError(Rcode::BADALG);
106
        $tsig->setOtherData(base64_decode($this->sampleOtherData));
107
108
        $wireFormat = $tsig->toWire();
109
        $rdLength = strlen($wireFormat);
110
        $wireFormat = 'abc'.$wireFormat;
111
        $offset = 3;
112
113
        $fromWire = new TSIG();
114
        $fromWire->fromWire($wireFormat, $offset, $rdLength);
115
        $this->assertEquals($tsig, $fromWire);
116
        $this->assertEquals(3 + $rdLength, $offset);
117
    }
118
119
    public function testFactory(): void
120
    {
121
        $timeSigned = new \DateTime('Tue Jan 21 00:00:00 1997');
122
        $tsig = Factory::TSIG(
123
            'SAMPLE-ALG.EXAMPLE.',
124
            $timeSigned,
125
            300,
126
            base64_decode($this->sampleMac),
127
            54321,
128
            Rcode::BADALG,
129
            base64_decode($this->sampleOtherData)
130
        );
131
132
        $this->assertEquals('SAMPLE-ALG.EXAMPLE.', $tsig->getAlgorithmName());
133
        $this->assertEquals($timeSigned, $tsig->getTimeSigned());
134
        $this->assertEquals(300, $tsig->getFudge());
135
        $this->assertEquals(base64_decode($this->sampleMac), $tsig->getMac());
136
        $this->assertEquals(54321, $tsig->getOriginalId());
137
        $this->assertEquals(Rcode::BADALG, $tsig->getError());
138
        $this->assertEquals(base64_decode($this->sampleOtherData), $tsig->getOtherData());
139
    }
140
}
141