ShellStreamTest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.VerifyAll();
  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(
  86. It.Is<byte[]>(data => data.Take(lineTerminator.Length).IsEqualTo(lineTerminator)),
  87. 0,
  88. lineTerminator.Length));
  89. shellStream.WriteLine(line);
  90. _channelSessionMock.VerifyAll();
  91. }
  92. [TestMethod]
  93. public void Write_AfterDispose_ThrowsObjectDisposedException()
  94. {
  95. var shellStream = CreateShellStream();
  96. _channelSessionMock.Setup(p => p.Dispose());
  97. shellStream.Dispose();
  98. var bytes = _encoding.GetBytes("Hello World!");
  99. Assert.ThrowsException<ObjectDisposedException>(() => shellStream.Write(bytes, 0, bytes.Length));
  100. }
  101. private ShellStream CreateShellStream()
  102. {
  103. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  104. _connectionInfoMock.Setup(p => p.Encoding).Returns(_encoding);
  105. _sessionMock.Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  106. _channelSessionMock.Setup(p => p.Open());
  107. _channelSessionMock.Setup(p => p.SendPseudoTerminalRequest(_terminalName, _widthColumns, _heightRows,
  108. _widthPixels, _heightPixels, _terminalModes)).Returns(true);
  109. _channelSessionMock.Setup(p => p.SendShellRequest()).Returns(true);
  110. return new ShellStream(_sessionMock.Object,
  111. _terminalName,
  112. _widthColumns,
  113. _heightRows,
  114. _widthPixels,
  115. _heightPixels,
  116. _terminalModes,
  117. _bufferSize);
  118. }
  119. }
  120. }