2
0

ScpClientTest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. #if FEATURE_TPL
  11. using System.Threading.Tasks;
  12. #endif // FEATURE_TPL
  13. namespace Renci.SshNet.Tests.Classes
  14. {
  15. /// <summary>
  16. /// Provides SCP client functionality.
  17. /// </summary>
  18. [TestClass]
  19. public partial class ScpClientTest : TestBase
  20. {
  21. private Random _random;
  22. [TestInitialize]
  23. public void SetUp()
  24. {
  25. _random = new Random();
  26. }
  27. [TestMethod]
  28. public void Ctor_ConnectionInfo_Null()
  29. {
  30. const ConnectionInfo connectionInfo = null;
  31. try
  32. {
  33. new ScpClient(connectionInfo);
  34. Assert.Fail();
  35. }
  36. catch (ArgumentNullException ex)
  37. {
  38. Assert.IsNull(ex.InnerException);
  39. Assert.AreEqual("connectionInfo", ex.ParamName);
  40. }
  41. }
  42. [TestMethod]
  43. public void Ctor_ConnectionInfo_NotNull()
  44. {
  45. var connectionInfo = new ConnectionInfo("HOST", "USER", new PasswordAuthenticationMethod("USER", "PWD"));
  46. var client = new ScpClient(connectionInfo);
  47. Assert.AreEqual(16 * 1024U, client.BufferSize);
  48. Assert.AreSame(connectionInfo, client.ConnectionInfo);
  49. Assert.IsFalse(client.IsConnected);
  50. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  51. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  52. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  53. Assert.IsNull(client.Session);
  54. }
  55. [TestMethod]
  56. public void Ctor_HostAndPortAndUsernameAndPassword()
  57. {
  58. var host = _random.Next().ToString();
  59. var port = _random.Next(1, 100);
  60. var userName = _random.Next().ToString();
  61. var password = _random.Next().ToString();
  62. var client = new ScpClient(host, port, userName, password);
  63. Assert.AreEqual(16 * 1024U, client.BufferSize);
  64. Assert.IsNotNull(client.ConnectionInfo);
  65. Assert.IsFalse(client.IsConnected);
  66. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  67. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  68. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  69. Assert.IsNull(client.Session);
  70. var passwordConnectionInfo = client.ConnectionInfo as PasswordConnectionInfo;
  71. Assert.IsNotNull(passwordConnectionInfo);
  72. Assert.AreEqual(host, passwordConnectionInfo.Host);
  73. Assert.AreEqual(port, passwordConnectionInfo.Port);
  74. Assert.AreSame(userName, passwordConnectionInfo.Username);
  75. Assert.IsNotNull(passwordConnectionInfo.AuthenticationMethods);
  76. Assert.AreEqual(1, passwordConnectionInfo.AuthenticationMethods.Count);
  77. var passwordAuthentication = passwordConnectionInfo.AuthenticationMethods[0] as PasswordAuthenticationMethod;
  78. Assert.IsNotNull(passwordAuthentication);
  79. Assert.AreEqual(userName, passwordAuthentication.Username);
  80. Assert.IsTrue(Encoding.UTF8.GetBytes(password).IsEqualTo(passwordAuthentication.Password));
  81. }
  82. [TestMethod]
  83. public void Ctor_HostAndUsernameAndPassword()
  84. {
  85. var host = _random.Next().ToString();
  86. var userName = _random.Next().ToString();
  87. var password = _random.Next().ToString();
  88. var client = new ScpClient(host, userName, password);
  89. Assert.AreEqual(16 * 1024U, client.BufferSize);
  90. Assert.IsNotNull(client.ConnectionInfo);
  91. Assert.IsFalse(client.IsConnected);
  92. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  93. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  94. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  95. Assert.IsNull(client.Session);
  96. var passwordConnectionInfo = client.ConnectionInfo as PasswordConnectionInfo;
  97. Assert.IsNotNull(passwordConnectionInfo);
  98. Assert.AreEqual(host, passwordConnectionInfo.Host);
  99. Assert.AreEqual(22, passwordConnectionInfo.Port);
  100. Assert.AreSame(userName, passwordConnectionInfo.Username);
  101. Assert.IsNotNull(passwordConnectionInfo.AuthenticationMethods);
  102. Assert.AreEqual(1, passwordConnectionInfo.AuthenticationMethods.Count);
  103. var passwordAuthentication = passwordConnectionInfo.AuthenticationMethods[0] as PasswordAuthenticationMethod;
  104. Assert.IsNotNull(passwordAuthentication);
  105. Assert.AreEqual(userName, passwordAuthentication.Username);
  106. Assert.IsTrue(Encoding.UTF8.GetBytes(password).IsEqualTo(passwordAuthentication.Password));
  107. }
  108. [TestMethod]
  109. public void Ctor_HostAndPortAndUsernameAndPrivateKeys()
  110. {
  111. var host = _random.Next().ToString();
  112. var port = _random.Next(1, 100);
  113. var userName = _random.Next().ToString();
  114. var privateKeys = new[] {GetRsaKey(), GetDsaKey()};
  115. var client = new ScpClient(host, port, userName, privateKeys);
  116. Assert.AreEqual(16 * 1024U, client.BufferSize);
  117. Assert.IsNotNull(client.ConnectionInfo);
  118. Assert.IsFalse(client.IsConnected);
  119. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  120. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  121. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  122. Assert.IsNull(client.Session);
  123. var privateKeyConnectionInfo = client.ConnectionInfo as PrivateKeyConnectionInfo;
  124. Assert.IsNotNull(privateKeyConnectionInfo);
  125. Assert.AreEqual(host, privateKeyConnectionInfo.Host);
  126. Assert.AreEqual(port, privateKeyConnectionInfo.Port);
  127. Assert.AreSame(userName, privateKeyConnectionInfo.Username);
  128. Assert.IsNotNull(privateKeyConnectionInfo.AuthenticationMethods);
  129. Assert.AreEqual(1, privateKeyConnectionInfo.AuthenticationMethods.Count);
  130. var privateKeyAuthentication = privateKeyConnectionInfo.AuthenticationMethods[0] as PrivateKeyAuthenticationMethod;
  131. Assert.IsNotNull(privateKeyAuthentication);
  132. Assert.AreEqual(userName, privateKeyAuthentication.Username);
  133. Assert.IsNotNull(privateKeyAuthentication.KeyFiles);
  134. Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeyFiles.Count);
  135. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[0]));
  136. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[1]));
  137. }
  138. [TestMethod]
  139. public void Ctor_HostAndUsernameAndPrivateKeys()
  140. {
  141. var host = _random.Next().ToString();
  142. var userName = _random.Next().ToString();
  143. var privateKeys = new[] { GetRsaKey(), GetDsaKey() };
  144. var client = new ScpClient(host, userName, privateKeys);
  145. Assert.AreEqual(16 * 1024U, client.BufferSize);
  146. Assert.IsNotNull(client.ConnectionInfo);
  147. Assert.IsFalse(client.IsConnected);
  148. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  149. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  150. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  151. Assert.IsNull(client.Session);
  152. var privateKeyConnectionInfo = client.ConnectionInfo as PrivateKeyConnectionInfo;
  153. Assert.IsNotNull(privateKeyConnectionInfo);
  154. Assert.AreEqual(host, privateKeyConnectionInfo.Host);
  155. Assert.AreEqual(22, privateKeyConnectionInfo.Port);
  156. Assert.AreSame(userName, privateKeyConnectionInfo.Username);
  157. Assert.IsNotNull(privateKeyConnectionInfo.AuthenticationMethods);
  158. Assert.AreEqual(1, privateKeyConnectionInfo.AuthenticationMethods.Count);
  159. var privateKeyAuthentication = privateKeyConnectionInfo.AuthenticationMethods[0] as PrivateKeyAuthenticationMethod;
  160. Assert.IsNotNull(privateKeyAuthentication);
  161. Assert.AreEqual(userName, privateKeyAuthentication.Username);
  162. Assert.IsNotNull(privateKeyAuthentication.KeyFiles);
  163. Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeyFiles.Count);
  164. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[0]));
  165. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[1]));
  166. }
  167. [TestMethod]
  168. public void RemotePathTransformation_Value_NotNull()
  169. {
  170. var client = new ScpClient("HOST", 22, "USER", "PWD");
  171. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  172. client.RemotePathTransformation = RemotePathTransformation.ShellQuote;
  173. Assert.AreSame(RemotePathTransformation.ShellQuote, client.RemotePathTransformation);
  174. }
  175. [TestMethod]
  176. public void RemotePathTransformation_Value_Null()
  177. {
  178. var client = new ScpClient("HOST", 22, "USER", "PWD")
  179. {
  180. RemotePathTransformation = RemotePathTransformation.ShellQuote
  181. };
  182. try
  183. {
  184. client.RemotePathTransformation = null;
  185. Assert.Fail();
  186. }
  187. catch (ArgumentNullException ex)
  188. {
  189. Assert.IsNull(ex.InnerException);
  190. Assert.AreEqual("value", ex.ParamName);
  191. }
  192. Assert.AreSame(RemotePathTransformation.ShellQuote, client.RemotePathTransformation);
  193. }
  194. [TestMethod]
  195. [TestCategory("Scp")]
  196. [TestCategory("integration")]
  197. public void Test_Scp_File_Upload_Download()
  198. {
  199. RemoveAllFiles();
  200. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  201. {
  202. scp.Connect();
  203. string uploadedFileName = Path.GetTempFileName();
  204. string downloadedFileName = Path.GetTempFileName();
  205. this.CreateTestFile(uploadedFileName, 1);
  206. scp.Upload(new FileInfo(uploadedFileName), Path.GetFileName(uploadedFileName));
  207. scp.Download(Path.GetFileName(uploadedFileName), new FileInfo(downloadedFileName));
  208. // Calculate MD5 value
  209. var uploadedHash = CalculateMD5(uploadedFileName);
  210. var downloadedHash = CalculateMD5(downloadedFileName);
  211. File.Delete(uploadedFileName);
  212. File.Delete(downloadedFileName);
  213. scp.Disconnect();
  214. Assert.AreEqual(uploadedHash, downloadedHash);
  215. }
  216. }
  217. [TestMethod]
  218. [TestCategory("Scp")]
  219. [TestCategory("integration")]
  220. public void Test_Scp_Stream_Upload_Download()
  221. {
  222. RemoveAllFiles();
  223. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  224. {
  225. scp.Connect();
  226. string uploadedFileName = Path.GetTempFileName();
  227. string downloadedFileName = Path.GetTempFileName();
  228. this.CreateTestFile(uploadedFileName, 1);
  229. // Calculate has value
  230. using (var stream = File.OpenRead(uploadedFileName))
  231. {
  232. scp.Upload(stream, Path.GetFileName(uploadedFileName));
  233. }
  234. using (var stream = File.OpenWrite(downloadedFileName))
  235. {
  236. scp.Download(Path.GetFileName(uploadedFileName), stream);
  237. }
  238. // Calculate MD5 value
  239. var uploadedHash = CalculateMD5(uploadedFileName);
  240. var downloadedHash = CalculateMD5(downloadedFileName);
  241. File.Delete(uploadedFileName);
  242. File.Delete(downloadedFileName);
  243. scp.Disconnect();
  244. Assert.AreEqual(uploadedHash, downloadedHash);
  245. }
  246. }
  247. [TestMethod]
  248. [TestCategory("Scp")]
  249. [TestCategory("integration")]
  250. public void Test_Scp_10MB_File_Upload_Download()
  251. {
  252. RemoveAllFiles();
  253. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  254. {
  255. scp.Connect();
  256. string uploadedFileName = Path.GetTempFileName();
  257. string downloadedFileName = Path.GetTempFileName();
  258. this.CreateTestFile(uploadedFileName, 10);
  259. scp.Upload(new FileInfo(uploadedFileName), Path.GetFileName(uploadedFileName));
  260. scp.Download(Path.GetFileName(uploadedFileName), new FileInfo(downloadedFileName));
  261. // Calculate MD5 value
  262. var uploadedHash = CalculateMD5(uploadedFileName);
  263. var downloadedHash = CalculateMD5(downloadedFileName);
  264. File.Delete(uploadedFileName);
  265. File.Delete(downloadedFileName);
  266. scp.Disconnect();
  267. Assert.AreEqual(uploadedHash, downloadedHash);
  268. }
  269. }
  270. [TestMethod]
  271. [TestCategory("Scp")]
  272. [TestCategory("integration")]
  273. public void Test_Scp_10MB_Stream_Upload_Download()
  274. {
  275. RemoveAllFiles();
  276. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  277. {
  278. scp.Connect();
  279. string uploadedFileName = Path.GetTempFileName();
  280. string downloadedFileName = Path.GetTempFileName();
  281. this.CreateTestFile(uploadedFileName, 10);
  282. // Calculate has value
  283. using (var stream = File.OpenRead(uploadedFileName))
  284. {
  285. scp.Upload(stream, Path.GetFileName(uploadedFileName));
  286. }
  287. using (var stream = File.OpenWrite(downloadedFileName))
  288. {
  289. scp.Download(Path.GetFileName(uploadedFileName), stream);
  290. }
  291. // Calculate MD5 value
  292. var uploadedHash = CalculateMD5(uploadedFileName);
  293. var downloadedHash = CalculateMD5(downloadedFileName);
  294. File.Delete(uploadedFileName);
  295. File.Delete(downloadedFileName);
  296. scp.Disconnect();
  297. Assert.AreEqual(uploadedHash, downloadedHash);
  298. }
  299. }
  300. [TestMethod]
  301. [TestCategory("Scp")]
  302. [TestCategory("integration")]
  303. public void Test_Scp_Directory_Upload_Download()
  304. {
  305. RemoveAllFiles();
  306. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  307. {
  308. scp.Connect();
  309. var uploadDirectory =
  310. Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));
  311. for (int i = 0; i < 3; i++)
  312. {
  313. var subfolder =
  314. Directory.CreateDirectory(string.Format(@"{0}\folder_{1}", uploadDirectory.FullName, i));
  315. for (int j = 0; j < 5; j++)
  316. {
  317. this.CreateTestFile(string.Format(@"{0}\file_{1}", subfolder.FullName, j), 1);
  318. }
  319. this.CreateTestFile(string.Format(@"{0}\file_{1}", uploadDirectory.FullName, i), 1);
  320. }
  321. scp.Upload(uploadDirectory, "uploaded_dir");
  322. var downloadDirectory =
  323. Directory.CreateDirectory(string.Format("{0}\\{1}", Path.GetTempPath(), Path.GetRandomFileName()));
  324. scp.Download("uploaded_dir", downloadDirectory);
  325. var uploadedFiles = uploadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  326. var downloadFiles = downloadDirectory.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  327. var result = from f1 in uploadedFiles
  328. from f2 in downloadFiles
  329. where
  330. f1.FullName.Substring(uploadDirectory.FullName.Length) ==
  331. f2.FullName.Substring(downloadDirectory.FullName.Length)
  332. && CalculateMD5(f1.FullName) == CalculateMD5(f2.FullName)
  333. select f1;
  334. var counter = result.Count();
  335. scp.Disconnect();
  336. Assert.IsTrue(counter == uploadedFiles.Length && uploadedFiles.Length == downloadFiles.Length);
  337. }
  338. }
  339. /// <summary>
  340. ///A test for OperationTimeout
  341. ///</summary>
  342. [TestMethod]
  343. [Ignore] // placeholder for actual test
  344. public void OperationTimeoutTest()
  345. {
  346. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  347. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  348. TimeSpan expected = new TimeSpan(); // TODO: Initialize to an appropriate value
  349. TimeSpan actual;
  350. target.OperationTimeout = expected;
  351. actual = target.OperationTimeout;
  352. Assert.AreEqual(expected, actual);
  353. Assert.Inconclusive("Verify the correctness of this test method.");
  354. }
  355. /// <summary>
  356. ///A test for BufferSize
  357. ///</summary>
  358. [TestMethod]
  359. [Ignore] // placeholder for actual test
  360. public void BufferSizeTest()
  361. {
  362. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  363. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  364. uint expected = 0; // TODO: Initialize to an appropriate value
  365. uint actual;
  366. target.BufferSize = expected;
  367. actual = target.BufferSize;
  368. Assert.AreEqual(expected, actual);
  369. Assert.Inconclusive("Verify the correctness of this test method.");
  370. }
  371. /// <summary>
  372. ///A test for Upload
  373. ///</summary>
  374. [TestMethod]
  375. [Ignore] // placeholder for actual test
  376. public void UploadTest()
  377. {
  378. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  379. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  380. DirectoryInfo directoryInfo = null; // TODO: Initialize to an appropriate value
  381. string filename = string.Empty; // TODO: Initialize to an appropriate value
  382. target.Upload(directoryInfo, filename);
  383. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  384. }
  385. /// <summary>
  386. ///A test for Upload
  387. ///</summary>
  388. [TestMethod]
  389. [Ignore] // placeholder for actual test
  390. public void UploadTest1()
  391. {
  392. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  393. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  394. FileInfo fileInfo = null; // TODO: Initialize to an appropriate value
  395. string filename = string.Empty; // TODO: Initialize to an appropriate value
  396. target.Upload(fileInfo, filename);
  397. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  398. }
  399. /// <summary>
  400. ///A test for Upload
  401. ///</summary>
  402. [TestMethod]
  403. [Ignore] // placeholder for actual test
  404. public void UploadTest2()
  405. {
  406. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  407. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  408. Stream source = null; // TODO: Initialize to an appropriate value
  409. string filename = string.Empty; // TODO: Initialize to an appropriate value
  410. target.Upload(source, filename);
  411. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  412. }
  413. /// <summary>
  414. ///A test for Download
  415. ///</summary>
  416. [TestMethod]
  417. [Ignore] // placeholder for actual test
  418. public void DownloadTest()
  419. {
  420. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  421. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  422. string directoryName = string.Empty; // TODO: Initialize to an appropriate value
  423. DirectoryInfo directoryInfo = null; // TODO: Initialize to an appropriate value
  424. target.Download(directoryName, directoryInfo);
  425. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  426. }
  427. /// <summary>
  428. ///A test for Download
  429. ///</summary>
  430. [TestMethod]
  431. [Ignore] // placeholder for actual test
  432. public void DownloadTest1()
  433. {
  434. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  435. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  436. string filename = string.Empty; // TODO: Initialize to an appropriate value
  437. FileInfo fileInfo = null; // TODO: Initialize to an appropriate value
  438. target.Download(filename, fileInfo);
  439. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  440. }
  441. /// <summary>
  442. ///A test for Download
  443. ///</summary>
  444. [TestMethod]
  445. [Ignore] // placeholder for actual test
  446. public void DownloadTest2()
  447. {
  448. ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
  449. ScpClient target = new ScpClient(connectionInfo); // TODO: Initialize to an appropriate value
  450. string filename = string.Empty; // TODO: Initialize to an appropriate value
  451. Stream destination = null; // TODO: Initialize to an appropriate value
  452. target.Download(filename, destination);
  453. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  454. }
  455. #if FEATURE_TPL
  456. [TestMethod]
  457. [TestCategory("Scp")]
  458. [TestCategory("integration")]
  459. public void Test_Scp_File_20_Parallel_Upload_Download()
  460. {
  461. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  462. {
  463. scp.Connect();
  464. var uploadFilenames = new string[20];
  465. for (int i = 0; i < uploadFilenames.Length; i++)
  466. {
  467. uploadFilenames[i] = Path.GetTempFileName();
  468. this.CreateTestFile(uploadFilenames[i], 1);
  469. }
  470. Parallel.ForEach(uploadFilenames,
  471. (filename) =>
  472. {
  473. scp.Upload(new FileInfo(filename), Path.GetFileName(filename));
  474. });
  475. Parallel.ForEach(uploadFilenames,
  476. (filename) =>
  477. {
  478. scp.Download(Path.GetFileName(filename), new FileInfo(string.Format("{0}.down", filename)));
  479. });
  480. var result = from file in uploadFilenames
  481. where
  482. CalculateMD5(file) == CalculateMD5(string.Format("{0}.down", file))
  483. select file;
  484. scp.Disconnect();
  485. Assert.IsTrue(result.Count() == uploadFilenames.Length);
  486. }
  487. }
  488. [TestMethod]
  489. [TestCategory("Scp")]
  490. [TestCategory("integration")]
  491. public void Test_Scp_File_Upload_Download_Events()
  492. {
  493. using (var scp = new ScpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  494. {
  495. scp.Connect();
  496. var uploadFilenames = new string[10];
  497. for (int i = 0; i < uploadFilenames.Length; i++)
  498. {
  499. uploadFilenames[i] = Path.GetTempFileName();
  500. this.CreateTestFile(uploadFilenames[i], 1);
  501. }
  502. var uploadedFiles = uploadFilenames.ToDictionary((filename) => Path.GetFileName(filename), (filename) => 0L);
  503. var downloadedFiles = uploadFilenames.ToDictionary((filename) => string.Format("{0}.down", Path.GetFileName(filename)), (filename) => 0L);
  504. scp.Uploading += delegate (object sender, ScpUploadEventArgs e)
  505. {
  506. uploadedFiles[e.Filename] = e.Uploaded;
  507. };
  508. scp.Downloading += delegate (object sender, ScpDownloadEventArgs e)
  509. {
  510. downloadedFiles[string.Format("{0}.down", e.Filename)] = e.Downloaded;
  511. };
  512. Parallel.ForEach(uploadFilenames,
  513. (filename) =>
  514. {
  515. scp.Upload(new FileInfo(filename), Path.GetFileName(filename));
  516. });
  517. Parallel.ForEach(uploadFilenames,
  518. (filename) =>
  519. {
  520. scp.Download(Path.GetFileName(filename), new FileInfo(string.Format("{0}.down", filename)));
  521. });
  522. var result = from uf in uploadedFiles
  523. from df in downloadedFiles
  524. where
  525. string.Format("{0}.down", uf.Key) == df.Key
  526. && uf.Value == df.Value
  527. select uf;
  528. scp.Disconnect();
  529. Assert.IsTrue(result.Count() == uploadFilenames.Length && uploadFilenames.Length == uploadedFiles.Count && uploadedFiles.Count == downloadedFiles.Count);
  530. }
  531. }
  532. #endif // FEATURE_TPL
  533. protected static string CalculateMD5(string fileName)
  534. {
  535. using (var file = new FileStream(fileName, FileMode.Open))
  536. {
  537. var md5 = new MD5CryptoServiceProvider();
  538. byte[] retVal = md5.ComputeHash(file);
  539. file.Close();
  540. var sb = new StringBuilder();
  541. for (var i = 0; i < retVal.Length; i++)
  542. {
  543. sb.Append(i.ToString("x2"));
  544. }
  545. return sb.ToString();
  546. }
  547. }
  548. private static void RemoveAllFiles()
  549. {
  550. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  551. {
  552. client.Connect();
  553. client.RunCommand("rm -rf *");
  554. client.Disconnect();
  555. }
  556. }
  557. private PrivateKeyFile GetRsaKey()
  558. {
  559. using (var stream = GetData("Key.RSA.txt"))
  560. {
  561. return new PrivateKeyFile(stream);
  562. }
  563. }
  564. private PrivateKeyFile GetDsaKey()
  565. {
  566. using (var stream = GetData("Key.SSH2.DSA.txt"))
  567. {
  568. return new PrivateKeyFile(stream);
  569. }
  570. }
  571. }
  572. }