2
0

ShellStreamTest_Write_WriteBufferNotEmptyAndWriteLessBytesThanBufferCanContain.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_WriteBufferNotEmptyAndWriteLessBytesThanBufferCanContain
  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. private byte[] _bufferData;
  30. [TestInitialize]
  31. public void Initialize()
  32. {
  33. Arrange();
  34. Act();
  35. }
  36. private void SetupData()
  37. {
  38. var random = new Random();
  39. _terminalName = random.Next().ToString();
  40. _widthColumns = (uint) random.Next();
  41. _heightRows = (uint) random.Next();
  42. _widthPixels = (uint) random.Next();
  43. _heightPixels = (uint) random.Next();
  44. _terminalModes = new Dictionary<TerminalModes, uint>();
  45. _bufferSize = random.Next(100, 1000);
  46. _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60);
  47. _data = CryptoAbstraction.GenerateRandom(_bufferSize + 100);
  48. _offset = 0;
  49. _count = _bufferSize - _bufferData.Length - random.Next(1, 10);
  50. }
  51. private void CreateMocks()
  52. {
  53. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  54. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  55. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  56. }
  57. private void SetupMocks()
  58. {
  59. _mockSequence = new MockSequence();
  60. _sessionMock.InSequence(_mockSequence)
  61. .Setup(p => p.ConnectionInfo)
  62. .Returns(_connectionInfoMock.Object);
  63. _connectionInfoMock.InSequence(_mockSequence)
  64. .Setup(p => p.Encoding)
  65. .Returns(new UTF8Encoding());
  66. _sessionMock.InSequence(_mockSequence)
  67. .Setup(p => p.CreateChannelSession())
  68. .Returns(_channelSessionMock.Object);
  69. _channelSessionMock.InSequence(_mockSequence)
  70. .Setup(p => p.Open());
  71. _channelSessionMock.InSequence(_mockSequence)
  72. .Setup(p => p.SendPseudoTerminalRequest(_terminalName,
  73. _widthColumns,
  74. _heightRows,
  75. _widthPixels,
  76. _heightPixels,
  77. _terminalModes))
  78. .Returns(true);
  79. _channelSessionMock.InSequence(_mockSequence)
  80. .Setup(p => p.SendShellRequest())
  81. .Returns(true);
  82. }
  83. private void Arrange()
  84. {
  85. SetupData();
  86. CreateMocks();
  87. SetupMocks();
  88. _shellStream = new ShellStream(_sessionMock.Object,
  89. _terminalName,
  90. _widthColumns,
  91. _heightRows,
  92. _widthPixels,
  93. _heightPixels,
  94. _terminalModes,
  95. _bufferSize);
  96. _shellStream.Write(_bufferData, 0, _bufferData.Length);
  97. }
  98. private void Act()
  99. {
  100. _shellStream.Write(_data, _offset, _count);
  101. }
  102. [TestMethod]
  103. public void NoDataShouldBeSentToServer()
  104. {
  105. _channelSessionMock.VerifyAll();
  106. }
  107. [TestMethod]
  108. public void FlushShouldSendWrittenBytesToServer()
  109. {
  110. byte[] bytesSent = null;
  111. _channelSessionMock.InSequence(_mockSequence)
  112. .Setup(p => p.SendData(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
  113. .Callback<byte[], int, int>((data, offset, count) => bytesSent = data.Take(offset, count));
  114. _shellStream.Flush();
  115. Assert.IsNotNull(bytesSent);
  116. Assert.AreEqual(_bufferData.Length + _count, bytesSent.Length);
  117. Assert.IsTrue(_bufferData.IsEqualTo(bytesSent.Take(_bufferData.Length)));
  118. Assert.IsTrue(_data.Take(0, _count).IsEqualTo(bytesSent.Take(_bufferData.Length, _count)));
  119. _channelSessionMock.VerifyAll();
  120. }
  121. }
  122. }