| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Moq;
- using Renci.SshNet.Channels;
- using Renci.SshNet.Common;
- namespace Renci.SshNet.Tests.Classes
- {
- [TestClass]
- public class ShellStreamTest_ReadExpect
- {
- private const int BufferSize = 1024;
- private const int ExpectSize = BufferSize * 2;
- private ShellStream _shellStream;
- private ChannelSessionStub _channelSessionStub;
- [TestInitialize]
- public void Initialize()
- {
- _channelSessionStub = new ChannelSessionStub();
- var connectionInfoMock = new Mock<IConnectionInfo>();
- connectionInfoMock.Setup(p => p.Encoding).Returns(Encoding.UTF8);
- var sessionMock = new Mock<ISession>();
- sessionMock.Setup(p => p.ConnectionInfo).Returns(connectionInfoMock.Object);
- sessionMock.Setup(p => p.CreateChannelSession()).Returns(_channelSessionStub);
- _shellStream = new ShellStream(
- sessionMock.Object,
- "terminalName",
- columns: 80,
- rows: 24,
- width: 800,
- height: 600,
- terminalModeValues: null,
- bufferSize: BufferSize,
- expectSize: ExpectSize);
- }
- [TestMethod]
- public void Read_String()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello "));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!"));
- Assert.AreEqual("Hello World!", _shellStream.Read());
- }
- [TestMethod]
- public void Read_Bytes()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello "));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!"));
- byte[] buffer = new byte[12];
- Assert.AreEqual(7, _shellStream.Read(buffer, 3, 7));
- CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("\0\0\0Hello W\0\0"), buffer);
- Assert.AreEqual(5, _shellStream.Read(buffer, 0, 12));
- CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("orld!llo W\0\0"), buffer);
- }
- [DataTestMethod]
- [DataRow("\r\n")]
- //[DataRow("\r")] These currently fail.
- //[DataRow("\n")]
- public void ReadLine(string newLine)
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello "));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!"));
- // We specify a nonzero timeout to avoid waiting infinitely.
- Assert.IsNull(_shellStream.ReadLine(TimeSpan.FromTicks(1)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(newLine));
- Assert.AreEqual("Hello World!", _shellStream.ReadLine(TimeSpan.FromTicks(1)));
- Assert.IsNull(_shellStream.ReadLine(TimeSpan.FromTicks(1)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Second line!" + newLine + "Third line!" + newLine));
- Assert.AreEqual("Second line!", _shellStream.ReadLine(TimeSpan.FromTicks(1)));
- Assert.AreEqual("Third line!", _shellStream.ReadLine(TimeSpan.FromTicks(1)));
- Assert.IsNull(_shellStream.ReadLine(TimeSpan.FromTicks(1)));
- }
- [DataTestMethod]
- [DataRow("\r\n")]
- [DataRow("\r")]
- [DataRow("\n")]
- public void Read_MultipleLines(string newLine)
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello "));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!"));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(newLine));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Second line!" + newLine + "Third line!" + newLine));
- Assert.AreEqual("Hello World!" + newLine + "Second line!" + newLine + "Third line!" + newLine, _shellStream.Read());
- }
- [TestMethod]
- [Ignore] // Currently returns 0 immediately
- public void Read_NonEmptyArray_OnlyReturnsZeroAfterClose()
- {
- Task closeTask = Task.Run(async () =>
- {
- // For the test to have meaning, we should be in
- // the call to Read before closing the channel.
- // Impose a short delay to make that more likely.
- await Task.Delay(50);
- _channelSessionStub.Close();
- });
- Assert.AreEqual(0, _shellStream.Read(new byte[16], 0, 16));
- Assert.AreEqual(TaskStatus.RanToCompletion, closeTask.Status);
- }
- [TestMethod]
- [Ignore] // Currently returns 0 immediately
- public void Read_EmptyArray_OnlyReturnsZeroWhenDataAvailable()
- {
- Task receiveTask = Task.Run(async () =>
- {
- // For the test to have meaning, we should be in
- // the call to Read before receiving the data.
- // Impose a short delay to make that more likely.
- await Task.Delay(50);
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello World!"));
- });
- Assert.AreEqual(0, _shellStream.Read(Array.Empty<byte>(), 0, 0));
- Assert.AreEqual(TaskStatus.RanToCompletion, receiveTask.Status);
- }
- [TestMethod]
- [Ignore] // Currently hangs
- public void ReadLine_NoData_ReturnsNullAfterClose()
- {
- Task closeTask = Task.Run(async () =>
- {
- await Task.Delay(50);
- _channelSessionStub.Close();
- });
- Assert.IsNull(_shellStream.ReadLine());
- Assert.AreEqual(TaskStatus.RanToCompletion, closeTask.Status);
- }
- [TestMethod]
- public void Expect()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello "));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!"));
- Assert.IsNull(_shellStream.Expect("123", TimeSpan.FromTicks(1)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("\r\n12345"));
- // Both of these cases fail
- // Case 1 above.
- Assert.AreEqual("Hello World!\r\n123", _shellStream.Expect("123")); // Fails, returns "Hello World!\r\n12345"
- Assert.AreEqual("45", _shellStream.Read()); // Passes, but should probably fail and return ""
- }
- [TestMethod]
- public void Read_MultiByte()
- {
- _channelSessionStub.Receive(new byte[] { 0xF0 });
- _channelSessionStub.Receive(new byte[] { 0x9F });
- _channelSessionStub.Receive(new byte[] { 0x91 });
- _channelSessionStub.Receive(new byte[] { 0x8D });
- Assert.AreEqual("👍", _shellStream.Read());
- }
- [TestMethod]
- public void ReadLine_MultiByte()
- {
- _channelSessionStub.Receive(new byte[] { 0xF0 });
- _channelSessionStub.Receive(new byte[] { 0x9F });
- _channelSessionStub.Receive(new byte[] { 0x91 });
- _channelSessionStub.Receive(new byte[] { 0x8D });
- _channelSessionStub.Receive(new byte[] { 0x0D });
- _channelSessionStub.Receive(new byte[] { 0x0A });
- Assert.AreEqual("👍", _shellStream.ReadLine());
- Assert.AreEqual("", _shellStream.Read());
- }
- [TestMethod]
- public void Expect_Regex_MultiByte()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
- Assert.AreEqual("𐓏𐓘𐓻𐓘𐓻𐓟 ", _shellStream.Expect(new Regex(@"\s")));
- Assert.AreEqual("𐒻𐓟", _shellStream.Read());
- }
- [TestMethod]
- public void Expect_String_MultiByte()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("hello 你好"));
- Assert.AreEqual("hello 你好", _shellStream.Expect("你好"));
- Assert.AreEqual("", _shellStream.Read());
- }
- [TestMethod]
- public void Expect_String_non_ASCII_characters()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello, こんにちは, Bonjour"));
- Assert.AreEqual("Hello, こ", _shellStream.Expect(new Regex(@"[^\u0000-\u007F]")));
- Assert.AreEqual("んにちは, Bonjour", _shellStream.Read());
- }
- [TestMethod]
- public void Expect_String_LargeExpect()
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('a', 100)));
- for (var i = 0; i < 10; i++)
- {
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('b', 100)));
- }
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello, こんにちは, Bonjour"));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('c', 100)));
- Assert.AreEqual($"{new string('a', 100)}{new string('b', 1000)}Hello, こんにちは, Bonjour", _shellStream.Expect($"{new string('b', 1000)}Hello, こんにちは, Bonjour"));
- Assert.AreEqual($"{new string('c', 100)}", _shellStream.Read());
- }
- [TestMethod]
- public void Expect_String_DequeueChecks()
- {
- const string expected = "ccccc";
- // Prime buffer
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string(' ', BufferSize)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string(' ', ExpectSize)));
- // Test data
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('a', 100)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('b', 100)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(expected));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('d', 100)));
- _channelSessionStub.Receive(Encoding.UTF8.GetBytes(new string('e', 100)));
- // Expected result
- var expectedResult = $"{new string(' ', BufferSize)}{new string(' ', ExpectSize)}{new string('a', 100)}{new string('b', 100)}{expected}";
- var expectedRead = $"{new string('d', 100)}{new string('e', 100)}";
- Assert.AreEqual(expectedResult, _shellStream.Expect(expected));
- Assert.AreEqual(expectedRead, _shellStream.Read());
- }
- [TestMethod]
- public void Expect_Timeout()
- {
- Stopwatch stopwatch = Stopwatch.StartNew();
- Assert.IsNull(_shellStream.Expect("Hello World!", TimeSpan.FromMilliseconds(200)));
- TimeSpan elapsed = stopwatch.Elapsed;
- // Account for variance in system timer resolution.
- Assert.IsTrue(elapsed > TimeSpan.FromMilliseconds(180), elapsed.ToString());
- }
- private class ChannelSessionStub : IChannelSession
- {
- public void Receive(byte[] data)
- {
- DataReceived.Invoke(this, new ChannelDataEventArgs(channelNumber: 0, data));
- }
- public void Close()
- {
- Closed.Invoke(this, new ChannelEventArgs(channelNumber: 0));
- }
- public bool SendShellRequest()
- {
- return true;
- }
- public bool SendPseudoTerminalRequest(string environmentVariable, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModeValues)
- {
- return true;
- }
- public void Dispose()
- {
- }
- public void Open()
- {
- }
- public event EventHandler<ChannelDataEventArgs> DataReceived;
- public event EventHandler<ChannelEventArgs> Closed;
- #pragma warning disable 0067
- public event EventHandler<ExceptionEventArgs> Exception;
- public event EventHandler<ChannelExtendedDataEventArgs> ExtendedDataReceived;
- public event EventHandler<ChannelRequestEventArgs> RequestReceived;
- #pragma warning restore 0067
- #pragma warning disable IDE0025 // Use block body for property
- #pragma warning disable IDE0022 // Use block body for method
- public uint LocalChannelNumber => throw new NotImplementedException();
- public uint LocalPacketSize => throw new NotImplementedException();
- public uint RemotePacketSize => throw new NotImplementedException();
- public bool IsOpen => throw new NotImplementedException();
- public bool SendBreakRequest(uint breakLength) => throw new NotImplementedException();
- public void SendData(byte[] data) => throw new NotImplementedException();
- public void SendData(byte[] data, int offset, int size) => throw new NotImplementedException();
- public bool SendEndOfWriteRequest() => throw new NotImplementedException();
- public bool SendEnvironmentVariableRequest(string variableName, string variableValue) => throw new NotImplementedException();
- public void SendEof() => throw new NotImplementedException();
- public bool SendExecRequest(string command) => throw new NotImplementedException();
- public bool SendExitSignalRequest(string signalName, bool coreDumped, string errorMessage, string language) => throw new NotImplementedException();
- public bool SendExitStatusRequest(uint exitStatus) => throw new NotImplementedException();
- public bool SendKeepAliveRequest() => throw new NotImplementedException();
- public bool SendLocalFlowRequest(bool clientCanDo) => throw new NotImplementedException();
- public bool SendSignalRequest(string signalName) => throw new NotImplementedException();
- public bool SendSubsystemRequest(string subsystem) => throw new NotImplementedException();
- public bool SendWindowChangeRequest(uint columns, uint rows, uint width, uint height) => throw new NotImplementedException();
- public bool SendX11ForwardingRequest(bool isSingleConnection, string protocol, byte[] cookie, uint screenNumber) => throw new NotImplementedException();
- #pragma warning restore IDE0022 // Use block body for method
- #pragma warning restore IDE0025 // Use block body for property
- }
- }
- }
|