ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Abstractions;
  8. using Renci.SshNet.Channels;
  9. using Renci.SshNet.Common;
  10. namespace Renci.SshNet.Tests.Classes
  11. {
  12. [TestClass]
  13. public class ShellStreamTest_Write_WriteBufferFullAndWriteZeroBytes
  14. {
  15. private Mock<ISession> _sessionMock;
  16. private Mock<IConnectionInfo> _connectionInfoMock;
  17. private Mock<IChannelSession> _channelSessionMock;
  18. private string _terminalName;
  19. private uint _widthColumns;
  20. private uint _heightRows;
  21. private uint _widthPixels;
  22. private uint _heightPixels;
  23. private Dictionary<TerminalModes, uint> _terminalModes;
  24. private ShellStream _shellStream;
  25. private int _bufferSize;
  26. private byte[] _data;
  27. private int _offset;
  28. private int _count;
  29. private MockSequence _mockSequence;
  30. private byte[] _bufferData;
  31. [TestInitialize]
  32. public void Initialize()
  33. {
  34. Arrange();
  35. Act();
  36. }
  37. private void SetupData()
  38. {
  39. var random = new Random();
  40. _terminalName = random.Next().ToString();
  41. _widthColumns = (uint)random.Next();
  42. _heightRows = (uint)random.Next();
  43. _widthPixels = (uint)random.Next();
  44. _heightPixels = (uint)random.Next();
  45. _terminalModes = new Dictionary<TerminalModes, uint>();
  46. _bufferSize = random.Next(100, 1000);
  47. _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize);
  48. _data = new byte[0];
  49. _offset = 0;
  50. _count = _data.Length;
  51. }
  52. private void CreateMocks()
  53. {
  54. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  55. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  56. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  57. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  58. }
  59. private void SetupMocks()
  60. {
  61. _mockSequence = new MockSequence();
  62. _sessionMock.InSequence(_mockSequence)
  63. .Setup(p => p.ConnectionInfo)
  64. .Returns(_connectionInfoMock.Object);
  65. _connectionInfoMock.InSequence(_mockSequence)
  66. .Setup(p => p.Encoding)
  67. .Returns(new UTF8Encoding());
  68. _sessionMock.InSequence(_mockSequence)
  69. .Setup(p => p.CreateChannelSession())
  70. .Returns(_channelSessionMock.Object);
  71. _channelSessionMock.InSequence(_mockSequence)
  72. .Setup(p => p.Open());
  73. _channelSessionMock.InSequence(_mockSequence)
  74. .Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  75. _widthColumns,
  76. _heightRows,
  77. _widthPixels,
  78. _heightPixels,
  79. _terminalModes))
  80. .Returns(true);
  81. _channelSessionMock.InSequence(_mockSequence)
  82. .Setup(p => p.SendShellRequest())
  83. .Returns(true);
  84. }
  85. private void Arrange()
  86. {
  87. SetupData();
  88. CreateMocks();
  89. SetupMocks();
  90. _shellStream = new ShellStream(_sessionMock.Object,
  91. _terminalName,
  92. _widthColumns,
  93. _heightRows,
  94. _widthPixels,
  95. _heightPixels,
  96. _terminalModes,
  97. _bufferSize);
  98. _shellStream.Write(_bufferData, 0, _bufferData.Length);
  99. }
  100. private void Act()
  101. {
  102. _shellStream.Write(_data, _offset, _count);
  103. }
  104. [TestMethod]
  105. public void NoDataShouldBeSentToServer()
  106. {
  107. _channelSessionMock.VerifyAll();
  108. }
  109. [TestMethod]
  110. public void FlushShouldSendBufferToServer()
  111. {
  112. _ = _channelSessionMock.InSequence(_mockSequence)
  113. .Setup(p => p.SendData(
  114. It.Is<byte[]>(data => data.Take(_bufferData.Length).IsEqualTo(_bufferData)),
  115. 0,
  116. _bufferData.Length));
  117. _shellStream.Flush();
  118. _channelSessionMock.VerifyAll();
  119. }
  120. }
  121. }