-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaderTest.php
More file actions
197 lines (175 loc) · 6.62 KB
/
HeaderTest.php
File metadata and controls
197 lines (175 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Tests\Unit\Utopia\DNS\Message;
use PHPUnit\Framework\TestCase;
use Utopia\DNS\Exception\Message\DecodingException;
use Utopia\DNS\Message\Header;
final class HeaderTest extends TestCase
{
public function testEncodeDecodeRoundTrip(): void
{
$header = new Header(
id: 0x1234,
isResponse: true,
opcode: 0,
authoritative: false,
truncated: true,
recursionDesired: true,
recursionAvailable: true,
responseCode: 0,
questionCount: 1,
answerCount: 2,
authorityCount: 3,
additionalCount: 4
);
$binary = $header->encode();
$decoded = Header::decode($binary);
$this->assertSame(0x1234, $decoded->id);
$this->assertTrue($decoded->isResponse);
$this->assertSame(0, $decoded->opcode);
$this->assertFalse($decoded->authoritative);
$this->assertTrue($decoded->truncated);
$this->assertTrue($decoded->recursionDesired);
$this->assertTrue($decoded->recursionAvailable);
$this->assertSame(0, $decoded->responseCode);
$this->assertSame(1, $decoded->questionCount);
$this->assertSame(2, $decoded->answerCount);
$this->assertSame(3, $decoded->authorityCount);
$this->assertSame(4, $decoded->additionalCount);
}
public function testDecodeThrowsOnShortData(): void
{
$this->expectException(DecodingException::class);
Header::decode("\x00\x01");
}
public function testDecodeHonorsOffset(): void
{
// Build header with flags: QR=1, opcode=0, AA=0, TC=1, RD=1, RA=1, RCODE=0
// = 1000 0011 1000 0000 = 0x8380
$binaryHeader = pack('nnnnnn', 0x0a0b, 0x8380, 0x0001, 0x0002, 0x0003, 0x0004);
$payload = "\xff\xff\xff\xff" . $binaryHeader . "\x00\x00";
$decoded = Header::decode($payload, 4);
$this->assertSame(0x0a0b, $decoded->id);
$this->assertTrue($decoded->isResponse);
$this->assertSame(0, $decoded->opcode);
$this->assertFalse($decoded->authoritative);
$this->assertTrue($decoded->truncated);
$this->assertTrue($decoded->recursionDesired);
$this->assertTrue($decoded->recursionAvailable);
$this->assertSame(0, $decoded->responseCode);
$this->assertSame(1, $decoded->questionCount);
$this->assertSame(2, $decoded->answerCount);
$this->assertSame(3, $decoded->authorityCount);
$this->assertSame(4, $decoded->additionalCount);
}
public function testEncodeUsesNetworkByteOrder(): void
{
// Flags: QR=0, opcode=0, AA=0, TC=0, RD=1, RA=0, RCODE=3
// = 0000 0001 0000 0011 = 0x0103
$header = new Header(
id: 0x1a2b,
isResponse: false,
opcode: 0,
authoritative: false,
truncated: false,
recursionDesired: true,
recursionAvailable: false,
responseCode: 3,
questionCount: 0x0506,
answerCount: 0x0708,
authorityCount: 0x090a,
additionalCount: 0x0b0c
);
$binary = $header->encode();
$this->assertSame('1a2b010305060708090a0b0c', bin2hex($binary));
}
public function testOpcodeValidation(): void
{
$this->expectException(DecodingException::class);
$this->expectExceptionMessage('Opcode must be 0-15');
new Header(
id: 1,
isResponse: false,
opcode: 16,
authoritative: false,
truncated: false,
recursionDesired: false,
recursionAvailable: false,
responseCode: 0,
questionCount: 0,
answerCount: 0,
authorityCount: 0,
additionalCount: 0
);
}
public function testResponseCodeValidation(): void
{
$this->expectException(DecodingException::class);
$this->expectExceptionMessage('Response code must be 0-15');
new Header(
id: 1,
isResponse: false,
opcode: 0,
authoritative: false,
truncated: false,
recursionDesired: false,
recursionAvailable: false,
responseCode: 16,
questionCount: 0,
answerCount: 0,
authorityCount: 0,
additionalCount: 0
);
}
/**
* Test that packets with non-zero Z bits are accepted for interoperability.
*
* RFC 1035 says Z bits MUST be zero, but many DNS clients/proxies
* (including Google's infrastructure) set these bits. We intentionally
* ignore them to maximize compatibility.
*/
public function testDecodeAcceptsNonZeroZBits(): void
{
// Flags with Z bits set (bits 4-6):
// QR=1, opcode=0, AA=0, TC=0, RD=1, RA=1, Z=0b111, RCODE=0
// = 1000 0001 1111 0000 = 0x81F0
$flagsWithAllZBits = 0x81F0;
$binaryHeader = pack('nnnnnn', 0x1234, $flagsWithAllZBits, 1, 0, 0, 0);
$decoded = Header::decode($binaryHeader);
// Verify the header is decoded correctly despite Z bits
$this->assertSame(0x1234, $decoded->id);
$this->assertTrue($decoded->isResponse);
$this->assertSame(0, $decoded->opcode);
$this->assertFalse($decoded->authoritative);
$this->assertFalse($decoded->truncated);
$this->assertTrue($decoded->recursionDesired);
$this->assertTrue($decoded->recursionAvailable);
$this->assertSame(0, $decoded->responseCode);
$this->assertSame(1, $decoded->questionCount);
}
/**
* Test various Z bit patterns are all accepted.
*/
public function testDecodeAcceptsVariousZBitPatterns(): void
{
$zBitPatterns = [
0x0010, // Z bit 4 only
0x0020, // Z bit 5 only
0x0040, // Z bit 6 only
0x0030, // Z bits 4 and 5
0x0050, // Z bits 4 and 6
0x0060, // Z bits 5 and 6
0x0070, // All Z bits (4, 5, 6)
];
foreach ($zBitPatterns as $zBits) {
// Base flags: QR=0, opcode=0, AA=0, TC=0, RD=1, RA=0, RCODE=0
// = 0000 0001 0000 0000 = 0x0100
$flags = 0x0100 | $zBits;
$binaryHeader = pack('nnnnnn', 0xABCD, $flags, 1, 0, 0, 0);
$decoded = Header::decode($binaryHeader);
$this->assertSame(0xABCD, $decoded->id, "Failed for Z bits pattern: " . sprintf('0x%04X', $zBits));
$this->assertFalse($decoded->isResponse);
$this->assertTrue($decoded->recursionDesired);
$this->assertSame(0, $decoded->responseCode);
}
}
}