From 70787cd96d02ad4f72f664c34966cf28dfde1cf6 Mon Sep 17 00:00:00 2001 From: drieseng Date: Thu, 23 Jun 2016 06:48:32 +0200 Subject: [PATCH] line endings :( --- ...ortDynamicTest_Dispose_PortNeverStarted.cs | 194 ++++---- ...namicTest_Stop_PortStarted_ChannelBound.cs | 414 +++++++++--------- .../Sftp/Requests/SftpOpenDirRequestTest.cs | 228 +++++----- 3 files changed, 418 insertions(+), 418 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Dispose_PortNeverStarted.cs b/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Dispose_PortNeverStarted.cs index 3d0e73b1..afa281d8 100644 --- a/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Dispose_PortNeverStarted.cs +++ b/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Dispose_PortNeverStarted.cs @@ -1,97 +1,97 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Sockets; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; -using Renci.SshNet.Channels; -using Renci.SshNet.Common; - -namespace Renci.SshNet.Tests.Classes -{ - [TestClass] - public class ForwardedPortDynamicTest_Dispose_PortNeverStarted - { - private Mock _sessionMock; - private Mock _connectionInfoMock; - private ForwardedPortDynamic _forwardedPort; - private IList _closingRegister; - private IList _exceptionRegister; - private IPEndPoint _endpoint; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - [TestCleanup] - public void Cleanup() - { - if (_forwardedPort != null) - { - _forwardedPort.Dispose(); - _forwardedPort = null; - } - } - - protected void Arrange() - { - _closingRegister = new List(); - _exceptionRegister = new List(); - _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); - - _connectionInfoMock = new Mock(MockBehavior.Strict); - _sessionMock = new Mock(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); - - _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; - } - - protected void Act() - { - _forwardedPort.Dispose(); - } - - [TestMethod] - public void IsStartedShouldReturnFalse() - { - Assert.IsFalse(_forwardedPort.IsStarted); - } - - [TestMethod] - public void ForwardedPortShouldRejectNewConnections() - { - using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) - { - try - { - client.Connect(_endpoint); - } - catch (SocketException ex) - { - Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode); - } - } - } - - [TestMethod] - public void ClosingShouldNotHaveFired() - { - Assert.AreEqual(0, _closingRegister.Count); - } - - [TestMethod] - public void ExceptionShouldNotHaveFired() - { - Assert.AreEqual(0, _exceptionRegister.Count); - } - } -} +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using Renci.SshNet.Channels; +using Renci.SshNet.Common; + +namespace Renci.SshNet.Tests.Classes +{ + [TestClass] + public class ForwardedPortDynamicTest_Dispose_PortNeverStarted + { + private Mock _sessionMock; + private Mock _connectionInfoMock; + private ForwardedPortDynamic _forwardedPort; + private IList _closingRegister; + private IList _exceptionRegister; + private IPEndPoint _endpoint; + + [TestInitialize] + public void Setup() + { + Arrange(); + Act(); + } + + [TestCleanup] + public void Cleanup() + { + if (_forwardedPort != null) + { + _forwardedPort.Dispose(); + _forwardedPort = null; + } + } + + protected void Arrange() + { + _closingRegister = new List(); + _exceptionRegister = new List(); + _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); + + _connectionInfoMock = new Mock(MockBehavior.Strict); + _sessionMock = new Mock(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); + + _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; + } + + protected void Act() + { + _forwardedPort.Dispose(); + } + + [TestMethod] + public void IsStartedShouldReturnFalse() + { + Assert.IsFalse(_forwardedPort.IsStarted); + } + + [TestMethod] + public void ForwardedPortShouldRejectNewConnections() + { + using (var client = new Socket(_endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) + { + try + { + client.Connect(_endpoint); + } + catch (SocketException ex) + { + Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode); + } + } + } + + [TestMethod] + public void ClosingShouldNotHaveFired() + { + Assert.AreEqual(0, _closingRegister.Count); + } + + [TestMethod] + public void ExceptionShouldNotHaveFired() + { + Assert.AreEqual(0, _exceptionRegister.Count); + } + } +} diff --git a/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Stop_PortStarted_ChannelBound.cs b/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Stop_PortStarted_ChannelBound.cs index 155f54b6..8b56473d 100644 --- a/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Stop_PortStarted_ChannelBound.cs +++ b/src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Stop_PortStarted_ChannelBound.cs @@ -1,207 +1,207 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading; -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_ChannelBound - { - private Mock _sessionMock; - private Mock _connectionInfoMock; - private Mock _channelMock; - private ForwardedPortDynamic _forwardedPort; - private IList _closingRegister; - private IList _exceptionRegister; - private IPEndPoint _endpoint; - private Socket _client; - private IPEndPoint _remoteEndpoint; - private string _userName; - private TimeSpan _expectedElapsedTime; - private TimeSpan _elapsedTimeOfStop; - - [TestInitialize] - public void Setup() - { - Arrange(); - Act(); - } - - [TestCleanup] - public void Cleanup() - { - if (_client != null) - { - _client.Dispose(); - _client = null; - } - if (_forwardedPort != null) - { - _forwardedPort.Dispose(); - _forwardedPort = null; - } - } - - protected void Arrange() - { - var random = new Random(); - - _closingRegister = new List(); - _exceptionRegister = new List(); - _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); - _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort)); - _expectedElapsedTime = TimeSpan.FromMilliseconds(random.Next(100, 500)); - _userName = random.Next().ToString(CultureInfo.InvariantCulture); - - _connectionInfoMock = new Mock(MockBehavior.Strict); - _sessionMock = new Mock(MockBehavior.Strict); - _channelMock = new Mock(MockBehavior.Strict); - - _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint) _endpoint.Port); - - Socket handlerSocket = null; - - _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.Open(_remoteEndpoint.Address.ToString(), (uint) _remoteEndpoint.Port, _forwardedPort, It.IsAny())).Callback((address, port, forwardedPort, socket) => handlerSocket = socket); - _channelMock.Setup(p => p.IsOpen).Returns(true); - _channelMock.Setup(p => p.Bind()).Callback(() => - { - Thread.Sleep(_expectedElapsedTime); - if (handlerSocket != null && handlerSocket.Connected) - handlerSocket.Shutdown(SocketShutdown.Both); - }); - _channelMock.Setup(p => p.Close()); - _channelMock.Setup(p => p.Dispose()); - - _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 - }; - EstablishSocks4Connection(_client); - } - - protected void Act() - { - var stopwatch = new Stopwatch(); - stopwatch.Start(); - - _forwardedPort.Stop(); - - stopwatch.Stop(); - _elapsedTimeOfStop = stopwatch.Elapsed; - } - - [TestMethod] - public void StopShouldBlockUntilBoundChannelHasClosed() - { - Assert.IsTrue(_elapsedTimeOfStop >=_expectedElapsedTime); - Assert.IsTrue(_elapsedTimeOfStop < _expectedElapsedTime.Add(TimeSpan.FromMilliseconds(200))); - } - - [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); - } - } - } - - [TestMethod] - 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.AreEqual(1, _closingRegister.Count); - } - - [TestMethod] - public void ExceptionShouldNotHaveFired() - { - Assert.AreEqual(0, _exceptionRegister.Count); - } - - [TestMethod] - public void CloseOnChannelShouldBeInvokedOnce() - { - _channelMock.Verify(p => p.Close(), Times.Once); - } - - [TestMethod] - public void DisposeOnChannelShouldBeInvokedOnce() - { - _channelMock.Verify(p => p.Dispose(), Times.Once); - } - - private void EstablishSocks4Connection(Socket client) - { - var userNameBytes = Encoding.ASCII.GetBytes(_userName); - var addressBytes = _remoteEndpoint.Address.GetAddressBytes(); - var portBytes = BitConverter.GetBytes((ushort)_remoteEndpoint.Port).Reverse().ToArray(); - - _client.Connect(_endpoint); - - // send SOCKS version - client.Send(new byte[] { 0x04 }, 0, 1, SocketFlags.None); - // send command byte - client.Send(new byte[] { 0x00 }, 0, 1, SocketFlags.None); - // send port - client.Send(portBytes, 0, portBytes.Length, SocketFlags.None); - // send address - client.Send(addressBytes, 0, addressBytes.Length, SocketFlags.None); - // send user name - client.Send(userNameBytes, 0, userNameBytes.Length, SocketFlags.None); - // terminate user name with null - client.Send(new byte[] { 0x00 }, 0, 1, SocketFlags.None); - - var buffer = new byte[16]; - client.Receive(buffer, 0, buffer.Length, SocketFlags.None); - } - } -} +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; +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_ChannelBound + { + private Mock _sessionMock; + private Mock _connectionInfoMock; + private Mock _channelMock; + private ForwardedPortDynamic _forwardedPort; + private IList _closingRegister; + private IList _exceptionRegister; + private IPEndPoint _endpoint; + private Socket _client; + private IPEndPoint _remoteEndpoint; + private string _userName; + private TimeSpan _expectedElapsedTime; + private TimeSpan _elapsedTimeOfStop; + + [TestInitialize] + public void Setup() + { + Arrange(); + Act(); + } + + [TestCleanup] + public void Cleanup() + { + if (_client != null) + { + _client.Dispose(); + _client = null; + } + if (_forwardedPort != null) + { + _forwardedPort.Dispose(); + _forwardedPort = null; + } + } + + protected void Arrange() + { + var random = new Random(); + + _closingRegister = new List(); + _exceptionRegister = new List(); + _endpoint = new IPEndPoint(IPAddress.Loopback, 8122); + _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort)); + _expectedElapsedTime = TimeSpan.FromMilliseconds(random.Next(100, 500)); + _userName = random.Next().ToString(CultureInfo.InvariantCulture); + + _connectionInfoMock = new Mock(MockBehavior.Strict); + _sessionMock = new Mock(MockBehavior.Strict); + _channelMock = new Mock(MockBehavior.Strict); + + _forwardedPort = new ForwardedPortDynamic(_endpoint.Address.ToString(), (uint) _endpoint.Port); + + Socket handlerSocket = null; + + _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.Open(_remoteEndpoint.Address.ToString(), (uint) _remoteEndpoint.Port, _forwardedPort, It.IsAny())).Callback((address, port, forwardedPort, socket) => handlerSocket = socket); + _channelMock.Setup(p => p.IsOpen).Returns(true); + _channelMock.Setup(p => p.Bind()).Callback(() => + { + Thread.Sleep(_expectedElapsedTime); + if (handlerSocket != null && handlerSocket.Connected) + handlerSocket.Shutdown(SocketShutdown.Both); + }); + _channelMock.Setup(p => p.Close()); + _channelMock.Setup(p => p.Dispose()); + + _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 + }; + EstablishSocks4Connection(_client); + } + + protected void Act() + { + var stopwatch = new Stopwatch(); + stopwatch.Start(); + + _forwardedPort.Stop(); + + stopwatch.Stop(); + _elapsedTimeOfStop = stopwatch.Elapsed; + } + + [TestMethod] + public void StopShouldBlockUntilBoundChannelHasClosed() + { + Assert.IsTrue(_elapsedTimeOfStop >=_expectedElapsedTime); + Assert.IsTrue(_elapsedTimeOfStop < _expectedElapsedTime.Add(TimeSpan.FromMilliseconds(200))); + } + + [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); + } + } + } + + [TestMethod] + 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.AreEqual(1, _closingRegister.Count); + } + + [TestMethod] + public void ExceptionShouldNotHaveFired() + { + Assert.AreEqual(0, _exceptionRegister.Count); + } + + [TestMethod] + public void CloseOnChannelShouldBeInvokedOnce() + { + _channelMock.Verify(p => p.Close(), Times.Once); + } + + [TestMethod] + public void DisposeOnChannelShouldBeInvokedOnce() + { + _channelMock.Verify(p => p.Dispose(), Times.Once); + } + + private void EstablishSocks4Connection(Socket client) + { + var userNameBytes = Encoding.ASCII.GetBytes(_userName); + var addressBytes = _remoteEndpoint.Address.GetAddressBytes(); + var portBytes = BitConverter.GetBytes((ushort)_remoteEndpoint.Port).Reverse().ToArray(); + + _client.Connect(_endpoint); + + // send SOCKS version + client.Send(new byte[] { 0x04 }, 0, 1, SocketFlags.None); + // send command byte + client.Send(new byte[] { 0x00 }, 0, 1, SocketFlags.None); + // send port + client.Send(portBytes, 0, portBytes.Length, SocketFlags.None); + // send address + client.Send(addressBytes, 0, addressBytes.Length, SocketFlags.None); + // send user name + client.Send(userNameBytes, 0, userNameBytes.Length, SocketFlags.None); + // terminate user name with null + client.Send(new byte[] { 0x00 }, 0, 1, SocketFlags.None); + + var buffer = new byte[16]; + client.Receive(buffer, 0, buffer.Length, SocketFlags.None); + } + } +} diff --git a/src/Renci.SshNet.Tests/Classes/Sftp/Requests/SftpOpenDirRequestTest.cs b/src/Renci.SshNet.Tests/Classes/Sftp/Requests/SftpOpenDirRequestTest.cs index 55fe352d..2ee19ad4 100644 --- a/src/Renci.SshNet.Tests/Classes/Sftp/Requests/SftpOpenDirRequestTest.cs +++ b/src/Renci.SshNet.Tests/Classes/Sftp/Requests/SftpOpenDirRequestTest.cs @@ -1,115 +1,115 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Renci.SshNet.Common; -using Renci.SshNet.Sftp; -using Renci.SshNet.Sftp.Requests; -using Renci.SshNet.Sftp.Responses; - -namespace Renci.SshNet.Tests.Classes.Sftp.Requests -{ - [TestClass] - public class SftpOpenDirRequestTest - { - private uint _protocolVersion; - private uint _requestId; - private Encoding _encoding; - private string _path; - private byte[] _pathBytes; - - [TestInitialize] - public void Init() - { - var random = new Random(); - - _protocolVersion = (uint)random.Next(0, int.MaxValue); - _requestId = (uint)random.Next(0, int.MaxValue); - _encoding = Encoding.Unicode; - _path = random.Next().ToString(CultureInfo.InvariantCulture); - _pathBytes = _encoding.GetBytes(_path); - } - - [TestMethod] - public void Constructor() - { - var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, null, null); - - Assert.AreSame(_encoding, request.Encoding); - Assert.AreEqual(_path, request.Path); - Assert.AreEqual(_protocolVersion, request.ProtocolVersion); - Assert.AreEqual(_requestId, request.RequestId); - Assert.AreEqual(SftpMessageTypes.OpenDir, request.SftpMessageType); - } - - [TestMethod] - public void Complete_SftpHandleResponse() - { - var statusActionInvocations = new List(); - var handleActionInvocations = new List(); - - Action statusAction = statusActionInvocations.Add; - Action handleAction = handleActionInvocations.Add; - var handleResponse = new SftpHandleResponse(_protocolVersion); - - var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, handleAction, statusAction); - - request.Complete(handleResponse); - - Assert.AreEqual(0, statusActionInvocations.Count); - Assert.AreEqual(1, handleActionInvocations.Count); - Assert.AreSame(handleResponse, handleActionInvocations[0]); - } - - [TestMethod] - public void Complete_SftpStatusResponse() - { - var statusActionInvocations = new List(); - var handleActionInvocations = new List(); - - Action statusAction = statusActionInvocations.Add; - Action handleAction = handleActionInvocations.Add; - var statusResponse = new SftpStatusResponse(_protocolVersion); - - var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, handleAction, statusAction); - - request.Complete(statusResponse); - - Assert.AreEqual(1, statusActionInvocations.Count); - Assert.AreSame(statusResponse, statusActionInvocations[0]); - Assert.AreEqual(0, handleActionInvocations.Count); - } - - [TestMethod] - public void GetBytes() - { - var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, null, null); - - var bytes = request.GetBytes(); - - var expectedBytesLength = 0; - expectedBytesLength += 4; // Length - expectedBytesLength += 1; // Type - expectedBytesLength += 4; // RequestId - expectedBytesLength += 4; // Path length - expectedBytesLength += _pathBytes.Length; // Path - - Assert.AreEqual(expectedBytesLength, bytes.Length); - - var sshDataStream = new SshDataStream(bytes); - - Assert.AreEqual((uint) bytes.Length - 4, sshDataStream.ReadUInt32()); - Assert.AreEqual((byte) SftpMessageTypes.OpenDir, sshDataStream.ReadByte()); - Assert.AreEqual(_requestId, sshDataStream.ReadUInt32()); - - Assert.AreEqual((uint) _pathBytes.Length, sshDataStream.ReadUInt32()); - var actualPath = new byte[_pathBytes.Length]; - sshDataStream.Read(actualPath, 0, actualPath.Length); - Assert.IsTrue(_pathBytes.SequenceEqual(actualPath)); - - Assert.IsTrue(sshDataStream.IsEndOfData); - } - } +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Renci.SshNet.Common; +using Renci.SshNet.Sftp; +using Renci.SshNet.Sftp.Requests; +using Renci.SshNet.Sftp.Responses; + +namespace Renci.SshNet.Tests.Classes.Sftp.Requests +{ + [TestClass] + public class SftpOpenDirRequestTest + { + private uint _protocolVersion; + private uint _requestId; + private Encoding _encoding; + private string _path; + private byte[] _pathBytes; + + [TestInitialize] + public void Init() + { + var random = new Random(); + + _protocolVersion = (uint)random.Next(0, int.MaxValue); + _requestId = (uint)random.Next(0, int.MaxValue); + _encoding = Encoding.Unicode; + _path = random.Next().ToString(CultureInfo.InvariantCulture); + _pathBytes = _encoding.GetBytes(_path); + } + + [TestMethod] + public void Constructor() + { + var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, null, null); + + Assert.AreSame(_encoding, request.Encoding); + Assert.AreEqual(_path, request.Path); + Assert.AreEqual(_protocolVersion, request.ProtocolVersion); + Assert.AreEqual(_requestId, request.RequestId); + Assert.AreEqual(SftpMessageTypes.OpenDir, request.SftpMessageType); + } + + [TestMethod] + public void Complete_SftpHandleResponse() + { + var statusActionInvocations = new List(); + var handleActionInvocations = new List(); + + Action statusAction = statusActionInvocations.Add; + Action handleAction = handleActionInvocations.Add; + var handleResponse = new SftpHandleResponse(_protocolVersion); + + var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, handleAction, statusAction); + + request.Complete(handleResponse); + + Assert.AreEqual(0, statusActionInvocations.Count); + Assert.AreEqual(1, handleActionInvocations.Count); + Assert.AreSame(handleResponse, handleActionInvocations[0]); + } + + [TestMethod] + public void Complete_SftpStatusResponse() + { + var statusActionInvocations = new List(); + var handleActionInvocations = new List(); + + Action statusAction = statusActionInvocations.Add; + Action handleAction = handleActionInvocations.Add; + var statusResponse = new SftpStatusResponse(_protocolVersion); + + var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, handleAction, statusAction); + + request.Complete(statusResponse); + + Assert.AreEqual(1, statusActionInvocations.Count); + Assert.AreSame(statusResponse, statusActionInvocations[0]); + Assert.AreEqual(0, handleActionInvocations.Count); + } + + [TestMethod] + public void GetBytes() + { + var request = new SftpOpenDirRequest(_protocolVersion, _requestId, _path, _encoding, null, null); + + var bytes = request.GetBytes(); + + var expectedBytesLength = 0; + expectedBytesLength += 4; // Length + expectedBytesLength += 1; // Type + expectedBytesLength += 4; // RequestId + expectedBytesLength += 4; // Path length + expectedBytesLength += _pathBytes.Length; // Path + + Assert.AreEqual(expectedBytesLength, bytes.Length); + + var sshDataStream = new SshDataStream(bytes); + + Assert.AreEqual((uint) bytes.Length - 4, sshDataStream.ReadUInt32()); + Assert.AreEqual((byte) SftpMessageTypes.OpenDir, sshDataStream.ReadByte()); + Assert.AreEqual(_requestId, sshDataStream.ReadUInt32()); + + Assert.AreEqual((uint) _pathBytes.Length, sshDataStream.ReadUInt32()); + var actualPath = new byte[_pathBytes.Length]; + sshDataStream.Read(actualPath, 0, actualPath.Length); + Assert.IsTrue(_pathBytes.SequenceEqual(actualPath)); + + Assert.IsTrue(sshDataStream.IsEndOfData); + } + } } \ No newline at end of file