2
0

SessionTest.cs 14 KB

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