-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathDnsClientTests.cs
More file actions
151 lines (127 loc) · 3.99 KB
/
DnsClientTests.cs
File metadata and controls
151 lines (127 loc) · 3.99 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Waher.Networking.DNS.Communication;
using Waher.Networking.DNS.Enumerations;
using Waher.Networking.DNS.ResourceRecords;
using Waher.Runtime.Console;
namespace Waher.Networking.DNS.Test
{
public abstract class DnsClientTests
{
private DnsClient client;
[TestInitialize]
public void TestInitialize()
{
this.client = this.CreateClient();
}
protected abstract DnsClient CreateClient();
[TestCleanup]
public void TestCleanup()
{
this.client?.Dispose();
this.client = null;
}
[TestMethod]
public async Task Test_01_Standard_Query_A()
{
DnsMessage Message = await this.client.QueryAsync("google.com", QTYPE.A, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
[TestMethod]
public async Task Test_02_Standard_Query_AAAA()
{
DnsMessage Message = await this.client.QueryAsync("google.com", QTYPE.AAAA, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
[TestMethod]
public async Task Test_03_Standard_Query_NonExistantDomain()
{
DnsMessage Message = await this.client.QueryAsync("dettanamnfinnsinte.se", QTYPE.A, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
[TestMethod]
public async Task Test_04_Standard_Query_MX()
{
DnsMessage Message = await this.client.QueryAsync("hotmail.com", QTYPE.MX, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
[TestMethod]
public async Task Test_05_Standard_Query_SRV()
{
DnsMessage Message = await this.client.QueryAsync("_xmpp-client._tcp.jabber.org", QTYPE.SRV, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
[TestMethod]
public async Task Test_06_Standard_Query_Reverse_IP4_Lookup()
{
DnsMessage Message = await this.client.QueryAsync("172.217.21.174.IN-ADDR.ARPA", QTYPE.PTR, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
[TestMethod]
public async Task Test_06_Standard_Query_Reverse_IP6_Lookup()
{
IPAddress Addr = IPAddress.Parse("2a00:1450:400f:80a::200e");
StringBuilder sb = new();
byte[] Bin = Addr.GetAddressBytes();
int i;
byte b, b2;
for (i = 15; i >= 0; i--)
{
b = Bin[i];
b2 = (byte)(b & 15);
if (b2 < 10)
sb.Append((char)('0' + b2));
else
sb.Append((char)('A' + b2- 10));
sb.Append('.');
b2 = (byte)(b >> 4);
if (b2 < 10)
sb.Append((char)('0' + b2));
else
sb.Append((char)('A' + b2 - 10));
sb.Append('.');
}
sb.Append("IP6.ARPA");
DnsMessage Message = await this.client.QueryAsync(sb.ToString(), QTYPE.PTR, QCLASS.IN, null);
Assert.IsTrue(Message.Response);
Print(Message);
}
private static void Print(DnsMessage Message)
{
ConsoleOut.WriteLine("ID: " + Message.ID);
ConsoleOut.WriteLine("OpCode: " + Message.OpCode);
ConsoleOut.WriteLine("AuthoritativeAnswer: " + Message.AuthoritativeAnswer);
ConsoleOut.WriteLine("Truncation: " + Message.Truncation);
ConsoleOut.WriteLine("RecursionDesired: " + Message.RecursionDesired);
ConsoleOut.WriteLine("RecursionAvailable: " + Message.RecursionAvailable);
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("Questions");
ConsoleOut.WriteLine(new string('=', 40));
foreach (Question Q in Message.Questions)
ConsoleOut.WriteLine(Q.ToString());
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("Answer");
ConsoleOut.WriteLine(new string('=', 40));
foreach (ResourceRecord RR in Message.Answer)
ConsoleOut.WriteLine(RR.ToString());
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("Authority");
ConsoleOut.WriteLine(new string('=', 40));
foreach (ResourceRecord RR in Message.Authority)
ConsoleOut.WriteLine(RR.ToString());
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("Additional");
ConsoleOut.WriteLine(new string('=', 40));
foreach (ResourceRecord RR in Message.Additional)
ConsoleOut.WriteLine(RR.ToString());
}
}
}