2
0

ScpTests.cs 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. using Renci.SshNet.Common;
  2. namespace Renci.SshNet.IntegrationTests
  3. {
  4. // TODO SCP: UPLOAD / DOWNLOAD ZERO LENGTH FILES
  5. // TODO SCP: UPLOAD / DOWNLOAD EMPTY DIRECTORY
  6. // TODO SCP: UPLOAD DIRECTORY THAT ALREADY EXISTS ON REMOTE HOST
  7. [TestClass]
  8. public class ScpTests : TestBase
  9. {
  10. private IConnectionInfoFactory _connectionInfoFactory;
  11. private IRemotePathTransformation _remotePathTransformation;
  12. [TestInitialize]
  13. public void SetUp()
  14. {
  15. _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
  16. _remotePathTransformation = RemotePathTransformation.ShellQuote;
  17. }
  18. [DataTestMethod]
  19. [DynamicData(nameof(GetScpDownloadStreamDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
  20. public void Scp_Download_Stream_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
  21. string remotePath,
  22. string remoteFile)
  23. {
  24. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  25. // remove complete directory if it's not the home directory of the user
  26. // or else remove the remote file
  27. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  28. {
  29. client.Connect();
  30. if (client.Exists(completeRemotePath))
  31. {
  32. client.DeleteFile(completeRemotePath);
  33. }
  34. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  35. {
  36. if (client.Exists(remotePath))
  37. {
  38. client.DeleteDirectory(remotePath);
  39. }
  40. }
  41. }
  42. try
  43. {
  44. using (var downloaded = new MemoryStream())
  45. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  46. {
  47. if (remotePathTransformation != null)
  48. {
  49. client.RemotePathTransformation = remotePathTransformation;
  50. }
  51. client.Connect();
  52. try
  53. {
  54. client.Download(completeRemotePath, downloaded);
  55. Assert.Fail();
  56. }
  57. catch (ScpException ex)
  58. {
  59. Assert.IsNull(ex.InnerException);
  60. Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
  61. }
  62. }
  63. }
  64. finally
  65. {
  66. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  67. {
  68. client.Connect();
  69. if (client.Exists(completeRemotePath))
  70. {
  71. client.DeleteFile(completeRemotePath);
  72. }
  73. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  74. {
  75. if (client.Exists(remotePath))
  76. {
  77. client.DeleteDirectory(remotePath);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. [DataTestMethod]
  84. [DynamicData(nameof(GetScpDownloadStreamFileDoesNotExistData), DynamicDataSourceType.Method)]
  85. public void Scp_Download_Stream_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
  86. string remotePath,
  87. string remoteFile)
  88. {
  89. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  90. // remove complete directory if it's not the home directory of the user
  91. // or else remove the remote file
  92. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  93. {
  94. client.Connect();
  95. if (client.Exists(completeRemotePath))
  96. {
  97. client.DeleteFile(completeRemotePath);
  98. }
  99. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  100. {
  101. if (client.Exists(remotePath))
  102. {
  103. client.DeleteDirectory(remotePath);
  104. }
  105. client.CreateDirectory(remotePath);
  106. }
  107. }
  108. try
  109. {
  110. using (var downloaded = new MemoryStream())
  111. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  112. {
  113. if (remotePathTransformation != null)
  114. {
  115. client.RemotePathTransformation = remotePathTransformation;
  116. }
  117. client.Connect();
  118. try
  119. {
  120. client.Download(completeRemotePath, downloaded);
  121. Assert.Fail();
  122. }
  123. catch (ScpException ex)
  124. {
  125. Assert.IsNull(ex.InnerException);
  126. Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
  127. }
  128. }
  129. }
  130. finally
  131. {
  132. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  133. {
  134. client.Connect();
  135. if (client.Exists(completeRemotePath))
  136. {
  137. client.DeleteFile(completeRemotePath);
  138. }
  139. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  140. {
  141. if (client.Exists(remotePath))
  142. {
  143. client.DeleteDirectory(remotePath);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. [DataTestMethod]
  150. [DynamicData(nameof(GetScpDownloadDirectoryInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
  151. public void Scp_Download_DirectoryInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
  152. string remotePath)
  153. {
  154. var localDirectory = Path.GetTempFileName();
  155. File.Delete(localDirectory);
  156. Directory.CreateDirectory(localDirectory);
  157. try
  158. {
  159. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  160. {
  161. client.Connect();
  162. if (client.Exists(remotePath))
  163. {
  164. client.DeleteDirectory(remotePath);
  165. }
  166. }
  167. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  168. {
  169. if (remotePathTransformation != null)
  170. {
  171. client.RemotePathTransformation = remotePathTransformation;
  172. }
  173. client.Connect();
  174. try
  175. {
  176. client.Download(remotePath, new DirectoryInfo(localDirectory));
  177. Assert.Fail();
  178. }
  179. catch (ScpException ex)
  180. {
  181. Assert.IsNull(ex.InnerException);
  182. Assert.AreEqual($"scp: {remotePath}: No such file or directory", ex.Message);
  183. }
  184. }
  185. }
  186. finally
  187. {
  188. Directory.Delete(localDirectory, true);
  189. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  190. {
  191. client.Connect();
  192. if (client.Exists(remotePath))
  193. {
  194. client.DeleteDirectory(remotePath);
  195. }
  196. }
  197. }
  198. }
  199. [DataTestMethod]
  200. [DynamicData(nameof(GetScpDownloadDirectoryInfoExistingFileData), DynamicDataSourceType.Method)]
  201. public void Scp_Download_DirectoryInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
  202. string remotePath)
  203. {
  204. var content = CreateMemoryStream(100);
  205. content.Position = 0;
  206. var localDirectory = Path.GetTempFileName();
  207. File.Delete(localDirectory);
  208. Directory.CreateDirectory(localDirectory);
  209. var localFile = Path.Combine(localDirectory, PosixPath.GetFileName(remotePath));
  210. try
  211. {
  212. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  213. {
  214. client.Connect();
  215. client.UploadFile(content, remotePath);
  216. }
  217. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  218. {
  219. if (remotePathTransformation != null)
  220. {
  221. client.RemotePathTransformation = remotePathTransformation;
  222. }
  223. client.Connect();
  224. client.Download(remotePath, new DirectoryInfo(localDirectory));
  225. }
  226. Assert.IsTrue(File.Exists(localFile));
  227. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  228. {
  229. client.Connect();
  230. using (var downloaded = new MemoryStream())
  231. {
  232. client.DownloadFile(remotePath, downloaded);
  233. downloaded.Position = 0;
  234. Assert.AreEqual(CreateFileHash(localFile), CreateHash(downloaded));
  235. }
  236. }
  237. }
  238. finally
  239. {
  240. Directory.Delete(localDirectory, true);
  241. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  242. {
  243. client.Connect();
  244. if (client.Exists(remotePath))
  245. {
  246. client.DeleteFile(remotePath);
  247. }
  248. }
  249. }
  250. }
  251. [DataTestMethod]
  252. [DynamicData(nameof(GetScpDownloadDirectoryInfoExistingDirectoryData), DynamicDataSourceType.Method)]
  253. public void Scp_Download_DirectoryInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
  254. string remotePath)
  255. {
  256. var localDirectory = Path.GetTempFileName();
  257. File.Delete(localDirectory);
  258. Directory.CreateDirectory(localDirectory);
  259. var localPathFile1 = Path.Combine(localDirectory, "file1 23");
  260. var remotePathFile1 = CombinePaths(remotePath, "file1 23");
  261. var contentFile1 = CreateMemoryStream(1024);
  262. contentFile1.Position = 0;
  263. var localPathFile2 = Path.Combine(localDirectory, "file2 #$%");
  264. var remotePathFile2 = CombinePaths(remotePath, "file2 #$%");
  265. var contentFile2 = CreateMemoryStream(2048);
  266. contentFile2.Position = 0;
  267. var localPathSubDirectory = Path.Combine(localDirectory, "subdir $1%#");
  268. var remotePathSubDirectory = CombinePaths(remotePath, "subdir $1%#");
  269. var localPathFile3 = Path.Combine(localPathSubDirectory, "file3 %$#");
  270. var remotePathFile3 = CombinePaths(remotePathSubDirectory, "file3 %$#");
  271. var contentFile3 = CreateMemoryStream(256);
  272. contentFile3.Position = 0;
  273. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  274. {
  275. client.Connect();
  276. if (client.Exists(remotePathFile1))
  277. {
  278. client.DeleteFile(remotePathFile1);
  279. }
  280. if (client.Exists(remotePathFile2))
  281. {
  282. client.DeleteFile(remotePathFile2);
  283. }
  284. if (client.Exists(remotePathFile3))
  285. {
  286. client.DeleteFile(remotePathFile3);
  287. }
  288. if (client.Exists(remotePathSubDirectory))
  289. {
  290. client.DeleteDirectory(remotePathSubDirectory);
  291. }
  292. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  293. {
  294. if (client.Exists(remotePath))
  295. {
  296. client.DeleteDirectory(remotePath);
  297. }
  298. }
  299. }
  300. try
  301. {
  302. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  303. {
  304. client.Connect();
  305. if (!client.Exists(remotePath))
  306. {
  307. client.CreateDirectory(remotePath);
  308. }
  309. client.UploadFile(contentFile1, remotePathFile1);
  310. client.UploadFile(contentFile1, remotePathFile2);
  311. client.CreateDirectory(remotePathSubDirectory);
  312. client.UploadFile(contentFile3, remotePathFile3);
  313. }
  314. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  315. {
  316. if (remotePathTransformation != null)
  317. {
  318. client.RemotePathTransformation = remotePathTransformation;
  319. }
  320. client.Connect();
  321. client.Download(remotePath, new DirectoryInfo(localDirectory));
  322. }
  323. var localFiles = Directory.GetFiles(localDirectory);
  324. Assert.AreEqual(2, localFiles.Length);
  325. Assert.IsTrue(localFiles.Contains(localPathFile1));
  326. Assert.IsTrue(localFiles.Contains(localPathFile2));
  327. var localSubDirecties = Directory.GetDirectories(localDirectory);
  328. Assert.AreEqual(1, localSubDirecties.Length);
  329. Assert.AreEqual(localPathSubDirectory, localSubDirecties[0]);
  330. var localFilesSubDirectory = Directory.GetFiles(localPathSubDirectory);
  331. Assert.AreEqual(1, localFilesSubDirectory.Length);
  332. Assert.AreEqual(localPathFile3, localFilesSubDirectory[0]);
  333. Assert.AreEqual(0, Directory.GetDirectories(localPathSubDirectory).Length);
  334. }
  335. finally
  336. {
  337. Directory.Delete(localDirectory, true);
  338. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  339. {
  340. client.Connect();
  341. if (client.Exists(remotePathFile1))
  342. {
  343. client.DeleteFile(remotePathFile1);
  344. }
  345. if (client.Exists(remotePathFile2))
  346. {
  347. client.DeleteFile(remotePathFile2);
  348. }
  349. if (client.Exists(remotePathFile3))
  350. {
  351. client.DeleteFile(remotePathFile3);
  352. }
  353. if (client.Exists(remotePathSubDirectory))
  354. {
  355. client.DeleteDirectory(remotePathSubDirectory);
  356. }
  357. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  358. {
  359. if (client.Exists(remotePath))
  360. {
  361. client.DeleteDirectory(remotePath);
  362. }
  363. }
  364. }
  365. }
  366. }
  367. [DataTestMethod]
  368. [DynamicData(nameof(GetScpDownloadFileInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
  369. public void Scp_Download_FileInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
  370. string remotePath,
  371. string remoteFile)
  372. {
  373. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  374. // remove complete directory if it's not the home directory of the user
  375. // or else remove the remote file
  376. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  377. {
  378. client.Connect();
  379. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  380. {
  381. if (client.Exists(remotePath))
  382. {
  383. client.DeleteDirectory(remotePath);
  384. }
  385. }
  386. }
  387. var fileInfo = new FileInfo(Path.GetTempFileName());
  388. try
  389. {
  390. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  391. {
  392. if (remotePathTransformation != null)
  393. {
  394. client.RemotePathTransformation = remotePathTransformation;
  395. }
  396. client.Connect();
  397. try
  398. {
  399. client.Download(completeRemotePath, fileInfo);
  400. Assert.Fail();
  401. }
  402. catch (ScpException ex)
  403. {
  404. Assert.IsNull(ex.InnerException);
  405. Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
  406. }
  407. }
  408. }
  409. finally
  410. {
  411. fileInfo.Delete();
  412. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  413. {
  414. client.Connect();
  415. if (client.Exists(completeRemotePath))
  416. {
  417. client.DeleteFile(completeRemotePath);
  418. }
  419. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  420. {
  421. if (client.Exists(remotePath))
  422. {
  423. client.DeleteDirectory(remotePath);
  424. }
  425. }
  426. }
  427. }
  428. }
  429. [DataTestMethod]
  430. [DynamicData(nameof(GetScpDownloadFileInfoFileDoesNotExistData), DynamicDataSourceType.Method)]
  431. public void Scp_Download_FileInfo_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
  432. string remotePath,
  433. string remoteFile)
  434. {
  435. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  436. // remove complete directory if it's not the home directory of the user
  437. // or else remove the remote file
  438. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  439. {
  440. client.Connect();
  441. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  442. {
  443. if (client.Exists(remotePath))
  444. {
  445. client.DeleteDirectory(remotePath);
  446. }
  447. client.CreateDirectory(remotePath);
  448. }
  449. }
  450. var fileInfo = new FileInfo(Path.GetTempFileName());
  451. try
  452. {
  453. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  454. {
  455. if (remotePathTransformation != null)
  456. {
  457. client.RemotePathTransformation = remotePathTransformation;
  458. }
  459. client.Connect();
  460. try
  461. {
  462. client.Download(completeRemotePath, fileInfo);
  463. Assert.Fail();
  464. }
  465. catch (ScpException ex)
  466. {
  467. Assert.IsNull(ex.InnerException);
  468. Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
  469. }
  470. }
  471. }
  472. finally
  473. {
  474. fileInfo.Delete();
  475. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  476. {
  477. client.Connect();
  478. if (client.Exists(completeRemotePath))
  479. {
  480. client.DeleteFile(completeRemotePath);
  481. }
  482. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  483. {
  484. if (client.Exists(remotePath))
  485. {
  486. client.DeleteDirectory(remotePath);
  487. }
  488. }
  489. }
  490. }
  491. }
  492. [DataTestMethod]
  493. [DynamicData(nameof(GetScpDownloadFileInfoExistingDirectoryData), DynamicDataSourceType.Method)]
  494. public void Scp_Download_FileInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
  495. string remotePath)
  496. {
  497. // remove complete directory if it's not the home directory of the user
  498. // or else remove the remote file
  499. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  500. {
  501. client.Connect();
  502. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  503. {
  504. if (client.Exists(remotePath))
  505. {
  506. client.DeleteDirectory(remotePath);
  507. }
  508. client.CreateDirectory(remotePath);
  509. }
  510. }
  511. var fileInfo = new FileInfo(Path.GetTempFileName());
  512. fileInfo.Delete();
  513. try
  514. {
  515. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  516. {
  517. if (remotePathTransformation != null)
  518. {
  519. client.RemotePathTransformation = remotePathTransformation;
  520. }
  521. client.Connect();
  522. try
  523. {
  524. client.Download(remotePath, fileInfo);
  525. Assert.Fail();
  526. }
  527. catch (ScpException ex)
  528. {
  529. Assert.IsNull(ex.InnerException);
  530. Assert.AreEqual($"scp: {remotePath}: not a regular file", ex.Message);
  531. }
  532. Assert.IsFalse(fileInfo.Exists);
  533. }
  534. }
  535. finally
  536. {
  537. fileInfo.Delete();
  538. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  539. {
  540. client.Connect();
  541. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  542. {
  543. if (client.Exists(remotePath))
  544. {
  545. client.DeleteDirectory(remotePath);
  546. }
  547. }
  548. }
  549. }
  550. }
  551. [DataTestMethod]
  552. [DynamicData(nameof(GetScpDownloadFileInfoExistingFileData), DynamicDataSourceType.Method)]
  553. public void Scp_Download_FileInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
  554. string remotePath,
  555. string remoteFile,
  556. int size)
  557. {
  558. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  559. // remove complete directory if it's not the home directory of the user
  560. // or else remove the remote file
  561. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  562. {
  563. client.Connect();
  564. if (client.Exists(completeRemotePath))
  565. {
  566. client.DeleteFile(completeRemotePath);
  567. }
  568. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  569. {
  570. if (client.Exists(remotePath))
  571. {
  572. client.DeleteDirectory(remotePath);
  573. }
  574. client.CreateDirectory(remotePath);
  575. }
  576. }
  577. var fileInfo = new FileInfo(Path.GetTempFileName());
  578. try
  579. {
  580. var content = CreateMemoryStream(size);
  581. content.Position = 0;
  582. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  583. {
  584. if (remotePathTransformation != null)
  585. {
  586. client.RemotePathTransformation = remotePathTransformation;
  587. }
  588. client.Connect();
  589. client.Upload(content, completeRemotePath);
  590. }
  591. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  592. {
  593. if (remotePathTransformation != null)
  594. {
  595. client.RemotePathTransformation = remotePathTransformation;
  596. }
  597. client.Connect();
  598. client.Download(completeRemotePath, fileInfo);
  599. }
  600. using (var fs = fileInfo.OpenRead())
  601. {
  602. var downloadedBytes = new byte[fs.Length];
  603. Assert.AreEqual(downloadedBytes.Length, fs.Read(downloadedBytes, 0, downloadedBytes.Length));
  604. content.Position = 0;
  605. Assert.AreEqual(CreateHash(content), CreateHash(downloadedBytes));
  606. }
  607. }
  608. finally
  609. {
  610. fileInfo.Delete();
  611. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  612. {
  613. client.Connect();
  614. if (client.Exists(completeRemotePath))
  615. {
  616. client.DeleteFile(completeRemotePath);
  617. }
  618. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  619. {
  620. if (client.Exists(remotePath))
  621. {
  622. client.DeleteDirectory(remotePath);
  623. }
  624. }
  625. }
  626. }
  627. }
  628. [DataTestMethod]
  629. [DynamicData(nameof(GetScpDownloadStreamExistingDirectoryData), DynamicDataSourceType.Method)]
  630. public void Scp_Download_Stream_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
  631. string remotePath)
  632. {
  633. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  634. {
  635. client.Connect();
  636. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  637. {
  638. if (client.Exists(remotePath))
  639. {
  640. client.DeleteDirectory(remotePath);
  641. }
  642. client.CreateDirectory(remotePath);
  643. }
  644. }
  645. var file = Path.GetTempFileName();
  646. File.Delete(file);
  647. try
  648. {
  649. using (var fs = File.OpenWrite(file))
  650. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  651. {
  652. if (remotePathTransformation != null)
  653. {
  654. client.RemotePathTransformation = remotePathTransformation;
  655. }
  656. client.Connect();
  657. try
  658. {
  659. client.Download(remotePath, fs);
  660. Assert.Fail();
  661. }
  662. catch (ScpException ex)
  663. {
  664. Assert.IsNull(ex.InnerException);
  665. Assert.AreEqual($"scp: {remotePath}: not a regular file", ex.Message);
  666. }
  667. Assert.AreEqual(0, fs.Length);
  668. }
  669. }
  670. finally
  671. {
  672. File.Delete(file);
  673. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  674. {
  675. client.Connect();
  676. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  677. {
  678. if (client.Exists(remotePath))
  679. {
  680. client.DeleteDirectory(remotePath);
  681. }
  682. }
  683. }
  684. }
  685. }
  686. [DataTestMethod]
  687. [DynamicData(nameof(GetScpDownloadStreamExistingFileData), DynamicDataSourceType.Method)]
  688. public void Scp_Download_Stream_ExistingFile(IRemotePathTransformation remotePathTransformation,
  689. string remotePath,
  690. string remoteFile,
  691. int size)
  692. {
  693. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  694. // remove complete directory if it's not the home directory of the user
  695. // or else remove the remote file
  696. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  697. {
  698. client.Connect();
  699. if (client.Exists(completeRemotePath))
  700. {
  701. client.DeleteFile(completeRemotePath);
  702. }
  703. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  704. {
  705. if (client.Exists(remotePath))
  706. {
  707. client.DeleteDirectory(remotePath);
  708. }
  709. client.CreateDirectory(remotePath);
  710. }
  711. }
  712. var file = CreateTempFile(size);
  713. try
  714. {
  715. using (var fs = File.OpenRead(file))
  716. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  717. {
  718. if (remotePathTransformation != null)
  719. {
  720. client.RemotePathTransformation = remotePathTransformation;
  721. }
  722. client.Connect();
  723. client.Upload(fs, completeRemotePath);
  724. }
  725. using (var fs = File.OpenRead(file))
  726. using (var downloaded = new MemoryStream(size))
  727. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  728. {
  729. if (remotePathTransformation != null)
  730. {
  731. client.RemotePathTransformation = remotePathTransformation;
  732. }
  733. client.Connect();
  734. client.Download(completeRemotePath, downloaded);
  735. downloaded.Position = 0;
  736. Assert.AreEqual(CreateHash(fs), CreateHash(downloaded));
  737. }
  738. }
  739. finally
  740. {
  741. File.Delete(file);
  742. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  743. {
  744. client.Connect();
  745. if (client.Exists(completeRemotePath))
  746. {
  747. client.DeleteFile(completeRemotePath);
  748. }
  749. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  750. {
  751. if (client.Exists(remotePath))
  752. {
  753. client.DeleteDirectory(remotePath);
  754. }
  755. }
  756. }
  757. }
  758. }
  759. [DataTestMethod]
  760. [DynamicData(nameof(GetScpUploadFileStreamDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
  761. public void Scp_Upload_FileStream_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
  762. string remotePath,
  763. string remoteFile)
  764. {
  765. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  766. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  767. {
  768. client.Connect();
  769. if (client.Exists(completeRemotePath))
  770. {
  771. client.DeleteFile(completeRemotePath);
  772. }
  773. if (client.Exists(remotePath))
  774. {
  775. client.DeleteDirectory(remotePath);
  776. }
  777. }
  778. var file = CreateTempFile(1000);
  779. try
  780. {
  781. using (var fs = File.OpenRead(file))
  782. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  783. {
  784. if (remotePathTransformation != null)
  785. {
  786. client.RemotePathTransformation = remotePathTransformation;
  787. }
  788. client.Connect();
  789. try
  790. {
  791. client.Upload(fs, completeRemotePath);
  792. Assert.Fail();
  793. }
  794. catch (ScpException ex)
  795. {
  796. Assert.IsNull(ex.InnerException);
  797. Assert.AreEqual($"scp: {remotePath}: No such file or directory", ex.Message);
  798. }
  799. }
  800. }
  801. finally
  802. {
  803. File.Delete(file);
  804. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  805. {
  806. client.Connect();
  807. if (client.Exists(completeRemotePath))
  808. {
  809. client.DeleteFile(completeRemotePath);
  810. }
  811. if (client.Exists(remotePath))
  812. {
  813. client.DeleteDirectory(remotePath);
  814. }
  815. }
  816. }
  817. }
  818. [DataTestMethod]
  819. [DynamicData(nameof(GetScpUploadFileStreamExistingDirectoryData), DynamicDataSourceType.Method)]
  820. public void Scp_Upload_FileStream_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
  821. string remoteFile)
  822. {
  823. using (var client = new SshClient(_connectionInfoFactory.Create()))
  824. {
  825. client.Connect();
  826. using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteFile)))
  827. {
  828. command.Execute();
  829. }
  830. }
  831. var file = CreateTempFile(1000);
  832. try
  833. {
  834. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  835. {
  836. client.Connect();
  837. client.CreateDirectory(remoteFile);
  838. }
  839. using (var fs = File.OpenRead(file))
  840. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  841. {
  842. if (remotePathTransformation != null)
  843. {
  844. client.RemotePathTransformation = remotePathTransformation;
  845. }
  846. client.Connect();
  847. try
  848. {
  849. client.Upload(fs, remoteFile);
  850. Assert.Fail();
  851. }
  852. catch (ScpException ex)
  853. {
  854. Assert.IsNull(ex.InnerException);
  855. Assert.AreEqual($"scp: {remoteFile}: Is a directory", ex.Message);
  856. }
  857. }
  858. }
  859. finally
  860. {
  861. File.Delete(file);
  862. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  863. {
  864. client.Connect();
  865. if (client.Exists(remoteFile))
  866. {
  867. client.DeleteDirectory(remoteFile);
  868. }
  869. }
  870. }
  871. }
  872. [DataTestMethod]
  873. [DynamicData(nameof(ScpUploadFileStreamExistingFileData), DynamicDataSourceType.Method)]
  874. public void Scp_Upload_FileStream_ExistingFile(IRemotePathTransformation remotePathTransformation,
  875. string remoteFile)
  876. {
  877. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  878. {
  879. client.Connect();
  880. if (client.Exists(remoteFile))
  881. {
  882. client.DeleteFile(remoteFile);
  883. }
  884. }
  885. // original content is bigger than new content to ensure file is fully overwritten
  886. var originalContent = CreateMemoryStream(2000);
  887. var file = CreateTempFile(1000);
  888. try
  889. {
  890. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  891. {
  892. client.Connect();
  893. originalContent.Position = 0;
  894. client.UploadFile(originalContent, remoteFile);
  895. }
  896. using (var fs = File.OpenRead(file))
  897. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  898. {
  899. if (remotePathTransformation != null)
  900. {
  901. client.RemotePathTransformation = remotePathTransformation;
  902. }
  903. client.Connect();
  904. client.Upload(fs, remoteFile);
  905. }
  906. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  907. {
  908. client.Connect();
  909. using (var downloaded = new MemoryStream(1000))
  910. {
  911. client.DownloadFile(remoteFile, downloaded);
  912. downloaded.Position = 0;
  913. Assert.AreEqual(CreateFileHash(file), CreateHash(downloaded));
  914. }
  915. }
  916. }
  917. finally
  918. {
  919. File.Delete(file);
  920. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  921. {
  922. client.Connect();
  923. if (client.Exists(remoteFile))
  924. {
  925. client.DeleteFile(remoteFile);
  926. }
  927. }
  928. }
  929. }
  930. [DataTestMethod]
  931. [DynamicData(nameof(GetScpUploadFileStreamFileDoesNotExistData), DynamicDataSourceType.Method)]
  932. public void Scp_Upload_FileStream_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
  933. string remotePath,
  934. string remoteFile,
  935. int size)
  936. {
  937. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  938. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  939. {
  940. client.Connect();
  941. if (client.Exists(completeRemotePath))
  942. {
  943. client.DeleteFile(completeRemotePath);
  944. }
  945. // remove complete directory if it's not the home directory of the user
  946. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  947. {
  948. if (client.Exists(remotePath))
  949. {
  950. client.DeleteDirectory(remotePath);
  951. }
  952. }
  953. }
  954. var file = CreateTempFile(size);
  955. try
  956. {
  957. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  958. {
  959. client.Connect();
  960. // create directory if it's not the home directory of the user
  961. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  962. {
  963. if (!client.Exists((remotePath)))
  964. {
  965. client.CreateDirectory(remotePath);
  966. }
  967. }
  968. }
  969. using (var fs = File.OpenRead(file))
  970. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  971. {
  972. if (remotePathTransformation != null)
  973. {
  974. client.RemotePathTransformation = remotePathTransformation;
  975. }
  976. client.Connect();
  977. client.Upload(fs, completeRemotePath);
  978. }
  979. using (var fs = File.OpenRead(file))
  980. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  981. {
  982. client.Connect();
  983. var sftpFile = client.Get(completeRemotePath);
  984. Assert.AreEqual(GetAbsoluteRemotePath(client, remotePath, remoteFile), sftpFile.FullName);
  985. Assert.AreEqual(size, sftpFile.Length);
  986. var downloaded = client.ReadAllBytes(completeRemotePath);
  987. Assert.AreEqual(CreateHash(fs), CreateHash(downloaded));
  988. }
  989. }
  990. finally
  991. {
  992. File.Delete(file);
  993. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  994. {
  995. client.Connect();
  996. if (client.Exists(completeRemotePath))
  997. {
  998. client.DeleteFile(completeRemotePath);
  999. }
  1000. // remove complete directory if it's not the home directory of the user
  1001. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  1002. {
  1003. if (client.Exists(remotePath))
  1004. {
  1005. client.DeleteDirectory(remotePath);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. /// <summary>
  1012. /// https://github.com/sshnet/SSH.NET/issues/289
  1013. /// </summary>
  1014. [DataTestMethod]
  1015. [DynamicData(nameof(GetScpUploadFileInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
  1016. public void Scp_Upload_FileInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
  1017. string remotePath,
  1018. string remoteFile)
  1019. {
  1020. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  1021. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1022. {
  1023. client.Connect();
  1024. if (client.Exists(completeRemotePath))
  1025. {
  1026. client.DeleteFile(completeRemotePath);
  1027. }
  1028. if (client.Exists(remotePath))
  1029. {
  1030. client.DeleteDirectory(remotePath);
  1031. }
  1032. }
  1033. var file = CreateTempFile(1000);
  1034. try
  1035. {
  1036. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1037. {
  1038. if (remotePathTransformation != null)
  1039. {
  1040. client.RemotePathTransformation = remotePathTransformation;
  1041. }
  1042. client.Connect();
  1043. try
  1044. {
  1045. client.Upload(new FileInfo(file), completeRemotePath);
  1046. Assert.Fail();
  1047. }
  1048. catch (ScpException ex)
  1049. {
  1050. Assert.IsNull(ex.InnerException);
  1051. Assert.AreEqual($"scp: {remotePath}: No such file or directory", ex.Message);
  1052. }
  1053. }
  1054. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1055. {
  1056. client.Connect();
  1057. Assert.IsFalse(client.Exists(completeRemotePath));
  1058. Assert.IsFalse(client.Exists(remotePath));
  1059. }
  1060. }
  1061. finally
  1062. {
  1063. File.Delete(file);
  1064. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1065. {
  1066. client.Connect();
  1067. if (client.Exists(completeRemotePath))
  1068. {
  1069. client.DeleteFile(completeRemotePath);
  1070. }
  1071. if (client.Exists(remotePath))
  1072. {
  1073. client.DeleteDirectory(remotePath);
  1074. }
  1075. }
  1076. }
  1077. }
  1078. /// <summary>
  1079. /// https://github.com/sshnet/SSH.NET/issues/286
  1080. /// </summary>
  1081. [DataTestMethod]
  1082. [DynamicData(nameof(GetScpUploadFileInfoExistingDirectoryData), DynamicDataSourceType.Method)]
  1083. public void Scp_Upload_FileInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
  1084. string remoteFile)
  1085. {
  1086. using (var client = new SshClient(_connectionInfoFactory.Create()))
  1087. {
  1088. client.Connect();
  1089. using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteFile)))
  1090. {
  1091. command.Execute();
  1092. }
  1093. }
  1094. var file = CreateTempFile(1000);
  1095. try
  1096. {
  1097. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1098. {
  1099. client.Connect();
  1100. client.CreateDirectory(remoteFile);
  1101. }
  1102. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1103. {
  1104. if (remotePathTransformation != null)
  1105. {
  1106. client.RemotePathTransformation = remotePathTransformation;
  1107. }
  1108. client.Connect();
  1109. try
  1110. {
  1111. client.Upload(new FileInfo(file), remoteFile);
  1112. Assert.Fail();
  1113. }
  1114. catch (ScpException ex)
  1115. {
  1116. Assert.IsNull(ex.InnerException);
  1117. Assert.AreEqual($"scp: {remoteFile}: Is a directory", ex.Message);
  1118. }
  1119. }
  1120. }
  1121. finally
  1122. {
  1123. File.Delete(file);
  1124. using (var client = new SshClient(_connectionInfoFactory.Create()))
  1125. {
  1126. client.Connect();
  1127. using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteFile)))
  1128. {
  1129. command.Execute();
  1130. }
  1131. }
  1132. }
  1133. }
  1134. [DataTestMethod]
  1135. [DynamicData(nameof(GetScpUploadFileInfoExistingFileData), DynamicDataSourceType.Method)]
  1136. public void Scp_Upload_FileInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
  1137. string remoteFile)
  1138. {
  1139. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1140. {
  1141. client.Connect();
  1142. if (client.Exists(remoteFile))
  1143. {
  1144. client.DeleteFile(remoteFile);
  1145. }
  1146. }
  1147. // original content is bigger than new content to ensure file is fully overwritten
  1148. var originalContent = CreateMemoryStream(2000);
  1149. var file = CreateTempFile(1000);
  1150. try
  1151. {
  1152. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1153. {
  1154. client.Connect();
  1155. originalContent.Position = 0;
  1156. client.UploadFile(originalContent, remoteFile);
  1157. }
  1158. var fileInfo = new FileInfo(file)
  1159. {
  1160. LastAccessTimeUtc = new DateTime(1973, 8, 13, 20, 15, 33, DateTimeKind.Utc),
  1161. LastWriteTimeUtc = new DateTime(1974, 1, 24, 3, 55, 12, DateTimeKind.Utc)
  1162. };
  1163. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1164. {
  1165. if (remotePathTransformation != null)
  1166. {
  1167. client.RemotePathTransformation = remotePathTransformation;
  1168. }
  1169. client.Connect();
  1170. client.Upload(fileInfo, remoteFile);
  1171. }
  1172. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1173. {
  1174. client.Connect();
  1175. var uploadedFile = client.Get(remoteFile);
  1176. Assert.AreEqual(fileInfo.LastAccessTimeUtc, uploadedFile.LastAccessTimeUtc);
  1177. Assert.AreEqual(fileInfo.LastWriteTimeUtc, uploadedFile.LastWriteTimeUtc);
  1178. using (var downloaded = new MemoryStream(1000))
  1179. {
  1180. client.DownloadFile(remoteFile, downloaded);
  1181. downloaded.Position = 0;
  1182. Assert.AreEqual(CreateFileHash(file), CreateHash(downloaded));
  1183. }
  1184. }
  1185. }
  1186. finally
  1187. {
  1188. File.Delete(file);
  1189. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1190. {
  1191. client.Connect();
  1192. if (client.Exists(remoteFile))
  1193. {
  1194. client.DeleteFile(remoteFile);
  1195. }
  1196. }
  1197. }
  1198. }
  1199. [DataTestMethod]
  1200. [DynamicData(nameof(GetScpUploadFileInfoFileDoesNotExistData), DynamicDataSourceType.Method)]
  1201. public void Scp_Upload_FileInfo_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
  1202. string remotePath,
  1203. string remoteFile,
  1204. int size)
  1205. {
  1206. var completeRemotePath = CombinePaths(remotePath, remoteFile);
  1207. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1208. {
  1209. client.Connect();
  1210. if (client.Exists(completeRemotePath))
  1211. {
  1212. client.DeleteFile(completeRemotePath);
  1213. }
  1214. // remove complete directory if it's not the home directory of the user
  1215. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  1216. {
  1217. if (client.Exists(remotePath))
  1218. {
  1219. client.DeleteDirectory(remotePath);
  1220. }
  1221. }
  1222. }
  1223. var file = CreateTempFile(size);
  1224. try
  1225. {
  1226. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1227. {
  1228. client.Connect();
  1229. // create directory if it's not the home directory of the user
  1230. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  1231. {
  1232. if (!client.Exists(remotePath))
  1233. {
  1234. client.CreateDirectory(remotePath);
  1235. }
  1236. }
  1237. }
  1238. var fileInfo = new FileInfo(file)
  1239. {
  1240. LastAccessTimeUtc = new DateTime(1973, 8, 13, 20, 15, 33, DateTimeKind.Utc),
  1241. LastWriteTimeUtc = new DateTime(1974, 1, 24, 3, 55, 12, DateTimeKind.Utc)
  1242. };
  1243. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1244. {
  1245. if (remotePathTransformation != null)
  1246. {
  1247. client.RemotePathTransformation = remotePathTransformation;
  1248. }
  1249. client.Connect();
  1250. client.Upload(fileInfo, completeRemotePath);
  1251. }
  1252. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1253. {
  1254. client.Connect();
  1255. var uploadedFile = client.Get(completeRemotePath);
  1256. Assert.AreEqual(fileInfo.LastAccessTimeUtc, uploadedFile.LastAccessTimeUtc);
  1257. Assert.AreEqual(fileInfo.LastWriteTimeUtc, uploadedFile.LastWriteTimeUtc);
  1258. Assert.AreEqual(size, uploadedFile.Length);
  1259. using (var downloaded = new MemoryStream(size))
  1260. {
  1261. client.DownloadFile(completeRemotePath, downloaded);
  1262. downloaded.Position = 0;
  1263. Assert.AreEqual(CreateFileHash(file), CreateHash(downloaded));
  1264. }
  1265. }
  1266. }
  1267. finally
  1268. {
  1269. File.Delete(file);
  1270. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1271. {
  1272. client.Connect();
  1273. if (client.Exists(completeRemotePath))
  1274. {
  1275. client.Delete(completeRemotePath);
  1276. }
  1277. // remove complete directory if it's not the home directory of the user
  1278. if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
  1279. {
  1280. if (client.Exists(remotePath))
  1281. {
  1282. client.DeleteDirectory(remotePath);
  1283. }
  1284. }
  1285. }
  1286. }
  1287. }
  1288. [DataTestMethod]
  1289. [DynamicData(nameof(GetScpUploadDirectoryInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
  1290. public void Scp_Upload_DirectoryInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
  1291. string remoteDirectory)
  1292. {
  1293. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1294. {
  1295. client.Connect();
  1296. if (client.Exists((remoteDirectory)))
  1297. {
  1298. client.DeleteDirectory(remoteDirectory);
  1299. }
  1300. }
  1301. var localDirectory = Path.GetTempFileName();
  1302. File.Delete(localDirectory);
  1303. Directory.CreateDirectory(localDirectory);
  1304. try
  1305. {
  1306. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1307. {
  1308. if (remotePathTransformation != null)
  1309. {
  1310. client.RemotePathTransformation = remotePathTransformation;
  1311. }
  1312. client.Connect();
  1313. try
  1314. {
  1315. client.Upload(new DirectoryInfo(localDirectory), remoteDirectory);
  1316. Assert.Fail();
  1317. }
  1318. catch (ScpException ex)
  1319. {
  1320. Assert.IsNull(ex.InnerException);
  1321. Assert.AreEqual($"scp: {remoteDirectory}: No such file or directory", ex.Message);
  1322. }
  1323. }
  1324. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1325. {
  1326. client.Connect();
  1327. Assert.IsFalse(client.Exists(remoteDirectory));
  1328. }
  1329. }
  1330. finally
  1331. {
  1332. Directory.Delete(localDirectory, true);
  1333. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1334. {
  1335. client.Connect();
  1336. if (client.Exists((remoteDirectory)))
  1337. {
  1338. client.DeleteDirectory(remoteDirectory);
  1339. }
  1340. }
  1341. }
  1342. }
  1343. [DataTestMethod]
  1344. [DynamicData(nameof(GetScpUploadDirectoryInfoExistingDirectoryData), DynamicDataSourceType.Method)]
  1345. public void Scp_Upload_DirectoryInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
  1346. string remoteDirectory)
  1347. {
  1348. string absoluteRemoteDirectory = GetAbsoluteRemotePath(_connectionInfoFactory, remoteDirectory);
  1349. var remotePathFile1 = CombinePaths(remoteDirectory, "file1");
  1350. var remotePathFile2 = CombinePaths(remoteDirectory, "file2");
  1351. var absoluteRemoteSubDirectory1 = CombinePaths(absoluteRemoteDirectory, "sub1");
  1352. var remoteSubDirectory1 = CombinePaths(remoteDirectory, "sub1");
  1353. var remotePathSubFile1 = CombinePaths(remoteSubDirectory1, "file1");
  1354. var remotePathSubFile2 = CombinePaths(remoteSubDirectory1, "file2");
  1355. var absoluteRemoteSubDirectory2 = CombinePaths(absoluteRemoteDirectory, "sub2");
  1356. var remoteSubDirectory2 = CombinePaths(remoteDirectory, "sub2");
  1357. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1358. {
  1359. client.Connect();
  1360. if (client.Exists(remotePathSubFile1))
  1361. {
  1362. client.DeleteFile(remotePathSubFile1);
  1363. }
  1364. if (client.Exists(remotePathSubFile2))
  1365. {
  1366. client.DeleteFile(remotePathSubFile2);
  1367. }
  1368. if (client.Exists(remoteSubDirectory1))
  1369. {
  1370. client.DeleteDirectory(remoteSubDirectory1);
  1371. }
  1372. if (client.Exists(remoteSubDirectory2))
  1373. {
  1374. client.DeleteDirectory(remoteSubDirectory2);
  1375. }
  1376. if (client.Exists(remotePathFile1))
  1377. {
  1378. client.DeleteFile(remotePathFile1);
  1379. }
  1380. if (client.Exists(remotePathFile2))
  1381. {
  1382. client.DeleteFile(remotePathFile2);
  1383. }
  1384. if (remoteDirectory.Length > 0 && remoteDirectory != "." && remoteDirectory != client.WorkingDirectory)
  1385. {
  1386. if (client.Exists(remoteDirectory))
  1387. {
  1388. client.DeleteDirectory(remoteDirectory);
  1389. }
  1390. client.CreateDirectory(remoteDirectory);
  1391. }
  1392. }
  1393. var localDirectory = Path.GetTempFileName();
  1394. File.Delete(localDirectory);
  1395. Directory.CreateDirectory(localDirectory);
  1396. var localPathFile1 = Path.Combine(localDirectory, "file1");
  1397. var localPathFile2 = Path.Combine(localDirectory, "file2");
  1398. var localSubDirectory1 = Path.Combine(localDirectory, "sub1");
  1399. var localPathSubFile1 = Path.Combine(localSubDirectory1, "file1");
  1400. var localPathSubFile2 = Path.Combine(localSubDirectory1, "file2");
  1401. var localSubDirectory2 = Path.Combine(localDirectory, "sub2");
  1402. try
  1403. {
  1404. CreateFile(localPathFile1, 2000);
  1405. File.SetLastWriteTimeUtc(localPathFile1, new DateTime(2015, 8, 24, 5, 32, 16, DateTimeKind.Utc));
  1406. CreateFile(localPathFile2, 1000);
  1407. File.SetLastWriteTimeUtc(localPathFile2, new DateTime(2012, 3, 27, 23, 2, 54, DateTimeKind.Utc));
  1408. // create subdirectory with two files
  1409. Directory.CreateDirectory(localSubDirectory1);
  1410. CreateFile(localPathSubFile1, 1000);
  1411. File.SetLastWriteTimeUtc(localPathSubFile1, new DateTime(2013, 4, 12, 16, 54, 22, DateTimeKind.Utc));
  1412. CreateFile(localPathSubFile2, 2000);
  1413. File.SetLastWriteTimeUtc(localPathSubFile2, new DateTime(2015, 8, 4, 12, 43, 12, DateTimeKind.Utc));
  1414. Directory.SetLastWriteTimeUtc(localSubDirectory1,
  1415. new DateTime(2014, 6, 12, 13, 2, 44, DateTimeKind.Utc));
  1416. // create empty subdirectory
  1417. Directory.CreateDirectory(localSubDirectory2);
  1418. Directory.SetLastWriteTimeUtc(localSubDirectory2,
  1419. new DateTime(2011, 5, 14, 1, 5, 12, DateTimeKind.Utc));
  1420. Directory.SetLastWriteTimeUtc(localDirectory, new DateTime(2015, 10, 14, 22, 45, 11, DateTimeKind.Utc));
  1421. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1422. {
  1423. if (remotePathTransformation != null)
  1424. {
  1425. client.RemotePathTransformation = remotePathTransformation;
  1426. }
  1427. client.Connect();
  1428. client.Upload(new DirectoryInfo(localDirectory), remoteDirectory);
  1429. }
  1430. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1431. {
  1432. client.Connect();
  1433. Assert.IsTrue(client.Exists(remoteDirectory));
  1434. var remoteSftpDirectory = client.Get(remoteDirectory);
  1435. Assert.IsNotNull(remoteSftpDirectory);
  1436. Assert.AreEqual(absoluteRemoteDirectory, remoteSftpDirectory.FullName);
  1437. Assert.IsTrue(remoteSftpDirectory.IsDirectory);
  1438. Assert.IsFalse(remoteSftpDirectory.IsRegularFile);
  1439. // Due to CVE-2018-20685, we can no longer set the times or modes on a file or directory
  1440. // that refers to the current directory ('.'), the parent directory ('..') or a directory
  1441. // containing a forward slash ('/').
  1442. Assert.AreNotEqual(Directory.GetLastWriteTimeUtc(localDirectory), remoteSftpDirectory.LastWriteTimeUtc);
  1443. Assert.IsTrue(client.Exists(remotePathFile1));
  1444. Assert.AreEqual(CreateFileHash(localPathFile1), CreateRemoteFileHash(client, remotePathFile1));
  1445. var remoteSftpFile = client.Get(remotePathFile1);
  1446. Assert.IsNotNull(remoteSftpFile);
  1447. Assert.IsFalse(remoteSftpFile.IsDirectory);
  1448. Assert.IsTrue(remoteSftpFile.IsRegularFile);
  1449. Assert.AreEqual(File.GetLastWriteTimeUtc(localPathFile1), remoteSftpFile.LastWriteTimeUtc);
  1450. Assert.IsTrue(client.Exists(remotePathFile2));
  1451. Assert.AreEqual(CreateFileHash(localPathFile2), CreateRemoteFileHash(client, remotePathFile2));
  1452. remoteSftpFile = client.Get(remotePathFile2);
  1453. Assert.IsNotNull(remoteSftpFile);
  1454. Assert.IsFalse(remoteSftpFile.IsDirectory);
  1455. Assert.IsTrue(remoteSftpFile.IsRegularFile);
  1456. Assert.AreEqual(File.GetLastWriteTimeUtc(localPathFile2), remoteSftpFile.LastWriteTimeUtc);
  1457. Assert.IsTrue(client.Exists(remoteSubDirectory1));
  1458. remoteSftpDirectory = client.Get(remoteSubDirectory1);
  1459. Assert.IsNotNull(remoteSftpDirectory);
  1460. Assert.AreEqual(absoluteRemoteSubDirectory1, remoteSftpDirectory.FullName);
  1461. Assert.IsTrue(remoteSftpDirectory.IsDirectory);
  1462. Assert.IsFalse(remoteSftpDirectory.IsRegularFile);
  1463. Assert.AreEqual(Directory.GetLastWriteTimeUtc(localSubDirectory1), remoteSftpDirectory.LastWriteTimeUtc);
  1464. Assert.IsTrue(client.Exists(remotePathSubFile1));
  1465. Assert.AreEqual(CreateFileHash(localPathSubFile1), CreateRemoteFileHash(client, remotePathSubFile1));
  1466. Assert.IsTrue(client.Exists(remotePathSubFile2));
  1467. Assert.AreEqual(CreateFileHash(localPathSubFile2), CreateRemoteFileHash(client, remotePathSubFile2));
  1468. Assert.IsTrue(client.Exists(remoteSubDirectory2));
  1469. remoteSftpDirectory = client.Get(remoteSubDirectory2);
  1470. Assert.IsNotNull(remoteSftpDirectory);
  1471. Assert.AreEqual(absoluteRemoteSubDirectory2, remoteSftpDirectory.FullName);
  1472. Assert.IsTrue(remoteSftpDirectory.IsDirectory);
  1473. Assert.IsFalse(remoteSftpDirectory.IsRegularFile);
  1474. Assert.AreEqual(Directory.GetLastWriteTimeUtc(localSubDirectory2), remoteSftpDirectory.LastWriteTimeUtc);
  1475. }
  1476. }
  1477. finally
  1478. {
  1479. Directory.Delete(localDirectory, true);
  1480. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1481. {
  1482. client.Connect();
  1483. if (client.Exists(remotePathSubFile1))
  1484. {
  1485. client.DeleteFile(remotePathSubFile1);
  1486. }
  1487. if (client.Exists(remotePathSubFile2))
  1488. {
  1489. client.DeleteFile(remotePathSubFile2);
  1490. }
  1491. if (client.Exists(remoteSubDirectory1))
  1492. {
  1493. client.DeleteDirectory(remoteSubDirectory1);
  1494. }
  1495. if (client.Exists(remoteSubDirectory2))
  1496. {
  1497. client.DeleteDirectory(remoteSubDirectory2);
  1498. }
  1499. if (client.Exists(remotePathFile1))
  1500. {
  1501. client.DeleteFile(remotePathFile1);
  1502. }
  1503. if (client.Exists(remotePathFile2))
  1504. {
  1505. client.DeleteFile(remotePathFile2);
  1506. }
  1507. if (remoteDirectory.Length > 0 && remoteDirectory != "." && remoteDirectory != client.WorkingDirectory)
  1508. {
  1509. if (client.Exists(remoteDirectory))
  1510. {
  1511. client.DeleteDirectory(remoteDirectory);
  1512. }
  1513. }
  1514. }
  1515. }
  1516. }
  1517. [DataTestMethod]
  1518. [DynamicData(nameof(GetScpUploadDirectoryInfoExistingFileData), DynamicDataSourceType.Method)]
  1519. public void Scp_Upload_DirectoryInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
  1520. string remoteDirectory)
  1521. {
  1522. var remotePathFile1 = CombinePaths(remoteDirectory, "file1");
  1523. var remotePathFile2 = CombinePaths(remoteDirectory, "file2");
  1524. using (var client = new SshClient(_connectionInfoFactory.Create()))
  1525. {
  1526. client.Connect();
  1527. Console.WriteLine(client.ConnectionInfo.CurrentKeyExchangeAlgorithm);
  1528. using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteDirectory)))
  1529. {
  1530. command.Execute();
  1531. }
  1532. }
  1533. var localDirectory = Path.GetTempFileName();
  1534. File.Delete(localDirectory);
  1535. Directory.CreateDirectory(localDirectory);
  1536. var localPathFile1 = Path.Combine(localDirectory, "file1");
  1537. var localPathFile2 = Path.Combine(localDirectory, "file2");
  1538. try
  1539. {
  1540. CreateFile(localPathFile1, 50);
  1541. CreateFile(localPathFile2, 50);
  1542. using (var client = new ScpClient(_connectionInfoFactory.Create()))
  1543. {
  1544. if (remotePathTransformation != null)
  1545. {
  1546. client.RemotePathTransformation = remotePathTransformation;
  1547. }
  1548. client.Connect();
  1549. CreateRemoteFile(client, remoteDirectory, 10);
  1550. try
  1551. {
  1552. client.Upload(new DirectoryInfo(localDirectory), remoteDirectory);
  1553. Assert.Fail();
  1554. }
  1555. catch (ScpException ex)
  1556. {
  1557. Assert.IsNull(ex.InnerException);
  1558. Assert.AreEqual($"scp: {remoteDirectory}: Not a directory", ex.Message);
  1559. }
  1560. }
  1561. }
  1562. finally
  1563. {
  1564. Directory.Delete(localDirectory, true);
  1565. using (var client = new SftpClient(_connectionInfoFactory.Create()))
  1566. {
  1567. client.Connect();
  1568. if (client.Exists(remotePathFile1))
  1569. {
  1570. client.DeleteFile(remotePathFile1);
  1571. }
  1572. if (client.Exists(remotePathFile2))
  1573. {
  1574. client.DeleteFile(remotePathFile2);
  1575. }
  1576. if (client.Exists((remoteDirectory)))
  1577. {
  1578. client.DeleteFile(remoteDirectory);
  1579. }
  1580. }
  1581. }
  1582. }
  1583. private static IEnumerable<object[]> GetScpDownloadStreamDirectoryDoesNotExistData()
  1584. {
  1585. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-directorydoesnotexist", "scp-file" };
  1586. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-directorydoesnotexist", "scp-file" };
  1587. }
  1588. private static IEnumerable<object[]> GetScpUploadFileInfoFileDoesNotExistData()
  1589. {
  1590. yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "test123", 0 };
  1591. yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "test123", 5 * 1024 * 1024 };
  1592. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
  1593. yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
  1594. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
  1595. yield return new object[] { null, "", "scp-issue280", 1024 };
  1596. }
  1597. private static IEnumerable<object[]> GetScpUploadFileStreamFileDoesNotExistData()
  1598. {
  1599. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 0 };
  1600. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
  1601. yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
  1602. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
  1603. yield return new object[] { RemotePathTransformation.None, "", "scp-issue280", 1024 };
  1604. }
  1605. private static IEnumerable<object[]> GetScpUploadDirectoryInfoExistingDirectoryData()
  1606. {
  1607. yield return new object[] { RemotePathTransformation.None, "scp-directorydoesnotexist" };
  1608. yield return new object[] { RemotePathTransformation.None, "." };
  1609. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%" };
  1610. }
  1611. private static IEnumerable<object[]> GetScpUploadDirectoryInfoExistingFileData()
  1612. {
  1613. yield return new object[] { RemotePathTransformation.None, "scp-upload-file" };
  1614. }
  1615. private static IEnumerable<object[]> ScpUploadFileStreamExistingFileData()
  1616. {
  1617. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-upload-file" };
  1618. }
  1619. private static IEnumerable<object[]> GetScpDownloadStreamFileDoesNotExistData()
  1620. {
  1621. yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "scp-filedoesnotexist" };
  1622. }
  1623. private static IEnumerable<object[]> GetScpDownloadDirectoryInfoDirectoryDoesNotExistData()
  1624. {
  1625. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-download" };
  1626. }
  1627. private static IEnumerable<object[]> GetScpDownloadDirectoryInfoExistingFileData()
  1628. {
  1629. yield return new object[] { RemotePathTransformation.None, "scp-download" };
  1630. }
  1631. private static IEnumerable<object[]> GetScpDownloadDirectoryInfoExistingDirectoryData()
  1632. {
  1633. yield return new object[] { RemotePathTransformation.None, "scp-download" };
  1634. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'space \\tab\tlf*?[#~=%" };
  1635. }
  1636. private static IEnumerable<object[]> GetScpDownloadFileInfoDirectoryDoesNotExistData()
  1637. {
  1638. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-directorydoesnotexist", "scp-file" };
  1639. }
  1640. private static IEnumerable<object[]> GetScpDownloadFileInfoFileDoesNotExistData()
  1641. {
  1642. yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "scp-filedoesnotexist" };
  1643. }
  1644. private static IEnumerable<object[]> GetScpDownloadFileInfoExistingDirectoryData()
  1645. {
  1646. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-test" };
  1647. }
  1648. private static IEnumerable<object[]> GetScpDownloadFileInfoExistingFileData()
  1649. {
  1650. yield return new object[] { null, "", "file 123", 0 };
  1651. yield return new object[] { null, "", "file 123", 1024 };
  1652. yield return new object[] { RemotePathTransformation.ShellQuote, "", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
  1653. yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
  1654. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
  1655. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
  1656. }
  1657. private static IEnumerable<object[]> GetScpDownloadStreamExistingDirectoryData()
  1658. {
  1659. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-test" };
  1660. }
  1661. private static IEnumerable<object[]> GetScpDownloadStreamExistingFileData()
  1662. {
  1663. yield return new object[] { null, "", "file 123", 0 };
  1664. yield return new object[] { null, "", "file 123", 1024 };
  1665. yield return new object[] { RemotePathTransformation.ShellQuote, "", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
  1666. yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
  1667. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
  1668. yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
  1669. }
  1670. private static IEnumerable<object[]> GetScpUploadFileStreamDirectoryDoesNotExistData()
  1671. {
  1672. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue289", "file123" };
  1673. }
  1674. private static IEnumerable<object[]> GetScpUploadFileStreamExistingDirectoryData()
  1675. {
  1676. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue286" };
  1677. }
  1678. private static IEnumerable<object[]> GetScpUploadFileInfoDirectoryDoesNotExistData()
  1679. {
  1680. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue289", "file123" };
  1681. }
  1682. private static IEnumerable<object[]> GetScpUploadFileInfoExistingDirectoryData()
  1683. {
  1684. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue286" };
  1685. }
  1686. private static IEnumerable<object[]> GetScpUploadFileInfoExistingFileData()
  1687. {
  1688. yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-upload-file" };
  1689. }
  1690. private static IEnumerable<object[]> GetScpUploadDirectoryInfoDirectoryDoesNotExistData()
  1691. {
  1692. yield return new object[] { RemotePathTransformation.None, "scp-directorydoesnotexist" };
  1693. }
  1694. private static void CreateRemoteFile(ScpClient client, string remoteFile, int size)
  1695. {
  1696. var file = CreateTempFile(size);
  1697. try
  1698. {
  1699. using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
  1700. {
  1701. client.Upload(fs, remoteFile);
  1702. }
  1703. }
  1704. finally
  1705. {
  1706. File.Delete(file);
  1707. }
  1708. }
  1709. private static string GetAbsoluteRemotePath(SftpClient client, string directoryName, string fileName)
  1710. {
  1711. var absolutePath = string.Empty;
  1712. if (directoryName.Length == 0)
  1713. {
  1714. absolutePath += client.WorkingDirectory;
  1715. }
  1716. else
  1717. {
  1718. if (directoryName[0] != '/')
  1719. {
  1720. absolutePath += client.WorkingDirectory + "/" + directoryName;
  1721. }
  1722. else
  1723. {
  1724. absolutePath = directoryName;
  1725. }
  1726. }
  1727. return absolutePath + "/" + fileName;
  1728. }
  1729. private static string GetAbsoluteRemotePath(IConnectionInfoFactory connectionInfoFactory, string directoryName)
  1730. {
  1731. var absolutePath = string.Empty;
  1732. if (directoryName.Length == 0 || directoryName == ".")
  1733. {
  1734. using (var client = new SftpClient(connectionInfoFactory.Create()))
  1735. {
  1736. client.Connect();
  1737. absolutePath += client.WorkingDirectory;
  1738. }
  1739. }
  1740. else
  1741. {
  1742. if (directoryName[0] != '/')
  1743. {
  1744. using (var client = new SftpClient(connectionInfoFactory.Create()))
  1745. {
  1746. client.Connect();
  1747. absolutePath += client.WorkingDirectory + "/" + directoryName;
  1748. }
  1749. }
  1750. else
  1751. {
  1752. absolutePath = directoryName;
  1753. }
  1754. }
  1755. return absolutePath;
  1756. }
  1757. private static string CreateRemoteFileHash(SftpClient client, string remotePath)
  1758. {
  1759. using (var fs = client.OpenRead(remotePath))
  1760. {
  1761. return CreateHash(fs);
  1762. }
  1763. }
  1764. private static string CombinePaths(string path1, string path2)
  1765. {
  1766. if (path1.Length == 0)
  1767. {
  1768. return path2;
  1769. }
  1770. if (path2.Length == 0)
  1771. {
  1772. return path1;
  1773. }
  1774. return path1 + "/" + path2;
  1775. }
  1776. }
  1777. }