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

SshfpTest::testFromText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
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\SSHFP;
18
use PHPUnit\Framework\TestCase;
19
20
class SshfpTest extends TestCase
21
{
22
    public function dataProvider_testExceptions(): array
23
    {
24
        return [
25
            //[Algorithm, FPType, Fingerprint, ExpectedException, ExpectedExceptionMessage]
26
            [-1, 255, '123456789abcdef67890123456789abcdef67890', \InvalidArgumentException::class, 'Algorithm must be an 8-bit integer between 0 and 255.'],
27
            [256, 255, '123456789abcdef67890123456789abcdef67890', \InvalidArgumentException::class, 'Algorithm must be an 8-bit integer between 0 and 255.'],
28
            [0, -1, '123456789abcdef67890123456789abcdef67890', \InvalidArgumentException::class, 'Fingerprint type must be an 8-bit integer between 0 and 255.'],
29
            [0, 256, '123456789abcdef67890123456789abcdef67890', \InvalidArgumentException::class, 'Fingerprint type must be an 8-bit integer between 0 and 255.'],
30
        ];
31
    }
32
33
    public function testOutput(): void
34
    {
35
        $sshfp = new SSHFP();
36
        $sshfp->setAlgorithm(SSHFP::ALGORITHM_DSA);
37
        $sshfp->setFingerprintType(SSHFP::FP_TYPE_SHA1);
38
        $sshfp->setFingerprint(hex2bin('123456789abcdef67890123456789abcdef67890'));
39
40
        $expectation = '2 1 123456789abcdef67890123456789abcdef67890';
41
        $this->assertEquals($expectation, $sshfp->toText());
42
    }
43
44
    /**
45
     * @dataProvider dataProvider_testExceptions
46
     */
47
    public function testExceptions(int $algorithm, int $fpType, string $fingerprint, string $expectedException, string $expectedExceptionMessage): void
48
    {
49
        $this->expectException($expectedException);
50
        $this->expectExceptionMessage($expectedExceptionMessage);
51
52
        Factory::SSHFP($algorithm, $fpType, $fingerprint);
53
    }
54
55
    public function testGetType(): void
56
    {
57
        $sshfp = new SSHFP();
58
        $this->assertEquals('SSHFP', $sshfp->getType());
59
    }
60
61
    public function testGetTypeCode(): void
62
    {
63
        $sshfp = new SSHFP();
64
        $this->assertEquals(44, $sshfp->getTypeCode());
65
    }
66
67 View Code Duplication
    public function testToText(): 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...
68
    {
69
        $expectation = '2 1 123456789abcdef67890123456789abcdef67890';
70
        $sshfp = new SSHFP();
71
        $sshfp->setAlgorithm(2);
72
        $sshfp->setFingerprintType(1);
73
        $sshfp->setFingerprint(hex2bin('123456789abcdef67890123456789abcdef67890'));
74
75
        $this->assertEquals($expectation, $sshfp->toText());
76
    }
77
78
    public function testWire(): void
79
    {
80
        $wireFormat = chr(2).chr(1).hex2bin('123456789abcdef67890123456789abcdef67890');
81
        $sshfp = new SSHFP();
82
        $sshfp->setAlgorithm(2);
83
        $sshfp->setFingerprintType(1);
84
        $sshfp->setFingerprint(hex2bin('123456789abcdef67890123456789abcdef67890'));
85
86
        $this->assertEquals($wireFormat, $sshfp->toWire());
87
88
        $wireFormat = 'zyxwvut'.$wireFormat;
89
        $offset = 7;
90
        $fromWire = new SSHFP();
91
        $fromWire->fromWire($wireFormat, $offset, 40);
92
        $this->assertEquals($sshfp, $fromWire);
93
    }
94
95 View Code Duplication
    public function testFromText(): 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...
96
    {
97
        $expectation = new SSHFP();
98
        $expectation->setAlgorithm(2);
99
        $expectation->setFingerprintType(1);
100
        $expectation->setFingerprint(hex2bin('123456789abcdef67890123456789abcdef67890'));
101
102
        $fromText = new SSHFP();
103
        $fromText->fromText('2 1 123456789abcdef67890123456789abcdef67890');
104
        $this->assertEquals($expectation, $fromText);
105
    }
106
107
    public function testFactory(): void
108
    {
109
        $sshfp = Factory::SSHFP(2, 1, '123456789abcdef67890123456789abcdef67890');
110
111
        $this->assertEquals(2, $sshfp->getAlgorithm());
112
        $this->assertEquals(1, $sshfp->getFingerprintType());
113
        $this->assertEquals('123456789abcdef67890123456789abcdef67890', $sshfp->getFingerprint());
114
    }
115
}
116