public void IPv4TcpLoopbackConsecutiveSendReceiveTest()
        {
            CancellationTokenSource cancelServer = new CancellationTokenSource();
            var serverChannel = new SIPTCPChannel(IPAddress.Loopback, 7064);

            serverChannel.DisableLocalTCPSocketsCheck = true;

            var serverTask = Task.Run(() => { RunServer(serverChannel, cancelServer); });

            for (int i = 1; i < 3; i++)
            {
                TaskCompletionSource <bool> testComplete = new TaskCompletionSource <bool>();

                var clientChannel = new SIPTCPChannel(IPAddress.Any, 7065);
                clientChannel.DisableLocalTCPSocketsCheck = true;
                SIPURI serverUri = serverChannel.GetContactURI(SIPSchemesEnum.sip, IPAddress.Loopback);

                logger.LogDebug($"Server URI {serverUri.ToString()}.");

                var clientTask = Task.Run(async() => { await RunClient(clientChannel, serverUri, testComplete); });
                Task.WhenAny(new Task[] { clientTask, Task.Delay(TRANSPORT_TEST_TIMEOUT) }).Wait();

                if (testComplete.Task.IsCompleted == false)
                {
                    // The client did not set the completed signal. This means the delay task must have completed and hence the test failed.
                    testComplete.SetResult(false);
                }

                Assert.True(testComplete.Task.Result);

                logger.LogDebug($"Completed for test run {i}.");
            }

            cancelServer.Cancel();
        }
        public void IPv4TcpLoopbackSendReceiveTest()
        {
            CancellationTokenSource     cancelServer = new CancellationTokenSource();
            TaskCompletionSource <bool> testComplete = new TaskCompletionSource <bool>();

            var serverChannel = new SIPTCPChannel(IPAddress.Loopback, 7060);

            serverChannel.DisableLocalTCPSocketsCheck = true;
            var clientChannel = new SIPTCPChannel(IPAddress.Loopback, 7061);

            clientChannel.DisableLocalTCPSocketsCheck = true;

            Task.Run(() => { RunServer(serverChannel, cancelServer); });
            var clientTask = Task.Run(async() => { await RunClient(clientChannel, serverChannel.GetContactURI(SIPSchemesEnum.sip, IPAddress.Loopback), testComplete); });

            Task.WhenAny(new Task[] { clientTask, Task.Delay(TRANSPORT_TEST_TIMEOUT) }).Wait();

            if (testComplete.Task.IsCompleted == false)
            {
                // The client did not set the completed signal. This means the delay task must have completed and hence the test failed.
                testComplete.SetResult(false);
            }

            Assert.True(testComplete.Task.Result);

            cancelServer.Cancel();
        }
        public void IPv4TcpLoopbackConsecutiveSendReceiveTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            // This test fails on WSL and Linux due to closed TCP sockets going into the TIME_WAIT state.
            // See comment in SIPTCPChannel.OnSIPStreamDisconnected for additional info.
            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                ManualResetEventSlim    serverReadyEvent = new ManualResetEventSlim(false);
                CancellationTokenSource cancelServer     = new CancellationTokenSource();
                var serverChannel = new SIPTCPChannel(IPAddress.Loopback, 0);
                serverChannel.DisableLocalTCPSocketsCheck = true;

                var serverTask = Task.Run(() => { RunServer(serverChannel, cancelServer, serverReadyEvent); });

                for (int i = 1; i < 3; i++)
                {
                    TaskCompletionSource <bool> testComplete = new TaskCompletionSource <bool>();

                    var clientChannel = new SIPTCPChannel(IPAddress.Loopback, 0);
                    clientChannel.DisableLocalTCPSocketsCheck = true;
                    SIPURI serverUri = serverChannel.GetContactURI(SIPSchemesEnum.sip, new SIPEndPoint(SIPProtocolsEnum.tcp, new IPEndPoint(IPAddress.Loopback, 0)));

                    logger.LogDebug($"Server URI {serverUri.ToString()}.");

                    var clientTask = Task.Run(async() => { await RunClient(clientChannel, serverUri, testComplete, cancelServer, serverReadyEvent); });

                    serverReadyEvent.Wait();
                    if (!Task.WhenAny(new Task[] { clientTask }).Wait(TRANSPORT_TEST_TIMEOUT))
                    {
                        logger.LogWarning($"Tasks timed out");
                    }

                    if (testComplete.Task.IsCompleted == false)
                    {
                        // The client did not set the completed signal. This means the delay task must have completed and hence the test failed.
                        testComplete.SetResult(false);
                    }

                    Assert.True(testComplete.Task.Result);

                    logger.LogDebug($"Completed for test run {i}.");

                    Task.Delay(1000).Wait();
                }

                cancelServer.Cancel();
            }
        }
