2
0
Эх сурвалжийг харах

Document exceptions for ConnectionInfo constructors.
Improve exception messages for argument checks.
Only perform null argument check for hosts. Fixes issue #1845.

Gert Driesen 11 жил өмнө
parent
commit
23a178f7aa

+ 44 - 8
Renci.SshClient/Renci.SshNet.Tests/Classes/Common/PortForwardEventArgsTest.cs

@@ -3,6 +3,7 @@ using Renci.SshNet.Common;
 using Renci.SshNet.Tests.Common;
 using System;
 using System.Net;
+using Renci.SshNet.Tests.Properties;
 
 namespace Renci.SshNet.Tests.Classes.Common
 {
@@ -13,19 +14,54 @@ namespace Renci.SshNet.Tests.Classes.Common
     public class PortForwardEventArgsTest : TestBase
     {
         [TestMethod]
-        [Description("Test passing null to constructor of PortForwardEventArgs.")]
-        [ExpectedException(typeof(ArgumentNullException))]
-        public void Test_PortForwardEventArgs_Host_Null()
+        public void ConstructorShouldThrowArgumentNullExceptionWhenHostIsNull()
         {
-            var args = new PortForwardEventArgs(null, 80);
+            try
+            {
+                new PortForwardEventArgs(null, 80);
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("host", ex.ParamName);
+            }
         }
 
         [TestMethod]
-        [Description("Test passing an invalid port to constructor of PortForwardEventArgs.")]
-        [ExpectedException(typeof(ArgumentOutOfRangeException))]
-        public void Test_PortForwardEventArgs_Port_Invalid()
+        public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
         {
-            var args = new PortForwardEventArgs("string", IPEndPoint.MaxPort + 1);
+            var host = string.Empty;
+
+            var eventArgs = new PortForwardEventArgs(host, 80);
+
+            Assert.AreSame(host, eventArgs.OriginatorHost);
+        }
+
+        [TestMethod]
+        public void ConstructorShouldNotThrowExceptionWhenHostIsInvalidDnsName()
+        {
+            const string host = "in_valid_host.";
+
+            var eventArgs = new PortForwardEventArgs(host, 80);
+
+            Assert.AreSame(host, eventArgs.OriginatorHost);
+        }
+
+        [TestMethod]
+        public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsGreaterThanMaximumValue()
+        {
+            const int port = IPEndPoint.MaxPort + 1;
+
+            try
+            {
+                new PortForwardEventArgs(Resources.HOST, port);
+                Assert.Fail();
+            }
+            catch (ArgumentOutOfRangeException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("port", ex.ParamName);
+            }
         }
     }
 }

+ 284 - 168
Renci.SshClient/Renci.SshNet.Tests/Classes/ConnectionInfoTest.cs

@@ -1,4 +1,6 @@
-using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Globalization;
+using System.Net;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Renci.SshNet.Tests.Common;
 using Renci.SshNet.Tests.Properties;
 using System;
