2
0

HttpConnectorTest_Connect_ProxyPasswordIsNull.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Tests.Common;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading;
  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. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(20);
  36. _expectedHttpRequest = string.Format("CONNECT {0}:{1} HTTP/1.0{2}" +
  37. "Proxy-Authorization: Basic cHJveHlVc2VyOg=={2}{2}",
  38. _connectionInfo.Host,
  39. _connectionInfo.Port.ToString(CultureInfo.InvariantCulture),
  40. "\r\n");
  41. _bytesReceivedByProxy = new List<byte>();
  42. _disconnected = false;
  43. _clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  44. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  45. _proxyServer.Disconnected += (socket) => _disconnected = true;
  46. _proxyServer.Connected += socket =>
  47. {
  48. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  49. socket.Send(Encoding.ASCII.GetBytes("SSH.NET\r\n"));
  50. socket.Send(Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\n"));
  51. socket.Send(Encoding.ASCII.GetBytes("Content-Type: application/octet-stream\r\n"));
  52. socket.Send(Encoding.ASCII.GetBytes("\r\n"));
  53. socket.Send(Encoding.ASCII.GetBytes("SSH4EVER"));
  54. socket.Shutdown(SocketShutdown.Send);
  55. };
  56. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  57. {
  58. _bytesReceivedByProxy.AddRange(bytesReceived);
  59. };
  60. _proxyServer.Start();
  61. }
  62. protected override void SetupMocks()
  63. {
  64. SocketFactoryMock.Setup(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  65. .Returns(_clientSocket);
  66. }
  67. protected override void TearDown()
  68. {
  69. base.TearDown();
  70. if (_proxyServer != null)
  71. {
  72. _proxyServer.Dispose();
  73. }
  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(400);
  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(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
  116. Times.Once());
  117. }
  118. }
  119. }