TkeyTest::testToText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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\Tokens;
17
use Badcow\DNS\Rdata\Factory;
18
use Badcow\DNS\Rdata\TKEY;
19
use PHPUnit\Framework\TestCase;
20
21
class TkeyTest extends TestCase
22
{
23
    private $dummyKeyData = <<<DATA
24
BAcIAwcAAAQGAQMGBwEFBwEGBAgGBwUFBwAIBwIDAwUEBgYIBQQBAwYIBQgFBQY
25
DAggDBgUCAgIBCAMDAwgDBQIFAggABggIAwMFBgABBwYDBAYHAwUEAAUIAgIICA
26
EHAAMIAAYBAAUEBggGAAMAAQMGAwAHAwUEAQQDBAgCCAAABQUAAQUCAAYEAAUHB
27
AMHAwYDAgQHBQEHAQcBAAQECAcEBQcHBwMBBAIIAQIGAAYCAAcDBwUIAQEBAwgI
28
BgQDBgECBQgGAQQGAQABAQUGAAUCBwQGAAQGCAUHBwMGAQcCAwcAAAIEAgECAQI
29
ECAgHBQQHAQgHBgAHCAIBAwUCAQMCAAEFBQMBAgcCAgIACAMECAEFCAUIBQEFAg
30
YGAgUIBQEHAggBCAcEBgMGAQYIAAAAAwQDAgUHCAUBAggDAgUAAAUBBwcBAwYEC
31
AIABAgIBwMEAggGAwUFAQAEBAQAAgIIBwgFBgMDCAABBggCAgcCBQYEBgIAAgIG
32
AwUFBAAFBQgGBwgGBwYBAQAGAQMHBwEICAYBBAMGAwEIBgIDAwgABAUCBwAIBAc
33
GAwcGBQEDAQcBBAMIAgUCAAUGAAgFBAQIBQcEBgA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ECCAICAQEIAQEI
69
BAICBQcHCAAFBAcBAQQDBgYCBggAAggEAgMEAwIDBwgFAwIBBAAIAQAFAwACCAg
70
CAwgFCAcBCAUBAAYFAAQGBwUGBwYABQQIBQYHAAUIBg==
71
DATA;
72
73
    public function testGetType(): void
74
    {
75
        $tkey = new TKEY();
76
        $this->assertEquals('TKEY', $tkey->getType());
77
    }
78
79
    public function testGetTypeCode(): void
80
    {
81
        $tkey = new TKEY();
82
        $this->assertEquals(249, $tkey->getTypeCode());
83
    }
84
85
    public function testToText(): void
86
    {
87
        $tkey = new TKEY();
88
        $tkey->setAlgorithm('alg-xx.iana.');
89
        $tkey->setInception(\DateTime::createFromFormat('YmdHis', '20191118000000'));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20191118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\TKEY::setInception() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
90
        $tkey->setExpiration(\DateTime::createFromFormat('YmdHis', '20251118000000'));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20251118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\TKEY::setExpiration() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
91
        $tkey->setMode(2);
92
        $tkey->setError(0);
93
        $tkey->setKeyData(base64_decode($this->dummyKeyData));
94
        $tkey->setOtherData(base64_decode($this->dummyOtherData));
95
96
        $expectation = 'alg-xx.iana. 1574035200 1763424000 2 0 ';
97
        $expectation .= str_replace([Tokens::CARRIAGE_RETURN, Tokens::LINE_FEED], '', $this->dummyKeyData).' ';
98
        $expectation .= str_replace([Tokens::CARRIAGE_RETURN, Tokens::LINE_FEED], '', $this->dummyOtherData);
99
100
        $this->assertEquals($expectation, $tkey->toText());
101
    }
102
103
    public function testWire(): void
104
    {
105
        $tkey = new TKEY();
106
        $tkey->setAlgorithm('alg-xx.iana.');
107
        $tkey->setInception(\DateTime::createFromFormat('YmdHis', '20191118000000'));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20191118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\TKEY::setInception() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
108
        $tkey->setExpiration(\DateTime::createFromFormat('YmdHis', '20251118000000'));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20251118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\TKEY::setExpiration() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
109
        $tkey->setMode(2);
110
        $tkey->setError(0);
111
        $tkey->setKeyData(base64_decode($this->dummyKeyData));
112
        $tkey->setOtherData(base64_decode($this->dummyOtherData));
113
114
        $wireFormat = $tkey->toWire();
115
        $rdLength = strlen($wireFormat);
116
        $wireFormat = 'abcdefg'.$wireFormat;
117
        $offset = 7;
118
        $fromWire = new TKEY();
119
        $fromWire->fromWire($wireFormat, $offset, $rdLength);
120
        $this->assertEquals($tkey, $fromWire);
121
        $this->assertEquals(7 + $rdLength, $offset);
122
    }
123
124
    public function testFromText(): void
125
    {
126
        $expectation = new TKEY();
127
        $expectation->setAlgorithm('alg-xx.iana.');
128
        $expectation->setInception(\DateTime::createFromFormat('YmdHis', '20191118000000'));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20191118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\TKEY::setInception() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
129
        $expectation->setExpiration(\DateTime::createFromFormat('YmdHis', '20251118000000'));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20251118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\TKEY::setExpiration() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
130
        $expectation->setMode(2);
131
        $expectation->setError(0);
132
        $expectation->setKeyData(base64_decode($this->dummyKeyData));
133
        $expectation->setOtherData(base64_decode($this->dummyOtherData));
134
135
        $text = 'alg-xx.iana. 1574035200 1763424000 2 0 ';
136
        $text .= str_replace([Tokens::CARRIAGE_RETURN, Tokens::LINE_FEED], '', $this->dummyKeyData).' ';
137
        $text .= str_replace([Tokens::CARRIAGE_RETURN, Tokens::LINE_FEED], '', $this->dummyOtherData);
138
139
        $fromText = new TKEY();
140
        $fromText->fromText($text);
141
        $this->assertEquals($expectation, $fromText);
142
    }
143
144
    public function testFactory(): void
145
    {
146
        $tkey = Factory::TKEY(
147
            'alg-xx.iana.',
148
            \DateTime::createFromFormat('YmdHis', '20191118000000'),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20191118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\Factory::TKEY() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
149
            \DateTime::createFromFormat('YmdHis', '20251118000000'),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...His', '20251118000000') targeting DateTime::createFromFormat() can also be of type false; however, Badcow\DNS\Rdata\Factory::TKEY() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
150
            2,
151
            0,
152
            base64_decode($this->dummyKeyData),
153
            base64_decode($this->dummyOtherData)
154
        );
155
156
        $this->assertEquals('alg-xx.iana.', $tkey->getAlgorithm());
157
        $this->assertEquals('2019-11-18', $tkey->getInception()->format('Y-m-d'));
158
        $this->assertEquals('2025-11-18', $tkey->getExpiration()->format('Y-m-d'));
159
        $this->assertEquals(2, $tkey->getMode());
160
        $this->assertEquals(0, $tkey->getError());
161
        $this->assertEquals(base64_decode($this->dummyKeyData), $tkey->getKeyData());
162
        $this->assertEquals(base64_decode($this->dummyOtherData), $tkey->getOtherData());
163
    }
164
}
165