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+
10+ namespace Renci . SshNet . Tests . Classes
11+ {
12+ [ TestClass ]
13+ public class ShellStreamTest_Write_WriteBufferEmptyAndWriteLessBytesThanBufferSize
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+
27+ private byte [ ] _data ;
28+ private int _offset ;
29+ private int _count ;
30+ private MockSequence _mockSequence ;
31+
32+ [ TestInitialize ]
33+ public void Initialize ( )
34+ {
35+ Arrange ( ) ;
36+ Act ( ) ;
37+ }
38+
39+ private void SetupData ( )
40+ {
41+ var random = new Random ( ) ;
42+
43+ _terminalName = random . Next ( ) . ToString ( ) ;
44+ _widthColumns = ( uint ) random . Next ( ) ;
45+ _heightRows = ( uint ) random . Next ( ) ;
46+ _widthPixels = ( uint ) random . Next ( ) ;
47+ _heightPixels = ( uint ) random . Next ( ) ;
48+ _terminalModes = new Dictionary < TerminalModes , uint > ( ) ;
49+ _bufferSize = random . Next ( 100 , 1000 ) ;
50+
51+ _data = CryptoAbstraction . GenerateRandom ( _bufferSize - 10 ) ;
52+ _offset = random . Next ( 1 , 5 ) ;
53+ _count = _data . Length - _offset - random . Next ( 1 , 10 ) ;
54+ }
55+
56+ private void CreateMocks ( )
57+ {
58+ _sessionMock = new Mock < ISession > ( MockBehavior . Strict ) ;
59+ _connectionInfoMock = new Mock < IConnectionInfo > ( MockBehavior . Strict ) ;
60+ _channelSessionMock = new Mock < IChannelSession > ( MockBehavior . Strict ) ;
61+ }
62+
63+ private void SetupMocks ( )
64+ {
65+ _mockSequence = new MockSequence ( ) ;
66+
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+ }
90+
91+ private void Arrange ( )
92+ {
93+ SetupData ( ) ;
94+ CreateMocks ( ) ;
95+ SetupMocks ( ) ;
96+
97+ _shellStream = new ShellStream ( _sessionMock . Object ,
98+ _terminalName ,
99+ _widthColumns ,
100+ _heightRows ,
101+ _widthPixels ,
102+ _heightPixels ,
103+ _terminalModes ,
104+ _bufferSize ) ;
105+ }
106+
107+ private void Act ( )
108+ {
109+ _shellStream . Write ( _data , _offset , _count ) ;
110+ }
111+
112+ [ TestMethod ]
113+ public void NoDataShouldBeSentToServer ( )
114+ {
115+ _channelSessionMock . Verify ( p => p . SendData ( It . IsAny < byte [ ] > ( ) ) , Times . Never ) ;
116+ }
117+
118+ [ TestMethod ]
119+ public void FlushShouldSendWrittenBytesToServer ( )
120+ {
121+ byte [ ] bytesSent = null ;
122+
123+ _channelSessionMock . InSequence ( _mockSequence )
124+ . Setup ( p => p . SendData ( It . IsAny < byte [ ] > ( ) ) )
125+ . Callback < byte [ ] > ( data => bytesSent = data ) ;
126+
127+ _shellStream . Flush ( ) ;
128+
129+ Assert . IsNotNull ( bytesSent ) ;
130+ Assert . IsTrue ( _data . Take ( _offset , _count ) . IsEqualTo ( bytesSent ) ) ;
131+
132+ _channelSessionMock . Verify ( p => p . SendData ( It . IsAny < byte [ ] > ( ) ) , Times . Once ) ;
133+ }
134+ }
135+ }
0 commit comments