Beispiel #4
0
        public void IPv6TcpLoopbackSendReceiveTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            if (!Socket.OSSupportsIPv6)
            {
                logger.LogDebug("Test skipped as OS does not support IPv6.");
            }
            else
            {
                ManualResetEventSlim        serverReadyEvent = new ManualResetEventSlim(false);
                CancellationTokenSource     cancelServer     = new CancellationTokenSource();
                TaskCompletionSource <bool> testComplete     = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

                var serverChannel = new SIPTCPChannel(IPAddress.IPv6Loopback, 0);
                serverChannel.DisableLocalTCPSocketsCheck = true;
                var clientChannel = new SIPTCPChannel(IPAddress.IPv6Loopback, 0);
                clientChannel.DisableLocalTCPSocketsCheck = true;

                var serverTask = Task.Run(() => { RunServer(serverChannel, cancelServer, serverReadyEvent); });
                var clientTask = Task.Run(async() =>
                {
#pragma warning disable RCS1090 // Add call to 'ConfigureAwait' (or vice versa).
                    await RunClient(
                        clientChannel,
                        serverChannel.GetContactURI(SIPSchemesEnum.sip, new SIPEndPoint(SIPProtocolsEnum.tcp, new IPEndPoint(IPAddress.IPv6Loopback, 0))),
                        testComplete,
                        cancelServer,
                        serverReadyEvent);
#pragma warning restore RCS1090 // Add call to 'ConfigureAwait' (or vice versa).
                });

                serverReadyEvent.Wait();
                if (!Task.WhenAny(new Task[] { serverTask, clientTask }).Wait(TRANSPORT_TEST_TIMEOUT))
                {
                    logger.LogWarning($"Tasks timed out");
                }

                if (testComplete.Task.IsCompleted == false)
                {
                    // The client did not set the completed signal. This means the delay task must have completed and hence the test failed.
                    testComplete.SetResult(false);
                }

                Assert.True(testComplete.Task.Result);

                cancelServer.Cancel();
            }
        }
        public void IPv4TcpLoopbackSendReceiveTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            ManualResetEventSlim        serverReadyEvent = new ManualResetEventSlim(false);
            CancellationTokenSource     cancelServer     = new CancellationTokenSource();
            TaskCompletionSource <bool> testComplete     = new TaskCompletionSource <bool>();

            var serverChannel = new SIPTCPChannel(IPAddress.Loopback, 0);

            serverChannel.DisableLocalTCPSocketsCheck = true;
            var clientChannel = new SIPTCPChannel(IPAddress.Loopback, 0);

            clientChannel.DisableLocalTCPSocketsCheck = true;

            Task.Run(() => { RunServer(serverChannel, cancelServer, serverReadyEvent); });
            var clientTask = Task.Run(async() => { await RunClient(
                                                       clientChannel,
                                                       serverChannel.GetContactURI(SIPSchemesEnum.sip, new SIPEndPoint(SIPProtocolsEnum.tcp, new IPEndPoint(IPAddress.Loopback, 0))),
                                                       testComplete,
                                                       cancelServer,
                                                       serverReadyEvent); });

            serverReadyEvent.Wait();
            if (!Task.WhenAny(new Task[] { clientTask }).Wait(TRANSPORT_TEST_TIMEOUT))
            {
                logger.LogWarning($"Tasks timed out");
            }

            if (testComplete.Task.IsCompleted == false)
            {
                // The client did not set the completed signal. This means the delay task must have completed and hence the test failed.
                testComplete.SetResult(false);
            }

            Assert.True(testComplete.Task.Result);

            cancelServer.Cancel();
        }
        public void TcpTrickleReceiveTest()
        {
            TaskCompletionSource <bool> testComplete = new TaskCompletionSource <bool>();

            IPEndPoint listenEP   = new IPEndPoint(IPAddress.Loopback, 9067);
            var        transport  = new SIPTransport();
            var        tcpChannel = new SIPTCPChannel(new IPEndPoint(IPAddress.Loopback, 9066));

            tcpChannel.DisableLocalTCPSocketsCheck = true;
            transport.AddSIPChannel(tcpChannel);

            int requestCount  = 10;
            int recvdReqCount = 0;

            Task.Run(() =>
            {
                try
                {
                    TcpListener listener = new TcpListener(listenEP);
                    listener.Start();
                    var tcpClient = listener.AcceptTcpClient();
                    logger.LogDebug($"Dummy TCP listener accepted client with remote end point {tcpClient.Client.RemoteEndPoint}.");
                    for (int i = 0; i < requestCount; i++)
                    {
                        logger.LogDebug($"Sending request {i}.");

                        var req         = transport.GetRequest(SIPMethodsEnum.OPTIONS, tcpChannel.GetContactURI(SIPSchemesEnum.sip, listenEP.Address));
                        byte[] reqBytes = Encoding.UTF8.GetBytes(req.ToString());

                        tcpClient.GetStream().Write(reqBytes, 0, reqBytes.Length);
                        tcpClient.GetStream().Flush();

                        Task.Delay(30).Wait();
                    }
                    tcpClient.GetStream().Close();
                }
                catch (Exception excp)
                {
                    logger.LogError($"Exception on dummy TCP listener task. {excp.Message}");
                    testComplete.SetResult(false);
                }
            });

            transport.SIPTransportRequestReceived += (SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest) =>
            {
                logger.LogDebug($"Request received {localSIPEndPoint.ToString()}<-{remoteEndPoint.ToString()}: {sipRequest.StatusLine}");
                logger.LogDebug(sipRequest.ToString());
                Interlocked.Increment(ref recvdReqCount);

                if (recvdReqCount == requestCount)
                {
                    testComplete.SetResult(true);
                }
            };

            transport.SIPTransportResponseReceived += (SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponse sipResponse) =>
            {
                logger.LogDebug($"Response received {localSIPEndPoint.ToString()}<-{remoteEndPoint.ToString()}: {sipResponse.ShortDescription}");
                logger.LogDebug(sipResponse.ToString());
            };

            tcpChannel.ConnectClientAsync(listenEP, null, null).Wait();

            Task.WhenAny(new Task[] { testComplete.Task, Task.Delay(TRANSPORT_TEST_TIMEOUT) }).Wait();

            transport.Shutdown();

            // Give the SIP transport time to shutdown. Keeps exception messages out of the logs.
            Task.Delay(500).Wait();

            Assert.True(testComplete.Task.IsCompleted);
            Assert.True(testComplete.Task.Result);
            Assert.True(requestCount == recvdReqCount, $"The count of {recvdReqCount} for the requests received did not match what was expected.");
        }