2
0

ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Abstractions;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Common;
  9. using Renci.SshNet.Tests.Common;
  10. namespace Renci.SshNet.Tests.Classes
  11. {
  12. [TestClass]
  13. public class ShellStreamTest_Write_WriteBufferNotEmptyAndWriteMoreBytesThanBufferCanContain
  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 int _expectSize;
  27. private byte[] _data;
  28. private int _offset;
  29. private int _count;
  30. private MockSequence _mockSequence;
  31. private byte[] _bufferData;
  32. private byte[] _expectedBytesSent;
  33. [TestInitialize]
  34. public void Initialize()
  35. {
  36. Arrange();
  37. Act();
  38. }
  39. private void SetupData()
  40. {
  41. var random = new Random();
  42. _terminalName = random.Next().ToString();
  43. _widthColumns = (uint) random.Next();
  44. _heightRows = (uint) random.Next();
  45. _widthPixels = (uint) random.Next();
  46. _heightPixels = (uint) random.Next();
  47. _terminalModes = new Dictionary<TerminalModes, uint>();
  48. _bufferSize = random.Next(100, 1000);
  49. _expectSize = random.Next(100, _bufferSize);
  50. _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60);
  51. _data = CryptoAbstraction.GenerateRandom(_bufferSize - _bufferData.Length + random.Next(1, 10));
  52. _offset = 0;
  53. _count = _data.Length;
  54. _expectedBytesSent = new ArrayBuilder<byte>().Add(_bufferData)
  55. .Add(_data, 0, _bufferSize - _bufferData.Length)
  56. .Build();
  57. }
  58. private void CreateMocks()
  59. {
  60. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  61. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  62. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  63. }
  64. private void SetupMocks()
  65. {
  66. _mockSequence = new MockSequence();
  67. _sessionMock.InSequence(_mockSequence)
  68. .Setup(p => p.ConnectionInfo)
  69. .Returns(_connectionInfoMock.Object);
  70. _connectionInfoMock.InSequence(_mockSequence)
  71. .Setup(p => p.Encoding)
  72. .Returns(new UTF8Encoding());
  73. _sessionMock.InSequence(_mockSequence)
  74. .Setup(p => p.CreateChannelSession())
  75. .Returns(_channelSessionMock.Object);
  76. _channelSessionMock.InSequence(_mockSequence)
  77. .Setup(p => p.Open());
  78. _channelSessionMock.InSequence(_mockSequence)
  79. .Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  80. _widthColumns,
  81. _heightRows,
  82. _widthPixels,
  83. _heightPixels,
  84. _terminalModes))
  85. .Returns(true);
  86. _channelSessionMock.InSequence(_mockSequence)
  87. .Setup(p => p.SendShellRequest())
  88. .Returns(true);
  89. _channelSessionMock.InSequence(_mockSequence)
  90. .Setup(p => p.SendData(_expectedBytesSent));
  91. }
  92. private void Arrange()
  93. {
  94. SetupData();
  95. CreateMocks();
  96. SetupMocks();
  97. _shellStream = new ShellStream(_sessionMock.Object,
  98. _terminalName,
  99. _widthColumns,
  100. _heightRows,
  101. _widthPixels,
  102. _heightPixels,
  103. _terminalModes,
  104. _bufferSize,
  105. _expectSize);
  106. _shellStream.Write(_bufferData, 0, _bufferData.Length);
  107. }
  108. private void Act()
  109. {
  110. _shellStream.Write(_data, _offset, _count);
  111. }
  112. [TestMethod]
  113. public void BufferShouldBeSentToServer()
  114. {
  115. _channelSessionMock.Verify(p => p.SendData(_expectedBytesSent), Times.Once);
  116. }
  117. [TestMethod]
  118. public void FlushShouldSendRemainingBytesInBufferToServer()
  119. {
  120. var expectedBytesSent = _data.Take(_bufferSize - _bufferData.Length, _data.Length + _bufferData.Length - _bufferSize);
  121. byte[] actualBytesSent = null;
  122. _channelSessionMock.InSequence(_mockSequence)
  123. .Setup(p => p.SendData(It.IsAny<byte[]>()))
  124. .Callback<byte[]>(data => actualBytesSent = data);
  125. _shellStream.Flush();
  126. Assert.IsNotNull(actualBytesSent);
  127. Assert.AreEqual(expectedBytesSent.Length, actualBytesSent.Length);
  128. Assert.IsTrue(expectedBytesSent.IsEqualTo(actualBytesSent));
  129. _channelSessionMock.Verify(p => p.SendData(It.IsAny<byte[]>()), Times.Exactly(2));
  130. }
  131. }
  132. }