示例#1
0
        public void WhenBufferIsTiny_ThenReadFailsWithIndexOutOfRangeException()
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[64],
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            Assert.AreEqual(0, endpoint.ConnectCount);

            byte[] buffer = new byte[relay.MinReadSize - 1];

            AssertEx.ThrowsAggregateException <IndexOutOfRangeException>(() =>
            {
                relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token).Wait();
            });
        }
示例#2
0
        public void WhenReadingTruncatedMessage_ThenReadFailsWithInvalidServerResponseException()
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0 },
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            Assert.AreEqual(0, endpoint.ConnectCount);

            byte[] buffer = new byte[relay.MinReadSize];

            AssertEx.ThrowsAggregateException <InvalidServerResponseException>(() =>
            {
                relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token).Wait();
            });
        }
示例#3
0
        public async Task WhenServerClosesConnectionWithDestinationReadFailedCode_ThenReadReturnsZero()
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.CONNECT_SUCCESS_SID, 0, 0, 0, 1, 0 },
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 }
                },
                ExpectServerCloseCodeOnRead = (WebSocketCloseStatus)CloseCode.DESTINATION_READ_FAILED
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            byte[] buffer    = new byte[relay.MinReadSize];
            int    bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(1, bytesRead);

            bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(0, bytesRead);
        }
示例#4
0
        public void WhenFirstReadEncountersUnrecognizedMessageTag_ThenReadFailsWithInvalidServerResponseException(
            [Values(
                 (byte)MessageTag.UNUSED,
                 (byte)MessageTag.DEPRECATED,
                 (byte)MessageTag.ACK_LATENCY,
                 (byte)MessageTag.REPLY_LATENCY,
                 (byte)MessageTag.ACK + 1)] byte tag)
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, tag },
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            Assert.AreEqual(0, endpoint.ConnectCount);

            AssertEx.ThrowsAggregateException <InvalidServerResponseException>(() =>
            {
                byte[] buffer = new byte[relay.MinReadSize];
                relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token).Wait();
            });
        }
示例#5
0
        public async Task WhenServerClosesConnectionGracefully_ThenReadReturnsZero()
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.CONNECT_SUCCESS_SID, 0, 0, 0, 1, 0 },
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 },
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            byte[] buffer    = new byte[relay.MinReadSize];
            int    bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(1, bytesRead);

            bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(0, bytesRead);
        }
