SoaTest::testOutput()   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\Rdata\Factory;
17
use Badcow\DNS\Rdata\SOA;
18
use PHPUnit\Framework\TestCase;
19
20
class SoaTest extends TestCase
21
{
22
    public function testSettersAndGetters(): void
23
    {
24
        $soa = new SOA();
25
        $mname = 'example.com.';
26
        $rname = 'post.example.com.';
27
        $serial = 1970010101;
28
        $refresh = 3600;
29
        $retry = 14400;
30
        $expire = 604800;
31
        $minimum = 3600;
32
33
        $soa->setMname($mname);
34
        $soa->setRname($rname);
35
        $soa->setSerial($serial);
36
        $soa->setRefresh($refresh);
37
        $soa->setRetry($retry);
38
        $soa->setExpire($expire);
39
        $soa->setMinimum($minimum);
40
41
        $this->assertEquals($mname, $soa->getMname());
42
        $this->assertEquals($rname, $soa->getRname());
43
        $this->assertEquals($serial, $soa->getSerial());
44
        $this->assertEquals($refresh, $soa->getRefresh());
45
        $this->assertEquals($retry, $soa->getRetry());
46
        $this->assertEquals($expire, $soa->getExpire());
47
        $this->assertEquals($minimum, $soa->getMinimum());
48
    }
49
50
    public function testOutput(): void
51
    {
52
        $soa = Factory::SOA(
53
            'example.com.',
54
            'postmaster.example.com.',
55
            2015042101,
56
            3600,
57
            14400,
58
            604800,
59
            3600
60
        );
61
62
        $expected = 'example.com. postmaster.example.com. 2015042101 3600 14400 604800 3600';
63
64
        $this->assertEquals($expected, $soa->toText());
65
        $this->assertEquals($expected, $soa->toText());
66
    }
67
68
    public function testFromText(): void
69
    {
70
        $text = 'example.com. post.example.com. 2015042101 3600 14400 604800 3600';
71
        $mname = 'example.com.';
72
        $rname = 'post.example.com.';
73
        $serial = 2015042101;
74
        $refresh = 3600;
75
        $retry = 14400;
76
        $expire = 604800;
77
        $minimum = 3600;
78
79
        $soa = new SOA();
80
        $soa->fromText($text);
81
82
        $this->assertEquals($mname, $soa->getMname());
83
        $this->assertEquals($rname, $soa->getRname());
84
        $this->assertEquals($serial, $soa->getSerial());
85
        $this->assertEquals($refresh, $soa->getRefresh());
86
        $this->assertEquals($retry, $soa->getRetry());
87
        $this->assertEquals($expire, $soa->getExpire());
88
        $this->assertEquals($minimum, $soa->getMinimum());
89
    }
90
91
    public function testWire(): void
92
    {
93
        $soa = new SOA();
94
        $mname = 'example.com.';
95
        $rname = 'post.example.com.';
96
        $serial = 1970010101;
97
        $refresh = 3600;
98
        $retry = 14400;
99
        $expire = 604800;
100
        $minimum = 3600;
101
102
        $soa->setMname($mname);
103
        $soa->setRname($rname);
104
        $soa->setSerial($serial);
105
        $soa->setRefresh($refresh);
106
        $soa->setRetry($retry);
107
        $soa->setExpire($expire);
108
        $soa->setMinimum($minimum);
109
110
        $wireFormat =
111
            chr(7).'example'.chr(3).'com'."\0".
112
            chr(4).'post'.chr(7).'example'.chr(3).'com'."\0".
113
            pack('NNNNN', 1970010101, 3600, 14400, 604800, 3600);
114
115
        $this->assertEquals($wireFormat, $soa->toWire());
116
117
        $rdLength = strlen($wireFormat);
118
        $wireFormat = 'abcde'.$wireFormat;
119
        $offset = 5;
120
        $fromWire = new SOA();
121
        $fromWire->fromWire($wireFormat, $offset, $rdLength);
122
        $this->assertEquals($soa, $fromWire);
123
        $this->assertEquals(5 + $rdLength, $offset);
124
    }
125
}
126