Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Renci.SshNet/Common/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ internal static class TimeSpanExtensions
/// </exception>
public static int AsTimeout(this TimeSpan timeSpan, [CallerArgumentExpression(nameof(timeSpan))] string? paramName = null)
{
var timeoutInMilliseconds = timeSpan.TotalMilliseconds;
return timeoutInMilliseconds is < -1d or > int.MaxValue
? throw new ArgumentOutOfRangeException(paramName, "The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.")
var timeoutInMilliseconds = (long)timeSpan.TotalMilliseconds;
return timeoutInMilliseconds is < -1 or > int.MaxValue
? throw new ArgumentOutOfRangeException(paramName, timeSpan, "The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.")
: (int)timeoutInMilliseconds;
}

Expand Down
63 changes: 14 additions & 49 deletions test/Renci.SshNet.Tests/Classes/Common/TimeSpanExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;

namespace Renci.SshNet.Tests.Classes.Common
{
Expand All @@ -21,34 +20,17 @@ public void AsTimeout_ValidTimeSpan_ReturnsExpectedMilliseconds()
}

[TestMethod]
public void AsTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
[DataRow(-2)]
[DataRow((double)int.MaxValue + 1)]
public void AsTimeout_InvalidTimeSpan_ThrowsArgumentOutOfRangeException(double milliseconds)
{
var timeSpan = TimeSpan.FromSeconds(-1);
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());
}
var timeSpan = TimeSpan.FromMilliseconds(milliseconds);

[TestMethod]
public void AsTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
{
var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());
}
var ex = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());

[TestMethod]
public void AsTimeout_ArgumentOutOfRangeException_HasCorrectInformation()
{
var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
try
{
Assert.Contains("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex.Message, StringComparison.Ordinal);

timeSpan.AsTimeout();
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
Assert.AreEqual(nameof(timeSpan), ex.ParamName);
}
Assert.AreEqual(nameof(timeSpan), ex.ParamName);
}

[TestMethod]
Expand All @@ -60,34 +42,17 @@ public void EnsureValidTimeout_ValidTimeSpan_DoesNotThrow()
}

[TestMethod]
public void EnsureValidTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
[DataRow(-2)]
[DataRow((double)int.MaxValue + 1)]
public void EnsureValidTimeout_InvalidTimeSpan_ThrowsArgumentOutOfRangeException(double milliseconds)
{
var timeSpan = TimeSpan.FromSeconds(-1);
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());
}
var timeSpan = TimeSpan.FromMilliseconds(milliseconds);

[TestMethod]
public void EnsureValidTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
{
var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());
}
var ex = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());

[TestMethod]
public void EnsureValidTimeout_ArgumentOutOfRangeException_HasCorrectInformation()
{
var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
try
{
Assert.Contains("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex.Message, StringComparison.Ordinal);

timeSpan.EnsureValidTimeout();
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
Assert.AreEqual(nameof(timeSpan), ex.ParamName);
}
Assert.AreEqual(nameof(timeSpan), ex.ParamName);
}
}
}
52 changes: 8 additions & 44 deletions test/Renci.SshNet.Tests/Classes/ConnectionInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,35 +286,17 @@ public void Test_ConnectionInfo_Timeout_Valid()
Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
Resources.PASSWORD, new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));

try
{
connectionInfo.Timeout = TimeSpan.FromMilliseconds(-2);
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => connectionInfo.Timeout = TimeSpan.FromMilliseconds(-2));
Assert.AreEqual("Timeout", ex.ParamName);

Assert.AreEqual("Timeout", ex.ParamName);
}
ex = Assert.Throws<ArgumentOutOfRangeException>(() => connectionInfo.Timeout = TimeSpan.FromMilliseconds((double)int.MaxValue + 1));
Assert.AreEqual("Timeout", ex.ParamName);

connectionInfo.Timeout = TimeSpan.FromMilliseconds(-1);
Assert.AreEqual(connectionInfo.Timeout, TimeSpan.FromMilliseconds(-1));

