2
0

SessionTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Globalization;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Renci.SshNet.Common;
  8. using Renci.SshNet.Tests.Common;
  9. using Renci.SshNet.Tests.Properties;
  10. namespace Renci.SshNet.Tests.Classes
  11. {
  12. /// <summary>
  13. /// Provides functionality to connect and interact with SSH server.
  14. /// </summary>
  15. [TestClass]
  16. public partial class SessionTest : TestBase
  17. {
  18. [TestMethod]
  19. public void ConnectShouldSkipLinesBeforeProtocolIdentificationString()
  20. {
  21. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  22. var connectionInfo = CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5));
  23. using (var serverStub = new AsyncSocketListener(serverEndPoint))
  24. {
  25. serverStub.Connected += (socket) =>
  26. {
  27. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  28. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
  29. socket.Send(Encoding.ASCII.GetBytes("SSH-666-SshStub\r\n"));
  30. socket.Shutdown(SocketShutdown.Send);
  31. };
  32. serverStub.Start();
  33. using (var session = new Session(connectionInfo))
  34. {
  35. try
  36. {
  37. session.Connect();
  38. Assert.Fail();
  39. }
  40. catch (SshConnectionException ex)
  41. {
  42. Assert.IsNull(ex.InnerException);
  43. Assert.AreEqual("Server version '666' is not supported.", ex.Message);
  44. Assert.AreEqual("SSH-666-SshStub", connectionInfo.ServerVersion);
  45. }
  46. }
  47. }
  48. }
  49. [TestMethod]
  50. public void ConnectShouldSupportProtocolIdentificationStringThatDoesNotEndWithCrlf()
  51. {
  52. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  53. var connectionInfo = CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5));
  54. using (var serverStub = new AsyncSocketListener(serverEndPoint))
  55. {
  56. serverStub.Connected += (socket) =>
  57. {
  58. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  59. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
  60. socket.Send(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
  61. socket.Shutdown(SocketShutdown.Send);
  62. };
  63. serverStub.Start();
  64. using (var session = new Session(connectionInfo))
  65. {
  66. try
  67. {
  68. session.Connect();
  69. Assert.Fail();
  70. }
  71. catch (SshConnectionException ex)
  72. {
  73. Assert.IsNull(ex.InnerException);
  74. Assert.AreEqual("Server version '666' is not supported.", ex.Message);
  75. Assert.AreEqual("SSH-666-SshStub", connectionInfo.ServerVersion);
  76. }
  77. }
  78. }
  79. }
  80. [TestMethod]
  81. public void ConnectShouldThrowSshOperationExceptionWhenServerDoesNotRespondWithinConnectionTimeout()
  82. {
  83. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  84. var timeout = TimeSpan.FromMilliseconds(500);
  85. Socket clientSocket = null;
  86. using (var serverStub = new AsyncSocketListener(serverEndPoint))
  87. {
  88. serverStub.Connected += (socket) =>
  89. {
  90. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  91. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
  92. clientSocket = socket;
  93. };
  94. serverStub.Start();
  95. using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromMilliseconds(500))))
  96. {
  97. try
  98. {
  99. session.Connect();
  100. Assert.Fail();
  101. }
  102. catch (SshOperationTimeoutException ex)
  103. {
  104. Assert.IsNull(ex.InnerException);
  105. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Socket read operation has timed out after {0:F0} milliseconds.", timeout.TotalMilliseconds), ex.Message);
  106. Assert.IsNotNull(clientSocket);
  107. Assert.IsTrue(clientSocket.Connected);
  108. // shut down socket
  109. clientSocket.Shutdown(SocketShutdown.Send);
  110. }
  111. }
  112. }
  113. }
  114. [TestMethod]
  115. public void ConnectShouldSshConnectionExceptionWhenServerResponseDoesNotContainProtocolIdentificationString()
  116. {
  117. var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  118. // response ends with CRLF
  119. using (var serverStub = new AsyncSocketListener(serverEndPoint))
  120. {
  121. serverStub.Connected += (socket) =>
  122. {
  123. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  124. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
  125. socket.Shutdown(SocketShutdown.Send);
  126. };
  127. serverStub.Start();
  128. using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5))))
  129. {
  130. try
  131. {
  132. session.Connect();
  133. Assert.Fail();
  134. }
  135. catch (SshConnectionException ex)
  136. {
  137. Assert.IsNull(ex.InnerException);
  138. Assert.AreEqual("Server response does not contain SSH protocol identification.", ex.Message);
  139. }
  140. }
  141. }
  142. // response does not end with CRLF
  143. using (var serverStub = new AsyncSocketListener(serverEndPoint))
  144. {
  145. serverStub.Connected += (socket) =>
  146. {
  147. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  148. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner"));
  149. socket.Shutdown(SocketShutdown.Send);
  150. };
  151. serverStub.Start();
  152. using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5))))
  153. {
  154. try
  155. {
  156. session.Connect();
  157. Assert.Fail();
  158. }
  159. catch (SshConnectionException ex)
  160. {
  161. Assert.IsNull(ex.InnerException);
  162. Assert.AreEqual("Server response does not contain SSH protocol identification.", ex.Message);
  163. }
  164. }
  165. }
  166. // last line is empty
  167. using (var serverStub = new AsyncSocketListener(serverEndPoint))
  168. {
  169. serverStub.Connected += (socket) =>
  170. {
  171. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  172. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
  173. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  174. socket.Shutdown(SocketShutdown.Send);
  175. };
  176. serverStub.Start();
  177. using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5))))
  178. {
  179. try
  180. {
  181. session.Connect();
  182. Assert.Fail();
  183. }
  184. catch (SshConnectionException ex)
  185. {
  186. Assert.IsNull(ex.InnerException);
  187. Assert.AreEqual("Server response does not contain SSH protocol identification.", ex.Message);
  188. }
  189. }
  190. }
  191. }
  192. [TestMethod]
  193. public void Connect_HostNameInvalid_ShouldThrowSocketExceptionWithErrorCodeHostNotFound()
  194. {
  195. var connectionInfo = new ConnectionInfo("invalid.", 40, "user",
  196. new KeyboardInteractiveAuthenticationMethod("user"));
  197. var session = new Session(connectionInfo);
  198. try
  199. {
  200. session.Connect();
  201. Assert.Fail();
  202. }
  203. catch (SocketException ex)
  204. {
  205. Assert.AreEqual(ex.ErrorCode, (int)SocketError.HostNotFound);
  206. }
  207. }
  208. [TestMethod]
  209. public void Connect_ProxyHostNameInvalid_ShouldThrowSocketExceptionWithErrorCodeHostNotFound()
  210. {
  211. var connectionInfo = new ConnectionInfo("localhost", 40, "user", ProxyTypes.Http, "invalid.", 80,
  212. "proxyUser", "proxyPwd", new KeyboardInteractiveAuthenticationMethod("user"));
  213. var session = new Session(connectionInfo);
  214. try
  215. {
  216. session.Connect();
  217. Assert.Fail();
  218. }
  219. catch (SocketException ex)
  220. {
  221. Assert.AreEqual(ex.ErrorCode, (int)SocketError.HostNotFound);
  222. }
  223. }
  224. [TestMethod]
  225. public void DisconnectShouldNotThrowExceptionWhenSocketIsNotConnected()
  226. {
  227. var connectionInfo = new ConnectionInfo("localhost", 6767, Resources.USERNAME,
  228. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  229. var session = new Session(connectionInfo);
  230. try
  231. {
  232. session.Connect();
  233. Assert.Fail();
  234. }
  235. catch (SocketException)
  236. {
  237. session.Disconnect();
  238. }
  239. }
  240. [TestMethod]
  241. public void DisconnectShouldNotThrowExceptionWhenConnectHasNotBeenInvoked()
  242. {
  243. var connectionInfo = new ConnectionInfo("localhost", 6767, Resources.USERNAME,
  244. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  245. var session = new Session(connectionInfo);
  246. session.Disconnect();
  247. }
  248. [TestMethod]
  249. public void DisposeShouldNotThrowExceptionWhenSocketIsNotConnected()
  250. {
  251. var connectionInfo = new ConnectionInfo("localhost", 6767, Resources.USERNAME,
  252. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  253. var session = new Session(connectionInfo);
  254. try
  255. {
  256. session.Connect();
  257. Assert.Fail();
  258. }
  259. catch (SocketException)
  260. {
  261. session.Dispose();
  262. }
  263. }
  264. [TestMethod]
  265. public void DisposeShouldNotThrowExceptionWhenConenectHasNotBeenInvoked()
  266. {
  267. var connectionInfo = new ConnectionInfo("localhost", 6767, Resources.USERNAME,
  268. new KeyboardInteractiveAuthenticationMethod(Resources.USERNAME));
  269. var session = new Session(connectionInfo);
  270. session.Disconnect();
  271. }
  272. private static ConnectionInfo CreateConnectionInfo(IPEndPoint serverEndPoint, TimeSpan timeout)
  273. {
  274. var connectionInfo = new ConnectionInfo(
  275. serverEndPoint.Address.ToString(),
  276. serverEndPoint.Port,
  277. "eric",
  278. new NoneAuthenticationMethod("eric"));
  279. connectionInfo.Timeout = timeout;
  280. return connectionInfo;
  281. }
  282. }
  283. }