|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | + |
| 10 | +using Moq; |
| 11 | + |
| 12 | +using Renci.SshNet.Channels; |
| 13 | +using Renci.SshNet.Common; |
| 14 | + |
| 15 | +namespace Renci.SshNet.Tests.Classes |
| 16 | +{ |
| 17 | + [TestClass] |
| 18 | + public class ShellStreamTest_ReadExpect |
| 19 | + { |
| 20 | + private ShellStream _shellStream; |
| 21 | + private ChannelSessionStub _channelSessionStub; |
| 22 | + |
| 23 | + [TestInitialize] |
| 24 | + public void Initialize() |
| 25 | + { |
| 26 | + _channelSessionStub = new ChannelSessionStub(); |
| 27 | + |
| 28 | + var connectionInfoMock = new Mock<IConnectionInfo>(); |
| 29 | + |
| 30 | + connectionInfoMock.Setup(p => p.Encoding).Returns(Encoding.UTF8); |
| 31 | + |
| 32 | + var sessionMock = new Mock<ISession>(); |
| 33 | + |
| 34 | + sessionMock.Setup(p => p.ConnectionInfo).Returns(connectionInfoMock.Object); |
| 35 | + sessionMock.Setup(p => p.CreateChannelSession()).Returns(_channelSessionStub); |
| 36 | + |
| 37 | + _shellStream = new ShellStream( |
| 38 | + sessionMock.Object, |
| 39 | + "terminalName", |
| 40 | + columns: 80, |
| 41 | + rows: 24, |
| 42 | + width: 800, |
| 43 | + height: 600, |
| 44 | + terminalModeValues: null, |
| 45 | + bufferSize: 1024); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void Read_String() |
| 50 | + { |
| 51 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello ")); |
| 52 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!")); |
| 53 | + |
| 54 | + Assert.AreEqual("Hello World!", _shellStream.Read()); |
| 55 | + } |
| 56 | + |
| 57 | + [TestMethod] |
| 58 | + public void Read_Bytes() |
| 59 | + { |
| 60 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello ")); |
| 61 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!")); |
| 62 | + |
| 63 | + byte[] buffer = new byte[12]; |
| 64 | + |
| 65 | + Assert.AreEqual(7, _shellStream.Read(buffer, 3, 7)); |
| 66 | + CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("\0\0\0Hello W\0\0"), buffer); |
| 67 | + |
| 68 | + Assert.AreEqual(5, _shellStream.Read(buffer, 0, 12)); |
| 69 | + CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("orld!llo W\0\0"), buffer); |
| 70 | + } |
| 71 | + |
| 72 | + [DataTestMethod] |
| 73 | + [DataRow("\r\n")] |
| 74 | + //[DataRow("\r")] These currently fail. |
| 75 | + //[DataRow("\n")] |
| 76 | + public void ReadLine(string newLine) |
| 77 | + { |
| 78 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello ")); |
| 79 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!")); |
| 80 | + |
| 81 | + // We specify a nonzero timeout to avoid waiting infinitely. |
| 82 | + Assert.IsNull(_shellStream.ReadLine(TimeSpan.FromTicks(1))); |
| 83 | + |
| 84 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes(newLine)); |
| 85 | + |
| 86 | + Assert.AreEqual("Hello World!", _shellStream.ReadLine(TimeSpan.FromTicks(1))); |
| 87 | + Assert.IsNull(_shellStream.ReadLine(TimeSpan.FromTicks(1))); |
| 88 | + |
| 89 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Second line!" + newLine + "Third line!" + newLine)); |
| 90 | + |
| 91 | + Assert.AreEqual("Second line!", _shellStream.ReadLine(TimeSpan.FromTicks(1))); |
| 92 | + Assert.AreEqual("Third line!", _shellStream.ReadLine(TimeSpan.FromTicks(1))); |
| 93 | + Assert.IsNull(_shellStream.ReadLine(TimeSpan.FromTicks(1))); |
| 94 | + } |
| 95 | + |
| 96 | + [DataTestMethod] |
| 97 | + [DataRow("\r\n")] |
| 98 | + [DataRow("\r")] |
| 99 | + [DataRow("\n")] |
| 100 | + public void Read_MultipleLines(string newLine) |
| 101 | + { |
| 102 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello ")); |
| 103 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!")); |
| 104 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes(newLine)); |
| 105 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Second line!" + newLine + "Third line!" + newLine)); |
| 106 | + |
| 107 | + Assert.AreEqual("Hello World!" + newLine + "Second line!" + newLine + "Third line!" + newLine, _shellStream.Read()); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + [Ignore] // Currently returns 0 immediately |
| 112 | + public void Read_NonEmptyArray_OnlyReturnsZeroAfterClose() |
| 113 | + { |
| 114 | + Task closeTask = Task.Run(async () => |
| 115 | + { |
| 116 | + // For the test to have meaning, we should be in |
| 117 | + // the call to Read before closing the channel. |
| 118 | + // Impose a short delay to make that more likely. |
| 119 | + await Task.Delay(50); |
| 120 | + |
| 121 | + _channelSessionStub.Close(); |
| 122 | + }); |
| 123 | + |
| 124 | + Assert.AreEqual(0, _shellStream.Read(new byte[16], 0, 16)); |
| 125 | + Assert.AreEqual(TaskStatus.RanToCompletion, closeTask.Status); |
| 126 | + } |
| 127 | + |
| 128 | + [TestMethod] |
| 129 | + [Ignore] // Currently returns 0 immediately |
| 130 | + public void Read_EmptyArray_OnlyReturnsZeroWhenDataAvailable() |
| 131 | + { |
| 132 | + Task receiveTask = Task.Run(async () => |
| 133 | + { |
| 134 | + // For the test to have meaning, we should be in |
| 135 | + // the call to Read before receiving the data. |
| 136 | + // Impose a short delay to make that more likely. |
| 137 | + await Task.Delay(50); |
| 138 | + |
| 139 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello World!")); |
| 140 | + }); |
| 141 | + |
| 142 | + Assert.AreEqual(0, _shellStream.Read(Array.Empty<byte>(), 0, 0)); |
| 143 | + Assert.AreEqual(TaskStatus.RanToCompletion, receiveTask.Status); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + [Ignore] // Currently hangs |
| 148 | + public void ReadLine_NoData_ReturnsNullAfterClose() |
| 149 | + { |
| 150 | + Task closeTask = Task.Run(async () => |
| 151 | + { |
| 152 | + await Task.Delay(50); |
| 153 | + |
| 154 | + _channelSessionStub.Close(); |
| 155 | + }); |
| 156 | + |
| 157 | + Assert.IsNull(_shellStream.ReadLine()); |
| 158 | + Assert.AreEqual(TaskStatus.RanToCompletion, closeTask.Status); |
| 159 | + } |
| 160 | + |
| 161 | + [TestMethod] |
| 162 | + [Ignore] // Fails because it returns the whole buffer i.e. "Hello World!\r\n12345" |
| 163 | + // We might actually want to keep that behaviour, but just make the documentation clearer. |
| 164 | + // The Expect documentation says: |
| 165 | + // "The text available in the shell that contains all the text that ends with expected expression." |
| 166 | + // Does that mean |
| 167 | + // 1. the returned string ends with the expected expression; or |
| 168 | + // 2. the returned string is all the text in the buffer, which is guaranteed to contain the expected expression? |
| 169 | + // The current behaviour is closer to 2. I think the documentation implies 1. |
| 170 | + // Either way, there are bugs. |
| 171 | + public void Expect() |
| 172 | + { |
| 173 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("Hello ")); |
| 174 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("World!")); |
| 175 | + |
| 176 | + Assert.IsNull(_shellStream.Expect("123", TimeSpan.FromTicks(1))); |
| 177 | + |
| 178 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("\r\n12345")); |
| 179 | + |
| 180 | + // Both of these cases fail |
| 181 | + // Case 1 above. |
| 182 | + Assert.AreEqual("Hello World!\r\n123", _shellStream.Expect("123")); // Fails, returns "Hello World!\r\n12345" |
| 183 | + Assert.AreEqual("45", _shellStream.Read()); // Passes, but should probably fail and return "" |
| 184 | + |
| 185 | + // Case 2 above. |
| 186 | + Assert.AreEqual("Hello World!\r\n12345", _shellStream.Expect("123")); // Passes |
| 187 | + Assert.AreEqual("", _shellStream.Read()); // Fails, returns "45" |
| 188 | + } |
| 189 | + |
| 190 | + [TestMethod] |
| 191 | + public void Read_MultiByte() |
| 192 | + { |
| 193 | + _channelSessionStub.Receive(new byte[] { 0xF0 }); |
| 194 | + _channelSessionStub.Receive(new byte[] { 0x9F }); |
| 195 | + _channelSessionStub.Receive(new byte[] { 0x91 }); |
| 196 | + _channelSessionStub.Receive(new byte[] { 0x8D }); |
| 197 | + |
| 198 | + Assert.AreEqual("👍", _shellStream.Read()); |
| 199 | + } |
| 200 | + |
| 201 | + [TestMethod] |
| 202 | + public void ReadLine_MultiByte() |
| 203 | + { |
| 204 | + _channelSessionStub.Receive(new byte[] { 0xF0 }); |
| 205 | + _channelSessionStub.Receive(new byte[] { 0x9F }); |
| 206 | + _channelSessionStub.Receive(new byte[] { 0x91 }); |
| 207 | + _channelSessionStub.Receive(new byte[] { 0x8D }); |
| 208 | + _channelSessionStub.Receive(new byte[] { 0x0D }); |
| 209 | + _channelSessionStub.Receive(new byte[] { 0x0A }); |
| 210 | + |
| 211 | + Assert.AreEqual("👍", _shellStream.ReadLine()); |
| 212 | + Assert.AreEqual("", _shellStream.Read()); |
| 213 | + } |
| 214 | + |
| 215 | + [TestMethod] |
| 216 | + [Ignore] |
| 217 | + public void Expect_Regex_MultiByte() |
| 218 | + { |
| 219 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟")); |
| 220 | + |
| 221 | + Assert.AreEqual("𐓏𐓘𐓻𐓘𐓻𐓟 ", _shellStream.Expect(new Regex(@"\s"))); |
| 222 | + Assert.AreEqual("𐒻𐓟", _shellStream.Read()); |
| 223 | + } |
| 224 | + |
| 225 | + [TestMethod] |
| 226 | + [Ignore] |
| 227 | + public void Expect_String_MultiByte() |
| 228 | + { |
| 229 | + _channelSessionStub.Receive(Encoding.UTF8.GetBytes("hello 你好")); |
| 230 | + |
| 231 | + Assert.AreEqual("hello 你好", _shellStream.Expect("你好")); |
| 232 | + Assert.AreEqual("", _shellStream.Read()); |
| 233 | + } |
| 234 | + |
| 235 | + [TestMethod] |
| 236 | + public void Expect_Timeout() |
| 237 | + { |
| 238 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 239 | + |
| 240 | + Assert.IsNull(_shellStream.Expect("Hello World!", TimeSpan.FromMilliseconds(200))); |
| 241 | + |
| 242 | + TimeSpan elapsed = stopwatch.Elapsed; |
| 243 | + |
| 244 | + // Account for variance in system timer resolution. |
| 245 | + Assert.IsTrue(elapsed > TimeSpan.FromMilliseconds(180), elapsed.ToString()); |
| 246 | + } |
| 247 | + |
| 248 | + private class ChannelSessionStub : IChannelSession |
| 249 | + { |
| 250 | + public void Receive(byte[] data) |
| 251 | + { |
| 252 | + DataReceived.Invoke(this, new ChannelDataEventArgs(channelNumber: 0, data)); |
| 253 | + } |
| 254 | + |
| 255 | + public void Close() |
| 256 | + { |
| 257 | + Closed.Invoke(this, new ChannelEventArgs(channelNumber: 0)); |
| 258 | + } |
| 259 | + |
| 260 | + public bool SendShellRequest() |
| 261 | + { |
| 262 | + return true; |
| 263 | + } |
| 264 | + |
| 265 | + public bool SendPseudoTerminalRequest(string environmentVariable, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModeValues) |
| 266 | + { |
| 267 | + return true; |
| 268 | + } |
| 269 | + |
| 270 | + public void Dispose() |
| 271 | + { |
| 272 | + } |
| 273 | + |
| 274 | + public void Open() |
| 275 | + { |
| 276 | + } |
| 277 | + |
| 278 | + public event EventHandler<ChannelDataEventArgs> DataReceived; |
| 279 | + public event EventHandler<ChannelEventArgs> Closed; |
| 280 | +#pragma warning disable 0067 |
| 281 | + public event EventHandler<ExceptionEventArgs> Exception; |
| 282 | + public event EventHandler<ChannelExtendedDataEventArgs> ExtendedDataReceived; |
| 283 | + public event EventHandler<ChannelRequestEventArgs> RequestReceived; |
| 284 | +#pragma warning restore 0067 |
| 285 | + |
| 286 | +#pragma warning disable IDE0025 // Use block body for property |
| 287 | +#pragma warning disable IDE0022 // Use block body for method |
| 288 | + public uint LocalChannelNumber => throw new NotImplementedException(); |
| 289 | + |
| 290 | + public uint LocalPacketSize => throw new NotImplementedException(); |
| 291 | + |
| 292 | + public uint RemotePacketSize => throw new NotImplementedException(); |
| 293 | + |
| 294 | + public bool IsOpen => throw new NotImplementedException(); |
| 295 | + |
| 296 | + public bool SendBreakRequest(uint breakLength) => throw new NotImplementedException(); |
| 297 | + |
| 298 | + public void SendData(byte[] data) => throw new NotImplementedException(); |
| 299 | + |
| 300 | + public void SendData(byte[] data, int offset, int size) => throw new NotImplementedException(); |
| 301 | + |
| 302 | + public bool SendEndOfWriteRequest() => throw new NotImplementedException(); |
| 303 | + |
| 304 | + public bool SendEnvironmentVariableRequest(string variableName, string variableValue) => throw new NotImplementedException(); |
| 305 | + |
| 306 | + public void SendEof() => throw new NotImplementedException(); |
| 307 | + |
| 308 | + public bool SendExecRequest(string command) => throw new NotImplementedException(); |
| 309 | + |
| 310 | + public bool SendExitSignalRequest(string signalName, bool coreDumped, string errorMessage, string language) => throw new NotImplementedException(); |
| 311 | + |
| 312 | + public bool SendExitStatusRequest(uint exitStatus) => throw new NotImplementedException(); |
| 313 | + |
| 314 | + public bool SendKeepAliveRequest() => throw new NotImplementedException(); |
| 315 | + |
| 316 | + public bool SendLocalFlowRequest(bool clientCanDo) => throw new NotImplementedException(); |
| 317 | + |
| 318 | + public bool SendSignalRequest(string signalName) => throw new NotImplementedException(); |
| 319 | + |
| 320 | + public bool SendSubsystemRequest(string subsystem) => throw new NotImplementedException(); |
| 321 | + |
| 322 | + public bool SendWindowChangeRequest(uint columns, uint rows, uint width, uint height) => throw new NotImplementedException(); |
| 323 | + |
| 324 | + public bool SendX11ForwardingRequest(bool isSingleConnection, string protocol, byte[] cookie, uint screenNumber) => throw new NotImplementedException(); |
| 325 | +#pragma warning restore IDE0022 // Use block body for method |
| 326 | +#pragma warning restore IDE0025 // Use block body for property |
| 327 | + } |
| 328 | + } |
| 329 | +} |
0 commit comments