-
-
Notifications
You must be signed in to change notification settings - Fork 978
Expand file tree
/
Copy pathForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound.cs
More file actions
146 lines (127 loc) · 4.63 KB
/
ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound.cs
File metadata and controls
146 lines (127 loc) · 4.63 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound
{
private Mock<ISession> _sessionMock;
private Mock<IConnectionInfo> _connectionInfoMock;
private Mock<IChannelDirectTcpip> _channelMock;
private ForwardedPortDynamic _forwardedPort;
private IList<EventArgs> _closingRegister;
private IList<ExceptionEventArgs> _exceptionRegister;
private IPEndPoint _endpoint;
private Socket _client;
[TestInitialize]
public void Setup()
{
Arrange();
Act();
}
[TestCleanup]
public void Cleanup()
{
_client?.Dispose();
_client = null;
_forwardedPort?.Dispose();
_forwardedPort = null;
}
protected void Arrange()
{
_closingRegister = new List<EventArgs>();
_exceptionRegister = new List<ExceptionEventArgs>();
_endpoint = new IPEndPoint(IPAddress.Loopback, 8122);
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
_channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
_ = _connectionInfoMock.Setup(p => p.Timeout)
.Returns(TimeSpan.FromSeconds(15));
_ = _sessionMock.Setup(p => p.IsConnected)
.Returns(true);
_ = _sessionMock.Setup(p => p.ConnectionInfo)
.Returns(_connectionInfoMock.Object);
_ = _sessionMock.Setup(p => p.CreateChannelDirectTcpip())
.Returns(_channelMock.Object);
_ = _channelMock.Setup(p => p.Dispose());
_forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port);
_forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
_forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
_forwardedPort.Session = _sessionMock.Object;
_forwardedPort.Start();
_client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
{
ReceiveTimeout = 500,
SendTimeout = 500,
SendBufferSize = 0
};
_client.Connect(_endpoint);
// allow for client socket to establish connection
Thread.Sleep(50);
}
protected void Act()
{
_forwardedPort.Stop();
}
[TestMethod]
public void IsStartedShouldReturnFalse()
{
Assert.IsFalse(_forwardedPort.IsStarted);
}
[TestMethod]
public void ForwardedPortShouldRefuseNewConnections()
{
using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
try
{
client.Connect(_endpoint);
Assert.Fail();
}
catch (SocketException ex)
{
Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
}
}
}
// TODO We should investigate why this method doesn't work on Linux
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ExistingConnectionShouldBeClosed()
{
try
{
_ = _client.Send(new byte[] { 0x0a }, 0, 1, SocketFlags.None);
Assert.Fail();
}
catch (SocketException ex)
{
Assert.AreEqual(SocketError.ConnectionReset, ex.SocketErrorCode);
}
}
[TestMethod]
public void ClosingShouldHaveFiredOnce()
{
Assert.HasCount(1, _closingRegister);
}
[TestMethod]
public void ExceptionShouldNotHaveFired()
{
Assert.IsEmpty(_exceptionRegister);
}
[TestMethod]
public void DisposeOnChannelShouldBeInvokedOnce()
{
_channelMock.Verify(p => p.Dispose(), Times.Once);
}
}
}