Skip to content

Commit

Permalink
Robustify a name resolution test (#63259)
Browse files Browse the repository at this point in the history
* fix TryGetAddrInfo_HostName_TryGetNameInfo()

* Fix network test
  • Loading branch information
danmoseley authored Jan 4, 2022
1 parent a76387e commit f686f65
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,18 @@ public void TryGetAddrInfo_LocalHost_TryGetNameInfo()
Assert.NotNull(aliases);
Assert.NotNull(addresses);

string name = NameResolutionPal.TryGetNameInfo(addresses[0], out error, out nativeErrorCode);
// 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);
}
Expand All @@ -181,12 +192,22 @@ public void TryGetAddrInfo_HostName_TryGetNameInfo()
Assert.NotNull(aliases);
Assert.NotNull(addresses);

string name = NameResolutionPal.TryGetNameInfo(addresses[0], out error, out nativeErrorCode);
if (error == SocketError.HostNotFound)
// 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.
Assert.Equal(PlatformID.Unix, Environment.OSVersion.Platform);
return;
}

Expand Down

0 comments on commit f686f65

Please sign in to comment.