ConnectionInfoTest.cs 14 KB

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