ScpClientTest.cs 17 KB

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