ShellStreamTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  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 int _bufferSize;
  29. private Dictionary<TerminalModes, uint> _terminalModes;
  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. _bufferSize = random.Next();
  41. _terminalModes = new Dictionary<TerminalModes, uint>();
  42. _encoding = Encoding.UTF8;
  43. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  44. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  45. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  46. }
  47. [TestMethod] // issue #2190
  48. public void ReadLine_MultiByteCharacters()
  49. {
  50. // bash: /root/menu.sh: Отказан
  51. const string data1 = "bash: /root/menu.sh: \u041e\u0442\u043a\u0430\u0437\u0430\u043d";
  52. // о в доступе
  53. const string data2 = "\u043e \u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0435";
  54. // done
  55. const string data3 = "done";
  56. var shellStream = CreateShellStream();
  57. var channelDataPublishTask = Task.Factory.StartNew(() =>
  58. {
  59. _channelSessionMock.Raise(p => p.DataReceived += null,
  60. new ChannelDataEventArgs(5, _encoding.GetBytes(data1)));
  61. Thread.Sleep(50);
  62. _channelSessionMock.Raise(p => p.DataReceived += null,
  63. new ChannelDataEventArgs(5, _encoding.GetBytes(data2 + "\r\n")));
  64. _channelSessionMock.Raise(p => p.DataReceived += null,
  65. new ChannelDataEventArgs(5, _encoding.GetBytes(data3 + "\r\n")));
  66. });
  67. Assert.AreEqual(data1 + data2, shellStream.ReadLine());
  68. Assert.AreEqual(data3, shellStream.ReadLine());
  69. channelDataPublishTask.Wait();
  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));
  86. shellStream.WriteLine(line);
  87. _channelSessionMock.Verify(p => p.SendData(lineTerminator), Times.Once);
  88. }
  89. private ShellStream CreateShellStream()
  90. {
  91. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  92. _connectionInfoMock.Setup(p => p.Encoding).Returns(_encoding);
  93. _sessionMock.Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  94. _channelSessionMock.Setup(p => p.Open());
  95. _channelSessionMock.Setup(p => p.SendPseudoTerminalRequest(_terminalName, _widthColumns, _heightRows,
  96. _widthPixels, _heightPixels, _terminalModes)).Returns(true);
  97. _channelSessionMock.Setup(p => p.SendShellRequest()).Returns(true);
  98. return new ShellStream(_sessionMock.Object, _terminalName, _widthColumns, _heightRows,
  99. _widthPixels, _heightPixels, _bufferSize, _terminalModes);
  100. }
  101. }
  102. }