@@ -13,215 +15,329 @@ namespace Renci.SshNet.Tests.Classes
     {
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass null as proxy host.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentException))]
-        public void Test_ConnectionInfo_ProxyHost_Null()
+        public void ConstructorShouldNotThrowExceptionhenProxyTypesIsNoneAndProxyHostIsNull()
         {
-            new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, null, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            const string proxyHost = null;
+
+            var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
+                ProxyTypes.None, proxyHost, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.IsNull(connectionInfo.ProxyHost);
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentNullExceptionhenProxyTypesIsNotNoneAndProxyHostIsNull()
+        {
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, null,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                    new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+                Assert.Fail();
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("proxyHost", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldNotThrowExceptionWhenProxyTypesIsNotNoneAndProxyHostIsEmpty()
+        {
+            var proxyHost = string.Empty;
+
+            var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
+                ProxyTypes.Http, string.Empty, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.AreSame(proxyHost, connectionInfo.ProxyHost);
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass too large proxy port.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentOutOfRangeException))]
-        public void Test_ConnectionInfo_ProxyPort_Large()
+        public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenProxyTypesIsNotNoneAndProxyPortIsGreaterThanMaximumValue()
         {
-            new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, Resources.HOST, int.MaxValue, Resources.USERNAME, Resources.PASSWORD, null);
+            var maxPort = IPEndPoint.MaxPort;
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, Resources.HOST, ++maxPort, Resources.USERNAME, Resources.PASSWORD, null);
+                Assert.Fail();
+            }
+            catch (ArgumentOutOfRangeException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("proxyPort", ex.ParamName);
+            }
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass too small proxy port.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentOutOfRangeException))]
-        public void Test_ConnectionInfo_ProxyPort_Small()
+        public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenProxyTypesIsNotNoneAndProxyPortIsLessThanMinimumValue()
         {
-            new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, Resources.HOST, int.MinValue, Resources.USERNAME, Resources.PASSWORD, null);
+            var minPort = IPEndPoint.MinPort;
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, Resources.HOST, --minPort, Resources.USERNAME, Resources.PASSWORD, null);
+                Assert.Fail();
+            }
+            catch (ArgumentOutOfRangeException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("proxyPort", ex.ParamName);
+            }
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass a valid proxy port.")]
-        [Owner("Kenneth_aa")]
         public void Test_ConnectionInfo_ProxyPort_Valid()
         {
-            new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            var proxyPort = new Random().Next(IPEndPoint.MinPort, IPEndPoint.MaxPort);
+
+            var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
+                ProxyTypes.None, Resources.HOST, proxyPort, Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.AreEqual(proxyPort, connectionInfo.ProxyPort);
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldNotThrowExceptionWhenProxyTypesIsNotNoneAndProxyUsernameIsNull()
+        {
+            const string proxyUsername = null;
+
+            var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http,
+                    Resources.PROXY_HOST, int.Parse(Resources.PORT), proxyUsername, Resources.PASSWORD,
+                    new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.IsNull(connectionInfo.ProxyUsername);
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentNullExceptionhenHostIsNull()
+        {
+            try
+            {
+                new ConnectionInfo(null, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("host", ex.ParamName);
+            }
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass null as host.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentException))]
-        public void Test_ConnectionInfo_Host_Null()
+        public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
         {
-            new ConnectionInfo(null, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            var host = string.Empty;
+
+            var connectionInfo = new ConnectionInfo(host, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
+                Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.AreSame(host, connectionInfo.Host);
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldNotThrowExceptionWhenHostIsInvalidDnsName()
+        {
+            const string host = "in_valid_host.";
+
+            var connectionInfo = new ConnectionInfo(host, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
+                Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.AreSame(host, connectionInfo.Host);
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass a valid host.")]
-        [Owner("Kenneth_aa")]
         public void Test_ConnectionInfo_Host_Valid()
         {
-            new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            var host = new Random().Next().ToString(CultureInfo.InvariantCulture);
+
+            var connectionInfo = new ConnectionInfo(host, int.Parse(Resources.PORT), Resources.USERNAME,
+                ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.AreSame(host, connectionInfo.Host);
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass too large port.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentOutOfRangeException))]
-        public void Test_ConnectionInfo_Port_Large()
+        public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsGreaterThanMaximumValue()
         {
-            new ConnectionInfo(Resources.HOST, int.MaxValue, Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            const int port = IPEndPoint.MaxPort + 1;
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, port, Resources.USERNAME, ProxyTypes.None, Resources.HOST,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+                Assert.Fail();
+            }
+            catch (ArgumentOutOfRangeException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("port", ex.ParamName);
+            }
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass too small port.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentOutOfRangeException))]
-        public void Test_ConnectionInfo_Port_Small()
+        public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsLessThanMinimumValue()
         {
-            new ConnectionInfo(Resources.HOST, int.MinValue, Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+            const int port = IPEndPoint.MinPort - 1;
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, port, Resources.USERNAME, ProxyTypes.None, Resources.HOST,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+                Assert.Fail();
+            }
+            catch (ArgumentOutOfRangeException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("port", ex.ParamName);
+            }
         }
 
         [TestMethod]
         [TestCategory("ConnectionInfo")]
-        [Description("Pass a valid port.")]
-        [Owner("Kenneth_aa")]
         public void Test_ConnectionInfo_Port_Valid()
         {
-            new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
-        }
-
-        [TestMethod]
-        [TestCategory("ConnectionInfo")]
-        [Description("Pass null as session.")]
-        [Owner("Kenneth_aa")]
-        [ExpectedException(typeof(ArgumentNullException))]
-        public void Test_ConnectionInfo_Authenticate_Null()
-        {
-            var ret = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
-            ret.Authenticate(null);
-        }
-
-        /// <summary>
-        ///A test for Timeout
-        ///</summary>
-        [TestMethod()]
-        public void TimeoutTest()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, username, authenticationMethods); // TODO: Initialize to an appropriate value
-            TimeSpan expected = new TimeSpan(); // TODO: Initialize to an appropriate value
-            TimeSpan actual;
-            target.Timeout = expected;
-            actual = target.Timeout;
-            Assert.AreEqual(expected, actual);
-            Assert.Inconclusive("Verify the correctness of this test method.");
-        }
-
-        /// <summary>
-        ///A test for RetryAttempts
-        ///</summary>
-        [TestMethod()]
-        public void RetryAttemptsTest()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, username, authenticationMethods); // TODO: Initialize to an appropriate value
-            int expected = 0; // TODO: Initialize to an appropriate value
-            int actual;
-            target.RetryAttempts = expected;
-            actual = target.RetryAttempts;
-            Assert.AreEqual(expected, actual);
-            Assert.Inconclusive("Verify the correctness of this test method.");
-        }
-
-        /// <summary>
-        ///A test for MaxSessions
-        ///</summary>
-        [TestMethod()]
-        public void MaxSessionsTest()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, username, authenticationMethods); // TODO: Initialize to an appropriate value
-            int expected = 0; // TODO: Initialize to an appropriate value
-            int actual;
-            target.MaxSessions = expected;
-            actual = target.MaxSessions;
-            Assert.AreEqual(expected, actual);
-            Assert.Inconclusive("Verify the correctness of this test method.");
-        }
-
-        /// <summary>
-        ///A test for Authenticate
-        ///</summary>
-        [TestMethod()]
-        public void AuthenticateTest()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, username, authenticationMethods); // TODO: Initialize to an appropriate value
-            Session session = null; // TODO: Initialize to an appropriate value
-            target.Authenticate(session);
-            Assert.Inconclusive("Verify the correctness of this test method.");
-        }
-
-        /// <summary>
-        ///A test for ConnectionInfo Constructor
-        ///</summary>
-        [TestMethod()]
-        public void ConnectionInfoConstructorTest()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            int port = 0; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            ProxyTypes proxyType = new ProxyTypes(); // TODO: Initialize to an appropriate value
-            string proxyHost = string.Empty; // TODO: Initialize to an appropriate value
-            int proxyPort = 0; // TODO: Initialize to an appropriate value
-            string proxyUsername = string.Empty; // TODO: Initialize to an appropriate value
-            string proxyPassword = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, authenticationMethods);
-            Assert.Inconclusive("TODO: Implement code to verify target");
-        }
-
-        /// <summary>
-        ///A test for ConnectionInfo Constructor
-        ///</summary>
-        [TestMethod()]
-        public void ConnectionInfoConstructorTest1()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            int port = 0; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, port, username, authenticationMethods);
-            Assert.Inconclusive("TODO: Implement code to verify target");
-        }
-
-        /// <summary>
-        ///A test for ConnectionInfo Constructor
-        ///</summary>
-        [TestMethod()]
-        public void ConnectionInfoConstructorTest2()
-        {
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            string username = string.Empty; // TODO: Initialize to an appropriate value
-            AuthenticationMethod[] authenticationMethods = null; // TODO: Initialize to an appropriate value
-            ConnectionInfo target = new ConnectionInfo(host, username, authenticationMethods);
-            Assert.Inconclusive("TODO: Implement code to verify target");
-        }
-    }
+            var port = new Random().Next(IPEndPoint.MinPort, IPEndPoint.MaxPort);
+
+            var connectionInfo = new ConnectionInfo(Resources.HOST, port, Resources.USERNAME, ProxyTypes.None,
+                Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            Assert.AreEqual(port, connectionInfo.Port);
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentExceptionhenUsernameIsNull()
+        {
+            const string username = null;
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), username, ProxyTypes.Http, Resources.USERNAME,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                    new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+                Assert.Fail();
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("username", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentExceptionhenUsernameIsEmpty()
+        {
+            var username = string.Empty;
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), username, ProxyTypes.Http, Resources.USERNAME,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                    new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+                Assert.Fail();
+            }
+            catch (ArgumentException ex)
+            {
+                Assert.AreEqual(typeof(ArgumentException), ex.GetType());
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("username", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentExceptionhenUsernameContainsOnlyWhitespace()
+        {
+            const string username = " \t\r";
+
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), username, ProxyTypes.Http, Resources.USERNAME,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                    new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+                Assert.Fail();
+            }
+            catch (ArgumentException ex)
+            {
+                Assert.AreEqual(typeof (ArgumentException), ex.GetType());
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("username", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentNullExceptionWhenAuthenticationMethodsIsNull()
+        {
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
+                Assert.Fail();
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("authenticationMethods", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void ConstructorShouldThrowArgumentNullExceptionWhenAuthenticationMethodsIsZeroLength()
+        {
+            try
+            {
+                new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST,
+                    int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, new AuthenticationMethod[0]);
+                Assert.Fail();
+            }
+            catch (ArgumentException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("authenticationMethods", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        [TestCategory("ConnectionInfo")]
+        public void AuthenticateShouldThrowArgumentNullExceptionWhenSessionIsNull()
+        {
+            var ret = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
+                Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
+                new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
+
+            try
+            {
+                ret.Authenticate(null);
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("session", ex.ParamName);
+            }
+        }
+   }
 }

+ 64 - 77
Renci.SshClient/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest.cs

@@ -91,17 +91,73 @@ namespace Renci.SshNet.Tests.Classes
         }
 
         [TestMethod]
-        [Description("Test passing null to AddForwardedPort hosts (local).")]
-        [ExpectedException(typeof(ArgumentNullException))]
-        public void Test_AddForwardedPort_Local_Hosts_Are_Null()
+        public void ConstructorShouldThrowArgumentNullExceptionWhenBoundHostIsNull()
         {
-            using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
+            try
             {
-                client.Connect();
-                var port1 = new ForwardedPortLocal(null, 8080, null, 80);
-                client.AddForwardedPort(port1);
-                client.Disconnect();
+                new ForwardedPortLocal(null, 8080, Resources.HOST, 80);
+                Assert.Fail();
             }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("boundHost", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        public void ConstructorShouldNotThrowExceptionWhenBoundHostIsEmpty()
+        {
+            var boundHost = string.Empty;
+
+            var forwardedPort = new ForwardedPortLocal(boundHost, 8080, Resources.HOST, 80);
+
+            Assert.AreSame(boundHost, forwardedPort.BoundHost);
+        }
+
+        [TestMethod]
+        public void ConstructorShouldNotThrowExceptionWhenBoundHostIsInvalidDnsName()
+        {
+            const string boundHost = "in_valid_host.";
+
+            var forwardedPort = new ForwardedPortLocal(boundHost, 8080, Resources.HOST, 80);
+
+            Assert.AreSame(boundHost, forwardedPort.BoundHost);
+        }
+
+        [TestMethod]
+        public void ConstructorShouldThrowArgumentNullExceptionWhenHostIsNull()
+        {
+            try
+            {
+                new ForwardedPortLocal(Resources.HOST, 8080, null, 80);
+                Assert.Fail();
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("host", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
+        {
+            var host = string.Empty;
+
+            var forwardedPort = new ForwardedPortLocal(Resources.HOST, 8080, string.Empty, 80);
+
+            Assert.AreSame(host, forwardedPort.Host);
+        }
+
+        [TestMethod]
+        public void ConstructorShouldNotThrowExceptionWhenHostIsInvalidDnsName()
+        {
+            const string host = "in_valid_host.";
+
+            var forwardedPort = new ForwardedPortLocal(Resources.HOST, 8080, host, 80);
+
+            Assert.AreSame(host, forwardedPort.Host);
         }
 
         /// <summary>
@@ -129,74 +185,5 @@ namespace Renci.SshNet.Tests.Classes
             }
             Assert.Inconclusive("TODO: Implement code to verify target");
         }
-
-        /// <summary>
-        ///A test for ForwardedPortLocal Constructor
-        ///</summary>
-        [TestMethod()]
-        public void ForwardedPortLocalConstructorTest()
-        {
-            string boundHost = string.Empty; // TODO: Initialize to an appropriate value
-            uint boundPort = 0; // TODO: Initialize to an appropriate value
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            uint port = 0; // TODO: Initialize to an appropriate value
-            ForwardedPortLocal target = new ForwardedPortLocal(boundHost, boundPort, host, port);
-            Assert.Inconclusive("TODO: Implement code to verify target");
-        }
-
-        /// <summary>
-        ///A test for ForwardedPortLocal Constructor
-        ///</summary>
-        [TestMethod()]
-        public void ForwardedPortLocalConstructorTest1()
-        {
-            uint boundPort = 0; // TODO: Initialize to an appropriate value
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            uint port = 0; // TODO: Initialize to an appropriate value
-            ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port);
-            Assert.Inconclusive("TODO: Implement code to verify target");
-        }
-
-        /// <summary>
-        ///A test for Stop
-        ///</summary>
-        [TestMethod()]
-        public void StopTest()
-        {
-            uint boundPort = 0; // TODO: Initialize to an appropriate value
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            uint port = 0; // TODO: Initialize to an appropriate value
-            ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
-            target.Stop();
-            Assert.Inconclusive("A method that does not return a value cannot be verified.");
-        }
-
-        /// <summary>
-        ///A test for Start
-        ///</summary>
-        [TestMethod()]
-        public void StartTest()
-        {
-            uint boundPort = 0; // TODO: Initialize to an appropriate value
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            uint port = 0; // TODO: Initialize to an appropriate value
-            ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
-            target.Start();
-            Assert.Inconclusive("A method that does not return a value cannot be verified.");
-        }
-
-        /// <summary>
-        ///A test for Dispose
-        ///</summary>
-        [TestMethod()]
-        public void DisposeTest()
-        {
-            uint boundPort = 0; // TODO: Initialize to an appropriate value
-            string host = string.Empty; // TODO: Initialize to an appropriate value
-            uint port = 0; // TODO: Initialize to an appropriate value
-            ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
-            target.Dispose();
-            Assert.Inconclusive("A method that does not return a value cannot be verified.");
-        }
     }
 }

+ 45 - 0
Renci.SshClient/Renci.SshNet.Tests/Classes/SftpClientTest.Connect.cs

@@ -0,0 +1,45 @@
+using System.Net.Sockets;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Renci.SshNet.Tests.Common;
+
+namespace Renci.SshNet.Tests.Classes
+{
+    public partial class SftpClientTest : TestBase
+    {
+        [TestMethod]
+        public void Connect_HostNameInvalid_ShouldThrowSocketExceptionWithErrorCodeHostNotFound()
+        {
+            var connectionInfo = new ConnectionInfo("invalid.", 40, "user",
+                new KeyboardInteractiveAuthenticationMethod("user"));
+            var sftpClient = new SftpClient(connectionInfo);
+
+            try
+            {
+                sftpClient.Connect();
+                Assert.Fail();
+            }
+            catch (SocketException ex)
+            {
+                Assert.AreEqual(ex.ErrorCode, (int) SocketError.HostNotFound);
+            }
+        }
+
+        [TestMethod]
+        public void Connect_ProxyHostNameInvalid_ShouldThrowSocketExceptionWithErrorCodeHostNotFound()
+        {
+            var connectionInfo = new ConnectionInfo("localhost", 40, "user", ProxyTypes.Http, "invalid.", 80,
+                "proxyUser", "proxyPwd", new KeyboardInteractiveAuthenticationMethod("user"));
+            var sftpClient = new SftpClient(connectionInfo);
+
+            try
+            {
+                sftpClient.Connect();
+                Assert.Fail();
+            }
+            catch (SocketException ex)
+            {
+                Assert.AreEqual(ex.ErrorCode, (int)SocketError.HostNotFound);
+            }
+        }
+    }
+}

+ 1 - 0
Renci.SshClient/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

@@ -196,6 +196,7 @@
     <Compile Include="Classes\Security\KeyHostAlgorithmTest.cs" />
     <Compile Include="Classes\SessionTest.cs" />
     <Compile Include="Classes\SftpClientTest.ChangeDirectory.cs" />
+    <Compile Include="Classes\SftpClientTest.Connect.cs" />
     <Compile Include="Classes\Sftp\Requests\ExtendedRequests\FStatVfsRequestTest.cs" />
     <Compile Include="Classes\Sftp\Requests\ExtendedRequests\HardLinkRequestTest.cs" />
     <Compile Include="Classes\Sftp\Requests\ExtendedRequests\PosixRenameRequestTest.cs" />

+ 11 - 39
Renci.SshClient/Renci.SshNet/Common/Extensions.cs

@@ -160,53 +160,25 @@ namespace Renci.SshNet
             return new byte[] { (byte)(value >> 56), (byte)(value >> 48), (byte)(value >> 40), (byte)(value >> 32), (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF) };
         }
 
-#if SILVERLIGHT
-        private static Regex _rehost = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$", RegexOptions.IgnoreCase);
-
-        private static Regex _reIPv6 = new Regex(@"^(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\2([0-9A-F]{1,4}(::?|$)){0,2}|((25[0-5]|(2[0-4]|1\d|[1-9])?\d)(\.|$)){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:|\.)\z", RegexOptions.IgnoreCase);
-#else
-        private static readonly Regex _rehost = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
-
-        private static readonly Regex _reIPv6 = new Regex(@"^(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\2([0-9A-F]{1,4}(::?|$)){0,2}|((25[0-5]|(2[0-4]|1\d|[1-9])?\d)(\.|$)){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:|\.)\z", RegexOptions.IgnoreCase | RegexOptions.Compiled);
-
-#endif
-
-        internal static bool IsValidHost(this string value)
+        internal static void ValidatePort(this uint value, string argument)
         {
-            if (value == null)
-                return false;
-
-            if (value == string.Empty)
-                return true;
-
-            if (_rehost.Match(value).Success)
-                return true;
-
-            if (_reIPv6.Match(value).Success)
-                return true;
-
-            return false;
-        }
-
-        internal static bool IsValidPort(this uint value)
-        {
-            if (value < IPEndPoint.MinPort)
-                return false;
-
             if (value > IPEndPoint.MaxPort)
-                return false;
-            return true;
+                throw new ArgumentOutOfRangeException(argument,
+                    string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.",
+                        IPEndPoint.MaxPort));
         }
 
-        internal static bool IsValidPort(this int value)
+        internal static void ValidatePort(this int value, string argument)
         {
             if (value < IPEndPoint.MinPort)
-                return false;
+                throw new ArgumentOutOfRangeException(argument,
+                    string.Format(CultureInfo.InvariantCulture, "Specified value cannot be less than {0}.",
+                        IPEndPoint.MinPort));
 
             if (value > IPEndPoint.MaxPort)
-                return false;
-            return true;
+                throw new ArgumentOutOfRangeException(argument,
+                    string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.",
+                        IPEndPoint.MaxPort));
         }
-
     }
 }

