2
0

ShellStreamTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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));
  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,
  99. _terminalName,
  100. _widthColumns,
  101. _heightRows,
  102. _widthPixels,
  103. _heightPixels,
  104. _terminalModes,
  105. _bufferSize);
  106. }
  107. }
  108. }