Files
ssh.net/test/Renci.SshNet.Tests/Classes/Connection/Socks5ConnectorTest_Connect_ProxySocksVersionIsNotSupported.cs
Rob Hague 919af754df Remove unnecessary DNS lookup in Connect (#1412)
The library currently performs a DNS lookup of the desired host, takes the first
returned IP address and connects to that. Instead, we can just pass the hostname
down to System.Net.Sockets which will do the right thing, potentially trying
multiple addresses if needed.

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-06-18 07:41:00 +02:00

124 lines
3.6 KiB
C#

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Connection
{
[TestClass]
public class Socks5ConnectorTest_Connect_ProxySocksVersionIsNotSupported : Socks5ConnectorTestBase
{
private ConnectionInfo _connectionInfo;
private AsyncSocketListener _proxyServer;
private Socket _clientSocket;
private byte _proxySocksVersion;
private bool _disconnected;
private ProxyException _actualException;
protected override void SetupData()
{
base.SetupData();
_connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
_proxySocksVersion = GetNotSupportedSocksVersion();
_actualException = null;
_clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
_proxyServer.Disconnected += socket => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
_ = socket.Send(new byte[] { _proxySocksVersion });
};
_proxyServer.Start();
}
protected override void SetupMocks()
{
_ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
.Returns(_clientSocket);
}
protected override void TearDown()
{
base.TearDown();
_proxyServer?.Dispose();
_clientSocket?.Dispose();
}
protected override void Act()
{
try
{
_ = Connector.Connect(_connectionInfo);
Assert.Fail();
}
catch (ProxyException ex)
{
_actualException = ex;
}
// Give some time to process all messages
Thread.Sleep(200);
}
[TestMethod]
public void ConnectShouldHaveThrownProxyException()
{
Assert.IsNotNull(_actualException);
Assert.IsNull(_actualException.InnerException);
Assert.AreEqual(string.Format("SOCKS Version '{0}' is not supported.", _proxySocksVersion), _actualException.Message);
}
[TestMethod]
public void ConnectionToProxyShouldHaveBeenShutDown()
{
Assert.IsTrue(_disconnected);
}
[TestMethod]
public void ClientSocketShouldHaveBeenDisposed()
{
try
{
_ = _clientSocket.Receive(new byte[0]);
Assert.Fail();
}
catch (ObjectDisposedException)
{
}
}
[TestMethod]
public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
{
SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
Times.Once());
}
private static byte GetNotSupportedSocksVersion()
{
var random = new Random();
while (true)
{
var socksVersion = random.Next(1, 255);
if (socksVersion != 5)
{
return (byte)socksVersion;
}
}
}
}
}