ShellStreamTest.cs 6.1 KB

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