示例#6
0
        public async Task WhenSubsequentReadEncountersUnrecognizedMessageTag_ThenReadFailsWithInvalidServerResponseException(
            [Values(
                 (byte)MessageTag.UNUSED,
                 (byte)MessageTag.DEPRECATED,
                 (byte)MessageTag.ACK_LATENCY,
                 (byte)MessageTag.REPLY_LATENCY,
                 (byte)MessageTag.ACK + 1)] byte tag)
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.CONNECT_SUCCESS_SID, 0, 0, 0, 1, 0 },
                    new byte[] { 0, tag },
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 2, 0xA, 0xB },
                    new byte[] { 0, tag },
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            byte[] buffer    = new byte[relay.MinReadSize];
            int    bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(2, bytesRead);
            Assert.AreEqual(0xA, buffer[0]);
            Assert.AreEqual(0xB, buffer[1]);
        }
        public async Task WhenServerClosedConnection_ThenSubsequentWriteFailsWithException()
        {
            var stream = new MockStream()
            {
                ExpectedWriteData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 },
                },
                ExpectServerCloseCodeOnWrite = WebSocketCloseStatus.NormalClosure
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            // Write first request.
            byte[] request = new byte[] { 1 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            // Write another request - this should fail.
            AssertEx.ThrowsAggregateException <WebSocketStreamClosedByServerException>(() =>
            {
                request = new byte[] { 2 };
                relay.WriteAsync(request, 0, request.Length, tokenSource.Token).Wait();
            });
        }
        public async Task WhenClientClosedConnection_ThenSubsequentWriteFailsWithException()
        {
            var stream = new MockStream()
            {
                ExpectedWriteData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 },
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            // Write first request, then close.
            byte[] request = new byte[] { 1 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            await relay.CloseAsync(tokenSource.Token);

            // Write another request - this should fail.
            AssertEx.ThrowsAggregateException <NetworkStreamClosedException>(() =>
            {
                request = new byte[] { 2 };
                relay.WriteAsync(request, 0, request.Length, tokenSource.Token).Wait();
            });
        }
        public async Task WhenPerformingWriteAfterDestinationWriteFailed_ThenWriteFailsWithException()
        {
            var stream = new MockStream()
            {
                ExpectedWriteData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 },
                },
                ExpectServerCloseCodeOnWrite = (WebSocketCloseStatus)CloseCode.DESTINATION_WRITE_FAILED
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            // Write first request.
            byte[] request = new byte[] { 1 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            // Write another request - this should fail.
            AssertEx.ThrowsAggregateException <WebSocketStreamClosedByServerException>(() =>
            {
                request = new byte[] { 2 };
                relay.WriteAsync(request, 0, request.Length, tokenSource.Token).Wait();
            });
        }
        public async Task WhenPerformingWriteWithoutPreviousRead_ThenNoAckIsSent()
        {
            var stream = new MockStream()
            {
                ExpectedWriteData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 },
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 2 }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            // Write two requests with no read in between.
            byte[] request = new byte[] { 1 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            request = new byte[] { 2 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            Assert.AreEqual(2, stream.WriteCount);
            Assert.AreEqual(0, stream.ReadCount);
        }
        public async Task WhenPerformingFirstWrite_ThenConnectionIsOpened()
        {
            var stream   = new MockStream();
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            Assert.AreEqual(0, endpoint.ConnectCount);

            byte[] request = new byte[] { 1, 2, 3 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            Assert.AreEqual(1, endpoint.ConnectCount);
        }
        public void WhenServerClosesConnectionWithNotAuthorizedCode_ThenWriteFailsWithUnauthorizedException()
        {
            var stream = new MockStream()
            {
                ExpectServerCloseCodeOnWrite = (WebSocketCloseStatus)CloseCode.NOT_AUTHORIZED
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            // Write first request - this should fail.
            AssertEx.ThrowsAggregateException <UnauthorizedException>(() =>
            {
                var request = new byte[] { 2 };
                relay.WriteAsync(request, 0, request.Length, tokenSource.Token).Wait();
            });
        }
        public async Task WhenPerformingWrite_ThenAckIsSentFirst()
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.CONNECT_SUCCESS_SID, 0, 0, 0, 1, 0 },
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 }
                },
                ExpectedWriteData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 1 },
                    new byte[] { 0, (byte)MessageTag.ACK, 0, 0, 0, 0, 0, 0, 0, 1 },
                    new byte[] { 0, (byte)MessageTag.DATA, 0, 0, 0, 1, 2 }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            // Write and read something.
            byte[] request = new byte[] { 1 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            byte[] buffer    = new byte[relay.MinReadSize];
            int    bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(1, bytesRead);

            Assert.AreEqual(1, stream.WriteCount);
            Assert.AreEqual(2, stream.ReadCount);

            // Write a second request - this should cause an ACK to be sent first.
            request = new byte[] { 2 };
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            Assert.AreEqual(3, stream.WriteCount);
            Assert.AreEqual(2, stream.ReadCount);
        }
示例#14
0
        public async Task WhenAckIsRead_ThenUnacknoledgedQueueIsTrimmed()
        {
            byte[] request = new byte[] { 1, 2, 3, 4 };

            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.ACK, 0, 0, 0, 0, 0, 0, 0, (byte)request.Length },
                    new byte[] { 0, (byte)MessageTag.ACK, 0, 0, 0, 0, 0, 0, 0, (byte)(request.Length * 3) },
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            Assert.AreEqual(0, relay.UnacknoledgedMessageCount);
            Assert.AreEqual(0, relay.ExpectedAck);

            // Send 3 messages.
            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            await relay.WriteAsync(request, 0, request.Length, tokenSource.Token);

            Assert.AreEqual(3, relay.UnacknoledgedMessageCount);
            Assert.AreEqual((byte)(request.Length * 3), relay.ExpectedAck);

            // Receive 2 ACKs.
            byte[] buffer    = new byte[relay.MinReadSize];
            int    bytesRead = await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(0, bytesRead);
            Assert.AreEqual(0, relay.UnacknoledgedMessageCount);
            Assert.AreEqual(0, relay.ExpectedAck);
        }
示例#15
0
        public async Task WhenPerformingFirstRead_ThenConnectionIsOpened()
        {
            var stream = new MockStream()
            {
                ExpectedReadData = new byte[][]
                {
                    new byte[] { 0, (byte)MessageTag.CONNECT_SUCCESS_SID, 0, 0, 0, 1, 0 },
                    new byte[] { }
                }
            };
            var endpoint = new MockSshRelayEndpoint()
            {
                ExpectedStream = stream
            };
            var relay = new SshRelayStream(endpoint);

            Assert.AreEqual(0, endpoint.ConnectCount);

            byte[] buffer = new byte[relay.MinReadSize];
            await relay.ReadAsync(buffer, 0, buffer.Length, tokenSource.Token);

            Assert.AreEqual(1, endpoint.ConnectCount);
        }