2
0

SshCommandTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.Tests.Common;
  4. using Renci.SshNet.Tests.Properties;
  5. using System;
  6. using System.IO;
  7. using System.Text;
  8. using System.Threading;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. /// <summary>
  12. /// Represents SSH command that can be executed.
  13. /// </summary>
  14. [TestClass]
  15. public partial class SshCommandTest : TestBase
  16. {
  17. [TestMethod]
  18. [ExpectedException(typeof(SshConnectionException))]
  19. public void Test_Execute_SingleCommand_Without_Connecting()
  20. {
  21. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  22. {
  23. var result = ExecuteTestCommand(client);
  24. Assert.IsTrue(result);
  25. }
  26. }
  27. [TestMethod]
  28. public void Test_Run_SingleCommand()
  29. {
  30. var host = Resources.HOST;
  31. var username = Resources.USERNAME;
  32. var password = Resources.PASSWORD;
  33. using (var client = new SshClient(host, username, password))
  34. {
  35. #region Example SshCommand RunCommand Result
  36. client.Connect();
  37. var testValue = Guid.NewGuid().ToString();
  38. var command = client.RunCommand(string.Format("echo {0}", testValue));
  39. var result = command.Result;
  40. result = result.Substring(0, result.Length - 1); // Remove \n character returned by command
  41. client.Disconnect();
  42. #endregion
  43. Assert.IsTrue(result.Equals(testValue));
  44. }
  45. }
  46. [TestMethod]
  47. public void Test_Execute_SingleCommand()
  48. {
  49. var host = Resources.HOST;
  50. var username = Resources.USERNAME;
  51. var password = Resources.PASSWORD;
  52. using (var client = new SshClient(host, username, password))
  53. {
  54. #region Example SshCommand CreateCommand Execute
  55. client.Connect();
  56. var testValue = Guid.NewGuid().ToString();
  57. var command = string.Format("echo {0}", testValue);
  58. var cmd = client.CreateCommand(command);
  59. var result = cmd.Execute();
  60. result = result.Substring(0, result.Length - 1); // Remove \n character returned by command
  61. client.Disconnect();
  62. #endregion
  63. Assert.IsTrue(result.Equals(testValue));
  64. }
  65. }
  66. [TestMethod]
  67. public void Test_Execute_OutputStream()
  68. {
  69. var host = Resources.HOST;
  70. var username = Resources.USERNAME;
  71. var password = Resources.PASSWORD;
  72. using (var client = new SshClient(host, username, password))
  73. {
  74. #region Example SshCommand CreateCommand Execute OutputStream
  75. client.Connect();
  76. var cmd = client.CreateCommand("ls -l"); // very long list
  77. var asynch = cmd.BeginExecute();
  78. var reader = new StreamReader(cmd.OutputStream);
  79. while (!asynch.IsCompleted)
  80. {
  81. var result = reader.ReadToEnd();
  82. if (string.IsNullOrEmpty(result))
  83. continue;
  84. Console.Write(result);
  85. }
  86. cmd.EndExecute(asynch);
  87. client.Disconnect();
  88. #endregion
  89. Assert.Inconclusive();
  90. }
  91. }
  92. [TestMethod]
  93. public void Test_Execute_ExtendedOutputStream()
  94. {
  95. var host = Resources.HOST;
  96. var username = Resources.USERNAME;
  97. var password = Resources.PASSWORD;
  98. using (var client = new SshClient(host, username, password))
  99. {
  100. #region Example SshCommand CreateCommand Execute ExtendedOutputStream
  101. client.Connect();
  102. var cmd = client.CreateCommand("echo 12345; echo 654321 >&2");
  103. var result = cmd.Execute();
  104. Console.Write(result);
  105. var reader = new StreamReader(cmd.ExtendedOutputStream);
  106. Console.WriteLine("DEBUG:");
  107. Console.Write(reader.ReadToEnd());
  108. client.Disconnect();
  109. #endregion
  110. Assert.Inconclusive();
  111. }
  112. }
  113. [TestMethod]
  114. [ExpectedException(typeof(SshOperationTimeoutException))]
  115. public void Test_Execute_Timeout()
  116. {
  117. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  118. {
  119. #region Example SshCommand CreateCommand Execute CommandTimeout
  120. client.Connect();
  121. var cmd = client.CreateCommand("sleep 10s");
  122. cmd.CommandTimeout = TimeSpan.FromSeconds(5);
  123. cmd.Execute();
  124. client.Disconnect();
  125. #endregion
  126. }
  127. }
  128. [TestMethod]
  129. public void Test_Execute_Infinite_Timeout()
  130. {
  131. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  132. {
  133. client.Connect();
  134. var cmd = client.CreateCommand("sleep 10s");
  135. cmd.Execute();
  136. client.Disconnect();
  137. }
  138. }
  139. [TestMethod]
  140. public void Test_Execute_InvalidCommand()
  141. {
  142. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  143. {
  144. client.Connect();
  145. var cmd = client.CreateCommand(";");
  146. cmd.Execute();
  147. if (string.IsNullOrEmpty(cmd.Error))
  148. {
  149. Assert.Fail("Operation should fail");
  150. }
  151. Assert.IsTrue(cmd.ExitStatus > 0);
  152. client.Disconnect();
  153. }
  154. }
  155. [TestMethod]
  156. public void Test_Execute_InvalidCommand_Then_Execute_ValidCommand()
  157. {
  158. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  159. {
  160. client.Connect();
  161. var cmd = client.CreateCommand(";");
  162. cmd.Execute();
  163. if (string.IsNullOrEmpty(cmd.Error))
  164. {
  165. Assert.Fail("Operation should fail");
  166. }
  167. Assert.IsTrue(cmd.ExitStatus > 0);
  168. var result = ExecuteTestCommand(client);
  169. client.Disconnect();
  170. Assert.IsTrue(result);
  171. }
  172. }
  173. [TestMethod]
  174. public void Test_Execute_Command_with_ExtendedOutput()
  175. {
  176. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  177. {
  178. client.Connect();
  179. var cmd = client.CreateCommand("echo 12345; echo 654321 >&2");
  180. cmd.Execute();
  181. //var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray());
  182. var extendedData = new StreamReader(cmd.ExtendedOutputStream, Encoding.ASCII).ReadToEnd();
  183. client.Disconnect();
  184. Assert.AreEqual("12345\n", cmd.Result);
  185. Assert.AreEqual("654321\n", extendedData);
  186. }
  187. }
  188. [TestMethod]
  189. public void Test_Execute_Command_Reconnect_Execute_Command()
  190. {
  191. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  192. {
  193. client.Connect();
  194. var result = ExecuteTestCommand(client);
  195. Assert.IsTrue(result);
  196. client.Disconnect();
  197. client.Connect();
  198. result = ExecuteTestCommand(client);
  199. Assert.IsTrue(result);
  200. client.Disconnect();
  201. }
  202. }
  203. [TestMethod]
  204. public void Test_Execute_Command_ExitStatus()
  205. {
  206. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  207. {
  208. #region Example SshCommand RunCommand ExitStatus
  209. client.Connect();
  210. var cmd = client.RunCommand("exit 128");
  211. Console.WriteLine(cmd.ExitStatus);
  212. client.Disconnect();
  213. #endregion
  214. Assert.IsTrue(cmd.ExitStatus == 128);
  215. }
  216. }
  217. [TestMethod]
  218. public void Test_Execute_Command_Asynchronously()
  219. {
  220. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  221. {
  222. client.Connect();
  223. var cmd = client.CreateCommand("sleep 5s; echo 'test'");
  224. var asyncResult = cmd.BeginExecute(null, null);
  225. while (!asyncResult.IsCompleted)
  226. {
  227. Thread.Sleep(100);
  228. }
  229. cmd.EndExecute(asyncResult);
  230. Assert.IsTrue(cmd.Result == "test\n");
  231. client.Disconnect();
  232. }
  233. }
  234. [TestMethod]
  235. public void Test_Execute_Command_Asynchronously_With_Error()
  236. {
  237. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  238. {
  239. client.Connect();
  240. var cmd = client.CreateCommand("sleep 5s; ;");
  241. var asyncResult = cmd.BeginExecute(null, null);
  242. while (!asyncResult.IsCompleted)
  243. {
  244. Thread.Sleep(100);
  245. }
  246. cmd.EndExecute(asyncResult);
  247. Assert.IsFalse(string.IsNullOrEmpty(cmd.Error));
  248. client.Disconnect();
  249. }
  250. }
  251. [TestMethod]
  252. public void Test_Execute_Command_Asynchronously_With_Callback()
  253. {
  254. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  255. {
  256. client.Connect();
  257. var callbackCalled = false;
  258. var cmd = client.CreateCommand("sleep 5s; echo 'test'");
  259. var asyncResult = cmd.BeginExecute(new AsyncCallback((s) =>
  260. {
  261. callbackCalled = true;
  262. }), null);
  263. while (!asyncResult.IsCompleted)
  264. {
  265. Thread.Sleep(100);
  266. }
  267. cmd.EndExecute(asyncResult);
  268. Assert.IsTrue(callbackCalled);
  269. client.Disconnect();
  270. }
  271. }
  272. [TestMethod]
  273. public void Test_Execute_Command_Asynchronously_With_Callback_On_Different_Thread()
  274. {
  275. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  276. {
  277. client.Connect();
  278. var currentThreadId = Thread.CurrentThread.ManagedThreadId;
  279. int callbackThreadId = 0;
  280. var cmd = client.CreateCommand("sleep 5s; echo 'test'");
  281. var asyncResult = cmd.BeginExecute(new AsyncCallback((s) =>
  282. {
  283. callbackThreadId = Thread.CurrentThread.ManagedThreadId;
  284. }), null);
  285. while (!asyncResult.IsCompleted)
  286. {
  287. Thread.Sleep(100);
  288. }
  289. cmd.EndExecute(asyncResult);
  290. Assert.AreNotEqual(currentThreadId, callbackThreadId);
  291. client.Disconnect();
  292. }
  293. }
  294. /// <summary>
  295. /// Tests for Issue 563.
  296. /// </summary>
  297. [WorkItem(563), TestMethod]
  298. public void Test_Execute_Command_Same_Object_Different_Commands()
  299. {
  300. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  301. {
  302. client.Connect();
  303. var cmd = client.CreateCommand("echo 12345");
  304. cmd.Execute();
  305. Assert.AreEqual("12345\n", cmd.Result);
  306. cmd.Execute("echo 23456");
  307. Assert.AreEqual("23456\n", cmd.Result);
  308. client.Disconnect();
  309. }
  310. }
  311. [TestMethod]
  312. public void Test_Get_Result_Without_Execution()
  313. {
  314. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  315. {
  316. client.Connect();
  317. var cmd = client.CreateCommand("ls -l");
  318. Assert.IsTrue(string.IsNullOrEmpty(cmd.Result));
  319. client.Disconnect();
  320. }
  321. }
  322. [TestMethod]
  323. public void Test_Get_Error_Without_Execution()
  324. {
  325. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  326. {
  327. client.Connect();
  328. var cmd = client.CreateCommand("ls -l");
  329. Assert.IsTrue(string.IsNullOrEmpty(cmd.Error));
  330. client.Disconnect();
  331. }
  332. }
  333. [WorkItem(703), TestMethod]
  334. [ExpectedException(typeof(ArgumentException))]
  335. public void Test_EndExecute_Before_BeginExecute()
  336. {
  337. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  338. {
  339. client.Connect();
  340. var cmd = client.CreateCommand("ls -l");
  341. cmd.EndExecute(null);
  342. client.Disconnect();
  343. }
  344. }
  345. /// <summary>
  346. ///A test for BeginExecute
  347. ///</summary>
  348. [TestMethod()]
  349. public void BeginExecuteTest()
  350. {
  351. string expected = "123\n";
  352. string result;
  353. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  354. {
  355. #region Example SshCommand CreateCommand BeginExecute IsCompleted EndExecute
  356. client.Connect();
  357. var cmd = client.CreateCommand("sleep 15s;echo 123"); // Perform long running task
  358. var asynch = cmd.BeginExecute();
  359. while (!asynch.IsCompleted)
  360. {
  361. // Waiting for command to complete...
  362. Thread.Sleep(2000);
  363. }
  364. result = cmd.EndExecute(asynch);
  365. client.Disconnect();
  366. #endregion
  367. Assert.IsNotNull(asynch);
  368. Assert.AreEqual(expected, result);
  369. }
  370. }
  371. [TestMethod]
  372. public void Test_Execute_Invalid_Command()
  373. {
  374. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  375. {
  376. #region Example SshCommand CreateCommand Error
  377. client.Connect();
  378. var cmd = client.CreateCommand(";");
  379. cmd.Execute();
  380. if (!string.IsNullOrEmpty(cmd.Error))
  381. {
  382. Console.WriteLine(cmd.Error);
  383. }
  384. client.Disconnect();
  385. #endregion
  386. Assert.Inconclusive();
  387. }
  388. }
  389. /// <summary>
  390. ///A test for BeginExecute
  391. ///</summary>
  392. [TestMethod()]
  393. public void BeginExecuteTest1()
  394. {
  395. Session session = null; // TODO: Initialize to an appropriate value
  396. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  397. var encoding = Encoding.UTF8;
  398. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  399. string commandText1 = string.Empty; // TODO: Initialize to an appropriate value
  400. AsyncCallback callback = null; // TODO: Initialize to an appropriate value
  401. object state = null; // TODO: Initialize to an appropriate value
  402. IAsyncResult expected = null; // TODO: Initialize to an appropriate value
  403. IAsyncResult actual;
  404. actual = target.BeginExecute(commandText1, callback, state);
  405. Assert.AreEqual(expected, actual);
  406. Assert.Inconclusive("Verify the correctness of this test method.");
  407. }
  408. /// <summary>
  409. ///A test for CancelAsync
  410. ///</summary>
  411. [TestMethod()]
  412. public void CancelAsyncTest()
  413. {
  414. Session session = null; // TODO: Initialize to an appropriate value
  415. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  416. var encoding = Encoding.UTF8;
  417. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  418. target.CancelAsync();
  419. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  420. }
  421. /// <summary>
  422. ///A test for Dispose
  423. ///</summary>
  424. [TestMethod()]
  425. public void DisposeTest()
  426. {
  427. Session session = null; // TODO: Initialize to an appropriate value
  428. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  429. var encoding = Encoding.UTF8;
  430. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  431. target.Dispose();
  432. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  433. }
  434. /// <summary>
  435. ///A test for EndExecute
  436. ///</summary>
  437. [TestMethod()]
  438. public void EndExecuteTest()
  439. {
  440. Session session = null; // TODO: Initialize to an appropriate value
  441. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  442. var encoding = Encoding.UTF8;
  443. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  444. IAsyncResult asyncResult = null; // TODO: Initialize to an appropriate value
  445. string expected = string.Empty; // TODO: Initialize to an appropriate value
  446. string actual;
  447. actual = target.EndExecute(asyncResult);
  448. Assert.AreEqual(expected, actual);
  449. Assert.Inconclusive("Verify the correctness of this test method.");
  450. }
  451. /// <summary>
  452. ///A test for Execute
  453. ///</summary>
  454. [TestMethod()]
  455. public void ExecuteTest()
  456. {
  457. Session session = null; // TODO: Initialize to an appropriate value
  458. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  459. var encoding = Encoding.UTF8;
  460. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  461. string expected = string.Empty; // TODO: Initialize to an appropriate value
  462. string actual;
  463. actual = target.Execute();
  464. Assert.AreEqual(expected, actual);
  465. Assert.Inconclusive("Verify the correctness of this test method.");
  466. }
  467. /// <summary>
  468. ///A test for Execute
  469. ///</summary>
  470. [TestMethod()]
  471. public void ExecuteTest1()
  472. {
  473. Session session = null; // TODO: Initialize to an appropriate value
  474. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  475. var encoding = Encoding.UTF8;
  476. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  477. string commandText1 = string.Empty; // TODO: Initialize to an appropriate value
  478. string expected = string.Empty; // TODO: Initialize to an appropriate value
  479. string actual;
  480. actual = target.Execute(commandText1);
  481. Assert.AreEqual(expected, actual);
  482. Assert.Inconclusive("Verify the correctness of this test method.");
  483. }
  484. /// <summary>
  485. ///A test for CommandTimeout
  486. ///</summary>
  487. [TestMethod()]
  488. public void CommandTimeoutTest()
  489. {
  490. Session session = null; // TODO: Initialize to an appropriate value
  491. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  492. var encoding = Encoding.UTF8;
  493. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  494. TimeSpan expected = new TimeSpan(); // TODO: Initialize to an appropriate value
  495. TimeSpan actual;
  496. target.CommandTimeout = expected;
  497. actual = target.CommandTimeout;
  498. Assert.AreEqual(expected, actual);
  499. Assert.Inconclusive("Verify the correctness of this test method.");
  500. }
  501. /// <summary>
  502. ///A test for Error
  503. ///</summary>
  504. [TestMethod()]
  505. public void ErrorTest()
  506. {
  507. Session session = null; // TODO: Initialize to an appropriate value
  508. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  509. var encoding = Encoding.UTF8;
  510. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  511. string actual;
  512. actual = target.Error;
  513. Assert.Inconclusive("Verify the correctness of this test method.");
  514. }
  515. /// <summary>
  516. ///A test for Result
  517. ///</summary>
  518. [TestMethod()]
  519. public void ResultTest()
  520. {
  521. Session session = null; // TODO: Initialize to an appropriate value
  522. string commandText = string.Empty; // TODO: Initialize to an appropriate value
  523. var encoding = Encoding.UTF8;
  524. SshCommand target = new SshCommand(session, commandText, encoding); // TODO: Initialize to an appropriate value
  525. string actual;
  526. actual = target.Result;
  527. Assert.Inconclusive("Verify the correctness of this test method.");
  528. }
  529. private static bool ExecuteTestCommand(SshClient s)
  530. {
  531. var testValue = Guid.NewGuid().ToString();
  532. var command = string.Format("echo {0}", testValue);
  533. var cmd = s.CreateCommand(command);
  534. var result = cmd.Execute();
  535. result = result.Substring(0, result.Length - 1); // Remove \n character returned by command
  536. return result.Equals(testValue);
  537. }
  538. }
  539. }