Skip to content

Commit

Permalink
Fix review part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechNagorski committed Apr 21, 2023
1 parent 8201be4 commit 3a4b62f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Renci.SshNet.Tests/Classes/ClientAuthenticationTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Tests.Common;

namespace Renci.SshNet.Tests.Classes
{
Expand Down Expand Up @@ -28,7 +29,7 @@ public void Ctor_PartialSuccessLimit_Zero()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
Assert.AreEqual(new ArgumentOutOfRangeException("partialSuccessLimit", "Cannot be less than one.").Message, ex.Message);
ArgumentExceptionAssert.MessageEquals("partialSuccessLimit", "Cannot be less than one.", ex);
Assert.AreEqual("partialSuccessLimit", ex.ParamName);
}
}
Expand All @@ -46,7 +47,7 @@ public void Ctor_PartialSuccessLimit_Negative()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
Assert.AreEqual(new ArgumentOutOfRangeException("partialSuccessLimit", "Cannot be less than one.").Message, ex.Message);
ArgumentExceptionAssert.MessageEquals("partialSuccessLimit", "Cannot be less than one.", ex);
Assert.AreEqual("partialSuccessLimit", ex.ParamName);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet.Tests/Classes/Common/PacketDumpTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using System;
using Renci.SshNet.Tests.Common;

namespace Renci.SshNet.Tests.Classes.Common
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public void Create_ByteArrayAndIndentLevel_IndentLevelLessThanZero()
{
Assert.IsNull(ex.InnerException);

Assert.AreEqual(new ArgumentOutOfRangeException("indentLevel", "Cannot be less than zero.").Message, ex.Message);
ArgumentExceptionAssert.MessageEquals("indentLevel", "Cannot be less than zero.", ex);

Assert.AreEqual("indentLevel", ex.ParamName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using System;
using Renci.SshNet.Tests.Common;

namespace Renci.SshNet.Tests.Classes.Common
{
Expand Down Expand Up @@ -37,7 +38,7 @@ public void Path_Empty()
catch (ArgumentException ex)
{
Assert.IsNull(ex.InnerException);
Assert.AreEqual(new ArgumentException("The path is a zero-length string.", "path").Message, ex.Message);
ArgumentExceptionAssert.MessageEquals("The path is a zero-length string.", "path", ex);
Assert.AreEqual("path", ex.ParamName);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet.Tests/Classes/NetConfClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void OperationTimeout_LessThanLowerLimit()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
Assert.AreEqual(new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.").Message, ex.Message);
ArgumentExceptionAssert.MessageEquals("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.", ex);
Assert.AreEqual("value", ex.ParamName);
}
}
Expand All @@ -105,7 +105,7 @@ public void OperationTimeout_GreaterThanLowerLimit()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
Assert.AreEqual(new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.").Message, ex.Message);
ArgumentExceptionAssert.MessageEquals("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.", ex);
Assert.AreEqual("value", ex.ParamName);
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/Renci.SshNet.Tests/Classes/SftpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public void OperationTimeout_LessThanLowerLimit()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);

Assert.AreEqual(new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.").Message, ex.Message);

ArgumentExceptionAssert.MessageEquals("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.", ex);
Assert.AreEqual("value", ex.ParamName);
}
}
Expand All @@ -115,9 +113,7 @@ public void OperationTimeout_GreaterThanLowerLimit()
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);

Assert.AreEqual(new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.").Message, ex.Message);

ArgumentExceptionAssert.MessageEquals("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.", ex);
Assert.AreEqual("value", ex.ParamName);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/Renci.SshNet.Tests/Common/ArgumentExceptionAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Renci.SshNet.Tests.Common
{
public static class ArgumentExceptionAssert
{
public static void MessageEquals(string paramName, string expected, ArgumentException exception)
{
var type = exception.GetType();
var newMessage = (ArgumentException)Activator.CreateInstance(type, paramName, expected);

Assert.AreEqual(newMessage?.Message, exception.Message);
}
}
}
5 changes: 3 additions & 2 deletions src/Renci.SshNet/ShellStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Renci.SshNet
public class ShellStream : Stream
{
private const string CrLf = "\r\n";
private const string Cr = "\n";

private readonly ISession _session;
private readonly Encoding _encoding;
Expand Down Expand Up @@ -620,14 +621,14 @@ public string ReadLine(TimeSpan timeout)
text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count);
}

var index = text.IndexOf(CrLf, StringComparison.Ordinal);
var index = text.IndexOf(Cr, StringComparison.Ordinal);

if (index >= 0)
{
text = text.Substring(0, index);

// determine how many bytes to remove from buffer
var bytesProcessed = _encoding.GetByteCount(text + CrLf);
var bytesProcessed = _encoding.GetByteCount(text + Cr);

// remove processed bytes from the queue
for (var i = 0; i < bytesProcessed; i++)
Expand Down

0 comments on commit 3a4b62f

Please sign in to comment.