<commit_before>﻿namespace Stumps.Server
{

    using System;
    using System.Net;
    using System.Net.Sockets;
    using NSubstitute;
    using NUnit.Framework;
    using Stumps;
    using Stumps.Server.Data;
    using Stumps.Server.Logging;

    [TestFixture]
    public class StumpsHostTests
    {

        private readonly int _defaultPort;

        public StumpsHostTests()
        {

            var randomGenerator = new Random();
            _defaultPort = randomGenerator.Next(IPEndPoint.MinPort, IPEndPoint.MinPort + 1);

        }

        [Test]
        public void Constructor_NullHostName_ThrowsException()
        {
            StumpsHost proxy = new StumpsHost(Substitute.For<IServerFactory>(), Substitute.For<ILogger>(), Substitute.For<IDataAccess>());
            Assert.That(
                () => proxy.CreateServerInstance(null, _defaultPort, true, false),
                Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("externalHostName"));
        }

        [Test]
        public void Constructor_PortInUse_ThrowsException()
        {

            int port = NetworkInformation.FindRandomOpenPort();
            string externalHostName = "www.foo.com";
            bool autoStart = false;
            bool useSsl = true;

            var proxyEntity = new ProxyServerEntity
            {
                AutoStart = autoStart,
                ExternalHostName = externalHostName,
                Port = port,
                UseSsl = useSsl,
                ProxyId = Stumps.Server.Utility.RandomGenerator.GenerateIdentifier()
            };

            var dataAccess = Substitute.For<IDataAccess>();
            dataAccess.ProxyServerFind (Arg.Any<string>()).Returns(proxyEntity);

            // create a TcpListener already listening on the port
            var tcpListener = new TcpListener (port);
            try 
            {
                tcpListener.Start ();

                StumpsHost proxy = new StumpsHost(Substitute.For<IServerFactory>(), Substitute.For<ILogger>(), dataAccess);
                Assert.That(
                    () => proxy.CreateServerInstance(externalHostName, port, useSsl, autoStart),
                    Throws.Exception.TypeOf<StumpsNetworkException>().With.Property("Message").EqualTo("The port is already in use."));
            } 
            finally 
            {
                tcpListener.Stop ();
            }

        }

        [Test]
        public void Constructor_PortNumberRange_ThrowsException()
        {
            StumpsHost proxy = new StumpsHost(Substitute.For<IServerFactory>(), Substitute.For<ILogger>(), Substitute.For<IDataAccess>());
            Assert.That(
                () => proxy.CreateServerInstance("www.foo.com", IPEndPoint.MaxPort + 1, true, false),
                Throws.Exception.TypeOf<ArgumentOutOfRangeException>().With.Property("ParamName").EqualTo("port"));

            Assert.That(
                () => proxy.CreateServerInstance("www.foo.com", IPEndPoint.MinPort - 1, true, false),
                Throws.Exception.TypeOf<ArgumentOutOfRangeException>().With.Property("ParamName").EqualTo("port"));
        }

    }

}<commit_msg>Updated the host tests to use a non-depricated version of the TcpListener.<commit_after>﻿namespace Stumps.Server
{

    using System;
    using System.Net;
    using System.Net.Sockets;
    using NSubstitute;
    using NUnit.Framework;
    using Stumps;
    using Stumps.Server.Data;
    using Stumps.Server.Logging;

    [TestFixture]
    public class StumpsHostTests
    {

        private readonly int _defaultPort;

        public StumpsHostTests()
        {

            var randomGenerator = new Random();
            _defaultPort = randomGenerator.Next(IPEndPoint.MinPort, IPEndPoint.MinPort + 1);

        }

        [Test]
        public void Constructor_NullHostName_ThrowsException()
        {
            StumpsHost proxy = new StumpsHost(Substitute.For<IServerFactory>(), Substitute.For<ILogger>(), Substitute.For<IDataAccess>());
            Assert.That(
                () => proxy.CreateServerInstance(null, _defaultPort, true, false),
                Throws.Exception.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("externalHostName"));
        }

        [Test]
        public void Constructor_PortInUse_ThrowsException()
        {

            int port = NetworkInformation.FindRandomOpenPort();
            string externalHostName = "www.foo.com";
            bool autoStart = false;
            bool useSsl = true;

            var proxyEntity = new ProxyServerEntity
            {
                AutoStart = autoStart,
                ExternalHostName = externalHostName,
                Port = port,
                UseSsl = useSsl,
                ProxyId = Stumps.Server.Utility.RandomGenerator.GenerateIdentifier()
            };

            var dataAccess = Substitute.For<IDataAccess>();
            dataAccess.ProxyServerFind (Arg.Any<string>()).Returns(proxyEntity);

            // create a TcpListener already listening on the port
            var tcpListener = new TcpListener(IPAddress.Loopback, port);

            try 
            {
                tcpListener.Start();

                StumpsHost proxy = new StumpsHost(Substitute.For<IServerFactory>(), Substitute.For<ILogger>(), dataAccess);
                Assert.That(
                    () => proxy.CreateServerInstance(externalHostName, port, useSsl, autoStart),
                    Throws.Exception.TypeOf<StumpsNetworkException>().With.Property("Message").EqualTo("The port is already in use."));
            } 
            finally 
            {
                tcpListener.Stop();
            }

        }

        [Test]
        public void Constructor_PortNumberRange_ThrowsException()
        {
            StumpsHost proxy = new StumpsHost(Substitute.For<IServerFactory>(), Substitute.For<ILogger>(), Substitute.For<IDataAccess>());
            Assert.That(
                () => proxy.CreateServerInstance("www.foo.com", IPEndPoint.MaxPort + 1, true, false),
                Throws.Exception.TypeOf<ArgumentOutOfRangeException>().With.Property("ParamName").EqualTo("port"));

            Assert.That(
                () => proxy.CreateServerInstance("www.foo.com", IPEndPoint.MinPort - 1, true, false),
                Throws.Exception.TypeOf<ArgumentOutOfRangeException>().With.Property("ParamName").EqualTo("port"));
        }

    }

}
{'fraction_non_alphanumeric': 0.10392430587870327, 'fraction_numerical': 0.0009306654257794323, 'mean_word_length': 2.111969111969112, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '5521977', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1656, 'n_words': 301}