Completed
Push — master ( 72b670...3b819e )
by Sam
05:26 queued 03:37
created

TlsaTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 111
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetType() 0 5 1
A testGetTypeCode() 0 5 1
A testToText() 0 11 1
A testToWire() 0 12 1
A testFromWire() 0 20 1
A testFromText() 0 11 1
A testFactory() 0 9 1
A testExceptions() 0 12 1
A testMalformedHexValueThrowsException() 0 7 1
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\Rdata\Factory;
18
use Badcow\DNS\Rdata\TLSA;
19
use PHPUnit\Framework\TestCase;
20
21
class TlsaTest extends TestCase
22
{
23
    private $cerAssociationData = '92003ba34942dc74152e2f2c408d29eca5a520e7f2e06bb944f4dca346baf63c1b177615d466f6c4b71c216a50292bd58c9ebdd2f74e38fe51ffd48c43326cbc';
24
25
    public function testGetType(): void
26
    {
27
        $tlsa = new TLSA();
28
        $this->assertEquals('TLSA', $tlsa->getType());
29
    }
30
31
    public function testGetTypeCode(): void
32
    {
33
        $tlsa = new TLSA();
34
        $this->assertEquals(52, $tlsa->getTypeCode());
35
    }
36
37
    public function testToText(): void
38
    {
39
        $tlsa = new TLSA();
40
        $tlsa->setCertificateUsage(0);
41
        $tlsa->setSelector(1);
42
        $tlsa->setMatchingType(2);
43
        $tlsa->setCertificateAssociationData(hex2bin($this->cerAssociationData));
44
45
        $expectation = '0 1 2 '.$this->cerAssociationData;
46
        $this->assertEquals($expectation, $tlsa->toText());
47
    }
48
49
    public function testToWire(): void
50
    {
51
        $tlsa = new TLSA();
52
        $tlsa->setCertificateUsage(0);
53
        $tlsa->setSelector(1);
54
        $tlsa->setMatchingType(2);
55
        $tlsa->setCertificateAssociationData(hex2bin($this->cerAssociationData));
56
57
        $expectation = chr(0).chr(1).chr(2).hex2bin($this->cerAssociationData);
58
59
        $this->assertEquals($expectation, $tlsa->toWire());
60
    }
61
62
    public function testFromWire(): void
63
    {
64
        $wireFormat = chr(0).chr(1).chr(2).hex2bin($this->cerAssociationData);
65
        $tlsa = new TLSA();
66
        $tlsa->fromWire($wireFormat);
67
68
        $this->assertEquals(0, $tlsa->getCertificateUsage());
69
        $this->assertEquals(1, $tlsa->getSelector());
70
        $this->assertEquals(2, $tlsa->getMatchingType());
71
        $this->assertEquals(hex2bin($this->cerAssociationData), $tlsa->getCertificateAssociationData());
72
73
        $rdLength = strlen($wireFormat);
74
        $wireFormat = 'abc'.$wireFormat;
75
        $offset = 3;
76
77
        $fromWire = new TLSA();
78
        $fromWire->fromWire($wireFormat, $offset, $rdLength);
79
        $this->assertEquals($tlsa, $fromWire);
80
        $this->assertEquals(3 + $rdLength, $offset);
81
    }
82
83
    /**
84
     * @throws ParseException
85
     */
86
    public function testFromText(): void
87
    {
88
        $text = '0 1 2 '.$this->cerAssociationData;
89
        $tlsa = new TLSA();
90
        $tlsa->fromText($text);
91
92
        $this->assertEquals(0, $tlsa->getCertificateUsage());
93
        $this->assertEquals(1, $tlsa->getSelector());
94
        $this->assertEquals(2, $tlsa->getMatchingType());
95
        $this->assertEquals(hex2bin($this->cerAssociationData), $tlsa->getCertificateAssociationData());
96
    }
97
98
    public function testFactory(): void
99
    {
100
        $tlsa = Factory::TLSA(0, 1, 2, hex2bin($this->cerAssociationData));
101
102
        $this->assertEquals(0, $tlsa->getCertificateUsage());
103
        $this->assertEquals(1, $tlsa->getSelector());
104
        $this->assertEquals(2, $tlsa->getMatchingType());
105
        $this->assertEquals(hex2bin($this->cerAssociationData), $tlsa->getCertificateAssociationData());
106
    }
107
108
    public function testExceptions(): void
109
    {
110
        $tlsa = new TLSA();
111
        $this->expectException(\InvalidArgumentException::class);
112
        $tlsa->setCertificateUsage(9);
113
114
        $this->expectException(\InvalidArgumentException::class);
115
        $tlsa->setSelector(12);
116
117
        $this->expectException(\InvalidArgumentException::class);
118
        $tlsa->setMatchingType(-1);
119
    }
120
121
    /**
122
     * @throws ParseException
123
     */
124
    public function testMalformedHexValueThrowsException(): void
125
    {
126
        $text = '0 1 2 92003ba34942dc74152e2f2c408d29eca5a520g';
127
        $this->expectException(ParseException::class);
128
        $tlsa = new TLSA();
129
        $tlsa->fromText($text);
130
    }
131
}
132