From 767fafbfc1606e55ee144fa8a49167bae0f0d798 Mon Sep 17 00:00:00 2001 From: drieseng Date: Wed, 30 Dec 2020 21:19:30 +0100 Subject: [PATCH] Improve exception messages further. --- ..._ConnectionClosedByServer_NoDataSentByServer.cs | 7 ++++++- ...angeTest_ServerResponseContainsNullCharacter.cs | 8 ++++++-- ...SshIdentificationOnlyContainsProtocolVersion.cs | 4 ++-- .../Connection/ProtocolVersionExchange.cs | 14 +++++++++----- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ConnectionClosedByServer_NoDataSentByServer.cs b/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ConnectionClosedByServer_NoDataSentByServer.cs index a26cf1a3..5a8fb691 100644 --- a/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ConnectionClosedByServer_NoDataSentByServer.cs +++ b/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ConnectionClosedByServer_NoDataSentByServer.cs @@ -86,7 +86,12 @@ namespace Renci.SshNet.Tests.Classes.Connection { Assert.IsNotNull(_actualException); Assert.IsNull(_actualException.InnerException); - Assert.AreEqual("Server response does not contain SSH protocol identification. Connection to remote server was closed before any data was received.", _actualException.Message); + Assert.AreEqual(string.Format("The server response does not contain an SSH protocol identification.{0}" + + "The connection to the remote server was closed before any data was received.{0}{0}" + + "More information on the Protocol Version Exchange is available here:{0}" + + "https://tools.ietf.org/html/rfc4253#section-4.2", + Environment.NewLine), + _actualException.Message); } [TestMethod] diff --git a/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseContainsNullCharacter.cs b/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseContainsNullCharacter.cs index f3134a72..29792888 100644 --- a/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseContainsNullCharacter.cs +++ b/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseContainsNullCharacter.cs @@ -88,8 +88,12 @@ namespace Renci.SshNet.Tests.Classes.Connection [TestMethod] public void StartShouldHaveThrownSshConnectionException() { - var expectedMessage = "The identification string contains a null character at position 0x00000005:" + Environment.NewLine + - " 00000000 ED 95 9C 21 00 ...!."; + var expectedMessage = string.Format("The server response contains a null character at position 0x00000005:{0}{0}" + + " 00000000 ED 95 9C 21 00 ...!.{0}{0}" + + "A server must not send a null character before the Protocol Version Exchange is complete.{0}{0}" + + "More information is available here:{0}" + + "https://tools.ietf.org/html/rfc4253#section-4.2", + Environment.NewLine); Assert.IsNotNull(_actualException); Assert.IsNull(_actualException.InnerException); diff --git a/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseInvalid_SshIdentificationOnlyContainsProtocolVersion.cs b/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseInvalid_SshIdentificationOnlyContainsProtocolVersion.cs index 2bfdf8c5..ff2bc5e5 100644 --- a/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseInvalid_SshIdentificationOnlyContainsProtocolVersion.cs +++ b/src/Renci.SshNet.Tests/Classes/Connection/ProtocolVersionExchangeTest_ServerResponseInvalid_SshIdentificationOnlyContainsProtocolVersion.cs @@ -89,9 +89,9 @@ namespace Renci.SshNet.Tests.Classes.Connection [TestMethod] public void StartShouldHaveThrownSshConnectionException() { - var expectedMessage = string.Format("The server response does not contain an SSH protocol identification:{0}{0}" + + var expectedMessage = string.Format("The server response does not contain an SSH identification string:{0}{0}" + " 00000000 53 53 48 2D 32 2E 30 0D 0A SSH-2.0..{0}{0}" + - "More information is available here:{0}" + + "More information on the Protocol Version Exchange is available here:{0}" + "https://tools.ietf.org/html/rfc4253#section-4.2", Environment.NewLine); diff --git a/src/Renci.SshNet/Connection/ProtocolVersionExchange.cs b/src/Renci.SshNet/Connection/ProtocolVersionExchange.cs index ab7a066b..21b0e469 100644 --- a/src/Renci.SshNet/Connection/ProtocolVersionExchange.cs +++ b/src/Renci.SshNet/Connection/ProtocolVersionExchange.cs @@ -52,11 +52,16 @@ namespace Renci.SshNet.Connection { if (bytesReceived.Count == 0) { - throw new SshConnectionException("The server response does not contain an SSH protocol identification. Connection to remote server was closed before any data was received.", DisconnectReason.ConnectionLost); + throw new SshConnectionException(string.Format("The server response does not contain an SSH identification string.{0}" + + "The connection to the remote server was closed before any data was received.{0}{0}" + + "More information on the Protocol Version Exchange is available here:{0}" + + "https://tools.ietf.org/html/rfc4253#section-4.2", + Environment.NewLine), + DisconnectReason.ConnectionLost); } - throw new SshConnectionException(string.Format("The server response does not contain an SSH protocol identification:{0}{0}{1}{0}{0}" + - "More information is available here:{0}" + + throw new SshConnectionException(string.Format("The server response does not contain an SSH identification string:{0}{0}{1}{0}{0}" + + "More information on the Protocol Version Exchange is available here:{0}" + "https://tools.ietf.org/html/rfc4253#section-4.2", Environment.NewLine, PacketDump.Create(bytesReceived, 2)), @@ -120,8 +125,7 @@ namespace Renci.SshNet.Connection { throw new SshConnectionException(string.Format(CultureInfo.InvariantCulture, "The server response contains a null character at position 0x{0:X8}:{1}{1}{2}{1}{1}" + - "A server must not send a null character before the Protocol Version Exchange is{1}" + - "complete.{1}{1}" + + "A server must not send a null character before the Protocol Version Exchange is complete.{1}{1}" + "More information is available here:{1}" + "https://tools.ietf.org/html/rfc4253#section-4.2", buffer.Count,