-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
NameResolutionPalTests.cs
483 lines (412 loc) · 18.3 KB
/
NameResolutionPalTests.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Win32;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.NameResolution.PalTests
{
public class NameResolutionPalTests
{
private readonly ITestOutputHelper _output;
private static Lazy<bool> s_ipv6LocalHostNameLookupBrokenByNrpRule = new Lazy<bool>(() =>
{
// On some machines using Microsoft corporate VPN, something sometimes installs an DNS Name Resolution Policy rule
// that breaks reverse lookup of ipv6 localhost names.
if (PlatformDetection.IsWindows)
{
// Equivalent of `Get-DnsClientNrptRule -Name .ip6.arpa`
using RegistryKey? key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig");
if (key != null)
{
// Also filtering out anything not written by MSFTVPN
foreach (string name in key.GetSubKeyNames().Where(name => name.Contains("MSFTVPN")))
{
using RegistryKey? key2 = key.OpenSubKey(name);
if (key2 != null && key2.GetValue("Name") is string[] values && values.Length == 1 && values[0] == ".ip6.arpa")
{
return true;
}
}
}
}
return false;
});
private static bool Ipv6LocalHostNameLookupNotBrokenByNrpRule => !s_ipv6LocalHostNameLookupBrokenByNrpRule.Value;
public NameResolutionPalTests(ITestOutputHelper output)
{
_output = output;
}
private void LogUnixInfo()
{
_output.WriteLine("--- /etc/hosts ---");
_output.WriteLine(File.ReadAllText("/etc/hosts"));
_output.WriteLine("--- /etc/resolv.conf ---");
_output.WriteLine(File.ReadAllText("/etc/resolv.conf"));
_output.WriteLine("------");
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TryGetAddrInfo_LocalHost(bool justAddresses)
{
SocketError error = NameResolutionPal.TryGetAddrInfo("localhost", justAddresses, AddressFamily.Unspecified, out string hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);
Assert.Equal(SocketError.Success, error);
if (!justAddresses)
{
Assert.NotNull(hostName);
}
Assert.NotNull(aliases);
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TryGetAddrInfo_EmptyHost(bool justAddresses)
{
SocketError error = NameResolutionPal.TryGetAddrInfo("", justAddresses, AddressFamily.Unspecified, out string hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);
if (error == SocketError.HostNotFound && !OperatingSystem.IsWindows())
{
// On Unix, we are not guaranteed to be able to resove the local host. The ability to do so depends on the
// machine configurations, which varies by distro and is often inconsistent.
return;
}
Assert.Equal(SocketError.Success, error);
if (!justAddresses)
{
Assert.NotNull(hostName);
}
Assert.NotNull(aliases);
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[OuterLoop("Uses external servers")]
public void TryGetAddrInfo_HostName(bool justAddresses)
{
string hostName = NameResolutionPal.GetHostName();
Assert.NotNull(hostName);
SocketError error = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, AddressFamily.Unspecified, out hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);
if (error == SocketError.HostNotFound && !OperatingSystem.IsWindows())
{
// On Unix, we are not guaranteed to be able to resove the local host. The ability to do so depends on the
// machine configurations, which varies by distro and is often inconsistent.
return;
}
Assert.Equal(SocketError.Success, error);
if (!justAddresses)
{
Assert.NotNull(hostName);
}
Assert.NotNull(aliases);
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TryGetAddrInfo_ExternalHost(bool justAddresses)
{
string hostName = "microsoft.com";
SocketError error = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, AddressFamily.Unspecified, out hostName, out string[] aliases, out IPAddress[] addresses, out _);
Assert.Equal(SocketError.Success, error);
Assert.NotNull(aliases);
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[OuterLoop("Uses external servers")]
public void TryGetAddrInfo_UnknownHost(bool justAddresses)
{
SocketError error = NameResolutionPal.TryGetAddrInfo("test.123", justAddresses, AddressFamily.Unspecified, out string? _, out string[] _, out IPAddress[] _, out int nativeErrorCode);
Assert.Equal(SocketError.HostNotFound, error);
Assert.NotEqual(0, nativeErrorCode);
}
[Fact]
public void TryGetNameInfo_LocalHost_IPv4()
{
SocketError error;
int nativeErrorCode;
string name = NameResolutionPal.TryGetNameInfo(new IPAddress(new byte[] { 127, 0, 0, 1 }), out error, out nativeErrorCode);
Assert.Equal(SocketError.Success, error);
Assert.NotNull(name);
}
[ConditionalFact(nameof(Ipv6LocalHostNameLookupNotBrokenByNrpRule))]
public void TryGetNameInfo_LocalHost_IPv6()
{
SocketError error;
int nativeErrorCode;
string name = NameResolutionPal.TryGetNameInfo(new IPAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), out error, out nativeErrorCode);
if (SocketError.Success != error && Environment.OSVersion.Platform == PlatformID.Unix)
{
LogUnixInfo();
}
Assert.Equal(SocketError.Success, error);
Assert.NotNull(name);
}
[Fact]
public void TryGetAddrInfo_LocalHost_TryGetNameInfo()
{
SocketError error = NameResolutionPal.TryGetAddrInfo("localhost", justAddresses: false, AddressFamily.Unspecified, out string hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);
Assert.Equal(SocketError.Success, error);
Assert.NotNull(hostName);
Assert.NotNull(aliases);
Assert.NotNull(addresses);
// Not all addresses returned by TryGetAddInfo can be resolved to host names, depending on network configuration.
// However at least one should be.
string name = null;
foreach(IPAddress address in addresses)
{
name = NameResolutionPal.TryGetNameInfo(address, out error, out nativeErrorCode);
if (error != SocketError.HostNotFound)
{
break;
}
}
Assert.Equal(SocketError.Success, error);
Assert.NotNull(name);
}
[Fact]
[OuterLoop("Uses external servers")]
public void TryGetAddrInfo_HostName_TryGetNameInfo()
{
string hostName = NameResolutionPal.GetHostName();
Assert.NotNull(hostName);
SocketError error = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses: false, AddressFamily.Unspecified, out hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);
if (error == SocketError.HostNotFound)
{
// On Unix, getaddrinfo returns host not found, if all the machine discovery settings on the local network
// is turned off. Hence dns lookup for it's own hostname fails.
Assert.Equal(PlatformID.Unix, Environment.OSVersion.Platform);
return;
}
Assert.Equal(SocketError.Success, error);
Assert.NotNull(hostName);
Assert.NotNull(aliases);
Assert.NotNull(addresses);
// Not all addresses returned by TryGetAddInfo can be resolved to host names, depending on network configuration.
// However at least one should be.
string name = null;
foreach(IPAddress address in addresses)
{
name = NameResolutionPal.TryGetNameInfo(address, out error, out nativeErrorCode);
if (error != SocketError.HostNotFound)
{
break;
}
}
if (error == SocketError.HostNotFound && Environment.OSVersion.Platform == PlatformID.Unix)
{
// On Unix, getaddrinfo returns private ipv4 address for hostname. If the OS doesn't have the
// reverse dns lookup entry for this address, getnameinfo returns host not found.
return;
}
Assert.Equal(SocketError.Success, error);
Assert.NotNull(name);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TryGetNameInfo_LocalHost_IPv4_TryGetAddrInfo(bool justAddresses)
{
string name = NameResolutionPal.TryGetNameInfo(new IPAddress(new byte[] { 127, 0, 0, 1 }), out SocketError error, out _);
Assert.Equal(SocketError.Success, error);
Assert.NotNull(name);
error = NameResolutionPal.TryGetAddrInfo(name, justAddresses, AddressFamily.Unspecified, out string hostName, out string[] aliases, out IPAddress[] addresses, out _);
Assert.Equal(SocketError.Success, error);
Assert.NotNull(aliases);
Assert.NotNull(addresses);
}
[ConditionalTheory(nameof(Ipv6LocalHostNameLookupNotBrokenByNrpRule))]
[InlineData(false)]
[InlineData(true)]
public void TryGetNameInfo_LocalHost_IPv6_TryGetAddrInfo(bool justAddresses)
{
SocketError error;
int nativeErrorCode;
string name = NameResolutionPal.TryGetNameInfo(new IPAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), out error, out nativeErrorCode);
if (SocketError.Success != error && Environment.OSVersion.Platform == PlatformID.Unix)
{
LogUnixInfo();
}
Assert.Equal(SocketError.Success, error);
Assert.NotNull(name);
error = NameResolutionPal.TryGetAddrInfo(name, justAddresses, AddressFamily.Unspecified, out string hostName, out string[] aliases, out IPAddress[] addresses, out _);
if (SocketError.Success != error && Environment.OSVersion.Platform == PlatformID.Unix)
{
LogUnixInfo();
}
Assert.Equal(SocketError.Success, error);
Assert.NotNull(aliases);
Assert.NotNull(addresses);
}
[Fact]
public void HostName_NotNull()
{
Assert.NotNull(NameResolutionPal.GetHostName());
}
#pragma warning disable CS0162 // Unreachable code detected -- SupportsGetAddrInfoAsync is a constant on *nix.
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task GetAddrInfoAsync_LocalHost(bool justAddresses)
{
if (!NameResolutionPal.SupportsGetAddrInfoAsync)
{
return;
}
if (justAddresses)
{
IPAddress[] addresses = await ((Task<IPAddress[]>)NameResolutionPal.GetAddrInfoAsync("localhost", justAddresses, AddressFamily.Unspecified, CancellationToken.None)).ConfigureAwait(false);
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
else
{
IPHostEntry hostEntry = await ((Task<IPHostEntry>)NameResolutionPal.GetAddrInfoAsync("localhost", justAddresses, AddressFamily.Unspecified, CancellationToken.None)).ConfigureAwait(false);
Assert.NotNull(hostEntry);
Assert.True(hostEntry.AddressList.Length > 0);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[OuterLoop("Uses external servers")]
public async Task GetAddrInfoAsync_EmptyHost(bool justAddresses)
{
if (!NameResolutionPal.SupportsGetAddrInfoAsync)
{
return;
}
Task task = NameResolutionPal.GetAddrInfoAsync("", justAddresses, AddressFamily.Unspecified, CancellationToken.None);
try
{
await task.ConfigureAwait(false);
}
catch (SocketException ex)
{
SocketError error = ex.SocketErrorCode;
if (error == SocketError.HostNotFound && !OperatingSystem.IsWindows())
{
// On Unix, we are not guaranteed to be able to resolve the local host. The ability to do so depends on the
// machine configurations, which varies by distro and is often inconsistent.
return;
}
throw;
}
if (justAddresses)
{
IPAddress[] addresses = ((Task<IPAddress[]>)task).Result;
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
else
{
IPHostEntry hostEntry = ((Task<IPHostEntry>)task).Result;
Assert.NotNull(hostEntry);
Assert.True(hostEntry.AddressList.Length > 0);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[OuterLoop("Uses external servers")]
public async Task GetAddrInfoAsync_HostName(bool justAddresses)
{
if (!NameResolutionPal.SupportsGetAddrInfoAsync)
{
return;
}
string hostName = NameResolutionPal.GetHostName();
Assert.NotNull(hostName);
Task task = NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, AddressFamily.Unspecified, CancellationToken.None);
try
{
await task.ConfigureAwait(false);
}
catch (SocketException ex)
{
SocketError error = ex.SocketErrorCode;
if (error == SocketError.HostNotFound && !OperatingSystem.IsWindows())
{
// On Unix, we are not guaranteed to be able to resolve the local host. The ability to do so depends on the
// machine configurations, which varies by distro and is often inconsistent.
return;
}
throw;
}
if (justAddresses)
{
IPAddress[] addresses = ((Task<IPAddress[]>)task).Result;
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
else
{
IPHostEntry hostEntry = ((Task<IPHostEntry>)task).Result;
Assert.NotNull(hostEntry);
Assert.True(hostEntry.AddressList.Length > 0);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task GetAddrInfoAsync_ExternalHost(bool justAddresses)
{
if (!NameResolutionPal.SupportsGetAddrInfoAsync)
{
return;
}
const string hostName = "microsoft.com";
if (!NameResolutionPal.SupportsGetAddrInfoAsync)
{
return;
}
if (justAddresses)
{
IPAddress[] addresses = await ((Task<IPAddress[]>)NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, AddressFamily.Unspecified, CancellationToken.None)).ConfigureAwait(false);
Assert.NotNull(addresses);
Assert.True(addresses.Length > 0);
}
else
{
IPHostEntry hostEntry = await ((Task<IPHostEntry>)NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, AddressFamily.Unspecified, CancellationToken.None)).ConfigureAwait(false);
Assert.NotNull(hostEntry);
Assert.True(hostEntry.AddressList.Length > 0);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[OuterLoop("Uses external servers")]
public async Task GetAddrInfoAsync_UnknownHost(bool justAddresses)
{
if (!NameResolutionPal.SupportsGetAddrInfoAsync)
{
return;
}
const string hostName = "test.123";
SocketException socketException = await Assert.ThrowsAsync<SocketException>(() => NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, AddressFamily.Unspecified, CancellationToken.None)).ConfigureAwait(false);
SocketError socketError = socketException.SocketErrorCode;
Assert.Equal(SocketError.HostNotFound, socketError);
}
#pragma warning restore CS0162
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)]
public void Exception_HostNotFound_Success()
{
var ex = new SocketException((int)SocketError.HostNotFound);
Assert.Equal(-1, ex.Message.IndexOf("Device"));
}
}
}