ScpTests.cs 90 KB

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