HttpConnectorTest_Connect_ProxyUserNameIsNull.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_ProxyUserNameIsNull : 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. null,
  33. "proxyPwd",
  34. new KeyboardInteractiveAuthenticationMethod("user"))
  35. {
  36. Timeout = TimeSpan.FromMilliseconds(20)
  37. };
  38. _expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}{2}",
  39. _connectionInfo.Host,
  40. _connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
  41. "\r\n");
  42. _bytesReceivedByProxy = new List<byte>();
  43. _disconnected = false;
  44. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  45. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  46. _proxyServer.Disconnected += (socket) => _disconnected = true;
  47. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  48. {
  49. _bytesReceivedByProxy.AddRange(bytesReceived);
  50. // Only send response back after we've received the complete CONNECT request
  51. // as we want to make sure HttpConnector is not waiting for any data before
  52. // it sends the CONNECT request
  53. if (_bytesReceivedByProxy.Count == _expectedHttpRequest.Length)
  54. {
  55. _ = socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  56. _ = socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
  57. _ = socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  58. _ = socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  59. _ = socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  60. _ = socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
  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. if (_clientSocket != null)
  74. {
  75. _clientSocket.Shutdown(SocketShutdown.Send);
  76. _clientSocket.Close();
  77. }
  78. _proxyServer?.Dispose();
  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. }
  105. [TestMethod]
  106. public void ClientSocketShouldBeConnected()
  107. {
  108. Assert.IsFalse(_disconnected);
  109. Assert.IsTrue(_actual.Connected);
  110. }
  111. [TestMethod]
  112. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  113. {
  114. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  115. Times.Once());
  116. }
  117. }
  118. }