ConnectionTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System.IO;
  7. using Renci.SshNet.Tests.Properties;
  8. using System.Security.Authentication;
  9. using Renci.SshNet.Common;
  10. using System.Net;
  11. namespace Renci.SshNet.Tests
  12. {
  13. [TestClass]
  14. public class ConnectionTest
  15. {
  16. [TestMethod]
  17. [TestCategory("Authentication")]
  18. public void Test_Connect_Using_Correct_Password()
  19. {
  20. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  21. {
  22. client.Connect();
  23. client.Disconnect();
  24. }
  25. }
  26. [TestMethod]
  27. [TestCategory("Authentication")]
  28. [ExpectedException(typeof(SshAuthenticationException))]
  29. public void Test_Connect_Using_Invalid_Password()
  30. {
  31. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  32. {
  33. client.Connect();
  34. client.Disconnect();
  35. }
  36. }
  37. [TestMethod]
  38. [TestCategory("Authentication")]
  39. public void Test_Connect_Using_Rsa_Key_Without_PassPhrase()
  40. {
  41. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITHOUT_PASS));
  42. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream)))
  43. {
  44. client.Connect();
  45. client.Disconnect();
  46. }
  47. }
  48. [TestMethod]
  49. [TestCategory("Authentication")]
  50. public void Test_Connect_Using_RsaKey_With_PassPhrase()
  51. {
  52. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITH_PASS));
  53. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream, Resources.PASSWORD)))
  54. {
  55. client.Connect();
  56. client.Disconnect();
  57. }
  58. }
  59. [TestMethod]
  60. [TestCategory("Authentication")]
  61. [ExpectedException(typeof(SshPassPhraseNullOrEmptyException))]
  62. public void Test_Connect_Using_Key_With_Empty_PassPhrase()
  63. {
  64. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITH_PASS));
  65. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream, null)))
  66. {
  67. client.Connect();
  68. client.Disconnect();
  69. }
  70. }
  71. [TestMethod]
  72. [TestCategory("Authentication")]
  73. public void Test_Connect_Using_DsaKey_Without_PassPhrase()
  74. {
  75. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITHOUT_PASS));
  76. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream)))
  77. {
  78. client.Connect();
  79. client.Disconnect();
  80. }
  81. }
  82. [TestMethod]
  83. [TestCategory("Authentication")]
  84. public void Test_Connect_Using_DsaKey_With_PassPhrase()
  85. {
  86. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITH_PASS));
  87. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream, Resources.PASSWORD)))
  88. {
  89. client.Connect();
  90. client.Disconnect();
  91. }
  92. }
  93. [TestMethod]
  94. [TestCategory("Authentication")]
  95. [ExpectedException(typeof(SshAuthenticationException))]
  96. public void Test_Connect_Using_Invalid_PrivateKey()
  97. {
  98. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.INVALID_KEY));
  99. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream)))
  100. {
  101. client.Connect();
  102. client.Disconnect();
  103. }
  104. }
  105. [TestMethod]
  106. [TestCategory("Authentication")]
  107. public void Test_Connect_Using_Multiple_PrivateKeys()
  108. {
  109. using (var client = new SshClient(Resources.HOST, Resources.USERNAME,
  110. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.INVALID_KEY))),
  111. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITH_PASS)), Resources.PASSWORD),
  112. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITH_PASS)), Resources.PASSWORD),
  113. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITHOUT_PASS))),
  114. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITHOUT_PASS)))
  115. ))
  116. {
  117. client.Connect();
  118. client.Disconnect();
  119. }
  120. }
  121. [TestMethod]
  122. [TestCategory("Authentication")]
  123. public void Test_Connect_Then_Reconnect()
  124. {
  125. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  126. {
  127. client.Connect();
  128. client.Disconnect();
  129. client.Connect();
  130. client.Disconnect();
  131. }
  132. }
  133. [WorkItem(703), TestMethod]
  134. [TestCategory("PasswordConnectionInfo")]
  135. [ExpectedException(typeof(ArgumentException))]
  136. public void Test_ConnectionInfo_Host_Is_Null()
  137. {
  138. var connectionInfo = new PasswordConnectionInfo(null, "username", "password");
  139. }
  140. [WorkItem(703), TestMethod]
  141. [TestCategory("PasswordConnectionInfo")]
  142. [ExpectedException(typeof(ArgumentException))]
  143. public void Test_ConnectionInfo_Username_Is_Null()
  144. {
  145. var connectionInfo = new PasswordConnectionInfo("host", null, "password");
  146. }
  147. [WorkItem(703), TestMethod]
  148. [TestCategory("PasswordConnectionInfo")]
  149. [ExpectedException(typeof(ArgumentNullException))]
  150. public void Test_ConnectionInfo_Password_Is_Null()
  151. {
  152. var connectionInfo = new PasswordConnectionInfo("host", "username", null);
  153. }
  154. [TestMethod]
  155. [TestCategory("PasswordConnectionInfo")]
  156. [Description("Test passing whitespace to host parameter.")]
  157. [ExpectedException(typeof(ArgumentException))]
  158. public void Test_ConnectionInfo_Host_Is_Whitespace()
  159. {
  160. var connectionInfo = new PasswordConnectionInfo(" ", Resources.USERNAME,Resources.PASSWORD);
  161. }
  162. [TestMethod]
  163. [TestCategory("PasswordConnectionInfo")]
  164. [Description("Test passing whitespace to username parameter.")]
  165. [ExpectedException(typeof(ArgumentException))]
  166. public void Test_ConnectionInfo_Username_Is_Whitespace()
  167. {
  168. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, " ", Resources.PASSWORD);
  169. }
  170. [WorkItem(703), TestMethod]
  171. [TestCategory("PasswordConnectionInfo")]
  172. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  173. public void Test_ConnectionInfo_SmallPortNumber()
  174. {
  175. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, IPEndPoint.MinPort - 1, Resources.USERNAME, Resources.PASSWORD);
  176. }
  177. [WorkItem(703), TestMethod]
  178. [TestCategory("PasswordConnectionInfo")]
  179. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  180. public void Test_ConnectionInfo_BigPortNumber()
  181. {
  182. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, IPEndPoint.MaxPort + 1, Resources.USERNAME, Resources.PASSWORD);
  183. }
  184. [TestMethod]
  185. [TestCategory("ConnectionInfo")]
  186. [Description("Pass null as proxy host.")]
  187. [Owner("Kenneth_aa")]
  188. [ExpectedException(typeof(ArgumentException))]
  189. public void Test_ConnectionInfo_ProxyHost_Null()
  190. {
  191. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, null, 22, Resources.USERNAME, Resources.PASSWORD, null);
  192. }
  193. [TestMethod]
  194. [TestCategory("ConnectionInfo")]
  195. [Description("Pass String.Empty as proxy host.")]
  196. [Owner("Kenneth_aa")]
  197. [ExpectedException(typeof(ArgumentException))]
  198. public void Test_ConnectionInfo_ProxyHost_Empty()
  199. {
  200. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, string.Empty, 22, Resources.USERNAME, Resources.PASSWORD, null);
  201. }
  202. [TestMethod]
  203. [TestCategory("ConnectionInfo")]
  204. [Description("Pass too large proxy port.")]
  205. [Owner("Kenneth_aa")]
  206. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  207. public void Test_ConnectionInfo_ProxyPort_Large()
  208. {
  209. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.MaxValue, Resources.USERNAME, Resources.PASSWORD, null);
  210. }
  211. [TestMethod]
  212. [TestCategory("ConnectionInfo")]
  213. [Description("Pass too small proxy port.")]
  214. [Owner("Kenneth_aa")]
  215. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  216. public void Test_ConnectionInfo_ProxyPort_Small()
  217. {
  218. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, int.MinValue, Resources.USERNAME, Resources.PASSWORD, null);
  219. }
  220. [TestMethod]
  221. [TestCategory("ConnectionInfo")]
  222. [Description("Pass a valid proxy port.")]
  223. [Owner("Kenneth_aa")]
  224. public void Test_ConnectionInfo_ProxyPort_Valid()
  225. {
  226. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  227. }
  228. [TestMethod]
  229. [TestCategory("ConnectionInfo")]
  230. [Description("Pass null as host.")]
  231. [Owner("Kenneth_aa")]
  232. [ExpectedException(typeof(ArgumentException))]
  233. public void Test_ConnectionInfo_Host_Null()
  234. {
  235. new ConnectionInfo(null, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  236. }
  237. [TestMethod]
  238. [TestCategory("ConnectionInfo")]
  239. [Description("Pass String.Empty as host.")]
  240. [Owner("Kenneth_aa")]
  241. [ExpectedException(typeof(ArgumentException))]
  242. public void Test_ConnectionInfo_Host_Empty()
  243. {
  244. new ConnectionInfo(string.Empty, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  245. }
  246. [TestMethod]
  247. [TestCategory("ConnectionInfo")]
  248. [Description("Pass a valid host.")]
  249. [Owner("Kenneth_aa")]
  250. public void Test_ConnectionInfo_Host_Valid()
  251. {
  252. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  253. }
  254. [TestMethod]
  255. [TestCategory("ConnectionInfo")]
  256. [Description("Pass too large port.")]
  257. [Owner("Kenneth_aa")]
  258. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  259. public void Test_ConnectionInfo_Port_Large()
  260. {
  261. new ConnectionInfo(Resources.HOST, int.MaxValue, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  262. }
  263. [TestMethod]
  264. [TestCategory("ConnectionInfo")]
  265. [Description("Pass too small port.")]
  266. [Owner("Kenneth_aa")]
  267. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  268. public void Test_ConnectionInfo_Port_Small()
  269. {
  270. new ConnectionInfo(Resources.HOST, int.MinValue, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  271. }
  272. [TestMethod]
  273. [TestCategory("ConnectionInfo")]
  274. [Description("Pass a valid port.")]
  275. [Owner("Kenneth_aa")]
  276. public void Test_ConnectionInfo_Port_Valid()
  277. {
  278. new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  279. }
  280. [TestMethod]
  281. [TestCategory("ConnectionInfo")]
  282. [Description("Pass null as session.")]
  283. [Owner("Kenneth_aa")]
  284. [ExpectedException(typeof(ArgumentNullException))]
  285. public void Test_ConnectionInfo_Authenticate_Null()
  286. {
  287. var ret = new ConnectionInfo(Resources.HOST, 22, Resources.USERNAME, ProxyTypes.None, Resources.HOST, 22, Resources.USERNAME, Resources.PASSWORD, null);
  288. ret.Authenticate(null);
  289. }
  290. }
  291. }