From 031f0a164be341ea0de90919922cc825300fd776 Mon Sep 17 00:00:00 2001 From: Gert Driesen Date: Sun, 22 Jul 2018 21:14:32 +0200 Subject: [PATCH] Fix name of argument in ArgumentOutOfRangeException thrown in OperationTimeout. Added tests for OperationTimeout. --- .../Classes/NetConfClientTest.cs | 125 +++++++++++++--- .../Classes/SftpClientTest.cs | 134 ++++++++++++++++++ src/Renci.SshNet/NetConfClient.cs | 14 +- src/Renci.SshNet/SftpClient.cs | 14 +- 4 files changed, 263 insertions(+), 24 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/NetConfClientTest.cs b/src/Renci.SshNet.Tests/Classes/NetConfClientTest.cs index f58fd0d0..d70fea36 100644 --- a/src/Renci.SshNet.Tests/Classes/NetConfClientTest.cs +++ b/src/Renci.SshNet.Tests/Classes/NetConfClientTest.cs @@ -5,13 +5,111 @@ using System.Xml; namespace Renci.SshNet.Tests.Classes { - // TODO: Please help with documentation here, as I don't know the details, specially for the methods not documented. - /// - /// - /// [TestClass] - public partial class NetConfClientTest : TestBase + public class NetConfClientTest : TestBase { + private Random _random; + + [TestInitialize] + public void SetUp() + { + _random = new Random(); + } + + [TestMethod] + public void OperationTimeout_Default() + { + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new NetConfClient(connectionInfo); + + var actual = target.OperationTimeout; + + Assert.AreEqual(TimeSpan.FromMilliseconds(-1), actual); + } + + [TestMethod] + public void OperationTimeout_InsideLimits() + { + var operationTimeout = TimeSpan.FromMilliseconds(_random.Next(0, int.MaxValue - 1)); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new NetConfClient(connectionInfo) + { + OperationTimeout = operationTimeout + }; + + var actual = target.OperationTimeout; + + Assert.AreEqual(operationTimeout, actual); + } + + [TestMethod] + public void OperationTimeout_LowerLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(-1); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new NetConfClient(connectionInfo) + { + OperationTimeout = operationTimeout + }; + + var actual = target.OperationTimeout; + + Assert.AreEqual(operationTimeout, actual); + } + + [TestMethod] + public void OperationTimeout_UpperLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new NetConfClient(connectionInfo) + { + OperationTimeout = operationTimeout + }; + + var actual = target.OperationTimeout; + + Assert.AreEqual(operationTimeout, actual); + } + + [TestMethod] + public void OperationTimeout_LessThanLowerLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(-2); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new NetConfClient(connectionInfo); + + try + { + target.OperationTimeout = operationTimeout; + } + catch (ArgumentOutOfRangeException ex) + { + Assert.IsNull(ex.InnerException); + Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message); + Assert.AreEqual("value", ex.ParamName); + } + } + + [TestMethod] + public void OperationTimeout_GreaterThanLowerLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue).Add(TimeSpan.FromMilliseconds(1)); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new NetConfClient(connectionInfo); + + try + { + target.OperationTimeout = operationTimeout; + } + catch (ArgumentOutOfRangeException ex) + { + Assert.IsNull(ex.InnerException); + Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message); + Assert.AreEqual("value", ex.ParamName); + } + } + /// ///A test for NetConfClient Constructor /// @@ -146,23 +244,6 @@ namespace Renci.SshNet.Tests.Classes Assert.Inconclusive("Verify the correctness of this test method."); } - /// - ///A test for OperationTimeout - /// - [TestMethod] - [Ignore] // placeholder for actual test - public void OperationTimeoutTest() - { - ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value - NetConfClient target = new NetConfClient(connectionInfo); // TODO: Initialize to an appropriate value - TimeSpan expected = new TimeSpan(); // TODO: Initialize to an appropriate value - TimeSpan actual; - target.OperationTimeout = expected; - actual = target.OperationTimeout; - Assert.AreEqual(expected, actual); - Assert.Inconclusive("Verify the correctness of this test method."); - } - /// ///A test for ClientCapabilities /// diff --git a/src/Renci.SshNet.Tests/Classes/SftpClientTest.cs b/src/Renci.SshNet.Tests/Classes/SftpClientTest.cs index 6f4ec964..3edd7c7b 100644 --- a/src/Renci.SshNet.Tests/Classes/SftpClientTest.cs +++ b/src/Renci.SshNet.Tests/Classes/SftpClientTest.cs @@ -16,6 +16,140 @@ namespace Renci.SshNet.Tests.Classes [TestClass] public partial class SftpClientTest : TestBase { + private Random _random; + + [TestInitialize] + public void SetUp() + { + _random = new Random(); + } + + [TestMethod] + public void OperationTimeout_Default() + { + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo); + + var actual = target.OperationTimeout; + + Assert.AreEqual(TimeSpan.FromMilliseconds(-1), actual); + } + + [TestMethod] + public void OperationTimeout_InsideLimits() + { + var operationTimeout = TimeSpan.FromMilliseconds(_random.Next(0, int.MaxValue - 1)); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo) + { + OperationTimeout = operationTimeout + }; + + var actual = target.OperationTimeout; + + Assert.AreEqual(operationTimeout, actual); + } + + [TestMethod] + public void OperationTimeout_LowerLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(-1); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo) + { + OperationTimeout = operationTimeout + }; + + var actual = target.OperationTimeout; + + Assert.AreEqual(operationTimeout, actual); + } + + [TestMethod] + public void OperationTimeout_UpperLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo) + { + OperationTimeout = operationTimeout + }; + + var actual = target.OperationTimeout; + + Assert.AreEqual(operationTimeout, actual); + } + + [TestMethod] + public void OperationTimeout_LessThanLowerLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(-2); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo); + + try + { + target.OperationTimeout = operationTimeout; + } + catch (ArgumentOutOfRangeException ex) + { + Assert.IsNull(ex.InnerException); + Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message); + Assert.AreEqual("value", ex.ParamName); + } + } + + [TestMethod] + public void OperationTimeout_GreaterThanLowerLimit() + { + var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue).Add(TimeSpan.FromMilliseconds(1)); + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo); + + try + { + target.OperationTimeout = operationTimeout; + } + catch (ArgumentOutOfRangeException ex) + { + Assert.IsNull(ex.InnerException); + Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message); + Assert.AreEqual("value", ex.ParamName); + } + } + + [TestMethod] + public void OperationTimeout_Disposed() + { + var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd"); + var target = new SftpClient(connectionInfo); + target.Dispose(); + + // getter + try + { + var actual = target.OperationTimeout; + Assert.Fail("Should have failed, but returned: " + actual); + } + catch (ObjectDisposedException ex) + { + Assert.IsNull(ex.InnerException); + Assert.AreEqual(typeof(SftpClient).FullName, ex.ObjectName); + } + + // setter + try + { + target.OperationTimeout = TimeSpan.FromMilliseconds(5); + Assert.Fail(); + } + catch (ObjectDisposedException ex) + { + Assert.IsNull(ex.InnerException); + Assert.AreEqual(typeof(SftpClient).FullName, ex.ObjectName); + } + } + /// ///A test for SftpClient Constructor /// diff --git a/src/Renci.SshNet/NetConfClient.cs b/src/Renci.SshNet/NetConfClient.cs index 408c136b..d2d4a8af 100644 --- a/src/Renci.SshNet/NetConfClient.cs +++ b/src/Renci.SshNet/NetConfClient.cs @@ -27,18 +27,30 @@ namespace Renci.SshNet /// The timeout to wait until an operation completes. The default value is negative /// one (-1) milliseconds, which indicates an infinite time-out period. /// + /// represents a value that is less than -1 or greater than milliseconds. public TimeSpan OperationTimeout { get { return TimeSpan.FromMilliseconds(_operationTimeout); } set { var timeoutInMilliseconds = value.TotalMilliseconds; if (timeoutInMilliseconds < -1d || timeoutInMilliseconds > int.MaxValue) - throw new ArgumentOutOfRangeException("timeout", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive."); + throw new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive."); _operationTimeout = (int) timeoutInMilliseconds; } } + /// + /// Gets the current NetConf session. + /// + /// + /// The current NetConf session. + /// + internal INetConfSession NetConfSession + { + get { return _netConfSession; } + } + #region Constructors /// diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index da7a2385..5a099300 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -44,6 +44,7 @@ namespace Renci.SshNet /// one (-1) milliseconds, which indicates an infinite timeout period. /// /// The method was called after the client was disposed. + /// represents a value that is less than -1 or greater than milliseconds. public TimeSpan OperationTimeout { get @@ -58,7 +59,7 @@ namespace Renci.SshNet var timeoutInMilliseconds = value.TotalMilliseconds; if (timeoutInMilliseconds < -1d || timeoutInMilliseconds > int.MaxValue) - throw new ArgumentOutOfRangeException("timeout", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive."); + throw new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive."); _operationTimeout = (int) timeoutInMilliseconds; } @@ -141,6 +142,17 @@ namespace Renci.SshNet } } + /// + /// Gets the current SFTP session. + /// + /// + /// The current SFTP session. + /// + internal ISftpSession SftpSession + { + get { return _sftpSession; } + } + #region Constructors ///