Added more tests for PseudoTerminalRequestInfo.

Fixed generating pty-ref with zero terminal modes.
This commit is contained in:
drieseng
2016-06-23 21:41:50 +02:00
parent 3143e14308
commit b79c63012d
2 changed files with 134 additions and 24 deletions
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Connection;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Messages.Connection
{
@@ -14,14 +12,14 @@ namespace Renci.SshNet.Tests.Classes.Messages.Connection
/// Represents "pty-req" type channel request information
/// </summary>
[TestClass]
public class PseudoTerminalRequestInfoTest : TestBase
public class PseudoTerminalRequestInfoTest
{
private string environmentVariable;
private uint columns;
private uint rows;
private uint width;
private uint height;
IDictionary<TerminalModes, uint> terminalModeValues;
private string _environmentVariable;
private uint _columns;
private uint _rows;
private uint _width;
private uint _height;
private IDictionary<TerminalModes, uint> _terminalModeValues;
private byte[] _environmentVariableBytes;
[TestInitialize]
@@ -29,21 +27,60 @@ namespace Renci.SshNet.Tests.Classes.Messages.Connection
{
var random = new Random();
environmentVariable = random.Next().ToString(CultureInfo.InvariantCulture);
columns = (uint) random.Next(0, int.MaxValue);
rows = (uint) random.Next(0, int.MaxValue);
width = (uint) random.Next(0, int.MaxValue);
height = (uint) random.Next(0, int.MaxValue);
terminalModeValues = new Dictionary<TerminalModes, uint>();
_environmentVariable = random.Next().ToString(CultureInfo.InvariantCulture);
_environmentVariableBytes = Encoding.UTF8.GetBytes(_environmentVariable);
_columns = (uint) random.Next(0, int.MaxValue);
_rows = (uint) random.Next(0, int.MaxValue);
_width = (uint) random.Next(0, int.MaxValue);
_height = (uint) random.Next(0, int.MaxValue);
_terminalModeValues = new Dictionary<TerminalModes, uint>
{
{TerminalModes.CS8, 433},
{TerminalModes.ECHO, 566}
};
}
[TestMethod]
public void GetBytes()
{
var target = new PseudoTerminalRequestInfo(_environmentVariable, _columns, _rows, _width, _height, _terminalModeValues);
_environmentVariableBytes = Encoding.UTF8.GetBytes(environmentVariable);
var bytes = target.GetBytes();
var expectedBytesLength = 1; // WantReply
expectedBytesLength += 4; // EnvironmentVariable length
expectedBytesLength += _environmentVariableBytes.Length; // EnvironmentVariable
expectedBytesLength += 4; // Columns
expectedBytesLength += 4; // Rows
expectedBytesLength += 4; // PixelWidth
expectedBytesLength += 4; // PixelHeight
expectedBytesLength += 4; // Length of "encoded terminal modes"
expectedBytesLength += _terminalModeValues.Count*(1 + 4) + 1; // encoded terminal modes
Assert.AreEqual(expectedBytesLength, bytes.Length);
var sshDataStream = new SshDataStream(bytes);
Assert.AreEqual(1, sshDataStream.ReadByte()); // WantReply
Assert.AreEqual(_environmentVariable, sshDataStream.ReadString(Encoding.UTF8));
Assert.AreEqual(_columns, sshDataStream.ReadUInt32());
Assert.AreEqual(_rows, sshDataStream.ReadUInt32());
Assert.AreEqual(_width, sshDataStream.ReadUInt32());
Assert.AreEqual(_height, sshDataStream.ReadUInt32());
Assert.AreEqual((uint) (_terminalModeValues.Count * (1 + 4) + 1), sshDataStream.ReadUInt32());
Assert.AreEqual((int) TerminalModes.CS8, sshDataStream.ReadByte());
Assert.AreEqual(_terminalModeValues[TerminalModes.CS8], sshDataStream.ReadUInt32());
Assert.AreEqual((int) TerminalModes.ECHO, sshDataStream.ReadByte());
Assert.AreEqual(_terminalModeValues[TerminalModes.ECHO], sshDataStream.ReadUInt32());
Assert.AreEqual((int) TerminalModes.TTY_OP_END, sshDataStream.ReadByte());
Assert.IsTrue(sshDataStream.IsEndOfData);
}
[TestMethod]
public void GetBytes_TerminalModeValues_Null()
{
var target = new PseudoTerminalRequestInfo(environmentVariable, columns, rows, width, height, null);
var target = new PseudoTerminalRequestInfo(_environmentVariable, _columns, _rows, _width, _height, null);
var bytes = target.GetBytes();
@@ -61,14 +98,86 @@ namespace Renci.SshNet.Tests.Classes.Messages.Connection
var sshDataStream = new SshDataStream(bytes);
Assert.AreEqual(1, sshDataStream.ReadByte()); // WantReply
Assert.AreEqual(environmentVariable, sshDataStream.ReadString(Encoding.UTF8));
Assert.AreEqual(columns, sshDataStream.ReadUInt32());
Assert.AreEqual(rows, sshDataStream.ReadUInt32());
Assert.AreEqual(width, sshDataStream.ReadUInt32());
Assert.AreEqual(height, sshDataStream.ReadUInt32());
Assert.AreEqual(0, sshDataStream.ReadUInt32());
Assert.AreEqual(_environmentVariable, sshDataStream.ReadString(Encoding.UTF8));
Assert.AreEqual(_columns, sshDataStream.ReadUInt32());
Assert.AreEqual(_rows, sshDataStream.ReadUInt32());
Assert.AreEqual(_width, sshDataStream.ReadUInt32());
Assert.AreEqual(_height, sshDataStream.ReadUInt32());
Assert.AreEqual((uint) 0, sshDataStream.ReadUInt32());
Assert.IsTrue(sshDataStream.IsEndOfData);
}
[TestMethod]
public void GetBytes_TerminalModeValues_Empty()
{
var target = new PseudoTerminalRequestInfo(_environmentVariable,
_columns,
_rows,
_width,
_height,
new Dictionary<TerminalModes, uint>());
var bytes = target.GetBytes();
var expectedBytesLength = 1; // WantReply
expectedBytesLength += 4; // EnvironmentVariable length
expectedBytesLength += _environmentVariableBytes.Length; // EnvironmentVariable
expectedBytesLength += 4; // Columns
expectedBytesLength += 4; // Rows
expectedBytesLength += 4; // PixelWidth
expectedBytesLength += 4; // PixelHeight
expectedBytesLength += 4; // Length of "encoded terminal modes"
Assert.AreEqual(expectedBytesLength, bytes.Length);
var sshDataStream = new SshDataStream(bytes);
Assert.AreEqual(1, sshDataStream.ReadByte()); // WantReply
Assert.AreEqual(_environmentVariable, sshDataStream.ReadString(Encoding.UTF8));
Assert.AreEqual(_columns, sshDataStream.ReadUInt32());
Assert.AreEqual(_rows, sshDataStream.ReadUInt32());
Assert.AreEqual(_width, sshDataStream.ReadUInt32());
Assert.AreEqual(_height, sshDataStream.ReadUInt32());
Assert.AreEqual((uint) 0, sshDataStream.ReadUInt32());
Assert.IsTrue(sshDataStream.IsEndOfData);
}
[TestMethod]
public void DefaultCtor()
{
var ptyReq = new PseudoTerminalRequestInfo();
Assert.IsTrue(ptyReq.WantReply);
Assert.AreEqual(uint.MinValue, ptyReq.Columns);
Assert.IsNull(ptyReq.EnvironmentVariable);
Assert.AreEqual("pty-req", ptyReq.RequestName);
Assert.AreEqual(uint.MinValue, ptyReq.PixelHeight);
Assert.AreEqual(uint.MinValue, ptyReq.PixelWidth);
Assert.AreEqual(uint.MinValue, ptyReq.Rows);
Assert.IsNull(ptyReq.TerminalModeValues);
}
[TestMethod]
public void FullCtor()
{
var ptyReq = new PseudoTerminalRequestInfo(_environmentVariable, _columns, _rows, _width, _height, _terminalModeValues);
Assert.IsTrue(ptyReq.WantReply);
Assert.AreEqual(_columns, ptyReq.Columns);
Assert.AreSame(_environmentVariable, ptyReq.EnvironmentVariable);
Assert.AreEqual("pty-req", ptyReq.RequestName);
Assert.AreEqual(_height, ptyReq.PixelHeight);
Assert.AreEqual(_width, ptyReq.PixelWidth);
Assert.AreEqual(_rows, ptyReq.Rows);
Assert.AreSame(_terminalModeValues, ptyReq.TerminalModeValues);
}
[TestMethod]
public void NameShouldReturnPtyReq()
{
Assert.AreEqual("pty-req", PseudoTerminalRequestInfo.Name);
}
}
}
@@ -135,7 +135,7 @@ namespace Renci.SshNet.Messages.Connection
Write(PixelWidth);
Write(PixelHeight);
if (TerminalModeValues != null)
if (TerminalModeValues != null && TerminalModeValues.Count > 0)
{
// write total length of encoded terminal modes, which is 1 bytes for the opcode / terminal mode
// and 4 bytes for the uint argument for each entry; the encoded terminal modes are terminated by
@@ -152,6 +152,7 @@ namespace Renci.SshNet.Messages.Connection
}
else
{
// when there are no terminal mode, the length of the string is zero
Write((uint) 0);
}
}