Bläddra i källkod

Fix name of argument in ArgumentOutOfRangeException thrown in OperationTimeout.
Added tests for OperationTimeout.

Gert Driesen 7 år sedan
förälder
incheckning
031f0a164b

+ 103 - 22
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.
-    /// <summary>
-    ///
-    /// </summary>
     [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);
+            }
+        }
+
         /// <summary>
         ///A test for NetConfClient Constructor
         ///</summary>
@@ -146,23 +244,6 @@ namespace Renci.SshNet.Tests.Classes
             Assert.Inconclusive("Verify the correctness of this test method.");
         }
 
-        /// <summary>
-        ///A test for OperationTimeout
-        ///</summary>
-        [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.");
-        }
-
         /// <summary>
         ///A test for ClientCapabilities
         ///</summary>

+ 134 - 0
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);
+            }
+        }
+
         /// <summary>
         ///A test for SftpClient Constructor
         ///</summary>

+ 13 - 1
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.
         /// </value>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="Int32.MaxValue"/> milliseconds.</exception>
         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;
             }
         }
 
+        /// <summary>
+        /// Gets the current NetConf session.
+        /// </summary>
+        /// <value>
+        /// The current NetConf session.
+        /// </value>
+        internal INetConfSession NetConfSession
+        {
+            get { return _netConfSession; }
+        }
+
         #region Constructors
 
         /// <summary>

+ 13 - 1
src/Renci.SshNet/SftpClient.cs

@@ -44,6 +44,7 @@ namespace Renci.SshNet
         /// one (-1) milliseconds, which indicates an infinite timeout period.
         /// </value>
         /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="Int32.MaxValue"/> milliseconds.</exception>
         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
             }
         }
 
+        /// <summary>
+        /// Gets the current SFTP session.
+        /// </summary>
+        /// <value>
+        /// The current SFTP session.
+        /// </value>
+        internal ISftpSession SftpSession
+        {
+            get { return _sftpSession; }
+        }
+
         #region Constructors
 
         /// <summary>