2
0

ConnectionInfoTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System.Globalization;
  2. using System.Net;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Tests.Common;
  6. using Renci.SshNet.Tests.Properties;
  7. using System;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. /// <summary>
  11. /// Represents remote connection information class.
  12. /// </summary>
  13. [TestClass]
  14. public class ConnectionInfoTest : TestBase
  15. {
  16. [TestMethod]
  17. [TestCategory("ConnectionInfo")]
  18. public void ConstructorShouldNotThrowExceptionhenProxyTypesIsNoneAndProxyHostIsNull()
  19. {
  20. const string proxyHost = null;
  21. var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
  22. ProxyTypes.None, proxyHost, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  23. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  24. Assert.IsNull(connectionInfo.ProxyHost);
  25. }
  26. [TestMethod]
  27. [TestCategory("ConnectionInfo")]
  28. public void ConstructorShouldThrowArgumentNullExceptionhenProxyTypesIsNotNoneAndProxyHostIsNull()
  29. {
  30. try
  31. {
  32. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, null,
  33. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  34. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  35. Assert.Fail();
  36. }
  37. catch (ArgumentNullException ex)
  38. {
  39. Assert.IsNull(ex.InnerException);
  40. Assert.AreEqual("proxyHost", ex.ParamName);
  41. }
  42. }
  43. [TestMethod]
  44. [TestCategory("ConnectionInfo")]
  45. public void ConstructorShouldNotThrowExceptionWhenProxyTypesIsNotNoneAndProxyHostIsEmpty()
  46. {
  47. var proxyHost = string.Empty;
  48. var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
  49. ProxyTypes.Http, string.Empty, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  50. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  51. Assert.AreSame(proxyHost, connectionInfo.ProxyHost);
  52. }
  53. [TestMethod]
  54. [TestCategory("ConnectionInfo")]
  55. public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenProxyTypesIsNotNoneAndProxyPortIsGreaterThanMaximumValue()
  56. {
  57. var maxPort = IPEndPoint.MaxPort;
  58. try
  59. {
  60. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, Resources.HOST, ++maxPort, Resources.USERNAME, Resources.PASSWORD, null);
  61. Assert.Fail();
  62. }
  63. catch (ArgumentOutOfRangeException ex)
  64. {
  65. Assert.IsNull(ex.InnerException);
  66. Assert.AreEqual("proxyPort", ex.ParamName);
  67. }
  68. }
  69. [TestMethod]
  70. [TestCategory("ConnectionInfo")]
  71. public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenProxyTypesIsNotNoneAndProxyPortIsLessThanMinimumValue()
  72. {
  73. var minPort = IPEndPoint.MinPort;
  74. try
  75. {
  76. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http, Resources.HOST, --minPort, Resources.USERNAME, Resources.PASSWORD, null);
  77. Assert.Fail();
  78. }
  79. catch (ArgumentOutOfRangeException ex)
  80. {
  81. Assert.IsNull(ex.InnerException);
  82. Assert.AreEqual("proxyPort", ex.ParamName);
  83. }
  84. }
  85. [TestMethod]
  86. [TestCategory("ConnectionInfo")]
  87. public void Test_ConnectionInfo_ProxyPort_Valid()
  88. {
  89. var proxyPort = new Random().Next(IPEndPoint.MinPort, IPEndPoint.MaxPort);
  90. var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME,
  91. ProxyTypes.None, Resources.HOST, proxyPort, Resources.USERNAME, Resources.PASSWORD,
  92. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  93. Assert.AreEqual(proxyPort, connectionInfo.ProxyPort);
  94. }
  95. [TestMethod]
  96. [TestCategory("ConnectionInfo")]
  97. public void ConstructorShouldNotThrowExceptionWhenProxyTypesIsNotNoneAndProxyUsernameIsNull()
  98. {
  99. const string proxyUsername = null;
  100. var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.Http,
  101. Resources.PROXY_HOST, int.Parse(Resources.PORT), proxyUsername, Resources.PASSWORD,
  102. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  103. Assert.IsNull(connectionInfo.ProxyUsername);
  104. }
  105. [TestMethod]
  106. [TestCategory("ConnectionInfo")]
  107. public void ConstructorShouldThrowArgumentNullExceptionhenHostIsNull()
  108. {
  109. try
  110. {
  111. new ConnectionInfo(null, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST,
  112. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
  113. }
  114. catch (ArgumentNullException ex)
  115. {
  116. Assert.IsNull(ex.InnerException);
  117. Assert.AreEqual("host", ex.ParamName);
  118. }
  119. }
  120. [TestMethod]
  121. [TestCategory("ConnectionInfo")]
  122. public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
  123. {
  124. var host = string.Empty;
  125. var connectionInfo = new ConnectionInfo(host, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
  126. Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  127. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  128. Assert.AreSame(host, connectionInfo.Host);
  129. }
  130. [TestMethod]
  131. [TestCategory("ConnectionInfo")]
  132. public void ConstructorShouldNotThrowExceptionWhenHostIsInvalidDnsName()
  133. {
  134. const string host = "in_valid_host.";
  135. var connectionInfo = new ConnectionInfo(host, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
  136. Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  137. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  138. Assert.AreSame(host, connectionInfo.Host);
  139. }
  140. [TestMethod]
  141. [TestCategory("ConnectionInfo")]
  142. public void Test_ConnectionInfo_Host_Valid()
  143. {
  144. var host = new Random().Next().ToString(CultureInfo.InvariantCulture);
  145. var connectionInfo = new ConnectionInfo(host, int.Parse(Resources.PORT), Resources.USERNAME,
  146. ProxyTypes.None, Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  147. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  148. Assert.AreSame(host, connectionInfo.Host);
  149. }
  150. [TestMethod]
  151. [TestCategory("ConnectionInfo")]
  152. public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsGreaterThanMaximumValue()
  153. {
  154. const int port = IPEndPoint.MaxPort + 1;
  155. try
  156. {
  157. new ConnectionInfo(Resources.HOST, port, Resources.USERNAME, ProxyTypes.None, Resources.HOST,
  158. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
  159. Assert.Fail();
  160. }
  161. catch (ArgumentOutOfRangeException ex)
  162. {
  163. Assert.IsNull(ex.InnerException);
  164. Assert.AreEqual("port", ex.ParamName);
  165. }
  166. }
  167. [TestMethod]
  168. [TestCategory("ConnectionInfo")]
  169. public void ConstructorShouldThrowArgumentOutOfRangeExceptionWhenPortIsLessThanMinimumValue()
  170. {
  171. const int port = IPEndPoint.MinPort - 1;
  172. try
  173. {
  174. new ConnectionInfo(Resources.HOST, port, Resources.USERNAME, ProxyTypes.None, Resources.HOST,
  175. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
  176. Assert.Fail();
  177. }
  178. catch (ArgumentOutOfRangeException ex)
  179. {
  180. Assert.IsNull(ex.InnerException);
  181. Assert.AreEqual("port", ex.ParamName);
  182. }
  183. }
  184. [TestMethod]
  185. [TestCategory("ConnectionInfo")]
  186. public void Test_ConnectionInfo_Port_Valid()
  187. {
  188. var port = new Random().Next(IPEndPoint.MinPort, IPEndPoint.MaxPort);
  189. var connectionInfo = new ConnectionInfo(Resources.HOST, port, Resources.USERNAME, ProxyTypes.None,
  190. Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  191. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  192. Assert.AreEqual(port, connectionInfo.Port);
  193. }
  194. [TestMethod]
  195. [TestCategory("ConnectionInfo")]
  196. public void ConstructorShouldThrowArgumentExceptionhenUsernameIsNull()
  197. {
  198. const string username = null;
  199. try
  200. {
  201. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), username, ProxyTypes.Http, Resources.USERNAME,
  202. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  203. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  204. Assert.Fail();
  205. }
  206. catch (ArgumentNullException ex)
  207. {
  208. Assert.IsNull(ex.InnerException);
  209. Assert.AreEqual("username", ex.ParamName);
  210. }
  211. }
  212. [TestMethod]
  213. [TestCategory("ConnectionInfo")]
  214. public void ConstructorShouldThrowArgumentExceptionhenUsernameIsEmpty()
  215. {
  216. var username = string.Empty;
  217. try
  218. {
  219. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), username, ProxyTypes.Http, Resources.USERNAME,
  220. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  221. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  222. Assert.Fail();
  223. }
  224. catch (ArgumentException ex)
  225. {
  226. Assert.AreEqual(typeof(ArgumentException), ex.GetType());
  227. Assert.IsNull(ex.InnerException);
  228. Assert.AreEqual("username", ex.ParamName);
  229. }
  230. }
  231. [TestMethod]
  232. [TestCategory("ConnectionInfo")]
  233. public void ConstructorShouldThrowArgumentExceptionhenUsernameContainsOnlyWhitespace()
  234. {
  235. const string username = " \t\r";
  236. try
  237. {
  238. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), username, ProxyTypes.Http, Resources.USERNAME,
  239. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  240. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  241. Assert.Fail();
  242. }
  243. catch (ArgumentException ex)
  244. {
  245. Assert.AreEqual(typeof (ArgumentException), ex.GetType());
  246. Assert.IsNull(ex.InnerException);
  247. Assert.AreEqual("username", ex.ParamName);
  248. }
  249. }
  250. [TestMethod]
  251. [TestCategory("ConnectionInfo")]
  252. public void ConstructorShouldThrowArgumentNullExceptionWhenAuthenticationMethodsIsNull()
  253. {
  254. try
  255. {
  256. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST,
  257. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, null);
  258. Assert.Fail();
  259. }
  260. catch (ArgumentNullException ex)
  261. {
  262. Assert.IsNull(ex.InnerException);
  263. Assert.AreEqual("authenticationMethods", ex.ParamName);
  264. }
  265. }
  266. [TestMethod]
  267. [TestCategory("ConnectionInfo")]
  268. public void ConstructorShouldThrowArgumentNullExceptionWhenAuthenticationMethodsIsZeroLength()
  269. {
  270. try
  271. {
  272. new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None, Resources.HOST,
  273. int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD, new AuthenticationMethod[0]);
  274. Assert.Fail();
  275. }
  276. catch (ArgumentException ex)
  277. {
  278. Assert.IsNull(ex.InnerException);
  279. Assert.AreEqual("authenticationMethods", ex.ParamName);
  280. }
  281. }
  282. [TestMethod]
  283. [TestCategory("ConnectionInfo")]
  284. public void AuthenticateShouldThrowArgumentNullExceptionWhenServiceFactoryIsNull()
  285. {
  286. var connectionInfo = new ConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, ProxyTypes.None,
  287. Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD,
  288. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  289. var session = new Mock<ISession>(MockBehavior.Strict).Object;
  290. IServiceFactory serviceFactory = null;
  291. try
  292. {
  293. connectionInfo.Authenticate(session, serviceFactory);
  294. Assert.Fail();
  295. }
  296. catch (ArgumentNullException ex)
  297. {
  298. Assert.IsNull(ex.InnerException);
  299. Assert.AreEqual("serviceFactory", ex.ParamName);
  300. }
  301. }
  302. }
  303. }