Repository: dotnet/runtime
File: src/libraries/System.Net.Sockets/tests/FunctionalTests/OSSupport.cs (C#)

Code before:
        [Fact]
        public void SupportsIPv6_MatchesOSSupportsIPv6()
        {
#pragma warning disable 0618 // Supports* are obsoleted
            Assert.Equal(Socket.SupportsIPv6, Socket.OSSupportsIPv6);
#pragma warning restore
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public async Task DisableIPv6_OSSupportsIPv6_False()
        {
            RemoteInvokeOptions options = new RemoteInvokeOptions();
            options.StartInfo.EnvironmentVariables["DOTNET_SYSTEM_NET_DISABLEIPV6"] = "1";
            await RemoteExecutor.Invoke(RunTest, options).DisposeAsync();

            static void RunTest()
            {
                Assert.False(Socket.OSSupportsIPv6);
            }
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public async Task DisableIPv6_SocketConstructor_CreatesIPv4Socket()
        {
            await RemoteExecutor.Invoke(RunTest).DisposeAsync();

            static void RunTest()
            {
                AppContext.SetSwitch("System.Net.DisableIPv6", true);
                using Socket socket1 = new Socket(SocketType.Stream, ProtocolType.Tcp);
                using Socket socket2 = new Socket(SocketType.Dgram, ProtocolType.Udp);

                Assert.Equal(AddressFamily.InterNetwork, socket1.AddressFamily);
                Assert.Equal(AddressFamily.InterNetwork, socket2.AddressFamily);
                Assert.False(socket1.DualMode);
                Assert.False(socket2.DualMode);
            }
        }

        [Fact]
        [ActiveIssue("https://github.com/dotnet/runtime/issues/107981", TestPlatforms.Wasi)] // https://github.com/WebAssembly/wasi-libc/issues/538
        public void IOControl_FIONREAD_Success()
        {
            using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.DataToRead, null, null));
                Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.DataToRead, null, new byte[0]));
                Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.DataToRead, null, new byte[sizeof(int) - 1]));

                byte[] fionreadResult = new byte[sizeof(int)];


Review comment (suggestion):
This test creates an IPv6 socket and performs socket operations when OSSupportsIPv6 is false, but doesn't assert that these operations actually succeed. The test should verify that setting DualMode = true doesn't throw an exception, and that the socket operations (Bind, Listen) also succeed. Consider adding assertions or wrapping the code to explicitly verify no exceptions are thrown, as the test's intent is to verify that DualMode can be set permissively even when OSSupportsIPv6 is false.

Code after:
        [Fact]
        public void SupportsIPv6_MatchesOSSupportsIPv6()
        {
#pragma warning disable 0618 // Supports* are obsoleted
            Assert.Equal(Socket.SupportsIPv6, Socket.OSSupportsIPv6);
#pragma warning restore
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public async Task DisableIPv6_OSSupportsIPv6_False()
        {
            RemoteInvokeOptions options = new RemoteInvokeOptions();
            options.StartInfo.EnvironmentVariables["DOTNET_SYSTEM_NET_DISABLEIPV6"] = "1";
            await RemoteExecutor.Invoke(RunTest, options).DisposeAsync();

            static void RunTest()
            {
                Assert.False(Socket.OSSupportsIPv6);

                // related to https://github.com/dotnet/runtime/issues/122435
                var listenSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                listenSocket.DualMode = true;

                listenSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
                listenSocket.Listen(1);
                listenSocket.Close();
            }
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public async Task DisableIPv6_SocketConstructor_CreatesIPv4Socket()
        {
            await RemoteExecutor.Invoke(RunTest).DisposeAsync();

            static void RunTest()
            {
                AppContext.SetSwitch("System.Net.DisableIPv6", true);
                using Socket socket1 = new Socket(SocketType.Stream, ProtocolType.Tcp);
                using Socket socket2 = new Socket(SocketType.Dgram, ProtocolType.Udp);

                Assert.Equal(AddressFamily.InterNetwork, socket1.AddressFamily);
                Assert.Equal(AddressFamily.InterNetwork, socket2.AddressFamily);
                Assert.False(socket1.DualMode);
                Assert.False(socket2.DualMode);
            }
        }

        [Fact]
        [ActiveIssue("https://github.com/dotnet/runtime/issues/107981", TestPlatforms.Wasi)] // https://github.com/WebAssembly/wasi-libc/issues/538
        public void IOControl_FIONREAD_Success()
        {