+ 5 - 8
Renci.SshClient/Renci.SshNet/Common/PortForwardEventArgs.cs

@@ -22,19 +22,16 @@ namespace Renci.SshNet.Common
         /// </summary>
         /// <param name="host">The host.</param>
         /// <param name="port">The port.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
         internal PortForwardEventArgs(string host, uint port)
         {
             if (host == null)
                 throw new ArgumentNullException("host");
+            port.ValidatePort("port");
 
-            if (!host.IsValidHost())
-                throw new ArgumentException("host");
-
-            if (!port.IsValidPort())
-                throw new ArgumentOutOfRangeException("port");
-
-            this.OriginatorHost = host;
-            this.OriginatorPort = port;
+            OriginatorHost = host;
+            OriginatorPort = port;
         }
     }
 }

+ 31 - 19
Renci.SshClient/Renci.SshNet/ConnectionInfo.cs

@@ -214,6 +214,11 @@ namespace Renci.SshNet
         /// <param name="host">The host.</param>
         /// <param name="username">The username.</param>
         /// <param name="authenticationMethods">The authentication methods.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="host"/> is a zero-length string.</exception>
+        /// <exception cref="ArgumentException"><paramref name="username" /> is null, a zero-length string or contains only whitespace characters.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
         public ConnectionInfo(string host, string username, params AuthenticationMethod[] authenticationMethods)
             : this(host, DEFAULT_PORT, username, ProxyTypes.None, null, 0, null, null, authenticationMethods)
         {
@@ -226,6 +231,11 @@ namespace Renci.SshNet
         /// <param name="port">The port.</param>
         /// <param name="username">The username.</param>
         /// <param name="authenticationMethods">The authentication methods.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="username" /> is null, a zero-length string or contains only whitespace characters.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
         public ConnectionInfo(string host, int port, string username, params AuthenticationMethod[] authenticationMethods)
             : this(host, port, username, ProxyTypes.None, null, 0, null, null, authenticationMethods)
         {
@@ -245,33 +255,35 @@ namespace Renci.SshNet
         /// <param name="proxyUsername">The proxy username.</param>
         /// <param name="proxyPassword">The proxy password.</param>
         /// <param name="authenticationMethods">The authentication methods.</param>
-        /// <exception cref="System.ArgumentException">host</exception>
-        /// <exception cref="System.ArgumentOutOfRangeException">proxyPort</exception>
-        /// <exception cref="ArgumentException"><paramref name="host" /> is invalid, or <paramref name="username" /> is null or contains whitespace characters.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException"><paramref name="username" /> is null, a zero-length string or contains only whitespace characters.</exception>
         /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
-        /// <exception cref="ArgumentException"><paramref name="host" /> is invalid, or <paramref name="username" /> is null or contains whitespace characters.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyHost" /> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyPort" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
         public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
         {
-            if (!host.IsValidHost())
-                throw new ArgumentException("host");
+            if (host == null)
+                throw new ArgumentNullException("host");
+            port.ValidatePort("port");
+
+            if (username == null)
+                throw new ArgumentNullException("username");
+            if (username.All(char.IsWhiteSpace))
+                throw new ArgumentException("Cannot be empty or contain only whitespace.", "username");
 
             if (proxyType != ProxyTypes.None)
             {
-                if (string.IsNullOrEmpty(proxyHost) && !proxyHost.IsValidHost())
-                    throw new ArgumentException("proxyHost");
-
-                if (!proxyPort.IsValidPort())
-                    throw new ArgumentOutOfRangeException("proxyPort");
+                if (proxyHost == null)
+                    throw new ArgumentNullException("proxyHost");
+                proxyPort.ValidatePort("proxyPort");
             }
 
-            if (!port.IsValidPort())
-                throw new ArgumentOutOfRangeException("port");
-
-            if (username.IsNullOrWhiteSpace())
-                throw new ArgumentException("username");
-
-            if (authenticationMethods == null || authenticationMethods.Length < 1)
-                throw new ArgumentException("authenticationMethods");
+            if (authenticationMethods == null)
+                throw new ArgumentNullException("authenticationMethods");
+            if (!authenticationMethods.Any())
+                throw new ArgumentException("At least one authentication method should be specified.", "authenticationMethods");
 
             //  Set default connection values
             this.Timeout = TimeSpan.FromSeconds(30);

+ 12 - 11
Renci.SshClient/Renci.SshNet/ForwardedPortLocal.cs

@@ -36,6 +36,9 @@ namespace Renci.SshNet
         /// <param name="boundPort">The bound port.</param>
         /// <param name="host">The host.</param>
         /// <param name="port">The port.</param>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
         /// <example>
         ///     <code source="..\..\Renci.SshNet.Tests\Classes\ForwardedPortLocalTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortLocal" language="C#" title="Local port forwarding" />
         /// </example>
@@ -50,6 +53,9 @@ namespace Renci.SshNet
         /// <param name="boundHost">The bound host.</param>
         /// <param name="host">The host.</param>
         /// <param name="port">The port.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="boundHost"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
         public ForwardedPortLocal(string boundHost, string host, uint port)
             : this(boundHost, 0, host, port) 
         {
@@ -62,6 +68,10 @@ namespace Renci.SshNet
         /// <param name="boundPort">The bound port.</param>
         /// <param name="host">The host.</param>
         /// <param name="port">The port.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="boundHost"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
         public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint port)
         {
             if (boundHost == null)
@@ -70,17 +80,8 @@ namespace Renci.SshNet
             if (host == null)
                 throw new ArgumentNullException("host");
 
-            if (!boundHost.IsValidHost())
-                throw new ArgumentException("boundHost");
-
-            if (!boundPort.IsValidPort())
-                throw new ArgumentOutOfRangeException("boundPort");
-
-            if (!host.IsValidHost())
-                throw new ArgumentException("host");
-
-            if (!port.IsValidPort())
-                throw new ArgumentOutOfRangeException("port");
+            boundPort.ValidatePort("boundPort");
+            port.ValidatePort("port");
 
             this.BoundHost = boundHost;
             this.BoundPort = boundPort;

+ 5 - 6
Renci.SshClient/Renci.SshNet/ForwardedPortRemote.cs

@@ -68,18 +68,17 @@ namespace Renci.SshNet
         /// <param name="port">The port.</param>
         /// <exception cref="ArgumentNullException"><paramref name="boundHostAddress"/> is <c>null</c>.</exception>
         /// <exception cref="ArgumentNullException"><paramref name="hostAddress"/> is <c>null</c>.</exception>
-        /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort"/> is not a valid port.</exception>
-        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not a valid port.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
         public ForwardedPortRemote(IPAddress boundHostAddress, uint boundPort, IPAddress hostAddress, uint port)
         {
             if (boundHostAddress == null)
                 throw new ArgumentNullException("boundHostAddress");
             if (hostAddress == null)
                 throw new ArgumentNullException("hostAddress");
-            if (!boundPort.IsValidPort())
-                throw new ArgumentOutOfRangeException("boundPort");
-            if (!port.IsValidPort())
-                throw new ArgumentOutOfRangeException("port");
+
+            boundPort.ValidatePort("boundPort");
+            port.ValidatePort("port");
 
             this.BoundHostAddress = boundHostAddress;
             this.BoundPort = boundPort;