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\URI; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
class UriTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function dataProvider_testExceptions(): array |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
//[Priority, Weight, Target, ExpectedException, ExpectedExceptionMessage] |
26
|
|
|
[-1, 1, 'http://www.example.com/path', \InvalidArgumentException::class, 'Priority must be an unsigned integer on the range [0-65535]'], |
27
|
|
|
[0x10000, 10, 'https://tools.ietf.org/html/rfc7553', \InvalidArgumentException::class, 'Priority must be an unsigned integer on the range [0-65535]'], |
28
|
|
|
[10, -1, 'http://www.example.com/path', \InvalidArgumentException::class, 'Weight must be an unsigned integer on the range [0-65535]'], |
29
|
|
|
[256, 0x10000, 'https://tools.ietf.org/html/rfc7553', \InvalidArgumentException::class, 'Weight must be an unsigned integer on the range [0-65535]'], |
30
|
|
|
[10, 0xff, '"https://tools.ietf.org/html/rfc7553"', \InvalidArgumentException::class, 'The target ""https://tools.ietf.org/html/rfc7553"" is not a valid URI.'], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testOutput(): void |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$srv = Factory::URI(10, 1, 'http://www.example.com/path'); |
37
|
|
|
$expectation = '10 1 "http://www.example.com/path"'; |
38
|
|
|
|
39
|
|
|
$this->assertEquals($expectation, $srv->toText()); |
40
|
|
|
$this->assertEquals(10, $srv->getPriority()); |
41
|
|
|
$this->assertEquals(1, $srv->getWeight()); |
42
|
|
|
$this->assertEquals('http://www.example.com/path', $srv->getTarget()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider dataProvider_testExceptions |
47
|
|
|
*/ |
48
|
|
|
public function testExceptions(int $priority, int $weight, string $target, string $expectedException, string $expectedExceptionMessage): void |
49
|
|
|
{ |
50
|
|
|
$this->expectException($expectedException); |
51
|
|
|
$this->expectExceptionMessage($expectedExceptionMessage); |
52
|
|
|
|
53
|
|
|
Factory::URI($priority, $weight, $target); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testFromText(): void |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$text = '10 1 "ftp://ftp1.example.com/public%20data"'; |
59
|
|
|
|
60
|
|
|
$uri = new URI(); |
61
|
|
|
$uri->setPriority(10); |
62
|
|
|
$uri->setWeight(1); |
63
|
|
|
$uri->setTarget('ftp://ftp1.example.com/public%20data'); |
64
|
|
|
|
65
|
|
|
$fromText = new URI(); |
66
|
|
|
$fromText->fromText($text); |
67
|
|
|
$this->assertEquals($uri, $fromText); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testWire(): void |
71
|
|
|
{ |
72
|
|
|
$wireFormat = pack('nn', 10, 1).'ftp://ftp1.example.com/public%20data'; |
73
|
|
|
$uri = new URI(); |
74
|
|
|
$uri->setPriority(10); |
75
|
|
|
$uri->setWeight(1); |
76
|
|
|
$uri->setTarget('ftp://ftp1.example.com/public%20data'); |
77
|
|
|
|
78
|
|
|
$this->assertEquals($wireFormat, $uri->toWire()); |
79
|
|
|
$fromWire = new URI(); |
80
|
|
|
$fromWire->fromWire($wireFormat); |
81
|
|
|
$this->assertEquals($uri, $fromWire); |
82
|
|
|
$rdLength = strlen($wireFormat); |
83
|
|
|
$wireFormat = 'abc'.$wireFormat; |
84
|
|
|
$offset = 3; |
85
|
|
|
|
86
|
|
|
$fromWire = new URI(); |
87
|
|
|
$fromWire->fromWire($wireFormat, $offset, $rdLength); |
88
|
|
|
$this->assertEquals($uri, $fromWire); |
89
|
|
|
$this->assertEquals(3 + $rdLength, $offset); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.