| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 | using System;using System.Globalization;using System.Net;using System.Net.Sockets;using System.Text;using Microsoft.VisualStudio.TestTools.UnitTesting;using Renci.SshNet.Common;using Renci.SshNet.Messages;using Renci.SshNet.Tests.Common;namespace Renci.SshNet.Tests.Classes{    /// <summary>    /// Provides functionality to connect and interact with SSH server.    /// </summary>    [TestClass]    public partial class SessionTest : TestBase    {        [TestMethod]        public void ConnectShouldSkipLinesBeforeProtocolIdentificationString()        {            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);            var connectionInfo = CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5));            using (var serverStub = new AsyncSocketListener(serverEndPoint))            {                serverStub.Connected += (socket) =>                {                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("SSH-666-SshStub\r\n"));                    socket.Shutdown(SocketShutdown.Send);                };                serverStub.Start();                using (var session = new Session(connectionInfo))                {                    try                    {                        session.Connect();                        Assert.Fail();                    }                    catch (SshConnectionException ex)                    {                        Assert.IsNull(ex.InnerException);                        Assert.AreEqual("Server version '666' is not supported.", ex.Message);                        Assert.AreEqual("SSH-666-SshStub", connectionInfo.ServerVersion);                    }                }            }        }        [TestMethod]        public void ConnectShouldSupportProtocolIdentificationStringThatDoesNotEndWithCrlf()        {            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);            var connectionInfo = CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5));            using (var serverStub = new AsyncSocketListener(serverEndPoint))            {                serverStub.Connected += (socket) =>                {                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("SSH-666-SshStub"));                    socket.Shutdown(SocketShutdown.Send);                };                serverStub.Start();                using (var session = new Session(connectionInfo))                {                    try                    {                        session.Connect();                        Assert.Fail();                    }                    catch (SshConnectionException ex)                    {                        Assert.IsNull(ex.InnerException);                        Assert.AreEqual("Server version '666' is not supported.", ex.Message);                        Assert.AreEqual("SSH-666-SshStub", connectionInfo.ServerVersion);                    }                }            }        }        [TestMethod]        public void ConnectShouldThrowSshOperationExceptionWhenServerDoesNotRespondWithinConnectionTimeout()        {            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);            var timeout = TimeSpan.FromMilliseconds(500);            Socket clientSocket = null;            using (var serverStub = new AsyncSocketListener(serverEndPoint))            {                serverStub.Connected += (socket) =>                {                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));                    clientSocket = socket;                };                serverStub.Start();                using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromMilliseconds(500))))                {                    try                    {                        session.Connect();                        Assert.Fail();                    }                    catch (SshOperationTimeoutException ex)                    {                        Assert.IsNull(ex.InnerException);                        Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Socket read operation has timed out after {0:F0} milliseconds.", timeout.TotalMilliseconds), ex.Message);                        Assert.IsNotNull(clientSocket);                        Assert.IsTrue(clientSocket.Connected);                        // shut down socket                        clientSocket.Shutdown(SocketShutdown.Send);                    }                }            }        }        [TestMethod]        public void ConnectShouldSshConnectionExceptionWhenServerResponseDoesNotContainProtocolIdentificationString()        {            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);            // response ends with CRLF            using (var serverStub = new AsyncSocketListener(serverEndPoint))            {                serverStub.Connected += (socket) =>                {                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));                    socket.Shutdown(SocketShutdown.Send);                };                serverStub.Start();                using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5))))                {                    try                    {                        session.Connect();                        Assert.Fail();                    }                    catch (SshConnectionException ex)                    {                        Assert.IsNull(ex.InnerException);                        Assert.AreEqual("Server response does not contain SSH protocol identification.", ex.Message);                    }                }            }            // response does not end with CRLF            using (var serverStub = new AsyncSocketListener(serverEndPoint))            {                serverStub.Connected += (socket) =>                {                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("WELCOME banner"));                    socket.Shutdown(SocketShutdown.Send);                };                serverStub.Start();                using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5))))                {                    try                    {                        session.Connect();                        Assert.Fail();                    }                    catch (SshConnectionException ex)                    {                        Assert.IsNull(ex.InnerException);                        Assert.AreEqual("Server response does not contain SSH protocol identification.", ex.Message);                    }                }            }            // last line is empty            using (var serverStub = new AsyncSocketListener(serverEndPoint))            {                serverStub.Connected += (socket) =>                {                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n"));                    socket.Send(Encoding.ASCII.GetBytes("\r\n"));                    socket.Shutdown(SocketShutdown.Send);                };                serverStub.Start();                using (var session = new Session(CreateConnectionInfo(serverEndPoint, TimeSpan.FromSeconds(5))))                {                    try                    {                        session.Connect();                        Assert.Fail();                    }                    catch (SshConnectionException ex)                    {                        Assert.IsNull(ex.InnerException);                        Assert.AreEqual("Server response does not contain SSH protocol identification.", ex.Message);                    }                }            }        }        /// <summary>        ///A test for SessionSemaphore        ///</summary>        [TestMethod()]        [Ignore]        public void SessionSemaphoreTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            SemaphoreLight actual;            actual = target.SessionSemaphore;            Assert.Inconclusive("Verify the correctness of this test method.");        }        /// <summary>        ///A test for IsConnected        ///</summary>        [TestMethod()]        [Ignore]        public void IsConnectedTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            bool actual;            actual = target.IsConnected;            Assert.Inconclusive("Verify the correctness of this test method.");        }        /// <summary>        ///A test for ClientInitMessage        ///</summary>        [TestMethod()]        [Ignore]        public void ClientInitMessageTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            Message actual;            actual = target.ClientInitMessage;            Assert.Inconclusive("Verify the correctness of this test method.");        }        /// <summary>        ///A test for UnRegisterMessage        ///</summary>        [TestMethod()]        [Ignore]        public void UnRegisterMessageTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            string messageName = string.Empty; // TODO: Initialize to an appropriate value            target.UnRegisterMessage(messageName);            Assert.Inconclusive("A method that does not return a value cannot be verified.");        }        /// <summary>        ///A test for RegisterMessage        ///</summary>        [TestMethod()]        [Ignore]        public void RegisterMessageTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            string messageName = string.Empty; // TODO: Initialize to an appropriate value            target.RegisterMessage(messageName);            Assert.Inconclusive("A method that does not return a value cannot be verified.");        }        /// <summary>        ///A test for Dispose        ///</summary>        [TestMethod()]        [Ignore]        public void DisposeTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            target.Dispose();            Assert.Inconclusive("A method that does not return a value cannot be verified.");        }        /// <summary>        ///A test for Disconnect        ///</summary>        [TestMethod()]        [Ignore]        public void DisconnectTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            target.Disconnect();            Assert.Inconclusive("A method that does not return a value cannot be verified.");        }        /// <summary>        ///A test for Connect        ///</summary>        [TestMethod()]        [Ignore]        public void ConnectTest()        {            ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value            Session target = new Session(connectionInfo); // TODO: Initialize to an appropriate value            target.Connect();            Assert.Inconclusive("A method that does not return a value cannot be verified.");        }        private static ConnectionInfo CreateConnectionInfo(IPEndPoint serverEndPoint, TimeSpan timeout)        {            var connectionInfo = new ConnectionInfo(                serverEndPoint.Address.ToString(),                serverEndPoint.Port,                "eric",                new NoneAuthenticationMethod("eric"));            connectionInfo.Timeout = timeout;            return connectionInfo;        }    }}
 |