2
0

SessionTest_Connected_ServerShutsDownSocket.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_ServerShutsDownSocket : SessionTest_ConnectedBase
  12. {
  13. protected override void Act()
  14. {
  15. ServerSocket.Shutdown(SocketShutdown.Send);
  16. // give session some time to process socket shutdown
  17. Thread.Sleep(200);
  18. }
  19. [TestMethod]
  20. public void IsConnectedShouldReturnFalse()
  21. {
  22. Assert.IsFalse(Session.IsConnected);
  23. }
  24. [TestMethod]
  25. public void DisconnectShouldFinishImmediately()
  26. {
  27. var stopwatch = new Stopwatch();
  28. stopwatch.Start();
  29. Session.Disconnect();
  30. stopwatch.Stop();
  31. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 500);
  32. }
  33. [TestMethod]
  34. public void DisconnectedIsNeverRaised()
  35. {
  36. Assert.AreEqual(0, DisconnectedRegister.Count);
  37. }
  38. [TestMethod]
  39. public void DisconnectReceivedIsNeverRaised()
  40. {
  41. Assert.AreEqual(0, DisconnectReceivedRegister.Count);
  42. }
  43. [TestMethod]
  44. public void ErrorOccurredIsRaisedOnce()
  45. {
  46. Assert.AreEqual(1, ErrorOccurredRegister.Count, ErrorOccurredRegister.AsString());
  47. var errorOccurred = ErrorOccurredRegister[0];
  48. Assert.IsNotNull(errorOccurred);
  49. var exception = errorOccurred.Exception;
  50. Assert.IsNotNull(exception);
  51. Assert.AreEqual(typeof(SshConnectionException), exception.GetType());
  52. var connectionException = (SshConnectionException) exception;
  53. Assert.AreEqual(DisconnectReason.ConnectionLost, connectionException.DisconnectReason);
  54. Assert.IsNull(connectionException.InnerException);
  55. Assert.AreEqual("An established connection was aborted by the server.", connectionException.Message);
  56. }
  57. [TestMethod]
  58. public void DisposeShouldFinishImmediately()
  59. {
  60. var stopwatch = new Stopwatch();
  61. stopwatch.Start();
  62. Session.Dispose();
  63. stopwatch.Stop();
  64. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 500);
  65. }
  66. [TestMethod]
  67. public void ReceiveOnServerSocketShouldReturnZero()
  68. {
  69. var buffer = new byte[1];
  70. var actual = ServerSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
  71. Assert.AreEqual(0, actual);
  72. }
  73. [TestMethod]
  74. public void SendMessageShouldThrowSshConnectionException()
  75. {
  76. try
  77. {
  78. Session.SendMessage(new IgnoreMessage());
  79. Assert.Fail();
  80. }
  81. catch (SshConnectionException ex)
  82. {
  83. Assert.IsNull(ex.InnerException);
  84. Assert.AreEqual("Client not connected.", ex.Message);
  85. }
  86. }
  87. [TestMethod]
  88. public void ISession_MessageListenerCompletedShouldBeSignaled()
  89. {
  90. var session = (ISession)Session;
  91. Assert.IsNotNull(session.MessageListenerCompleted);
  92. Assert.IsTrue(session.MessageListenerCompleted.WaitOne());
  93. }
  94. [TestMethodAttribute]
  95. public void ISession_SendMessageShouldThrowSshConnectionException()
  96. {
  97. var session = (ISession) Session;
  98. try
  99. {
  100. session.SendMessage(new IgnoreMessage());
  101. Assert.Fail();
  102. }
  103. catch (SshConnectionException ex)
  104. {
  105. Assert.IsNull(ex.InnerException);
  106. Assert.AreEqual("Client not connected.", ex.Message);
  107. }
  108. }
  109. [TestMethod]
  110. public void ISession_TrySendMessageShouldReturnFalse()
  111. {
  112. var session = (ISession)Session;
  113. var actual = session.TrySendMessage(new IgnoreMessage());
  114. Assert.IsFalse(actual);
  115. }
  116. [TestMethod]
  117. public void ISession_WaitOnHandleShouldThrowSshConnectionExceptionDetailingAbortedConnection()
  118. {
  119. var session = (ISession)Session;
  120. var waitHandle = new ManualResetEvent(false);
  121. try
  122. {
  123. session.WaitOnHandle(waitHandle);
  124. Assert.Fail();
  125. }
  126. catch (SshConnectionException ex)
  127. {
  128. Assert.IsNull(ex.InnerException);
  129. Assert.AreEqual("An established connection was aborted by the server.", ex.Message);
  130. }
  131. }
  132. }
  133. }