-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEdnsOptionsTests.cs
More file actions
114 lines (104 loc) · 4.17 KB
/
EdnsOptionsTests.cs
File metadata and controls
114 lines (104 loc) · 4.17 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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace DnsClientX.Tests {
/// <summary>
/// Tests EDNS options handling for wire format queries.
/// </summary>
public class EdnsOptionsTests {
private static byte[] CreateDnsHeader() {
byte[] bytes = new byte[12];
ushort id = 0x1234;
bytes[0] = (byte)(id >> 8);
bytes[1] = (byte)(id & 0xFF);
ushort flags = 0x8180;
bytes[2] = (byte)(flags >> 8);
bytes[3] = (byte)(flags & 0xFF);
return bytes;
}
private static int GetFreePort() {
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)socket.LocalEndPoint!).Port;
}
private static async Task<byte[]> RunUdpServerAsync(int port, byte[] response, CancellationToken token) {
using var udp = new UdpClient(new IPEndPoint(IPAddress.Loopback, port));
UdpReceiveResult result = await udp.ReceiveAsync();
await udp.SendAsync(response, response.Length, result.RemoteEndPoint);
return result.Buffer;
}
private static void AssertEcsOption(byte[] query, string name) {
int offset = 12;
foreach (var label in name.Split('.')) {
offset += 1 + label.Length;
}
offset += 1 + 2 + 2;
Assert.Equal(0, query[offset]);
offset += 1;
ushort type = (ushort)((query[offset] << 8) | query[offset + 1]);
Assert.Equal((ushort)DnsRecordType.OPT, type);
offset += 2 + 2 + 4;
ushort rdlen = (ushort)((query[offset] << 8) | query[offset + 1]);
Assert.True(rdlen > 0);
offset += 2;
ushort optionCode = (ushort)((query[offset] << 8) | query[offset + 1]);
Assert.Equal(8, optionCode);
}
/// <summary>
/// Ensures the ECS option is added when EDNS is enabled.
/// </summary>
[Fact]
public async Task UdpRequest_ShouldIncludeEcsOption_WhenOptionsConfigured() {
int port = GetFreePort();
var response = CreateDnsHeader();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var udpTask = RunUdpServerAsync(port, response, cts.Token);
var config = new Configuration("127.0.0.1", DnsRequestFormat.DnsOverUDP) {
Port = port,
EdnsOptions = new EdnsOptions { EnableEdns = true, Subnet = new EdnsClientSubnetOption("192.0.2.1/24") }
};
await DnsWireResolveUdp.ResolveWireFormatUdp(
"127.0.0.1",
port,
"example.com",
DnsRecordType.A,
requestDnsSec: false,
validateDnsSec: false,
debug: false,
config,
1,
cts.Token);
byte[] query = await udpTask;
AssertEcsOption(query, "example.com");
}
/// <summary>
/// Verifies that null EDNS options do not throw during UDP requests.
/// </summary>
[Fact]
public async Task UdpRequest_ShouldNotThrow_WhenEdnsOptionsNull() {
int port = GetFreePort();
var response = CreateDnsHeader();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var udpTask = RunUdpServerAsync(port, response, cts.Token);
var config = new Configuration("127.0.0.1", DnsRequestFormat.DnsOverUDP) {
Port = port,
EdnsOptions = null
};
await DnsWireResolveUdp.ResolveWireFormatUdp(
"127.0.0.1",
port,
"example.com",
DnsRecordType.A,
requestDnsSec: false,
validateDnsSec: false,
debug: false,
config,
1,
cts.Token);
await udpTask;
}
}
}