ScpClientTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Tests.Common;
  3. using Renci.SshNet.Tests.Properties;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. /// <summary>
  12. /// Provides SCP client functionality.
  13. /// </summary>
  14. [TestClass]
  15. public partial class ScpClientTest : TestBase
  16. {
  17. [TestMethod]
  18. [TestCategory("Scp")]
  19. public void Test_Scp_File_Upload_Download()
  20. {
  21. RemoveAllFiles();
  22. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  23. {
  24. scp.Connect();
  25. string uploadedFileName = Path.GetTempFileName();
  26. string downloadedFileName = Path.GetTempFileName();
  27. this.CreateTestFile(uploadedFileName, 1);
  28. scp.Upload(new FileInfo(uploadedFileName), Path.GetFileName(uploadedFileName));
  29. scp.Download(Path.GetFileName(uploadedFileName), new FileInfo(downloadedFileName));
  30. // Calculate MD5 value
  31. var uploadedHash = CalculateMD5(uploadedFileName);
  32. var downloadedHash = CalculateMD5(downloadedFileName);
  33. File.Delete(uploadedFileName);
  34. File.Delete(downloadedFileName);
  35. scp.Disconnect();
  36. Assert.AreEqual(uploadedHash, downloadedHash);
  37. }
  38. }
  39. [TestMethod]
  40. [TestCategory("Scp")]
  41. public void Test_Scp_Stream_Upload_Download()
  42. {
  43. RemoveAllFiles();
  44. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  45. {
  46. scp.Connect();
  47. string uploadedFileName = Path.GetTempFileName();
  48. string downloadedFileName = Path.GetTempFileName();
  49. this.CreateTestFile(uploadedFileName, 1);
  50. // Calculate has value
  51. using (var stream = File.OpenRead(uploadedFileName))
  52. {
  53. scp.Upload(stream, Path.GetFileName(uploadedFileName));
  54. }
  55. using (var stream = File.OpenWrite(downloadedFileName))
  56. {
  57. scp.Download(Path.GetFileName(uploadedFileName), stream);
  58. }
  59. // Calculate MD5 value
  60. var uploadedHash = CalculateMD5(uploadedFileName);
  61. var downloadedHash = CalculateMD5(downloadedFileName);
  62. File.Delete(uploadedFileName);
  63. File.Delete(downloadedFileName);
  64. scp.Disconnect();
  65. Assert.AreEqual(uploadedHash, downloadedHash);
  66. }
  67. }
  68. [TestMethod]
  69. [TestCategory("Scp")]
  70. public void Test_Scp_10MB_File_Upload_Download()
  71. {
  72. RemoveAllFiles();
  73. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  74. {
  75. scp.Connect();
  76. string uploadedFileName = Path.GetTempFileName();
  77. string downloadedFileName = Path.GetTempFileName();
  78. this.CreateTestFile(uploadedFileName, 10);
  79. scp.Upload(new FileInfo(uploadedFileName), Path.GetFileName(uploadedFileName));
  80. scp.Download(Path.GetFileName(uploadedFileName), new FileInfo(downloadedFileName));
  81. // Calculate MD5 value
  82. var uploadedHash = CalculateMD5(uploadedFileName);
  83. var downloadedHash = CalculateMD5(downloadedFileName);
  84. File.Delete(uploadedFileName);
  85. File.Delete(downloadedFileName);
  86. scp.Disconnect();
  87. Assert.AreEqual(uploadedHash, downloadedHash);
  88. }
  89. }
  90. [TestMethod]
  91. [TestCategory("Scp")]
  92. public void Test_Scp_10MB_Stream_Upload_Download()
  93. {
  94. RemoveAllFiles();
  95. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  96. {
  97. scp.Connect();
  98. string uploadedFileName = Path.GetTempFileName();
  99. string downloadedFileName = Path.GetTempFileName();
  100. this.CreateTestFile(uploadedFileName, 10);
  101. // Calculate has value
  102. using (var stream = File.OpenRead(uploadedFileName))
  103. {
  104. scp.Upload(stream, Path.GetFileName(uploadedFileName));
  105. }
  106. using (var stream = File.OpenWrite(downloadedFileName))
  107. {
  108. scp.Download(Path.GetFileName(uploadedFileName), stream);
  109. }
  110. // Calculate MD5 value
  111. var uploadedHash = CalculateMD5(uploadedFileName);
  112. var downloadedHash = CalculateMD5(downloadedFileName);
  113. File.Delete(uploadedFileName);
  114. File.Delete(downloadedFileName);
  115. scp.Disconnect();
  116. Assert.AreEqual(uploadedHash, downloadedHash);
  117. }
  118. }
  119. [TestMethod]
  120. [TestCategory("Scp")]
  121. public void Test_Scp_Directory_Upload_Download()
  122. {
  123. RemoveAllFiles();
  124. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  125. {
  126. scp.Connect();
  127. var uploadDirectory = Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));
  128. for (int i = 0; i < 3; i++)
  129. {
  130. var subfolder = Directory.CreateDirectory(string.Format(@"{0}\folder_{1}", uploadDirectory.FullName, i));
  131. for (int j = 0; j < 5; j++)
  132. {
  133. this.CreateTestFile(string.Format(@"{0}\file_{1}", subfolder.FullName, j), 1);
  134. }
  135. this.CreateTestFile(string.Format(@"{0}\file_{1}", uploadDirectory.FullName, i), 1);
  136. }
  137. scp.Upload(uploadDirectory, "uploaded_dir");
  138. var downloadDirectory = Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));
  139. scp.Download("uploaded_dir", downloadDirectory);
  140. var uploadedFiles = uploadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  141. var downloadFiles = downloadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  142. var result = from f1 in uploadedFiles
  143. from f2 in downloadFiles
  144. where
  145. f1.FullName.Substring(uploadDirectory.FullName.Length) == f2.FullName.Substring(downloadDirectory.FullName.Length)
  146. && CalculateMD5(f1.FullName) == CalculateMD5(f2.FullName)
  147. select f1;
  148. var counter = result.Count();
  149. scp.Disconnect();
  150. Assert.IsTrue(counter == uploadedFiles.Length && uploadedFiles.Length == downloadFiles.Length);
  151. }
  152. }
  153. /// <summary>
  154. ///A test for OperationTimeout
  155. ///</summary>
  156. [TestMethod]
  157. [Ignore] // placeholder for actual test
  158. public void OperationTimeoutTest()
  159. {
  160. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  161. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  162. TimeSpan expected = new TimeSpan(); // TODO: Initialize to an appropriate value
  163. TimeSpan actual;
  164. target.OperationTimeout = expected;
  165. actual = target.OperationTimeout;
  166. Assert.AreEqual(expected, actual);
  167. Assert.Inconclusive("Verify the correctness of this test method.");
  168. }
  169. /// <summary>
  170. ///A test for BufferSize
  171. ///</summary>
  172. [TestMethod]
  173. [Ignore] // placeholder for actual test
  174. public void BufferSizeTest()
  175. {
  176. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  177. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  178. uint expected = 0; // TODO: Initialize to an appropriate value
  179. uint actual;
  180. target.BufferSize = expected;
  181. actual = target.BufferSize;
  182. Assert.AreEqual(expected, actual);
  183. Assert.Inconclusive("Verify the correctness of this test method.");
  184. }
  185. /// <summary>
  186. ///A test for Upload
  187. ///</summary>
  188. [TestMethod]
  189. [Ignore] // placeholder for actual test
  190. public void UploadTest()
  191. {
  192. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  193. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  194. DirectoryInfo directoryInfo = null; // TODO: Initialize to an appropriate value
  195. string filename = string.Empty; // TODO: Initialize to an appropriate value
  196. target.Upload(directoryInfo, filename);
  197. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  198. }
  199. /// <summary>
  200. ///A test for Upload
  201. ///</summary>
  202. [TestMethod]
  203. [Ignore] // placeholder for actual test
  204. public void UploadTest1()
  205. {
  206. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  207. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  208. FileInfo fileInfo = null; // TODO: Initialize to an appropriate value
  209. string filename = string.Empty; // TODO: Initialize to an appropriate value
  210. target.Upload(fileInfo, filename);
  211. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  212. }
  213. /// <summary>
  214. ///A test for Upload
  215. ///</summary>
  216. [TestMethod]
  217. [Ignore] // placeholder for actual test
  218. public void UploadTest2()
  219. {
  220. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  221. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  222. Stream source = null; // TODO: Initialize to an appropriate value
  223. string filename = string.Empty; // TODO: Initialize to an appropriate value
  224. target.Upload(source, filename);
  225. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  226. }
  227. /// <summary>
  228. ///A test for Download
  229. ///</summary>
  230. [TestMethod]
  231. [Ignore] // placeholder for actual test
  232. public void DownloadTest()
  233. {
  234. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  235. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  236. string directoryName = string.Empty; // TODO: Initialize to an appropriate value
  237. DirectoryInfo directoryInfo = null; // TODO: Initialize to an appropriate value
  238. target.Download(directoryName, directoryInfo);
  239. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  240. }
  241. /// <summary>
  242. ///A test for Download
  243. ///</summary>
  244. [TestMethod]
  245. [Ignore] // placeholder for actual test
  246. public void DownloadTest1()
  247. {
  248. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  249. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  250. string filename = string.Empty; // TODO: Initialize to an appropriate value
  251. FileInfo fileInfo = null; // TODO: Initialize to an appropriate value
  252. target.Download(filename, fileInfo);
  253. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  254. }
  255. /// <summary>
  256. ///A test for Download
  257. ///</summary>
  258. [TestMethod]
  259. [Ignore] // placeholder for actual test
  260. public void DownloadTest2()
  261. {
  262. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  263. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  264. string filename = string.Empty; // TODO: Initialize to an appropriate value
  265. Stream destination = null; // TODO: Initialize to an appropriate value
  266. target.Download(filename, destination);
  267. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  268. }
  269. /// <summary>
  270. ///A test for ScpClient Constructor
  271. ///</summary>
  272. [TestMethod]
  273. [Ignore] // placeholder for actual test
  274. public void ScpClientConstructorTest()
  275. {
  276. string host = string.Empty; // TODO: Initialize to an appropriate value
  277. string username = string.Empty; // TODO: Initialize to an appropriate value
  278. PrivateKeyFile[] keyFiles = null; // TODO: Initialize to an appropriate value
  279. ScpClient target = new ScpClient(host, username, keyFiles);
  280. Assert.Inconclusive("TODO: Implement code to verify target");
  281. }
  282. /// <summary>
  283. ///A test for ScpClient Constructor
  284. ///</summary>
  285. [TestMethod]
  286. [Ignore] // placeholder for actual test
  287. public void ScpClientConstructorTest1()
  288. {
  289. string host = string.Empty; // TODO: Initialize to an appropriate value
  290. int port = 0; // TODO: Initialize to an appropriate value
  291. string username = string.Empty; // TODO: Initialize to an appropriate value
  292. PrivateKeyFile[] keyFiles = null; // TODO: Initialize to an appropriate value
  293. ScpClient target = new ScpClient(host, port, username, keyFiles);
  294. Assert.Inconclusive("TODO: Implement code to verify target");
  295. }
  296. /// <summary>
  297. ///A test for ScpClient Constructor
  298. ///</summary>
  299. [TestMethod]
  300. [Ignore] // placeholder for actual test
  301. public void ScpClientConstructorTest2()
  302. {
  303. string host = string.Empty; // TODO: Initialize to an appropriate value
  304. string username = string.Empty; // TODO: Initialize to an appropriate value
  305. string password = string.Empty; // TODO: Initialize to an appropriate value
  306. ScpClient target = new ScpClient(host, username, password);
  307. Assert.Inconclusive("TODO: Implement code to verify target");
  308. }
  309. /// <summary>
  310. ///A test for ScpClient Constructor
  311. ///</summary>
  312. [TestMethod]
  313. [Ignore] // placeholder for actual test
  314. public void ScpClientConstructorTest3()
  315. {
  316. string host = string.Empty; // TODO: Initialize to an appropriate value
  317. int port = 0; // TODO: Initialize to an appropriate value
  318. string username = string.Empty; // TODO: Initialize to an appropriate value
  319. string password = string.Empty; // TODO: Initialize to an appropriate value
  320. ScpClient target = new ScpClient(host, port, username, password);
  321. Assert.Inconclusive("TODO: Implement code to verify target");
  322. }
  323. /// <summary>
  324. ///A test for ScpClient Constructor
  325. ///</summary>
  326. [TestMethod]
  327. [Ignore] // placeholder for actual test
  328. public void ScpClientConstructorTest4()
  329. {
  330. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  331. ScpClient target = new ScpClient(connectionInfo);
  332. Assert.Inconclusive("TODO: Implement code to verify target");
  333. }
  334. protected static string CalculateMD5(string fileName)
  335. {
  336. using (var file = new FileStream(fileName, FileMode.Open))
  337. {
  338. var md5 = new MD5CryptoServiceProvider();
  339. byte[] retVal = md5.ComputeHash(file);
  340. file.Close();
  341. var sb = new StringBuilder();
  342. for (var i = 0; i < retVal.Length; i++)
  343. {
  344. sb.Append(i.ToString("x2"));
  345. }
  346. return sb.ToString();
  347. }
  348. }
  349. private static void RemoveAllFiles()
  350. {
  351. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  352. {
  353. client.Connect();
  354. client.RunCommand("rm -rf *");
  355. client.Disconnect();
  356. }
  357. }
  358. }
  359. }