HttpConnectorTest_Connect_ProxyResponseStatusIs200_OnlyHeaders.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Moq;
  10. using Renci.SshNet.Tests.Common;
  11. namespace Renci.SshNet.Tests.Classes.Connection
  12. {
  13. [TestClass]
  14. public class HttpConnectorTest_Connect_ProxyResponseStatusIs200_OnlyHeaders : HttpConnectorTestBase
  15. {
  16. private ConnectionInfo _connectionInfo;
  17. private AsyncSocketListener _proxyServer;
  18. private bool _disconnected;
  19. private Socket _clientSocket;
  20. private List<byte> _bytesReceivedByProxy;
  21. private string _expectedHttpRequest;
  22. private Socket _actual;
  23. protected override void SetupData()
  24. {
  25. base.SetupData();
  26. _connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
  27. 777,
  28. "user",
  29. ProxyTypes.Http,
  30. IPAddress.Loopback.ToString(),
  31. 8122,
  32. "proxyUser",
  33. "proxyPwd",
  34. new KeyboardInteractiveAuthenticationMethod("user"))
  35. {
  36. Timeout = TimeSpan.FromMilliseconds(20)
  37. };
  38. _expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}" +
  39. "Proxy-Authorization: Basic cHJveHlVc2VyOnByb3h5UHdk{2}{2}",
  40. _connectionInfo.Host,
  41. _connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
  42. "\r\n");
  43. _bytesReceivedByProxy = new List<byte>();
  44. _disconnected = false;
  45. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  46. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  47. _proxyServer.Disconnected += (socket) => _disconnected = true;
  48. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  49. {
  50. _bytesReceivedByProxy.AddRange(bytesReceived);
  51. // Only send response back after we've received the complete CONNECT request
  52. // as we want to make sure HttpConnector is not waiting for any data before
  53. // it sends the CONNECT request
  54. if (_bytesReceivedByProxy.Count == _expectedHttpRequest.Length)
  55. {
  56. _ = socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  57. _ = socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  58. _ = socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  59. _ = socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
  60. socket.Shutdown(SocketShutdown.Send);
  61. }
  62. };
  63. _proxyServer.Start();
  64. }
  65. protected override void SetupMocks()
  66. {
  67. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  68. .Returns(_clientSocket);
  69. }
  70. protected override void TearDown()
  71. {
  72. base.TearDown();
  73. _proxyServer?.Dispose();
  74. if (_clientSocket != null)
  75. {
  76. _clientSocket.Shutdown(SocketShutdown.Both);
  77. _clientSocket.Close();
  78. }
  79. }
  80. protected override void Act()
  81. {
  82. _actual = Connector.Connect(_connectionInfo);
  83. // Give some time to process all messages
  84. Thread.Sleep(200);
  85. }
  86. [TestMethod]
  87. public void ProxyShouldHaveReceivedExpectedHttpRequest()
  88. {
  89. Assert.AreEqual(_expectedHttpRequest, Encoding.ASCII.GetString(_bytesReceivedByProxy.ToArray()));
  90. }
  91. [TestMethod]
  92. public void ConnectShouldReturnSocketCreatedUsingSocketFactory()
  93. {
  94. Assert.IsNotNull(_actual);
  95. Assert.AreSame(_clientSocket, _actual);
  96. }
  97. [TestMethod]
  98. public void OnlyHttpResponseShouldHaveBeenConsumed()
  99. {
  100. var buffer = new byte[8];
  101. Assert.AreEqual(8, _actual.Available);
  102. Assert.AreEqual(8, _actual.Receive(buffer));
  103. Assert.AreEqual("SSH4EVER", Encoding.ASCII.GetString(buffer));
  104. Assert.AreEqual(0, _actual.Receive(buffer));
  105. }
  106. [TestMethod]
  107. public void ClientSocketShouldBeConnected()
  108. {
  109. Assert.IsFalse(_disconnected);
  110. Assert.IsTrue(_actual.Connected);
  111. }
  112. [TestMethod]
  113. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  114. {
  115. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  116. Times.Once());
  117. }
  118. }
  119. }