SshClientTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. /// <summary>
  11. /// Provides client connection to SSH server.
  12. /// </summary>
  13. [TestClass]
  14. public class SshClientTest : TestBase
  15. {
  16. [TestMethod]
  17. public void CreateShellStream1_NeverConnected()
  18. {
  19. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  20. {
  21. const string terminalName = "vt100";
  22. const uint columns = 80;
  23. const uint rows = 25;
  24. const uint width = 640;
  25. const uint height = 480;
  26. const int bufferSize = 4096;
  27. try
  28. {
  29. client.CreateShellStream(terminalName, columns, rows, width, height, bufferSize);
  30. Assert.Fail();
  31. }
  32. catch (SshConnectionException ex)
  33. {
  34. Assert.IsNull(ex.InnerException);
  35. Assert.AreEqual("Client not connected.", ex.Message);
  36. }
  37. }
  38. }
  39. [TestMethod]
  40. public void CreateShellStream2_NeverConnected()
  41. {
  42. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  43. {
  44. const string terminalName = "vt100";
  45. const uint columns = 80;
  46. const uint rows = 25;
  47. const uint width = 640;
  48. const uint height = 480;
  49. var terminalModes = new Dictionary<TerminalModes, uint>();
  50. const int bufferSize = 4096;
  51. try
  52. {
  53. client.CreateShellStream(terminalName, columns, rows, width, height, bufferSize, terminalModes);
  54. Assert.Fail();
  55. }
  56. catch (SshConnectionException ex)
  57. {
  58. Assert.IsNull(ex.InnerException);
  59. Assert.AreEqual("Client not connected.", ex.Message);
  60. }
  61. }
  62. }
  63. [TestMethod]
  64. public void CreateShell1_NeverConnected()
  65. {
  66. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  67. {
  68. var encoding = Encoding.UTF8;
  69. const string input = "INPUT";
  70. var output = new MemoryStream();
  71. var extendedOutput = new MemoryStream();
  72. try
  73. {
  74. client.CreateShell(encoding, input, output, extendedOutput);
  75. Assert.Fail();
  76. }
  77. catch (SshConnectionException ex)
  78. {
  79. Assert.IsNull(ex.InnerException);
  80. Assert.AreEqual("Client not connected.", ex.Message);
  81. }
  82. }
  83. }
  84. [TestMethod]
  85. public void CreateShell2_NeverConnected()
  86. {
  87. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  88. {
  89. var encoding = Encoding.UTF8;
  90. const string input = "INPUT";
  91. var output = new MemoryStream();
  92. var extendedOutput = new MemoryStream();
  93. const string terminalName = "vt100";
  94. const uint columns = 80;
  95. const uint rows = 25;
  96. const uint width = 640;
  97. const uint height = 480;
  98. var terminalModes = new Dictionary<TerminalModes, uint>();
  99. try
  100. {
  101. client.CreateShell(
  102. encoding,
  103. input,
  104. output,
  105. extendedOutput,
  106. terminalName,
  107. columns,
  108. rows,
  109. width,
  110. height,
  111. terminalModes);
  112. Assert.Fail();
  113. }
  114. catch (SshConnectionException ex)
  115. {
  116. Assert.IsNull(ex.InnerException);
  117. Assert.AreEqual("Client not connected.", ex.Message);
  118. }
  119. }
  120. }
  121. [TestMethod]
  122. public void CreateShell3_NeverConnected()
  123. {
  124. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  125. {
  126. var encoding = Encoding.UTF8;
  127. const string input = "INPUT";
  128. var output = new MemoryStream();
  129. var extendedOutput = new MemoryStream();
  130. const string terminalName = "vt100";
  131. const uint columns = 80;
  132. const uint rows = 25;
  133. const uint width = 640;
  134. const uint height = 480;
  135. var terminalModes = new Dictionary<TerminalModes, uint>();
  136. const int bufferSize = 4096;
  137. try
  138. {
  139. client.CreateShell(
  140. encoding,
  141. input,
  142. output,
  143. extendedOutput,
  144. terminalName,
  145. columns,
  146. rows,
  147. width,
  148. height,
  149. terminalModes,
  150. bufferSize);
  151. Assert.Fail();
  152. }
  153. catch (SshConnectionException ex)
  154. {
  155. Assert.IsNull(ex.InnerException);
  156. Assert.AreEqual("Client not connected.", ex.Message);
  157. }
  158. }
  159. }
  160. [TestMethod]
  161. public void CreateShell4_NeverConnected()
  162. {
  163. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  164. {
  165. var input = new MemoryStream();
  166. var output = new MemoryStream();
  167. var extendedOutput = new MemoryStream();
  168. try
  169. {
  170. client.CreateShell(input, output, extendedOutput);
  171. Assert.Fail();
  172. }
  173. catch (SshConnectionException ex)
  174. {
  175. Assert.IsNull(ex.InnerException);
  176. Assert.AreEqual("Client not connected.", ex.Message);
  177. }
  178. }
  179. }
  180. [TestMethod]
  181. public void CreateShell5_NeverConnected()
  182. {
  183. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  184. {
  185. var input = new MemoryStream();
  186. var output = new MemoryStream();
  187. var extendedOutput = new MemoryStream();
  188. const string terminalName = "vt100";
  189. const uint columns = 80;
  190. const uint rows = 25;
  191. const uint width = 640;
  192. const uint height = 480;
  193. var terminalModes = new Dictionary<TerminalModes, uint>();
  194. try
  195. {
  196. client.CreateShell(
  197. input,
  198. output,
  199. extendedOutput,
  200. terminalName,
  201. columns,
  202. rows,
  203. width,
  204. height,
  205. terminalModes);
  206. Assert.Fail();
  207. }
  208. catch (SshConnectionException ex)
  209. {
  210. Assert.IsNull(ex.InnerException);
  211. Assert.AreEqual("Client not connected.", ex.Message);
  212. }
  213. }
  214. }
  215. [TestMethod]
  216. public void CreateShell6_NeverConnected()
  217. {
  218. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  219. {
  220. var input = new MemoryStream();
  221. var output = new MemoryStream();
  222. var extendedOutput = new MemoryStream();
  223. const string terminalName = "vt100";
  224. const uint columns = 80;
  225. const uint rows = 25;
  226. const uint width = 640;
  227. const uint height = 480;
  228. var terminalModes = new Dictionary<TerminalModes, uint>();
  229. const int bufferSize = 4096;
  230. try
  231. {
  232. client.CreateShell(
  233. input,
  234. output,
  235. extendedOutput,
  236. terminalName,
  237. columns,
  238. rows,
  239. width,
  240. height,
  241. terminalModes,
  242. bufferSize);
  243. Assert.Fail();
  244. }
  245. catch (SshConnectionException ex)
  246. {
  247. Assert.IsNull(ex.InnerException);
  248. Assert.AreEqual("Client not connected.", ex.Message);
  249. }
  250. }
  251. }
  252. [TestMethod]
  253. public void CreateCommand_CommandText_NeverConnected()
  254. {
  255. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  256. {
  257. try
  258. {
  259. client.CreateCommand("ls");
  260. Assert.Fail();
  261. }
  262. catch (SshConnectionException ex)
  263. {
  264. Assert.IsNull(ex.InnerException);
  265. Assert.AreEqual("Client not connected.", ex.Message);
  266. }
  267. }
  268. }
  269. [TestMethod]
  270. public void CreateCommand_CommandTextAndEncoding_NeverConnected()
  271. {
  272. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  273. {
  274. try
  275. {
  276. client.CreateCommand("ls", Encoding.UTF8);
  277. Assert.Fail();
  278. }
  279. catch (SshConnectionException ex)
  280. {
  281. Assert.IsNull(ex.InnerException);
  282. Assert.AreEqual("Client not connected.", ex.Message);
  283. }
  284. }
  285. }
  286. [TestMethod]
  287. public void AddForwardedPort_NeverConnected()
  288. {
  289. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  290. {
  291. var port = new ForwardedPortLocal(50, "host", 8080);
  292. try
  293. {
  294. client.AddForwardedPort(port);
  295. Assert.Fail();
  296. }
  297. catch (SshConnectionException ex)
  298. {
  299. Assert.IsNull(ex.InnerException);
  300. Assert.AreEqual("Client not connected.", ex.Message);
  301. }
  302. }
  303. }
  304. [TestMethod]
  305. public void RunCommand_CommandText_NeverConnected()
  306. {
  307. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  308. {
  309. try
  310. {
  311. client.RunCommand("ls");
  312. Assert.Fail();
  313. }
  314. catch (SshConnectionException ex)
  315. {
  316. Assert.IsNull(ex.InnerException);
  317. Assert.AreEqual("Client not connected.", ex.Message);
  318. }
  319. }
  320. }
  321. }
  322. }