mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-18 14:36:11 +08:00
03ae6bfa0f
* Bump the dependencies group with 4 updates Bumps BouncyCastle.Cryptography from 2.6.1 to 2.6.2 Bumps Meziantou.Analyzer from 2.0.205 to 2.0.210 Bumps MSTest from 3.9.3 to 3.10.0 Bumps SonarAnalyzer.CSharp from 10.13.0.120203 to 10.15.0.120848 Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Rob Hague <rob.hague00@gmail.com>
119 lines
3.7 KiB
C#
119 lines
3.7 KiB
C#
namespace Renci.SshNet.IntegrationTests
|
|
{
|
|
/// <summary>
|
|
/// The SSH client integration tests
|
|
/// </summary>
|
|
[TestClass]
|
|
public class SshClientTests : IntegrationTestBase, IDisposable
|
|
{
|
|
private readonly SshClient _sshClient;
|
|
|
|
public SshClientTests()
|
|
{
|
|
_sshClient = new SshClient(SshServerHostName, SshServerPort, User.UserName, User.Password);
|
|
_sshClient.Connect();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Echo_Command_with_all_characters()
|
|
{
|
|
var response = _sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'");
|
|
|
|
Assert.AreEqual("test !@#$%^&*()_+{}:,./<>[];\\|\n", response.Result);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_BigCommand()
|
|
{
|
|
using var command = _sshClient.CreateCommand("head -c 10000000 /dev/urandom | base64"); // 10MB of data please
|
|
|
|
var asyncResult = command.BeginExecute();
|
|
|
|
long totalBytesRead = 0;
|
|
int bytesRead;
|
|
byte[] buffer = new byte[4096];
|
|
|
|
while ((bytesRead = command.OutputStream.Read(buffer, 0, buffer.Length)) != 0)
|
|
{
|
|
totalBytesRead += bytesRead;
|
|
}
|
|
|
|
var result = command.EndExecute(asyncResult);
|
|
|
|
Assert.AreEqual(13_508_775, totalBytesRead);
|
|
Assert.AreEqual(0, result.Length);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Send_InputStream_to_Command()
|
|
{
|
|
var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
|
|
|
|
// Make the server echo back the input file with "cat"
|
|
using (var command = _sshClient.CreateCommand("cat"))
|
|
{
|
|
var asyncResult = command.BeginExecute();
|
|
|
|
using (var inputStream = command.CreateInputStream())
|
|
{
|
|
inputStream.Write(inputByteArray, 0, inputByteArray.Length);
|
|
}
|
|
|
|
command.EndExecute(asyncResult);
|
|
|
|
Assert.AreEqual("Hello world!", command.Result);
|
|
Assert.AreEqual(string.Empty, command.Error);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Send_InputStream_to_Command_OneByteAtATime()
|
|
{
|
|
var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
|
|
|
|
// Make the server echo back the input file with "cat"
|
|
using (var command = _sshClient.CreateCommand("cat"))
|
|
{
|
|
var asyncResult = command.BeginExecute();
|
|
|
|
using (var inputStream = command.CreateInputStream())
|
|
{
|
|
for (var i = 0; i < inputByteArray.Length; i++)
|
|
{
|
|
inputStream.WriteByte(inputByteArray[i]);
|
|
}
|
|
}
|
|
|
|
command.EndExecute(asyncResult);
|
|
|
|
Assert.AreEqual("Hello world!", command.Result);
|
|
Assert.AreEqual(string.Empty, command.Error);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CreateInputStream_BeforeBeginExecute_ThrowsInvalidOperationException()
|
|
{
|
|
var command = _sshClient.CreateCommand("ls");
|
|
|
|
Assert.ThrowsExactly<InvalidOperationException>(command.CreateInputStream);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CreateInputStream_AfterEndExecute_ThrowsInvalidOperationException()
|
|
{
|
|
var command = _sshClient.CreateCommand("ls");
|
|
var asyncResult = command.BeginExecute();
|
|
command.EndExecute(asyncResult);
|
|
|
|
Assert.ThrowsExactly<InvalidOperationException>(command.CreateInputStream);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_sshClient.Disconnect();
|
|
_sshClient.Dispose();
|
|
}
|
|
}
|
|
}
|