SessionTest_SocketConnected_BadPacketAndDispose.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Common;
  8. using Renci.SshNet.Messages.Transport;
  9. using Renci.SshNet.Tests.Common;
  10. namespace Renci.SshNet.Tests.Classes
  11. {
  12. [TestClass]
  13. public class SessionTest_SocketConnected_BadPacketAndDispose
  14. {
  15. private Mock<IServiceFactory> _serviceFactoryMock;
  16. private ConnectionInfo _connectionInfo;
  17. private Session _session;
  18. private AsyncSocketListener _serverListener;
  19. private IPEndPoint _serverEndPoint;
  20. private Socket _serverSocket;
  21. private SshConnectionException _actualException;
  22. [TestInitialize]
  23. public void Setup()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. [TestCleanup]
  29. public void TearDown()
  30. {
  31. if (_serverListener != null)
  32. {
  33. _serverListener.Dispose();
  34. }
  35. }
  36. protected void Arrange()
  37. {
  38. _serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
  39. _connectionInfo = new ConnectionInfo(
  40. _serverEndPoint.Address.ToString(),
  41. _serverEndPoint.Port,
  42. "user",
  43. new PasswordAuthenticationMethod("user", "password"));
  44. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(200);
  45. _actualException = null;
  46. _serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
  47. _serverListener = new AsyncSocketListener(_serverEndPoint);
  48. _serverListener.Connected += (socket) =>
  49. {
  50. _serverSocket = socket;
  51. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  52. socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));
  53. socket.Send(Encoding.ASCII.GetBytes("SSH-2.0-SshStub\r\n"));
  54. };
  55. _serverListener.BytesReceived += (received, socket) =>
  56. {
  57. var badPacket = new byte[] {0x0a, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05};
  58. _serverSocket.Send(badPacket, 0, badPacket.Length, SocketFlags.None);
  59. _serverSocket.Shutdown(SocketShutdown.Send);
  60. };
  61. _serverListener.Start();
  62. }
  63. protected virtual void Act()
  64. {
  65. try
  66. {
  67. using (_session = new Session(_connectionInfo, _serviceFactoryMock.Object))
  68. {
  69. _session.Connect();
  70. }
  71. }
  72. catch (SshConnectionException ex)
  73. {
  74. _actualException = ex;
  75. }
  76. }
  77. [TestMethod]
  78. public void ConnectShouldThrowSshConnectionException()
  79. {
  80. Assert.IsNotNull(_actualException);
  81. Assert.IsNull(_actualException.InnerException);
  82. Assert.AreEqual(DisconnectReason.ProtocolError, _actualException.DisconnectReason);
  83. Assert.AreEqual("Bad packet length: 168101125.", _actualException.Message);
  84. }
  85. }
  86. }