Files
ssh.net/test/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ConnectionClosedByServer_NoDataSentByServer.cs
Robert Hague 11e7a52cb3 Cap data received during the protocol version exchange
Add upper bounds on the number of banner lines and line length before the SSH identification
string, analogous to OpenSSH. Also don't buffer all the data unnecessarily.
2026-08-09 18:25:04 +02:00

121 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Connection;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Connection
{
[TestClass]
public class ProtocolVersionExchangeTest_ConnectionClosedByServer_NoDataSentByServer
{
private AsyncSocketListener _server;
private ProtocolVersionExchange _protocolVersionExchange;
private string _clientVersion;
private TimeSpan _timeout;
private IPEndPoint _serverEndPoint;
private List<byte> _dataReceivedByServer;
private bool _clientDisconnected;
private Socket _client;
private SshConnectionException _actualException;
[TestInitialize]
public void Setup()
{
Arrange();
Act();
}
[TestCleanup]
public void Cleanup()
{
_server?.Dispose();
_server = null;
if (_client != null)
{
_client.Shutdown(SocketShutdown.Both);
_client.Close();
_client = null;
}
}
protected void Arrange()
{
_clientVersion = "\uD55C";
_timeout = TimeSpan.FromSeconds(5);
_serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
_dataReceivedByServer = new List<byte>();
_server = new AsyncSocketListener(_serverEndPoint);
_server.Start();
_server.BytesReceived += (bytes, socket) =>
{
_dataReceivedByServer.AddRange(bytes);
socket.Shutdown(SocketShutdown.Send);
};
_server.Disconnected += (socket) => _clientDisconnected = true;
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_client.Connect(_serverEndPoint);
_protocolVersionExchange = new ProtocolVersionExchange();
}
protected void Act()
{
try
{
_protocolVersionExchange.Start(_clientVersion, _client, _timeout);
Assert.Fail();
}
catch (SshConnectionException ex)
{
_actualException = ex;
}
// Give some time to process all messages
Thread.Sleep(200);
}
[TestMethod]
public void StartShouldHaveThrownSshConnectionException()
{
Assert.IsInstanceOfType<SshConnectionException>(_actualException);
Assert.IsNull(_actualException.InnerException);
Assert.AreEqual("The connection to the remote server was closed before a valid SSH identification string was received.", _actualException.Message);
}
[TestMethod]
public void ClientIdentificationWasSentToServer()
{
Assert.HasCount(5, _dataReceivedByServer);
Assert.AreEqual(0xed, _dataReceivedByServer[0]);
Assert.AreEqual(0x95, _dataReceivedByServer[1]);
Assert.AreEqual(0x9c, _dataReceivedByServer[2]);
Assert.AreEqual(0x0d, _dataReceivedByServer[3]);
Assert.AreEqual(0x0a, _dataReceivedByServer[4]);
}
[TestMethod]
public void ConnectionIsClosedByServer()
{
Assert.IsTrue(_client.Connected);
Assert.IsFalse(_clientDisconnected);
var bytesReceived = _client.Receive(new byte[1]);
Assert.AreEqual(0, bytesReceived);
Assert.IsTrue(_client.Connected);
Assert.IsFalse(_clientDisconnected);
}
}
}