HttpConnectorTest_Connect_ProxyPasswordIsNull.cs 4.9 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_ProxyPasswordIsNull : 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. null,
  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 cHJveHlVc2VyOg=={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.Connected += socket =>
  49. {
  50. _ = socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  51. _ = socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
  52. _ = socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  53. _ = socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  54. _ = socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  55. _ = socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
  56. socket.Shutdown(SocketShutdown.Send);
  57. };
  58. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  59. {
  60. _bytesReceivedByProxy.AddRange(bytesReceived);
  61. };
  62. _proxyServer.Start();
  63. }
  64. protected override void SetupMocks()
  65. {
  66. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  67. .Returns(_clientSocket);
  68. }
  69. protected override void TearDown()
  70. {
  71. base.TearDown();
  72. _proxyServer?.Dispose();
  73. if (_clientSocket != null)
  74. {
  75. _clientSocket.Shutdown(SocketShutdown.Both);
  76. _clientSocket.Close();
  77. }
  78. }
  79. protected override void Act()
  80. {
  81. _actual = Connector.Connect(_connectionInfo);
  82. // Give some time to process all messages
  83. Thread.Sleep(400);
  84. }
  85. [TestMethod]
  86. public void ProxyShouldHaveReceivedExpectedHttpRequest()
  87. {
  88. Assert.AreEqual(_expectedHttpRequest, Encoding.ASCII.GetString(_bytesReceivedByProxy.ToArray()));
  89. }
  90. [TestMethod]
  91. public void ConnectShouldReturnSocketCreatedUsingSocketFactory()
  92. {
  93. Assert.IsNotNull(_actual);
  94. Assert.AreSame(_clientSocket, _actual);
  95. }
  96. [TestMethod]
  97. public void OnlyHttpResponseShouldHaveBeenConsumed()
  98. {
  99. var buffer = new byte[8];
  100. Assert.AreEqual(8, _actual.Available);
  101. Assert.AreEqual(8, _actual.Receive(buffer));
  102. Assert.AreEqual("SSH4EVER", Encoding.ASCII.GetString(buffer));
  103. Assert.AreEqual(0, _actual.Receive(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. }