public async Task Send_Garbage()
        {
            using (var testEnvironment = new TestEnvironment())
            {
                await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(1)));

                // Send an invalid packet and ensure that the server will close the connection and stay in a waiting state
                // forever. This is security related.
                var client = new CrossPlatformSocket(AddressFamily.InterNetwork);
                await client.ConnectAsync("localhost", testEnvironment.ServerPort, CancellationToken.None);

                var buffer = Encoding.UTF8.GetBytes("Garbage");
                await client.SendAsync(new ArraySegment <byte>(buffer), SocketFlags.None);

                await Task.Delay(TimeSpan.FromSeconds(3));

                try
                {
                    var receivedBytes = await client.ReceiveAsync(new ArraySegment <byte>(new byte[10]), SocketFlags.Partial);

                    if (receivedBytes == 0)
                    {
                        return;
                    }

                    Assert.Fail("Receive should throw an exception.");
                }
                catch (SocketException)
                {
                }
            }
        }
        public async Task Close_Idle_Connection_On_Connect()
        {
            using (var testEnvironment = new TestEnvironment())
            {
                await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(1)));

                var client = new CrossPlatformSocket(AddressFamily.InterNetwork);
                await client.ConnectAsync("localhost", testEnvironment.ServerPort, CancellationToken.None);

                // Don't send anything. The server should close the connection.
                await Task.Delay(TimeSpan.FromSeconds(3));

                try
                {
                    var receivedBytes = await client.ReceiveAsync(new ArraySegment <byte>(new byte[10]), SocketFlags.Partial);

                    if (receivedBytes == 0)
                    {
                        return;
                    }

                    Assert.Fail("Receive should throw an exception.");
                }
                catch (SocketException)
                {
                }
            }
        }
示例#3
0
        public async Task Dispose_Channel_While_Used()
        {
            var ct           = new CancellationTokenSource();
            var serverSocket = new CrossPlatformSocket(AddressFamily.InterNetwork);

            try
            {
                serverSocket.Bind(new IPEndPoint(IPAddress.Any, 50001));
                serverSocket.Listen(0);

#pragma warning disable 4014
                Task.Run(async() =>
#pragma warning restore 4014
                {
                    while (!ct.IsCancellationRequested)
                    {
                        var client = await serverSocket.AcceptAsync();
                        var data   = new byte[] { 128 };
                        await client.SendAsync(new ArraySegment <byte>(data), SocketFlags.None);
                    }
                }, ct.Token);

                var clientSocket = new CrossPlatformSocket(AddressFamily.InterNetwork);
                await clientSocket.ConnectAsync("localhost", 50001, CancellationToken.None);

                var tcpChannel = new MqttTcpChannel(clientSocket.GetStream(), "test", null);

                await Task.Delay(100, ct.Token);

                var buffer = new byte[1];
                await tcpChannel.ReadAsync(buffer, 0, 1, ct.Token);

                Assert.AreEqual(128, buffer[0]);

                // This block should fail after dispose.
#pragma warning disable 4014
                Task.Run(() =>
#pragma warning restore 4014
                {
                    Task.Delay(200, ct.Token);
                    tcpChannel.Dispose();
                }, ct.Token);

                try
                {
                    await tcpChannel.ReadAsync(buffer, 0, 1, CancellationToken.None);
                }
                catch (Exception exception)
                {
                    Assert.IsInstanceOfType(exception, typeof(SocketException));
                    Assert.AreEqual(SocketError.OperationAborted, ((SocketException)exception).SocketErrorCode);
                }
            }
            finally
            {
                ct.Cancel(false);
                serverSocket.Dispose();
            }
        }
示例#4
0
        public async Task Try_Connect_Invalid_Host()
        {
            var crossPlatformSocket = new CrossPlatformSocket();

            var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            cancellationToken.Token.Register(() => crossPlatformSocket.Dispose());

            await crossPlatformSocket.ConnectAsync("www.google.de", 54321, cancellationToken.Token);
        }
示例#5
0
        public void Set_Options()
        {
            var crossPlatformSocket = new CrossPlatformSocket();

            Assert.IsFalse(crossPlatformSocket.ReuseAddress);
            crossPlatformSocket.ReuseAddress = true;
            Assert.IsTrue(crossPlatformSocket.ReuseAddress);

            Assert.IsFalse(crossPlatformSocket.NoDelay);
            crossPlatformSocket.NoDelay = true;
            Assert.IsTrue(crossPlatformSocket.NoDelay);
        }
示例#6
0
        public async Task Connect_Send_Receive()
        {
            var crossPlatformSocket = new CrossPlatformSocket();
            await crossPlatformSocket.ConnectAsync("www.google.de", 80, CancellationToken.None);

            var requestBuffer = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\nHost: www.google.de\r\n\r\n");
            await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);

            var buffer = new byte[1024];
            var length = await crossPlatformSocket.ReceiveAsync(new ArraySegment<byte>(buffer), System.Net.Sockets.SocketFlags.None);
            crossPlatformSocket.Dispose();

            var responseText = Encoding.UTF8.GetString(buffer, 0, length);

            Assert.IsTrue(responseText.Contains("HTTP/1.1 200 OK"));
        }