<commit_before>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Net.Test.Common;

using Xunit;

namespace System.Net.Sockets.Tests
{
    public class SocketOptionNameTest
    {
        private static bool IsWindows10Platform
        {
            get
            {
                return Capability.IsWindows10Platform();
            }
        }

        private static bool IsNotWindows10Platform
        {
            get
            {
                return !Capability.IsWindows10Platform();
            }
        }

        [ConditionalFact("IsNotWindows10Platform")]
        public void ReuseUnicastPort_CreateSocketGetOption_NotWindows10Platform_SocketException()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            Assert.Throws<SocketException>(() =>
                socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
        }

        [ConditionalFact("IsWindows10Platform")]
        public void ReuseUnicastPort_CreateSocketGetOption_Windows10Platform_OptionIsZero()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            var optionValue = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort);
            Assert.Equal(0, optionValue);
        }

        [ConditionalFact("IsNotWindows10Platform")]
        public void ReuseUnicastPort_CreateSocketSetOption_NotWindows10Platform_SocketException()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            Assert.Throws<SocketException>(() =>
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
        }

        // TODO: Issue #4887
        // The socket option 'ReuseUnicastPost' only works on Windows 10 systems. In addition, setting the option
        // is a no-op unless specialized network settings using PowerShell configuration are first applied to the
        // machine. This is currently difficult to test in the CI environment. So, this ests will be disabled for now
        [ActiveIssue(4887)]
        public void ReuseUnicastPort_CreateSocketSetOptionToOneAndGetOption_Windows10Platform_OptionIsOne()<commit_msg>Adjust tests for Windows vs. non-Windows platforms<commit_after>// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Net.Test.Common;
using System.Runtime.InteropServices;

using Xunit;

namespace System.Net.Sockets.Tests
{
    public class SocketOptionNameTest
    {
        private static bool IsWindows10Platform
        {
            get
            {
                return Capability.IsWindows10Platform();
            }
        }

        private static bool IsNotWindows10Platform
        {
            get
            {
                return !Capability.IsWindows10Platform();
            }
        }

        [ConditionalFact("IsNotWindows10Platform")]
        public void ReuseUnicastPort_CreateSocketGetOption_NotWindows10Platform_Throws()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Assert.Throws<SocketException>(() =>
                    socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
            }
            else
            {
                Assert.Throws<PlatformNotSupportedException>(() =>
                    socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
            }
        }

        [ConditionalFact("IsWindows10Platform")]
        public void ReuseUnicastPort_CreateSocketGetOption_Windows10Platform_OptionIsZero()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            var optionValue = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort);
            Assert.Equal(0, optionValue);
        }

        [ConditionalFact("IsNotWindows10Platform")]
        public void ReuseUnicastPort_CreateSocketSetOption_NotWindows10Platform_Throws()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Assert.Throws<SocketException>(() =>
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
            }
            else
            {
                Assert.Throws<PlatformNotSupportedException>(() =>
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
            }
        }

        // TODO: Issue #4887
        // The socket option 'ReuseUnicastPost' only works on Windows 10 systems. In addition, setting the option
        // is a no-op unless specialized network settings using PowerShell configuration are first applied to the
        // machine. This is currently difficult to test in the CI environment. So, this ests will be disabled for now
        [ActiveIssue(4887)]
        public void ReuseUnicastPort_CreateSocketSetOptionToOneAndGetOption_Windows10Platform_OptionIsOne()
{'fraction_non_alphanumeric': 0.07002111189303308, 'fraction_numerical': 0.012139338494018296, 'mean_word_length': 2.4288299155609168, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6440752', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1393, 'n_words': 314}