ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IConnectionInfo> _connectionInfoMock;
  16. private Mock<IChannelSession> _channelSessionMock;
  17. private string _terminalName;
  18. private uint _widthColumns;
  19. private uint _heightRows;
  20. private uint _widthPixels;
  21. private uint _heightPixels;
  22. private Dictionary<TerminalModes, uint> _terminalModes;
  23. private ShellStream _shellStream;
  24. private int _bufferSize;
  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. _data = CryptoAbstraction.GenerateRandom(_bufferSize - 10);
  46. _offset = random.Next(1, 5);
  47. _count = _data.Length - _offset - random.Next(1, 10);
  48. }
  49. private void CreateMocks()
  50. {
  51. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  52. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  53. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  54. }
  55. private void SetupMocks()
  56. {
  57. _mockSequence = new MockSequence();
  58. _sessionMock.InSequence(_mockSequence)
  59. .Setup(p => p.ConnectionInfo)
  60. .Returns(_connectionInfoMock.Object);
  61. _connectionInfoMock.InSequence(_mockSequence)
  62. .Setup(p => p.Encoding)
  63. .Returns(new UTF8Encoding());
  64. _sessionMock.InSequence(_mockSequence)
  65. .Setup(p => p.CreateChannelSession())
  66. .Returns(_channelSessionMock.Object);
  67. _channelSessionMock.InSequence(_mockSequence)
  68. .Setup(p => p.Open());
  69. _channelSessionMock.InSequence(_mockSequence)
  70. .Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  71. _widthColumns,
  72. _heightRows,
  73. _widthPixels,
  74. _heightPixels,
  75. _terminalModes))
  76. .Returns(true);
  77. _channelSessionMock.InSequence(_mockSequence)
  78. .Setup(p => p.SendShellRequest())
  79. .Returns(true);
  80. }
  81. private void Arrange()
  82. {
  83. SetupData();
  84. CreateMocks();
  85. SetupMocks();
  86. _shellStream = new ShellStream(_sessionMock.Object,
  87. _terminalName,
  88. _widthColumns,
  89. _heightRows,
  90. _widthPixels,
  91. _heightPixels,
  92. _terminalModes,
  93. _bufferSize);
  94. }
  95. private void Act()
  96. {
  97. _shellStream.Write(_data, _offset, _count);
  98. }
  99. [TestMethod]
  100. public void NoDataShouldBeSentToServer()
  101. {
  102. _channelSessionMock.Verify(p => p.SendData(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never);
  103. }
  104. [TestMethod]
  105. public void FlushShouldSendWrittenBytesToServer()
  106. {
  107. byte[] bytesSent = null;
  108. _channelSessionMock.InSequence(_mockSequence)
  109. .Setup(p => p.SendData(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
  110. .Callback<byte[], int, int>((data, offset, count) => bytesSent = data.Take(offset, count));
  111. _shellStream.Flush();
  112. Assert.IsNotNull(bytesSent);
  113. Assert.IsTrue(_data.Take(_offset, _count).IsEqualTo(bytesSent));
  114. _channelSessionMock.Verify(p => p.SendData(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Once);
  115. }
  116. }
  117. }