2
0

SshCommandTest.cs 23 KB

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