2
0

SessionTest_Connected_ServerShutsDownSendAfterSendingIncompletePacket.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Diagnostics;
  2. using System.Net.Sockets;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages.Transport;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. [TestClass]
  11. public class SessionTest_Connected_ServerShutsDownSendAfterSendingIncompletePacket : SessionTest_ConnectedBase
  12. {
  13. protected override void Act()
  14. {
  15. var incompletePacket = new byte[] { 0x0a, 0x05, 0x05 };
  16. _ = ServerSocket.Send(incompletePacket, 0, incompletePacket.Length, SocketFlags.None);
  17. // give session some time to start reading packet
  18. Thread.Sleep(100);
  19. ServerSocket.Shutdown(SocketShutdown.Send);
  20. // give session some time to process shut down of server socket
  21. Thread.Sleep(100);
  22. }
  23. [TestMethod]
  24. public void IsConnectedShouldReturnFalse()
  25. {
  26. Assert.IsFalse(Session.IsConnected);
  27. }
  28. [TestMethod]
  29. public void DisconnectShouldFinishImmediately()
  30. {
  31. var stopwatch = new Stopwatch();
  32. stopwatch.Start();
  33. Session.Disconnect();
  34. stopwatch.Stop();
  35. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 500);
  36. }
  37. [TestMethod]
  38. public void DisconnectedIsNeverRaised()
  39. {
  40. Assert.AreEqual(0, DisconnectedRegister.Count);
  41. }
  42. [TestMethod]
  43. public void DisconnectReceivedIsNeverRaised()
  44. {
  45. Assert.AreEqual(0, DisconnectReceivedRegister.Count);
  46. }
  47. [TestMethod]
  48. public void ErrorOccurredIsRaisedOnce()
  49. {
  50. Assert.AreEqual(1, ErrorOccurredRegister.Count, ErrorOccurredRegister.AsString());
  51. var errorOccurred = ErrorOccurredRegister[0];
  52. Assert.IsNotNull(errorOccurred);
  53. var exception = errorOccurred.Exception;
  54. Assert.IsNotNull(exception);
  55. Assert.AreEqual(typeof(SshConnectionException), exception.GetType());
  56. var connectionException = (SshConnectionException)exception;
  57. Assert.AreEqual(DisconnectReason.ConnectionLost, connectionException.DisconnectReason);
  58. Assert.IsNull(connectionException.InnerException);
  59. Assert.AreEqual("An established connection was aborted by the server.", connectionException.Message);
  60. }
  61. [TestMethod]
  62. public void DisposeShouldFinishImmediately()
  63. {
  64. var stopwatch = new Stopwatch();
  65. stopwatch.Start();
  66. Session.Dispose();
  67. stopwatch.Stop();
  68. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 500);
  69. }
  70. [TestMethod]
  71. public void ReceiveOnServerSocketShouldReturnZero()
  72. {
  73. var buffer = new byte[1];
  74. var actual = ServerSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
  75. Assert.AreEqual(0, actual);
  76. }
  77. [TestMethod]
  78. public void SendMessageShouldSucceed()
  79. {
  80. try
  81. {
  82. Session.SendMessage(new IgnoreMessage());
  83. Assert.Fail();
  84. }
  85. catch (SshConnectionException ex)
  86. {
  87. Assert.IsNull(ex.InnerException);
  88. Assert.AreEqual("Client not connected.", ex.Message);
  89. }
  90. }
  91. [TestMethod]
  92. public void ISession_MessageListenerCompletedShouldBeSignaled()
  93. {
  94. var session = (ISession)Session;
  95. Assert.IsNotNull(session.MessageListenerCompleted);
  96. Assert.IsTrue(session.MessageListenerCompleted.WaitOne());
  97. }
  98. [TestMethod]
  99. public void ISession_SendMessageShouldSucceed()
  100. {
  101. var session = (ISession)Session;
  102. try
  103. {
  104. session.SendMessage(new IgnoreMessage());
  105. Assert.Fail();
  106. }
  107. catch (SshConnectionException ex)
  108. {
  109. Assert.IsNull(ex.InnerException);
  110. Assert.AreEqual("Client not connected.", ex.Message);
  111. }
  112. }
  113. [TestMethod]
  114. public void ISession_TrySendMessageShouldReturnTrue()
  115. {
  116. var session = (ISession)Session;
  117. Assert.IsFalse(session.TrySendMessage(new IgnoreMessage()));
  118. }
  119. [TestMethod]
  120. public void ISession_WaitOnHandle_WaitHandle_ShouldThrowSshConnectionExceptionDetailingBadPacket()
  121. {
  122. var session = (ISession)Session;
  123. var waitHandle = new ManualResetEvent(false);
  124. try
  125. {
  126. session.WaitOnHandle(waitHandle);
  127. Assert.Fail();
  128. }
  129. catch (SshConnectionException ex)
  130. {
  131. Assert.AreEqual(DisconnectReason.ConnectionLost, ex.DisconnectReason);
  132. Assert.IsNull(ex.InnerException);
  133. Assert.AreEqual("An established connection was aborted by the server.", ex.Message);
  134. }
  135. }
  136. [TestMethod]
  137. public void ISession_WaitOnHandle_WaitHandleAndTimeout_ShouldThrowSshConnectionExceptionDetailingBadPacket()
  138. {
  139. var session = (ISession)Session;
  140. var waitHandle = new ManualResetEvent(false);
  141. try
  142. {
  143. session.WaitOnHandle(waitHandle, Timeout.InfiniteTimeSpan);
  144. Assert.Fail();
  145. }
  146. catch (SshConnectionException ex)
  147. {
  148. Assert.AreEqual(DisconnectReason.ConnectionLost, ex.DisconnectReason);
  149. Assert.IsNull(ex.InnerException);
  150. Assert.AreEqual("An established connection was aborted by the server.", ex.Message);
  151. }
  152. }
  153. [TestMethod]
  154. public void ISession_TryWait_WaitHandleAndTimeout_ShouldReturnDisconnected()
  155. {
  156. var session = (ISession)Session;
  157. var waitHandle = new ManualResetEvent(false);
  158. var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan);
  159. Assert.AreEqual(WaitResult.Disconnected, result);
  160. }
  161. [TestMethod]
  162. public void ISession_TryWait_WaitHandleAndTimeoutAndException_ShouldReturnDisconnected()
  163. {
  164. var session = (ISession)Session;
  165. var waitHandle = new ManualResetEvent(false);
  166. var result = session.TryWait(waitHandle, Timeout.InfiniteTimeSpan, out var exception);
  167. Assert.AreEqual(WaitResult.Disconnected, result);
  168. Assert.IsNull(exception);
  169. }
  170. }
  171. }