ShellStreamTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Threading;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Moq;
  9. using Renci.SshNet.Channels;
  10. using Renci.SshNet.Common;
  11. using Renci.SshNet.Tests.Common;
  12. namespace Renci.SshNet.Tests.Classes
  13. {
  14. /// <summary>
  15. /// Contains operation for working with SSH Shell.
  16. /// </summary>
  17. [TestClass]
  18. public class ShellStreamTest : TestBase
  19. {
  20. private Mock<ISession> _sessionMock;
  21. private Mock<IConnectionInfo> _connectionInfoMock;
  22. private Encoding _encoding;
  23. private string _terminalName;
  24. private uint _widthColumns;
  25. private uint _heightRows;
  26. private uint _widthPixels;
  27. private uint _heightPixels;
  28. private Dictionary<TerminalModes, uint> _terminalModes;
  29. private int _bufferSize;
  30. private Mock<IChannelSession> _channelSessionMock;
  31. protected override void OnInit()
  32. {
  33. base.OnInit();
  34. var random = new Random();
  35. _terminalName = random.Next().ToString(CultureInfo.InvariantCulture);
  36. _widthColumns = (uint)random.Next();
  37. _heightRows = (uint)random.Next();
  38. _widthPixels = (uint)random.Next();
  39. _heightPixels = (uint)random.Next();
  40. _terminalModes = new Dictionary<TerminalModes, uint>();
  41. _bufferSize = random.Next(100, 500);
  42. _encoding = Encoding.UTF8;
  43. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  44. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  45. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  46. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  47. }
  48. [TestMethod] // issue #2190
  49. public void ReadLine_MultiByteCharacters()
  50. {
  51. // bash: /root/menu.sh: Отказан
  52. const string data1 = "bash: /root/menu.sh: \u041e\u0442\u043a\u0430\u0437\u0430\u043d";
  53. // о в доступе
  54. const string data2 = "\u043e \u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0435";
  55. // done
  56. const string data3 = "done";
  57. var shellStream = CreateShellStream();
  58. var channelDataPublishThread = new Thread(() =>
  59. {
  60. _channelSessionMock.Raise(p => p.DataReceived += null,
  61. new ChannelDataEventArgs(5, _encoding.GetBytes(data1)));
  62. Thread.Sleep(50);
  63. _channelSessionMock.Raise(p => p.DataReceived += null,
  64. new ChannelDataEventArgs(5, _encoding.GetBytes(data2 + "\r\n")));
  65. _channelSessionMock.Raise(p => p.DataReceived += null,
  66. new ChannelDataEventArgs(5, _encoding.GetBytes(data3 + "\r\n")));
  67. });
  68. channelDataPublishThread.Start();
  69. Assert.AreEqual(data1 + data2, shellStream.ReadLine());
  70. Assert.AreEqual(data3, shellStream.ReadLine());
  71. channelDataPublishThread.Join();
  72. }
  73. [TestMethod]
  74. public void Write_Text_ShouldWriteNothingWhenTextIsNull()
  75. {
  76. var shellStream = CreateShellStream();
  77. const string text = null;
  78. shellStream.Write(text);
  79. _channelSessionMock.VerifyAll();
  80. }
  81. [TestMethod]
  82. public void WriteLine_Line_ShouldOnlyWriteLineTerminatorWhenLineIsNull()
  83. {
  84. var shellStream = CreateShellStream();
  85. const string line = null;
  86. var lineTerminator = _encoding.GetBytes("\r");
  87. _ = _channelSessionMock.Setup(p => p.SendData(
  88. It.Is<byte[]>(data => data.Take(lineTerminator.Length).IsEqualTo(lineTerminator)),
  89. 0,
  90. lineTerminator.Length));
  91. shellStream.WriteLine(line);
  92. _channelSessionMock.VerifyAll();
  93. }
  94. [TestMethod]
  95. public void Write_AfterDispose_ThrowsObjectDisposedException()
  96. {
  97. var shellStream = CreateShellStream();
  98. _channelSessionMock.Setup(p => p.Dispose());
  99. shellStream.Dispose();
  100. var bytes = _encoding.GetBytes("Hello World!");
  101. Assert.ThrowsException<ObjectDisposedException>(() => shellStream.Write(bytes, 0, bytes.Length));
  102. }
  103. [TestMethod]
  104. public void ChangeWindowSize_AfterDispose_ThrowsObjectDisposedException()
  105. {
  106. var shellStream = CreateShellStream();
  107. _channelSessionMock.Setup(p => p.Dispose());
  108. shellStream.Dispose();
  109. Assert.ThrowsException<ObjectDisposedException>(() => shellStream.ChangeWindowSize(80, 25, 0, 0));
  110. }
  111. [TestMethod]
  112. public void ChangeWindowSize_SendsWindowChangeRequest()
  113. {
  114. var shellStream = CreateShellStream();
  115. _channelSessionMock.Setup(s => s.SendWindowChangeRequest(80, 25, 0, 1));
  116. shellStream.ChangeWindowSize(80, 25, 0, 1);
  117. _channelSessionMock.Verify(v => v.SendWindowChangeRequest(80, 25, 0, 1), Times.Once());
  118. }
  119. private ShellStream CreateShellStream()
  120. {
  121. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  122. _connectionInfoMock.Setup(p => p.Encoding).Returns(_encoding);
  123. _sessionMock.Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  124. _channelSessionMock.Setup(p => p.Open());
  125. _channelSessionMock.Setup(p => p.SendPseudoTerminalRequest(_terminalName, _widthColumns, _heightRows,
  126. _widthPixels, _heightPixels, _terminalModes)).Returns(true);
  127. _channelSessionMock.Setup(p => p.SendShellRequest()).Returns(true);
  128. return new ShellStream(_sessionMock.Object,
  129. _terminalName,
  130. _widthColumns,
  131. _heightRows,
  132. _widthPixels,
  133. _heightPixels,
  134. _terminalModes,
  135. _bufferSize);
  136. }
  137. }
  138. }