| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- using System.Globalization;
- using System.Net;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Moq;
- using Renci.SshNet.Tests.Common;
- using Renci.SshNet.Tests.Properties;
- using System;
- namespace Renci.SshNet.Tests.Classes
- {
- /// <summary>
- /// Represents remote connection information class.
- /// </summary>
- [TestClass]
- public class ConnectionInfoTest : TestBase
- {
- [TestMethod]
- [TestCategory("ConnectionInfo")]
- public void ConstructorShouldNotThrowExceptionhenProxyTypesIsNoneAndProxyHostIsNull()
- {
- 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")]
- public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenProxyTypesIsNotNoneAndProxyPortIsGreaterThanMaximumValue()
- {
- 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")]
- public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenProxyTypesIsNotNoneAndProxyPortIsLessThanMinimumValue()
- {
- 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")]
- public void Test_ConnectionInfo_ProxyPort_Valid()
- {
- 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")]
- public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
- {
- 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")]
- public void Test_ConnectionInfo_Host_Valid()
- {
- 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")]
- public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsGreaterThanMaximumValue()
- {
- 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")]
- public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsLessThanMinimumValue()
- {
- 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")]
- public void Test_ConnectionInfo_Port_Valid()
- {
- 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 AuthenticateShouldThrowArgumentNullExceptionWhenServiceFactoryIsNull()
- {
- var connectionInfo = 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));
- var session = new Mock<ISession>(MockBehavior.Strict).Object;
- const IServiceFactory serviceFactory = null;
- try
- {
- connectionInfo.Authenticate(session, serviceFactory);
- Assert.Fail();
- }
- catch (ArgumentNullException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual("serviceFactory", ex.ParamName);
- }
- }
- }
- }
|