SessionTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.Messages;
  9. using Renci.SshNet.Tests.Common;
  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. /// <summary>
  193. ///A test for SessionSemaphore
  194. ///</summary>
  195. [TestMethod()]
  196. [Ignore]
  197. public void SessionSemaphoreTest()
  198. {
  199. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  200. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  201. SemaphoreLight actual;
  202. actual = target.SessionSemaphore;
  203. Assert.Inconclusive("Verify the correctness of this test method.");
  204. }
  205. /// <summary>
  206. ///A test for IsConnected
  207. ///</summary>
  208. [TestMethod()]
  209. [Ignore]
  210. public void IsConnectedTest()
  211. {
  212. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  213. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  214. bool actual;
  215. actual = target.IsConnected;
  216. Assert.Inconclusive("Verify the correctness of this test method.");
  217. }
  218. /// <summary>
  219. ///A test for ClientInitMessage
  220. ///</summary>
  221. [TestMethod()]
  222. [Ignore]
  223. public void ClientInitMessageTest()
  224. {
  225. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  226. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  227. Message actual;
  228. actual = target.ClientInitMessage;
  229. Assert.Inconclusive("Verify the correctness of this test method.");
  230. }
  231. /// <summary>
  232. ///A test for UnRegisterMessage
  233. ///</summary>
  234. [TestMethod()]
  235. [Ignore]
  236. public void UnRegisterMessageTest()
  237. {
  238. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  239. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  240. string messageName = string.Empty; // TODO: Initialize to an appropriate value
  241. target.UnRegisterMessage(messageName);
  242. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  243. }
  244. /// <summary>
  245. ///A test for RegisterMessage
  246. ///</summary>
  247. [TestMethod()]
  248. [Ignore]
  249. public void RegisterMessageTest()
  250. {
  251. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  252. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  253. string messageName = string.Empty; // TODO: Initialize to an appropriate value
  254. target.RegisterMessage(messageName);
  255. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  256. }
  257. /// <summary>
  258. ///A test for Dispose
  259. ///</summary>
  260. [TestMethod()]
  261. [Ignore]
  262. public void DisposeTest()
  263. {
  264. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  265. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  266. target.Dispose();
  267. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  268. }
  269. /// <summary>
  270. ///A test for Disconnect
  271. ///</summary>
  272. [TestMethod()]
  273. [Ignore]
  274. public void DisconnectTest()
  275. {
  276. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  277. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  278. target.Disconnect();
  279. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  280. }
  281. /// <summary>
  282. ///A test for Connect
  283. ///</summary>
  284. [TestMethod()]
  285. [Ignore]
  286. public void ConnectTest()
  287. {
  288. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  289. Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value
  290. target.Connect();
  291. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  292. }
  293. private static ConnectionInfo CreateConnectionInfo(IPEndPoint serverEndPoint, TimeSpan timeout)
  294. {
  295. var connectionInfo = new ConnectionInfo(
  296. serverEndPoint.Address.ToString(),
  297. serverEndPoint.Port,
  298. "eric",
  299. new NoneAuthenticationMethod("eric"));
  300. connectionInfo.Timeout = timeout;
  301. return connectionInfo;
  302. }
  303. }
  304. }