connectionInfo.Timeout = TimeSpan.FromMilliseconds(int.MaxValue);
Assert.AreEqual(connectionInfo.Timeout, TimeSpan.FromMilliseconds(int.MaxValue));

try
{
connectionInfo.Timeout = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);

Assert.AreEqual("Timeout", ex.ParamName);
}
}

[TestMethod]
Expand All @@ -325,35 +307,17 @@ public void Test_ConnectionInfo_ChannelCloseTimeout_Valid()
Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
Resources.PASSWORD, new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));

try
{
connectionInfo.ChannelCloseTimeout = TimeSpan.FromMilliseconds(-2);
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => connectionInfo.ChannelCloseTimeout = TimeSpan.FromMilliseconds(-2));
Assert.AreEqual("ChannelCloseTimeout", ex.ParamName);

Assert.AreEqual("ChannelCloseTimeout", ex.ParamName);
}
ex = Assert.Throws<ArgumentOutOfRangeException>(() => connectionInfo.ChannelCloseTimeout = TimeSpan.FromMilliseconds((double)int.MaxValue + 1));
Assert.AreEqual("ChannelCloseTimeout", ex.ParamName);

connectionInfo.ChannelCloseTimeout = TimeSpan.FromMilliseconds(-1);
Assert.AreEqual(connectionInfo.ChannelCloseTimeout, TimeSpan.FromMilliseconds(-1));

connectionInfo.ChannelCloseTimeout = TimeSpan.FromMilliseconds(int.MaxValue);
Assert.AreEqual(connectionInfo.ChannelCloseTimeout, TimeSpan.FromMilliseconds(int.MaxValue));

try
{
connectionInfo.ChannelCloseTimeout = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);

Assert.AreEqual("ChannelCloseTimeout", ex.ParamName);
}
}

[TestMethod]
Expand Down
26 changes: 4 additions & 22 deletions test/Renci.SshNet.Tests/Classes/NetConfClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,8 @@ public void OperationTimeout_LessThanLowerLimit()
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
var target = new NetConfClient(connectionInfo);

try
{
target.OperationTimeout = operationTimeout;
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);

Assert.AreEqual("OperationTimeout", ex.ParamName);
}
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => target.OperationTimeout = operationTimeout);
Assert.AreEqual("OperationTimeout", ex.ParamName);
}

[TestMethod]
Expand All @@ -100,17 +91,8 @@ public void OperationTimeout_GreaterThanLowerLimit()
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
var target = new NetConfClient(connectionInfo);

try
{
target.OperationTimeout = operationTimeout;
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);

Assert.AreEqual("OperationTimeout", ex.ParamName);
}
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => target.OperationTimeout = operationTimeout);
Assert.AreEqual("OperationTimeout", ex.ParamName);
}
}
}
26 changes: 4 additions & 22 deletions test/Renci.SshNet.Tests/Classes/SftpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,8 @@ public void OperationTimeout_LessThanLowerLimit()
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
var target = new SftpClient(connectionInfo);

try
{
target.OperationTimeout = operationTimeout;
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);

Assert.AreEqual("OperationTimeout", ex.ParamName);
}
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => target.OperationTimeout = operationTimeout);
Assert.AreEqual("OperationTimeout", ex.ParamName);
}

[TestMethod]
Expand All @@ -103,17 +94,8 @@ public void OperationTimeout_GreaterThanLowerLimit()
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
var target = new SftpClient(connectionInfo);

try
{
target.OperationTimeout = operationTimeout;
}
catch (ArgumentOutOfRangeException ex)
{
Assert.IsNull(ex.InnerException);
ArgumentExceptionAssert.MessageEquals("The timeout must represent a value between -1 and Int32.MaxValue milliseconds, inclusive.", ex);

Assert.AreEqual("OperationTimeout", ex.ParamName);
}
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => target.OperationTimeout = operationTimeout);
Assert.AreEqual("OperationTimeout", ex.ParamName);
}
}
}