Socks5ConnectorTest_Connect_ProxySocksVersionIsNotSupported.cs 3.6 KB

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