SshClientTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Send_InputStream_to_Command()
  23. {
  24. var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
  25. // Make the server echo back the input file with "cat"
  26. using (var command = _sshClient.CreateCommand("cat"))
  27. {
  28. var asyncResult = command.BeginExecute();
  29. using (var inputStream = command.CreateInputStream())
  30. {
  31. inputStream.Write(inputByteArray, 0, inputByteArray.Length);
  32. }
  33. command.EndExecute(asyncResult);
  34. Assert.AreEqual("Hello world!", command.Result);
  35. Assert.AreEqual(string.Empty, command.Error);
  36. }
  37. }
  38. [TestMethod]
  39. public void Send_InputStream_to_Command_OneByteAtATime()
  40. {
  41. var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
  42. // Make the server echo back the input file with "cat"
  43. using (var command = _sshClient.CreateCommand("cat"))
  44. {
  45. var asyncResult = command.BeginExecute();
  46. using (var inputStream = command.CreateInputStream())
  47. {
  48. for (var i = 0; i < inputByteArray.Length; i++)
  49. {
  50. inputStream.WriteByte(inputByteArray[i]);
  51. }
  52. }
  53. command.EndExecute(asyncResult);
  54. Assert.AreEqual("Hello world!", command.Result);
  55. Assert.AreEqual(string.Empty, command.Error);
  56. }
  57. }
  58. [TestMethod]
  59. public void Send_InputStream_to_Command_InputStreamNotInUsingBlock_StillWorks()
  60. {
  61. var inputByteArray = Encoding.UTF8.GetBytes("Hello world!");
  62. // Make the server echo back the input file with "cat"
  63. using (var command = _sshClient.CreateCommand("cat"))
  64. {
  65. var asyncResult = command.BeginExecute();
  66. var inputStream = command.CreateInputStream();
  67. for (var i = 0; i < inputByteArray.Length; i++)
  68. {
  69. inputStream.WriteByte(inputByteArray[i]);
  70. }
  71. command.EndExecute(asyncResult);
  72. Assert.AreEqual("Hello world!", command.Result);
  73. Assert.AreEqual(string.Empty, command.Error);
  74. }
  75. }
  76. [TestMethod]
  77. public void CreateInputStream_BeforeBeginExecute_ThrowsInvalidOperationException()
  78. {
  79. var command = _sshClient.CreateCommand("ls");
  80. Assert.ThrowsException<InvalidOperationException>(command.CreateInputStream);
  81. }
  82. [TestMethod]
  83. public void CreateInputStream_AfterEndExecute_ThrowsInvalidOperationException()
  84. {
  85. var command = _sshClient.CreateCommand("ls");
  86. var asyncResult = command.BeginExecute();
  87. command.EndExecute(asyncResult);
  88. Assert.ThrowsException<InvalidOperationException>(command.CreateInputStream);
  89. }
  90. public void Dispose()
  91. {
  92. _sshClient.Disconnect();
  93. _sshClient.Dispose();
  94. }
  95. }
  96. }