Beispiel #1
0
        public void GetHostByName_LocalHost_GetHostByAddr()
        {
            IPHostEntry hostEntry1 = NameResolutionPal.GetHostByName("localhost");

            Assert.NotNull(hostEntry1);
            IPHostEntry hostEntry2 = NameResolutionPal.GetHostByAddr(hostEntry1.AddressList[0]);

            Assert.NotNull(hostEntry2);

            IPAddress[] list1 = hostEntry1.AddressList;
            IPAddress[] list2 = hostEntry2.AddressList;

            for (int i = 0; i < list1.Length; i++)
            {
                Assert.NotEqual(-1, Array.IndexOf(list2, list1[i]));
            }
        }
Beispiel #2
0
        public void TryGetAddrInfo_HostName_TryGetNameInfo()
        {
            string hostName = NameResolutionPal.GetHostName();

            Assert.NotNull(hostName);

            IPHostEntry hostEntry;
            int         nativeErrorCode;
            SocketError error = NameResolutionPal.TryGetAddrInfo(hostName, out hostEntry, out nativeErrorCode);

            Assert.Equal(SocketError.Success, error);
            Assert.NotNull(hostEntry);

            string name = NameResolutionPal.TryGetNameInfo(hostEntry.AddressList[0], out error, out nativeErrorCode);

            Assert.Equal(SocketError.Success, error);
            Assert.NotNull(name);
        }
Beispiel #3
0
        public void TryGetAddrInfo_HostName_TryGetNameInfo()
        {
            string hostName = NameResolutionPal.GetHostName();

            Assert.NotNull(hostName);

            IPHostEntry hostEntry;
            int         nativeErrorCode;
            SocketError error = NameResolutionPal.TryGetAddrInfo(hostName, out hostEntry, out 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;
            }

            // Temporary instrumentation for #32797
            if (error == SocketError.TryAgain && Environment.OSVersion.Platform == PlatformID.Unix)
            {
                error = NameResolutionPal.TryGetAddrInfo(hostName, out hostEntry, out nativeErrorCode);
                if (error != SocketError.TryAgain)
                {
                    throw new InvalidOperationException("Name resolution failure preventable with retry");
                }
            }

            Assert.Equal(SocketError.Success, error);
            Assert.NotNull(hostEntry);

            string name = NameResolutionPal.TryGetNameInfo(hostEntry.AddressList[0], out error, out nativeErrorCode);

            if (error == SocketError.HostNotFound)
            {
                // 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;
            }

            Assert.Equal(SocketError.Success, error);
            Assert.NotNull(name);
        }
Beispiel #4
0
        public void GetHostByName_HostName_GetHostByAddr()
        {
            var hostName = NameResolutionPal.GetHostName();

            Assert.NotNull(hostName);

            var hostEntry1 = NameResolutionPal.GetHostByName(hostName);

            Assert.NotNull(hostEntry1);
            var hostEntry2 = NameResolutionPal.GetHostByAddr(hostEntry1.AddressList[0]);

            Assert.NotNull(hostEntry2);

            var list1 = hostEntry1.AddressList;
            var list2 = hostEntry2.AddressList;

            for (int i = 0; i < list1.Length; i++)
            {
                Assert.Equal(list1[i], list2[i]);
            }
        }
Beispiel #5
0
        public void GetHostByName_HostName_GetHostByAddr()
        {
            IPHostEntry hostEntry1 = NameResolutionPal.GetHostByName(HttpTestServers.Http2Host);

            Assert.NotNull(hostEntry1);

            IPAddress[] list1 = hostEntry1.AddressList;
            Assert.InRange(list1.Length, 1, Int32.MaxValue);

            foreach (IPAddress addr1 in list1)
            {
                IPHostEntry hostEntry2 = NameResolutionPal.GetHostByAddr(addr1);
                Assert.NotNull(hostEntry2);

                IPAddress[] list2 = hostEntry2.AddressList;
                Assert.InRange(list2.Length, 1, list1.Length);

                foreach (IPAddress addr2 in list2)
                {
                    Assert.NotEqual(-1, Array.IndexOf(list1, addr2));
                }
            }
        }
        public void TryGetAddrInfo_HostName()
        {
            string hostName = NameResolutionPal.GetHostName();

            Assert.NotNull(hostName);

            IPHostEntry hostEntry;
            int         nativeErrorCode;
            SocketError error = NameResolutionPal.TryGetAddrInfo(hostName, out hostEntry, out nativeErrorCode);

            if (error == SocketError.HostNotFound && (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)))
            {
                // 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);
            Assert.NotNull(hostEntry);
            Assert.NotNull(hostEntry.HostName);
            Assert.NotNull(hostEntry.AddressList);
            Assert.NotNull(hostEntry.Aliases);
        }
Beispiel #7
0
 public void GetHostByAddr_LocalHost()
 {
     Assert.NotNull(NameResolutionPal.GetHostByAddr(new IPAddress(0x0100007f)));
 }
Beispiel #8
0
 public void HostName_NotNull()
 {
     Assert.NotNull(NameResolutionPal.GetHostName());
 }
Beispiel #9
0
 static NameResolutionPalTests()
 {
     NameResolutionPal.EnsureSocketsAreInitialized();
 }
 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);
 public NameResolutionPalTests(ITestOutputHelper output)
 {
     NameResolutionPal.EnsureSocketsAreInitialized();
     _output = output;
 }
Beispiel #12
0
 public void Dns_Resolve_CallSocketInit_Ok()
 {
     NameResolutionPal.FakesReset();
     Assert.ThrowsAny <Exception>(() => Dns.Resolve(null));
     Assert.NotEqual(0, NameResolutionPal.FakesEnsureSocketsAreInitializedCallCount);
 }
Beispiel #13
0
 public void Dns_GetHostEntry_IPAddress_CallSocketInit_Ok()
 {
     NameResolutionPal.FakesReset();
     Assert.ThrowsAny <Exception>(() => Dns.GetHostEntry((IPAddress)null));
     Assert.NotEqual(0, NameResolutionPal.FakesEnsureSocketsAreInitializedCallCount);
 }
Beispiel #14
0
 public void Dns_BeginGetHostEntry_String_CallSocketInit_Ok()
 {
     NameResolutionPal.FakesReset();
     Assert.ThrowsAny <Exception>(() => Dns.BeginGetHostEntry((string)null, null, null));
     Assert.NotEqual(0, NameResolutionPal.FakesEnsureSocketsAreInitializedCallCount);
 }
Beispiel #15
0
 public void Dns_GetHostName_CallsSocketInit_Ok()
 {
     NameResolutionPal.FakesReset();
     Assert.ThrowsAny <Exception>(() => Dns.GetHostName());
     Assert.Equal(1, NameResolutionPal.FakesEnsureSocketsAreInitializedCallCount);
 }
 public void GetHostByName_InvalidHostName_Throws(string hostName)
 {
     Assert.ThrowsAny <SocketException>(() => NameResolutionPal.GetHostByName(hostName));
 }