Socks4ConnectorTest_Connect_ConnectionRejectedByProxy.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Common;
  9. using Renci.SshNet.Tests.Common;
  10. namespace Renci.SshNet.Tests.Classes.Connection
  11. {
  12. [TestClass]
  13. public class Socks4ConnectorTest_Connect_ConnectionRejectedByProxy : Socks4ConnectorTestBase
  14. {
  15. private ConnectionInfo _connectionInfo;
  16. private AsyncSocketListener _proxyServer;
  17. private Socket _clientSocket;
  18. private List<byte> _bytesReceivedByProxy;
  19. private bool _disconnected;
  20. private ProxyException _actualException;
  21. protected override void SetupData()
  22. {
  23. base.SetupData();
  24. _connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
  25. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
  26. _bytesReceivedByProxy = new List<byte>();
  27. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  28. _actualException = null;
  29. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  30. _proxyServer.Disconnected += socket => _disconnected = true;
  31. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  32. {
  33. if (_bytesReceivedByProxy.Count == 0)
  34. {
  35. _ = socket.Send(new byte[]
  36. {
  37. // Reply version (null byte)
  38. 0x00,
  39. // Connection refused
  40. 0x5b
  41. });
  42. }
  43. _bytesReceivedByProxy.AddRange(bytesReceived);
  44. };
  45. _proxyServer.Start();
  46. }
  47. protected override void SetupMocks()
  48. {
  49. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  50. .Returns(_clientSocket);
  51. }
  52. protected override void TearDown()
  53. {
  54. base.TearDown();
  55. _proxyServer?.Dispose();
  56. _clientSocket?.Dispose();
  57. }
  58. protected override void Act()
  59. {
  60. try
  61. {
  62. _ = Connector.Connect(_connectionInfo);
  63. Assert.Fail();
  64. }
  65. catch (ProxyException ex)
  66. {
  67. _actualException = ex;
  68. }
  69. // Give some time to process all messages
  70. Thread.Sleep(200);
  71. }
  72. [TestMethod]
  73. public void ConnectShouldHaveThrownProxyException()
  74. {
  75. Assert.IsNotNull(_actualException);
  76. Assert.IsNull(_actualException.InnerException);
  77. Assert.AreEqual("SOCKS4: Connection rejected.", _actualException.Message);
  78. }
  79. [TestMethod]
  80. public void ConnectionToProxyShouldHaveBeenShutDown()
  81. {
  82. Assert.IsTrue(_disconnected);
  83. }
  84. [TestMethod]
  85. public void ClientSocketShouldHaveBeenDisposed()
  86. {
  87. try
  88. {
  89. _ = _clientSocket.Receive(new byte[0]);
  90. Assert.Fail();
  91. }
  92. catch (ObjectDisposedException)
  93. {
  94. }
  95. }
  96. [TestMethod]
  97. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  98. {
  99. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  100. Times.Once());
  101. }
  102. }
  103. }