ShellStreamTest_Write_WriteBufferEmptyAndWriteZeroBytes.cs 4.7 KB

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