ShellTest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. namespace Renci.SshClient.Tests
  7. {
  8. /// <summary>
  9. /// Summary description for UnitTest1
  10. /// </summary>
  11. [TestClass]
  12. public class ShellTest
  13. {
  14. public ShellTest()
  15. {
  16. //
  17. // TODO: Add constructor logic here
  18. //
  19. }
  20. private TestContext testContextInstance;
  21. /// <summary>
  22. ///Gets or sets the test context which provides
  23. ///information about and functionality for the current test run.
  24. ///</summary>
  25. public TestContext TestContext
  26. {
  27. get
  28. {
  29. return testContextInstance;
  30. }
  31. set
  32. {
  33. testContextInstance = value;
  34. }
  35. }
  36. #region Additional test attributes
  37. //
  38. // You can use the following additional attributes as you write your tests:
  39. //
  40. // Use ClassInitialize to run code before running the first test in the class
  41. // [ClassInitialize()]
  42. // public static void MyClassInitialize(TestContext testContext) { }
  43. //
  44. // Use ClassCleanup to run code after all tests in a class have run
  45. // [ClassCleanup()]
  46. // public static void MyClassCleanup() { }
  47. //
  48. // Use TestInitialize to run code before running each test
  49. // [TestInitialize()]
  50. // public void MyTestInitialize() { }
  51. //
  52. // Use TestCleanup to run code after each test has run
  53. // [TestCleanup()]
  54. // public void MyTestCleanup() { }
  55. //
  56. #endregion
  57. [TestMethod]
  58. public void TestConnectUsingPassword()
  59. {
  60. var s = CreateShellUsingPassword();
  61. s.Connect();
  62. s.Disconnect();
  63. }
  64. [TestMethod]
  65. public void TestExecuteSingleCommand()
  66. {
  67. var s = CreateShellUsingPassword();
  68. s.Connect();
  69. var result = ExecuteTestCommand(s);
  70. s.Disconnect();
  71. Assert.IsTrue(result);
  72. }
  73. [TestMethod]
  74. public void TestReconnecting()
  75. {
  76. var s = CreateShellUsingPassword();
  77. s.Connect();
  78. var result = ExecuteTestCommand(s);
  79. s.Disconnect();
  80. Assert.IsTrue(result);
  81. s.Connect();
  82. result = ExecuteTestCommand(s);
  83. s.Disconnect();
  84. Assert.IsTrue(result);
  85. }
  86. [TestMethod]
  87. public void TestMultipleThreadMultipleSessions_10000()
  88. {
  89. var s = CreateShellUsingPassword();
  90. s.Connect();
  91. System.Threading.Tasks.Parallel.For(0, 10000,
  92. (counter) =>
  93. {
  94. var result = ExecuteTestCommand(s);
  95. Debug.WriteLine(string.Format("TestMultipleThreadMultipleConnections #{0}", counter));
  96. Assert.IsTrue(result);
  97. }
  98. );
  99. s.Disconnect();
  100. }
  101. [TestMethod]
  102. public void TestMultipleThreadMultipleConnections_10000()
  103. {
  104. try
  105. {
  106. System.Threading.Tasks.Parallel.For(0, 10000,
  107. () =>
  108. {
  109. var s = CreateShellUsingPassword();
  110. s.Connect();
  111. return s;
  112. },
  113. (int counter, ParallelLoopState pls, SshClient s) =>
  114. {
  115. var result = ExecuteTestCommand(s);
  116. Debug.WriteLine(string.Format("TestMultipleThreadMultipleConnections #{0}", counter));
  117. Assert.IsTrue(result);
  118. return s;
  119. },
  120. (SshClient s) =>
  121. {
  122. s.Disconnect();
  123. }
  124. );
  125. }
  126. catch (Exception exp)
  127. {
  128. Assert.Fail(exp.ToString());
  129. }
  130. }
  131. [TestMethod]
  132. public void TestExtendedOutput()
  133. {
  134. var s = CreateShellUsingPassword();
  135. s.Connect();
  136. var cmd = s.CreateCommand("echo 12345; echo 654321 >&2");
  137. cmd.Execute();
  138. var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray());
  139. s.Disconnect();
  140. Assert.AreEqual("12345\n", cmd.Result);
  141. Assert.AreEqual("654321\n", extendedData);
  142. }
  143. [TestMethod]
  144. public void TestInvalidCommandExecution()
  145. {
  146. var s = CreateShellUsingPassword();
  147. s.Connect();
  148. var cmd = s.CreateCommand(";");
  149. cmd.Execute();
  150. if (string.IsNullOrEmpty(cmd.Error))
  151. {
  152. Assert.Fail("Operation should fail");
  153. }
  154. Assert.IsTrue(cmd.ExitStatus > 0);
  155. s.Disconnect();
  156. }
  157. [TestMethod]
  158. public void TestInvalidCommandThenValidCommandExecution()
  159. {
  160. var s = CreateShellUsingPassword();
  161. s.Connect();
  162. var cmd = s.CreateCommand(";");
  163. cmd.Execute();
  164. if (string.IsNullOrEmpty(cmd.Error))
  165. {
  166. Assert.Fail("Operation should fail");
  167. }
  168. Assert.IsTrue(cmd.ExitStatus > 0);
  169. var result = ExecuteTestCommand(s);
  170. s.Disconnect();
  171. Assert.IsTrue(result);
  172. }
  173. [TestMethod]
  174. public void TestRsaKeyConnection()
  175. {
  176. var s = CreateShellUsingRSAKey();
  177. s.Connect();
  178. var result = ExecuteTestCommand(s);
  179. s.Disconnect();
  180. Assert.IsTrue(result);
  181. }
  182. [TestMethod]
  183. public void TestDssKeyConnection()
  184. {
  185. var s = CreateShellUsingRSAKey();
  186. s.Connect();
  187. var result = ExecuteTestCommand(s);
  188. s.Disconnect();
  189. Assert.IsTrue(result);
  190. }
  191. private static SshClient CreateShellUsingPassword()
  192. {
  193. return new SshClient(ConnectionData.Host, ConnectionData.Port, ConnectionData.Username, ConnectionData.Password);
  194. }
  195. private static SshClient CreateShellUsingRSAKey()
  196. {
  197. return new SshClient(ConnectionData.Host, ConnectionData.Port, ConnectionData.Username, new PrivateKeyFile(ConnectionData.RsaKeyFilePath));
  198. }
  199. private static SshClient CreateShellUsingDSSKey()
  200. {
  201. return new SshClient(ConnectionData.Host, ConnectionData.Port, ConnectionData.Username, new PrivateKeyFile(ConnectionData.DssKeyFilePath));
  202. }
  203. private static bool ExecuteTestCommand(SshClient s)
  204. {
  205. var testValue = Guid.NewGuid().ToString();
  206. var command = string.Format("echo {0}", testValue);
  207. //var command = string.Format("echo {0};sleep 2s", testValue);
  208. var cmd = s.CreateCommand(command);
  209. var result = cmd.Execute();
  210. result = result.Substring(0, result.Length - 1); // Remove \n chararacter returned by command
  211. return result.Equals(testValue);
  212. }
  213. }
  214. }