|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Net; |
4 |
| -using System.Net.Sockets; |
5 |
| -using Microsoft.VisualStudio.TestTools.UnitTesting; |
6 |
| -using Moq; |
7 |
| -using Renci.SshNet.Channels; |
8 |
| -using Renci.SshNet.Common; |
9 |
| - |
10 |
| -namespace Renci.SshNet.Tests.Classes |
11 |
| -{ |
12 |
| - [TestClass] |
13 |
| - public class ForwardedPortDynamicTest_Dispose_PortNeverStarted |
14 |
| - { |
15 |
| - private Mock<ISession> _sessionMock; |
16 |
| - private Mock<IConnectionInfo> _connectionInfoMock; |
17 |
| - private ForwardedPortDynamic _forwardedPort; |
18 |
| - private IList<EventArgs> _closingRegister; |
19 |
| - private IList<ExceptionEventArgs> _exceptionRegister; |
20 |
| - private IPEndPoint _endpoint; |
21 |
| - |
22 |
| - [TestInitialize] |
23 |
| - public void Setup() |
24 |
| - { |
25 |
| - Arrange(); |
26 |
| - Act(); |
27 |
| - } |
28 |
| - |
29 |
| - [TestCleanup] |
30 |
| - public void Cleanup() |
31 |
| - { |
32 |
| - if (_forwardedPort != null) |
33 |
| - { |
34 |
| - _forwardedPort.Dispose(); |
35 |
| - _forwardedPort = null; |
36 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| - protected void Arrange() |
40 |
| - { |
41 |
| - _closingRegister = new List<EventArgs>(); |
42 |
| - _exceptionRegister = new List<ExceptionEventArgs>(); |
43 |
| - _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); |
44 |
| - |
45 |
| - _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict); |
46 |
| - _sessionMock = new Mock<ISession>(MockBehavior.Strict); |
47 |
| - |
48 |
| - _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15)); |
49 |
| - _sessionMock.Setup(p => p.IsConnected).Returns(true); |
50 |
| - _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object); |
51 |
| - |
52 |
| - _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port); |
53 |
| - _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args); |
54 |
| - _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args); |
55 |
| - _forwardedPort.Session = _sessionMock.Object; |
56 |
| - } |
57 |
| - |
58 |
| - protected void Act() |
59 |
| - { |
60 |
| - _forwardedPort.Dispose(); |
61 |
| - } |
62 |
| - |
63 |
| - [TestMethod] |
64 |
| - public void IsStartedShouldReturnFalse() |
65 |
| - { |
66 |
| - Assert.IsFalse(_forwardedPort.IsStarted); |
67 |
| - } |
68 |
| - |
69 |
| - [TestMethod] |
70 |
| - public void ForwardedPortShouldRejectNewConnections() |
71 |
| - { |
72 |
| - using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) |
73 |
| - { |
74 |
| - try |
75 |
| - { |
76 |
| - client.Connect(_endpoint); |
77 |
| - } |
78 |
| - catch (SocketException ex) |
79 |
| - { |
80 |
| - Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode); |
81 |
| - } |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - [TestMethod] |
86 |
| - public void ClosingShouldNotHaveFired() |
87 |
| - { |
88 |
| - Assert.AreEqual(0, _closingRegister.Count); |
89 |
| - } |
90 |
| - |
91 |
| - [TestMethod] |
92 |
| - public void ExceptionShouldNotHaveFired() |
93 |
| - { |
94 |
| - Assert.AreEqual(0, _exceptionRegister.Count); |
95 |
| - } |
96 |
| - } |
97 |
| -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Sockets; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | +using Moq; |
| 7 | +using Renci.SshNet.Channels; |
| 8 | +using Renci.SshNet.Common; |
| 9 | + |
| 10 | +namespace Renci.SshNet.Tests.Classes |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + public class ForwardedPortDynamicTest_Dispose_PortNeverStarted |
| 14 | + { |
| 15 | + private Mock<ISession> _sessionMock; |
| 16 | + private Mock<IConnectionInfo> _connectionInfoMock; |
| 17 | + private ForwardedPortDynamic _forwardedPort; |
| 18 | + private IList<EventArgs> _closingRegister; |
| 19 | + private IList<ExceptionEventArgs> _exceptionRegister; |
| 20 | + private IPEndPoint _endpoint; |
| 21 | + |
| 22 | + [TestInitialize] |
| 23 | + public void Setup() |
| 24 | + { |
| 25 | + Arrange(); |
| 26 | + Act(); |
| 27 | + } |
| 28 | + |
| 29 | + [TestCleanup] |
| 30 | + public void Cleanup() |
| 31 | + { |
| 32 | + if (_forwardedPort != null) |
| 33 | + { |
| 34 | + _forwardedPort.Dispose(); |
| 35 | + _forwardedPort = null; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + protected void Arrange() |
| 40 | + { |
| 41 | + _closingRegister = new List<EventArgs>(); |
| 42 | + _exceptionRegister = new List<ExceptionEventArgs>(); |
| 43 | + _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); |
| 44 | + |
| 45 | + _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict); |
| 46 | + _sessionMock = new Mock<ISession>(MockBehavior.Strict); |
| 47 | + |
| 48 | + _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15)); |
| 49 | + _sessionMock.Setup(p => p.IsConnected).Returns(true); |
| 50 | + _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object); |
| 51 | + |
| 52 | + _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint)_endpoint.Port); |
| 53 | + _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args); |
| 54 | + _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args); |
| 55 | + _forwardedPort.Session = _sessionMock.Object; |
| 56 | + } |
| 57 | + |
| 58 | + protected void Act() |
| 59 | + { |
| 60 | + _forwardedPort.Dispose(); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void IsStartedShouldReturnFalse() |
| 65 | + { |
| 66 | + Assert.IsFalse(_forwardedPort.IsStarted); |
| 67 | + } |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + public void ForwardedPortShouldRejectNewConnections() |
| 71 | + { |
| 72 | + using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) |
| 73 | + { |
| 74 | + try |
| 75 | + { |
| 76 | + client.Connect(_endpoint); |
| 77 | + } |
| 78 | + catch (SocketException ex) |
| 79 | + { |
| 80 | + Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + [TestMethod] |
| 86 | + public void ClosingShouldNotHaveFired() |
| 87 | + { |
| 88 | + Assert.AreEqual(0, _closingRegister.Count); |
| 89 | + } |
| 90 | + |
| 91 | + [TestMethod] |
| 92 | + public void ExceptionShouldNotHaveFired() |
| 93 | + { |
| 94 | + Assert.AreEqual(0, _exceptionRegister.Count); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments