-
-
Notifications
You must be signed in to change notification settings - Fork 940
/
Copy pathSocks4ConnectorTest_Connect_ConnectionRejectedByProxy.cs
134 lines (118 loc) · 4.03 KB
/
Socks4ConnectorTest_Connect_ConnectionRejectedByProxy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace Renci.SshNet.Tests.Classes.Connection
{
[TestClass]
public class Socks4ConnectorTest_Connect_ConnectionRejectedByProxy : Socks4ConnectorTestBase
{
private ConnectionInfo _connectionInfo;
private AsyncSocketListener _proxyServer;
private Socket _clientSocket;
private List<byte> _bytesReceivedByProxy;
private bool _disconnected;
private ProxyException _actualException;
protected override void SetupData()
{
base.SetupData();
_connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
_connectionInfo.Timeout = TimeSpan.FromMilliseconds(100);
_bytesReceivedByProxy = new List<byte>();
_clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_actualException = null;
_proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
_proxyServer.Disconnected += socket => _disconnected = true;
_proxyServer.BytesReceived += (bytesReceived, socket) =>
{
if (_bytesReceivedByProxy.Count == 0)
{
socket.Send(new byte[]
{
// Reply version (null byte)
0x00,
// Connection refused
0x5b
});
}
_bytesReceivedByProxy.AddRange(bytesReceived);
};
_proxyServer.Start();
}
protected override void SetupMocks()
{
SocketFactoryMock.Setup(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
.Returns(_clientSocket);
}
protected override void TearDown()
{
base.TearDown();
if (_proxyServer != null)
{
_proxyServer.Dispose();
}
if (_clientSocket != null)
{
_clientSocket.Dispose();
}
}
protected override void Act()
{
try
{
Connector.Connect(_connectionInfo);
Assert.Fail();
}
catch (ProxyException ex)
{
_actualException = ex;
}
}
[TestMethod]
public void ConnectShouldHaveThrownProxyException()
{
Assert.IsNotNull(_actualException);
Assert.IsNull(_actualException.InnerException);
Assert.AreEqual("SOCKS4: Connection rejected.", _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(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
Times.Once());
}
private static byte GetNotSupportedSocksVersion()
{
var random = new Random();
while (true)
{
var socksVersion = random.Next(1, 255);
if (socksVersion != 4)
{
return (byte) socksVersion;
}
}
}
}
}