-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
GetHostEntryTest.cs
234 lines (202 loc) · 11 KB
/
GetHostEntryTest.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using Xunit;
namespace System.Net.NameResolution.Tests
{
public class GetHostEntryTest
{
[Fact]
public async Task Dns_GetHostEntryAsync_IPAddress_Ok()
{
IPAddress localIPAddress = await TestSettings.GetLocalIPAddress();
await TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(localIPAddress));
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/1488", TestPlatforms.OSX)]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/27622")]
[InlineData("")]
[InlineData(TestSettings.LocalHost)]
public async Task Dns_GetHostEntry_HostString_Ok(string hostName)
{
try
{
await TestGetHostEntryAsync(() => Task.FromResult(Dns.GetHostEntry(hostName)));
}
catch (Exception ex) when (hostName == "")
{
// Additional data for debugging sporadic CI failures https://github.com/dotnet/runtime/issues/1488
string actualHostName = Dns.GetHostName();
string etcHosts = "";
Exception getHostEntryException = null;
Exception etcHostsException = null;
try
{
Dns.GetHostEntry(actualHostName);
}
catch (Exception e2)
{
getHostEntryException = e2;
}
try
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
etcHosts = File.ReadAllText("/etc/hosts");
}
}
catch (Exception e2)
{
etcHostsException = e2;
}
throw new Exception(
$"Failed for empty hostname.{Environment.NewLine}" +
$"Dns.GetHostName() == {actualHostName}{Environment.NewLine}" +
$"{nameof(getHostEntryException)}=={getHostEntryException}{Environment.NewLine}" +
$"{nameof(etcHostsException)}=={etcHostsException}{Environment.NewLine}" +
$"/etc/host =={Environment.NewLine}{etcHosts}",
ex);
}
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/1488", TestPlatforms.OSX)]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/27622")]
[InlineData("")]
[InlineData(TestSettings.LocalHost)]
public async Task Dns_GetHostEntryAsync_HostString_Ok(string hostName) =>
await TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(hostName));
[Fact]
public async Task Dns_GetHostEntryAsync_IPString_Ok() =>
await TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(TestSettings.LocalIPString));
private static async Task TestGetHostEntryAsync(Func<Task<IPHostEntry>> getHostEntryFunc)
{
Task<IPHostEntry> hostEntryTask1 = getHostEntryFunc();
Task<IPHostEntry> hostEntryTask2 = getHostEntryFunc();
await TestSettings.WhenAllOrAnyFailedWithTimeout(hostEntryTask1, hostEntryTask2);
IPAddress[] list1 = hostEntryTask1.Result.AddressList;
IPAddress[] list2 = hostEntryTask2.Result.AddressList;
Assert.NotNull(list1);
Assert.NotNull(list2);
Assert.Equal<IPAddress>(list1, list2);
}
[Fact]
public async Task Dns_GetHostEntry_NullStringHost_Fail()
{
Assert.Throws<ArgumentNullException>(() => Dns.GetHostEntry((string)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => Dns.GetHostEntryAsync((string)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, (string)null, null));
}
[Fact]
public async Task Dns_GetHostEntryAsync_NullIPAddressHost_Fail()
{
Assert.Throws<ArgumentNullException>(() => Dns.GetHostEntry((IPAddress)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => Dns.GetHostEntryAsync((IPAddress)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, (IPAddress)null, null));
}
public static IEnumerable<object[]> GetInvalidAddresses()
{
yield return new object[] { IPAddress.Any };
yield return new object[] { IPAddress.IPv6Any };
yield return new object[] { IPAddress.IPv6None };
}
[Theory]
[MemberData(nameof(GetInvalidAddresses))]
public async Task Dns_GetHostEntry_AnyIPAddress_Fail(IPAddress address)
{
Assert.Throws<ArgumentException>(() => Dns.GetHostEntry(address));
Assert.Throws<ArgumentException>(() => Dns.GetHostEntry(address.ToString()));
await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(address));
await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(address.ToString()));
await Assert.ThrowsAsync<ArgumentException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, address, null));
await Assert.ThrowsAsync<ArgumentException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, address.ToString(), null));
}
[Fact]
public async Task DnsGetHostEntry_MachineName_AllVariationsMatch()
{
IPHostEntry syncResult = Dns.GetHostEntry(TestSettings.LocalHost);
IPHostEntry apmResult = Dns.EndGetHostEntry(Dns.BeginGetHostEntry(TestSettings.LocalHost, null, null));
IPHostEntry asyncResult = await Dns.GetHostEntryAsync(TestSettings.LocalHost);
Assert.Equal(syncResult.HostName, apmResult.HostName);
Assert.Equal(syncResult.HostName, asyncResult.HostName);
Assert.Equal(syncResult.AddressList, apmResult.AddressList);
Assert.Equal(syncResult.AddressList, asyncResult.AddressList);
}
[Fact]
public async Task DnsGetHostEntry_Loopback_AllVariationsMatch()
{
IPHostEntry syncResult = Dns.GetHostEntry(IPAddress.Loopback);
IPHostEntry apmResult = Dns.EndGetHostEntry(Dns.BeginGetHostEntry(IPAddress.Loopback, null, null));
IPHostEntry asyncResult = await Dns.GetHostEntryAsync(IPAddress.Loopback);
Assert.Equal(syncResult.HostName, apmResult.HostName);
Assert.Equal(syncResult.HostName, asyncResult.HostName);
Assert.Equal(syncResult.AddressList, apmResult.AddressList);
Assert.Equal(syncResult.AddressList, asyncResult.AddressList);
}
[Theory]
[InlineData("BadName")] // unknown name
[InlineData("0.0.1.1")] // unknown address
[InlineData("Test-\u65B0-Unicode")] // unknown unicode name
[InlineData("xn--test--unicode-0b01a")] // unknown punicode name
[InlineData("Really.Long.Name.Over.One.Hundred.And.Twenty.Six.Chars.Eeeeeeeventualllllllly.I.Will.Get.To.The.Eeeee"
+ "eeeeend.Almost.There.Are.We.Really.Long.Name.Over.One.Hundred.And.Twenty.Six.Chars.Eeeeeeeventualll"
+ "llllly.I.Will.Get.To.The.Eeeeeeeeeend.Almost.There.Are")] // very long name but not too long
public async Task DnsGetHostEntry_BadName_ThrowsSocketException(string hostNameOrAddress)
{
Assert.ThrowsAny<SocketException>(() => Dns.GetHostEntry(hostNameOrAddress));
await Assert.ThrowsAnyAsync<SocketException>(() => Dns.GetHostEntryAsync(hostNameOrAddress));
await Assert.ThrowsAnyAsync<SocketException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, hostNameOrAddress, null));
}
[Theory]
[InlineData("Really.Long.Name.Over.One.Hundred.And.Twenty.Six.Chars.Eeeeeeeventualllllllly.I.Will.Get.To.The.Eeeee"
+ "eeeeend.Almost.There.Are.We.Really.Long.Name.Over.One.Hundred.And.Twenty.Six.Chars.Eeeeeeeventualll"
+ "llllly.I.Will.Get.To.The.Eeeeeeeeeend.Almost.There.Aret")]
public async Task DnsGetHostEntry_BadName_ThrowsArgumentOutOfRangeException(string hostNameOrAddress)
{
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => Dns.GetHostEntry(hostNameOrAddress));
await Assert.ThrowsAnyAsync<ArgumentOutOfRangeException>(() => Dns.GetHostEntryAsync(hostNameOrAddress));
await Assert.ThrowsAnyAsync<ArgumentOutOfRangeException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, hostNameOrAddress, null));
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/34317")]
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public async Task DnsGetHostEntry_LocalHost_ReturnsFqdnAndLoopbackIPs(int mode)
{
IPHostEntry entry = mode switch
{
0 => Dns.GetHostEntry("localhost"),
1 => await Dns.GetHostEntryAsync("localhost"),
_ => await Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, "localhost", null)
};
Assert.NotNull(entry.HostName);
Assert.True(entry.HostName.Length > 0, "Empty host name");
Assert.True(entry.AddressList.Length >= 1, "No local IPs");
Assert.All(entry.AddressList, addr => Assert.True(IPAddress.IsLoopback(addr), "Not a loopback address: " + addr));
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public async Task DnsGetHostEntry_LoopbackIP_MatchesGetHostEntryLoopbackString(int mode)
{
IPAddress address = IPAddress.Loopback;
IPHostEntry ipEntry = mode switch
{
0 => Dns.GetHostEntry(address),
1 => await Dns.GetHostEntryAsync(address),
_ => await Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, address, null)
};
IPHostEntry stringEntry = mode switch
{
0 => Dns.GetHostEntry(address.ToString()),
1 => await Dns.GetHostEntryAsync(address.ToString()),
_ => await Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, address.ToString(), null)
};
Assert.Equal(ipEntry.HostName, stringEntry.HostName);
Assert.Equal(ipEntry.AddressList, stringEntry.AddressList);
}
}
}