<commit_before>﻿// 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.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();

            TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(localIPAddress));
        }

        [Fact]
        public void Dns_GetHostEntryAsync_HostString_Ok()
        {
            TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(TestSettings.LocalHost));
        }

        [Fact]
        public void Dns_GetHostEntryAsync_IPString_Ok()
        {
            TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(TestSettings.LocalIPString));
        }

        private static void TestGetHostEntryAsync(Func<Task<IPHostEntry>> getHostEntryFunc)
        {
            Task<IPHostEntry> hostEntryTask1 = getHostEntryFunc();
            Task<IPHostEntry> hostEntryTask2 = getHostEntryFunc();

            Task.WaitAll(hostEntryTask1, hostEntryTask2);

            IPAddress[] list1 = hostEntryTask1.Result.AddressList;
            IPAddress[] list2 = hostEntryTask2.Result.AddressList;

            Assert.NotNull(list1);
            Assert.NotNull(list2);

            Assert.Equal(list1.Length, list2.Length);
            for (var i = 0; i < list1.Length; i++)
            {
                Assert.Equal(list1[i], list2[i]);
            }
        }

        [Fact]
        public async Task Dns_GetHostEntryAsync_NullStringHost_Fail()
        {
            await Assert.ThrowsAsync<ArgumentNullException>(() => Dns.GetHostEntryAsync((string)null));
        }

        [Fact]
        public async Task Dns_GetHostEntryAsync_NullIPAddressHost_Fail()
        {
            await Assert.ThrowsAsync<ArgumentNullException>(() => Dns.GetHostEntryAsync((IPAddress)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_GetHostEntryAsync_AnyIPAddress_Fail(IPAddress address)
        {
            string addressString = address.ToString();

            await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(address));
            await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(addressString));
        }

        public static IEnumerable<object[]> GetNoneAddresses()
        {
            yield return new object[] { IPAddress.None };
        }

        [ActiveIssue(10345, TestPlatforms.AnyUnix)]
        [Theory]
        [MemberData(nameof(GetNoneAddresses))]
        public async Task Dns_GetHostEntryAsync_NoneIPAddress_Fail(IPAddress address)
        {
            string addressString = address.ToString();

            await Assert.ThrowsAsync<SocketException>(() => Dns.GetHostEntryAsync(address));
            await Assert.ThrowsAsync<SocketException>(() => Dns.GetHostEntryAsync(addressString));
        }

        [Fact]
        public void DnsBeginGetHostEntry_BadName_Throws()
        {
            IAsyncResult asyncObject = Dns.BeginGetHostEntry("BadName", null, null);
            Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostEntry(asyncObject));
        }

        [Fact]
        public void DnsBeginGetHostEntry_BadIpString_Throws()
        {
            IAsyncResult asyncObject = Dns.BeginGetHostEntry("0.0.1.1", null, null);
            Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostEntry(asyncObject));
        }

        [Fact]
        public void DnsBeginGetHostEntry_MachineName_MatchesGetHostEntry()
        {
            IAsyncResult asyncObject = Dns.BeginGetHostEntry(TestSettings.LocalHost, null, null);
            IPHostEntry results = Dns.EndGetHostEntry(asyncObject);
            IPHostEntry entry = Dns.GetHostEntry(TestSettings.LocalHost);
<commit_msg>Fix Dns_GetHostEntryAsync_NoneIPAddress_Fail test on Unix<commit_after>﻿// 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.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();

            TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(localIPAddress));
        }

        [Fact]
        public void Dns_GetHostEntryAsync_HostString_Ok()
        {
            TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(TestSettings.LocalHost));
        }

        [Fact]
        public void Dns_GetHostEntryAsync_IPString_Ok()
        {
            TestGetHostEntryAsync(() => Dns.GetHostEntryAsync(TestSettings.LocalIPString));
        }

        private static void TestGetHostEntryAsync(Func<Task<IPHostEntry>> getHostEntryFunc)
        {
            Task<IPHostEntry> hostEntryTask1 = getHostEntryFunc();
            Task<IPHostEntry> hostEntryTask2 = getHostEntryFunc();

            Task.WaitAll(hostEntryTask1, hostEntryTask2);

            IPAddress[] list1 = hostEntryTask1.Result.AddressList;
            IPAddress[] list2 = hostEntryTask2.Result.AddressList;

            Assert.NotNull(list1);
            Assert.NotNull(list2);

            Assert.Equal(list1.Length, list2.Length);
            for (var i = 0; i < list1.Length; i++)
            {
                Assert.Equal(list1[i], list2[i]);
            }
        }

        [Fact]
        public async Task Dns_GetHostEntryAsync_NullStringHost_Fail()
        {
            await Assert.ThrowsAsync<ArgumentNullException>(() => Dns.GetHostEntryAsync((string)null));
        }

        [Fact]
        public async Task Dns_GetHostEntryAsync_NullIPAddressHost_Fail()
        {
            await Assert.ThrowsAsync<ArgumentNullException>(() => Dns.GetHostEntryAsync((IPAddress)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_GetHostEntryAsync_AnyIPAddress_Fail(IPAddress address)
        {
            string addressString = address.ToString();

            await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(address));
            await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(addressString));
        }

        public static IEnumerable<object[]> GetNoneAddresses()
        {
            yield return new object[] { IPAddress.None };
        }

        [PlatformSpecific(~TestPlatforms.OSX)] // macOS will resolve IPAddress.None to broadcasthost and produce a valid listing
        [Theory]
        [MemberData(nameof(GetNoneAddresses))]
        public async Task Dns_GetHostEntryAsync_NoneIPAddress_Fail(IPAddress address)
        {
            string addressString = address.ToString();

            await Assert.ThrowsAnyAsync<SocketException>(() => Dns.GetHostEntryAsync(address));
            await Assert.ThrowsAnyAsync<SocketException>(() => Dns.GetHostEntryAsync(addressString));
        }

        [PlatformSpecific(TestPlatforms.OSX)] // macOS will resolve IPAddress.None to broadcasthost and produce a valid listing
        [Theory]
        [MemberData(nameof(GetNoneAddresses))]
        public async Task Dns_GetHostEntryAsync_NoneIPAddress_Success(IPAddress address)
        {
            IPHostEntry result = await Dns.GetHostEntryAsync(address);
            Assert.NotNull(result);
            Assert.NotNull(result.AddressList);
            Assert.Equal(1, result.AddressList.Length);
            Assert.Equal(address, result.AddressList[0]);
        }

        [Fact]
        public void DnsBeginGetHostEntry_BadName_Throws()
        {
            IAsyncResult asyncObject = Dns.BeginGetHostEntry("BadName", null, null);
            Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostEntry(asyncObject));
        }

        [Fact]
        public void DnsBeginGetHostEntry_BadIpString_Throws()
        {
            IAsyncResult asyncObject = Dns.BeginGetHostEntry("0.0.1.1", null, null);
            Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostEntry(asyncObject));
        }

        [Fact]
        public void DnsBeginGetHostEntry_MachineName_MatchesGetHostEntry()
        {
            IAsyncResult asyncObject = Dns.BeginGetHostEntry(TestSettings.LocalHost, null, null);
            IPHostEntry results = Dns.EndGetHostEntry(asyncObject);
            IPHostEntry entry = Dns.GetHostEntry(TestSettings.LocalHost);

{'fraction_non_alphanumeric': 0.09365687526607067, 'fraction_numerical': 0.005427841634738187, 'mean_word_length': 2.611452728670254, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6648990', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2537, 'n_words': 477}