2
0

SshClientTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. namespace Renci.SshNet.IntegrationTests
  2. {
  3. /// <summary>
  4. /// The SSH client integration tests
  5. /// </summary>
  6. [TestClass]
  7. public class SshClientTests : IntegrationTestBase, IDisposable
  8. {
  9. private readonly SshClient _sshClient;
  10. public SshClientTests()
  11. {
  12. _sshClient = new SshClient(SshServerHostName, SshServerPort, User.UserName, User.Password);
  13. _sshClient.Connect();
  14. }
  15. [TestMethod]
  16. public void Echo_Command_with_all_characters()
  17. {
  18. var response = _sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'");
  19. Assert.AreEqual("test !@#$%^&*()_+{}:,./<>[];\\|\n", response.Result);
  20. }
  21. [TestMethod]
  22. public void Test_BigCommand()
  23. {
  24. using var command = _sshClient.CreateCommand("head -c 10000000 /dev/urandom | base64"); // 10MB of data please
  25. var asyncResult = command.BeginExecute();
  26. long totalBytesRead = 0;
  27. int bytesRead;
  28. byte[] buffer = new byte[4096];
  29. while ((bytesRead = command.OutputStream.Read(buffer, 0, buffer.Length)) != 0)
  30. {
  31. totalBytesRead += bytesRead;
  32. }
  33. var result = command.EndExecute(asyncResult);
  34. Assert.AreEqual(13_508_775, totalBytesRead);
  35. Assert.AreEqual(0, result.Length);
  36. }
  37. [TestMethod]
  38. public void Send_InputStream_to_Command()
  39. {
  40. var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
  41. // Make the server echo back the input file with "cat"
  42. using (var command = _sshClient.CreateCommand("cat"))
  43. {
  44. var asyncResult = command.BeginExecute();
  45. using (var inputStream = command.CreateInputStream())
  46. {
  47. inputStream.Write(inputByteArray, 0, inputByteArray.Length);
  48. }
  49. command.EndExecute(asyncResult);
  50. Assert.AreEqual("Hello world!", command.Result);
  51. Assert.AreEqual(string.Empty, command.Error);
  52. }
  53. }
  54. [TestMethod]
  55. public void Send_InputStream_to_Command_OneByteAtATime()
  56. {
  57. var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
  58. // Make the server echo back the input file with "cat"
  59. using (var command = _sshClient.CreateCommand("cat"))
  60. {
  61. var asyncResult = command.BeginExecute();
  62. using (var inputStream = command.CreateInputStream())
  63. {
  64. for (var i = 0; i < inputByteArray.Length; i++)
  65. {
  66. inputStream.WriteByte(inputByteArray[i]);
  67. }
  68. }
  69. command.EndExecute(asyncResult);
  70. Assert.AreEqual("Hello world!", command.Result);
  71. Assert.AreEqual(string.Empty, command.Error);
  72. }
  73. }
  74. [TestMethod]
  75. public void Send_InputStream_to_Command_InputStreamNotInUsingBlock_StillWorks()
  76. {
  77. var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
  78. // Make the server echo back the input file with "cat"
  79. using (var command = _sshClient.CreateCommand("cat"))
  80. {
  81. var asyncResult = command.BeginExecute();
  82. var inputStream = command.CreateInputStream();
  83. for (var i = 0; i < inputByteArray.Length; i++)
  84. {
  85. inputStream.WriteByte(inputByteArray[i]);
  86. }
  87. command.EndExecute(asyncResult);
  88. Assert.AreEqual("Hello world!", command.Result);
  89. Assert.AreEqual(string.Empty, command.Error);
  90. }
  91. }
  92. [TestMethod]
  93. public void CreateInputStream_BeforeBeginExecute_ThrowsInvalidOperationException()
  94. {
  95. var command = _sshClient.CreateCommand("ls");
  96. Assert.ThrowsException<InvalidOperationException>(command.CreateInputStream);
  97. }
  98. [TestMethod]
  99. public void CreateInputStream_AfterEndExecute_ThrowsInvalidOperationException()
  100. {
  101. var command = _sshClient.CreateCommand("ls");
  102. var asyncResult = command.BeginExecute();
  103. command.EndExecute(asyncResult);
  104. Assert.ThrowsException<InvalidOperationException>(command.CreateInputStream);
  105. }
  106. public void Dispose()
  107. {
  108. _sshClient.Disconnect();
  109. _sshClient.Dispose();
  110. }
  111. }
  112. }