forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellStreamTest.cs
More file actions
170 lines (136 loc) · 6.15 KB
/
ShellStreamTest.cs
File metadata and controls
170 lines (136 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes
{
/// <summary>
/// Contains operation for working with SSH Shell.
/// </summary>
[TestClass]
public class ShellStreamTest : TestBase
{
private Mock<ISession> _sessionMock;
private Mock<IConnectionInfo> _connectionInfoMock;
private Encoding _encoding;
private string _terminalName;
private uint _widthColumns;
private uint _heightRows;
private uint _widthPixels;
private uint _heightPixels;
private Dictionary<TerminalModes, uint> _terminalModes;
private int _bufferSize;
private Mock<IChannelSession> _channelSessionMock;
protected override void OnInit()
{
base.OnInit();
var random = new Random();
_terminalName = random.Next().ToString(CultureInfo.InvariantCulture);
_widthColumns = (uint)random.Next();
_heightRows = (uint)random.Next();
_widthPixels = (uint)random.Next();
_heightPixels = (uint)random.Next();
_terminalModes = new Dictionary<TerminalModes, uint>();
_bufferSize = random.Next(100, 500);
_encoding = Encoding.UTF8;
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
}
[TestMethod] // issue #2190
public void ReadLine_MultiByteCharacters()
{
// bash: /root/menu.sh: Отказан
const string data1 = "bash: /root/menu.sh: \u041e\u0442\u043a\u0430\u0437\u0430\u043d";
// о в доступе
const string data2 = "\u043e \u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0435";
// done
const string data3 = "done";
var shellStream = CreateShellStream();
var channelDataPublishThread = new Thread(() =>
{
_channelSessionMock.Raise(p => p.DataReceived += null,
new ChannelDataEventArgs(5, _encoding.GetBytes(data1)));
Thread.Sleep(50);
_channelSessionMock.Raise(p => p.DataReceived += null,
new ChannelDataEventArgs(5, _encoding.GetBytes(data2 + "\r\n")));
_channelSessionMock.Raise(p => p.DataReceived += null,
new ChannelDataEventArgs(5, _encoding.GetBytes(data3 + "\r\n")));
});
channelDataPublishThread.Start();
Assert.AreEqual(data1 + data2, shellStream.ReadLine());
Assert.AreEqual(data3, shellStream.ReadLine());
channelDataPublishThread.Join();
}
[TestMethod]
public void Write_Text_ShouldWriteNothingWhenTextIsNull()
{
var shellStream = CreateShellStream();
const string text = null;
shellStream.Write(text);
_channelSessionMock.VerifyAll();
}
[TestMethod]
public void WriteLine_Line_ShouldOnlyWriteLineTerminatorWhenLineIsNull()
{
var shellStream = CreateShellStream();
const string line = null;
var lineTerminator = _encoding.GetBytes("\r");
_ = _channelSessionMock.Setup(p => p.SendData(
It.Is<byte[]>(data => data.Take(lineTerminator.Length).IsEqualTo(lineTerminator)),
0,
lineTerminator.Length));
shellStream.WriteLine(line);
_channelSessionMock.VerifyAll();
}
[TestMethod]
public void Write_AfterDispose_ThrowsObjectDisposedException()
{
var shellStream = CreateShellStream();
_channelSessionMock.Setup(p => p.Dispose());
shellStream.Dispose();
var bytes = _encoding.GetBytes("Hello World!");
Assert.ThrowsException<ObjectDisposedException>(() => shellStream.Write(bytes, 0, bytes.Length));
}
[TestMethod]
public void ChangeWindowSize_AfterDispose_ThrowsObjectDisposedException()
{
var shellStream = CreateShellStream();
_channelSessionMock.Setup(p => p.Dispose());
shellStream.Dispose();
Assert.ThrowsException<ObjectDisposedException>(() => shellStream.ChangeWindowSize(80, 25, 0, 0));
}
[TestMethod]
public void ChangeWindowSize_SendsWindowChangeRequest()
{
var shellStream = CreateShellStream();
_channelSessionMock.Setup(s => s.SendWindowChangeRequest(80, 25, 0, 1));
shellStream.ChangeWindowSize(80, 25, 0, 1);
_channelSessionMock.Verify(v => v.SendWindowChangeRequest(80, 25, 0, 1), Times.Once());
}
private ShellStream CreateShellStream()
{
_sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
_connectionInfoMock.Setup(p => p.Encoding).Returns(_encoding);
_sessionMock.Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
_channelSessionMock.Setup(p => p.Open());
_channelSessionMock.Setup(p => p.SendPseudoTerminalRequest(_terminalName, _widthColumns, _heightRows,
_widthPixels, _heightPixels, _terminalModes)).Returns(true);
_channelSessionMock.Setup(p => p.SendShellRequest()).Returns(true);
return new ShellStream(_sessionMock.Object,
_terminalName,
_widthColumns,
_heightRows,
_widthPixels,
_heightPixels,
_terminalModes,
_bufferSize);
}
}
}