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

DhcidTest::testFromText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\DHCID;
17
use Badcow\DNS\Rdata\Factory;
18
use PHPUnit\Framework\TestCase;
19
20
class DhcidTest extends TestCase
21
{
22
    public function getDataProvider(): array
23
    {
24
        return [
25
            //[Text,                                             IDType, Identifier,                                  FQDN]
26
            ['AAIBY2/AuCccgoJbsaxcQc9TUapptP69lOjxfNuVAA2kjEA=', 2,      '00:01:00:06:41:2d:f1:66:01:02:03:04:05:06', 'chi6.example.com.'],
27
            ['AAEBOSD+XR3Os/0LozeXVqcNc7FwCfQdWL3b/NaiUDlW2No=', 1,      '01:07:08:09:0a:0b:0c',                      'chi.example.com.'],
28
            ['AAABxLmlskllE0MVjd57zHcWmEH3pCQ6VytcKD//7es/deY=', 0,      '01:02:03:04:05:06',                         'client.example.com.'],
29
        ];
30
    }
31
32
    public function testGetType(): void
33
    {
34
        $dhcid = new DHCID();
35
        $this->assertEquals('DHCID', $dhcid->getType());
36
    }
37
38
    public function testGetTypeCode(): void
39
    {
40
        $dhcid = new DHCID();
41
        $this->assertEquals(49, $dhcid->getTypeCode());
42
    }
43
44 View Code Duplication
    public function testSetGetHtype(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
45
    {
46
        $dhcid = new DHCID();
47
        $dhcid->setHtype(3);
48
        $this->assertEquals(3, $dhcid->getHtype());
49
50
        $this->expectException(\InvalidArgumentException::class);
51
        $this->expectExceptionMessage('HType must be an 8-bit integer.');
52
        $dhcid->setHtype(256);
53
    }
54
55 View Code Duplication
    public function testSetGetFqdn(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
56
    {
57
        $dhcid = new DHCID();
58
        $dhcid->setFqdn('abc.example.com.');
59
        $this->assertEquals('abc.example.com.', $dhcid->getFqdn());
60
61
        $this->expectException(\InvalidArgumentException::class);
62
        $this->expectExceptionMessage('"abc.example.com" is not a fully qualified domain name.');
63
        $dhcid->setFqdn('abc.example.com');
64
    }
65
66
    public function testSetIdentifierTypeThrowsExceptionIfTypeIsMoreThanSixteenBits(): void
67
    {
68
        $dhcid = new DHCID();
69
70
        $this->expectException(\InvalidArgumentException::class);
71
        $this->expectExceptionMessage('Identifier type must be a 16-bit integer.');
72
        $dhcid->setIdentifierType(65536);
73
    }
74
75
    public function testCalculateDigestThrowsExceptionIfIdentifierIsNotSet(): void
76
    {
77
        $dhcid = new DHCID();
78
        $dhcid->setFqdn('abc.example.com.');
79
80
        $this->expectException(\BadMethodCallException::class);
81
        $this->expectExceptionMessage('Identifier and Fully Qualified Domain Name (FQDN) must both be set on DHCID object before calling calculateDigest().');
82
        $dhcid->calculateDigest();
83
    }
84
85
    /**
86
     * @dataProvider getDataProvider
87
     */
88
    public function testToText(string $text, int $identifierType, string $identifier, string $fqdn): void
89
    {
90
        $dhcid = new DHCID();
91
        $dhcid->setIdentifier($identifierType, $identifier);
92
        $dhcid->setFqdn($fqdn);
93
94
        $this->assertEquals($identifier, $dhcid->getIdentifier());
95
        $this->assertEquals($text, $dhcid->toText());
96
    }
97
98
    /**
99
     * @dataProvider getDataProvider
100
     */
101
    public function testToFromWire(string $text, int $identifierType, string $identifier, string $fqdn): void
102
    {
103
        $expectation = new DHCID();
104
        $expectation->setIdentifier($identifierType, $identifier);
105
        $expectation->setFqdn($fqdn);
106
107
        $dhcid = new DHCID();
108
        $dhcid->fromWire($expectation->toWire());
109
110
        $this->assertEquals($expectation->getIdentifierType(), $dhcid->getIdentifierType());
111
        $this->assertEquals($expectation->getDigestType(), $dhcid->getDigestType());
112
        $this->assertEquals($expectation->getDigest(), $dhcid->getDigest());
113
        $this->assertEquals($expectation->toText(), $dhcid->toText());
114
        $this->assertEquals($text, $dhcid->toText());
115
    }
116
117
    /**
118
     * @dataProvider getDataProvider
119
     *
120
     * @throws \Exception
121
     */
122
    public function testFromText(string $text, int $identifierType, string $identifier, string $fqdn): void
123
    {
124
        $expectation = new DHCID();
125
        $expectation->setIdentifier($identifierType, $identifier);
126
        $expectation->setFqdn($fqdn);
127
        $expectation->calculateDigest();
128
129
        $dhcid = new DHCID();
130
        $dhcid->fromText($text);
131
132
        $this->assertEquals($expectation->getIdentifierType(), $dhcid->getIdentifierType());
133
        $this->assertEquals($expectation->getDigestType(), $dhcid->getDigestType());
134
        $this->assertEquals($expectation->getDigest(), $dhcid->getDigest());
135
        $this->assertEquals($expectation->toText(), $dhcid->toText());
136
137
        $dhcid = new DHCID();
138
        $this->expectException(\Exception::class);
139
        $this->expectExceptionMessageMatches('/Unable to base64 decode text ".*"\./');
140
        $dhcid->fromText($text.'%');
141
    }
142
143
    /**
144
     * @dataProvider getDataProvider
145
     */
146
    public function testFactory(string $text, int $identifierType, string $identifier, string $fqdn): void
147
    {
148
        $dhcid = Factory::DHCID(null, $identifierType, $identifier, $fqdn);
149
        $this->assertEquals($text, $dhcid->toText());
150
151
        $digest = $dhcid->getDigest();
152
        $_dhcid = Factory::DHCID($digest, $identifierType);
153
154
        $this->assertEquals($dhcid->toText(), $_dhcid->toText());
155
    }
156
157
    public function testFactoryThrowsExceptionIfIdAndFqdnAreNull(): void
158
    {
159
        $this->expectException(\InvalidArgumentException::class);
160
        $this->expectExceptionMessage('Identifier and FQDN cannot be null if digest is null.');
161
        Factory::DHCID(null, 2, null);
162
    }
163
}
164