| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101 |
- using Renci.SshNet.Common;
- namespace Renci.SshNet.IntegrationTests
- {
- // TODO SCP: UPLOAD / DOWNLOAD ZERO LENGTH FILES
- // TODO SCP: UPLOAD / DOWNLOAD EMPTY DIRECTORY
- // TODO SCP: UPLOAD DIRECTORY THAT ALREADY EXISTS ON REMOTE HOST
- [TestClass]
- public class ScpTests : TestBase
- {
- private IConnectionInfoFactory _connectionInfoFactory;
- private IRemotePathTransformation _remotePathTransformation;
- [TestInitialize]
- public void SetUp()
- {
- _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
- _remotePathTransformation = RemotePathTransformation.ShellQuote;
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadStreamDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Download_Stream_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- try
- {
- using (var downloaded = new MemoryStream())
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(completeRemotePath, downloaded);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
- }
- }
- }
- finally
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadStreamFileDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Download_Stream_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- client.CreateDirectory(remotePath);
- }
- }
- try
- {
- using (var downloaded = new MemoryStream())
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(completeRemotePath, downloaded);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
- }
- }
- }
- finally
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadDirectoryInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Download_DirectoryInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath)
- {
- var localDirectory = Path.GetTempFileName();
- File.Delete(localDirectory);
- Directory.CreateDirectory(localDirectory);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(remotePath, new DirectoryInfo(localDirectory));
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remotePath}: No such file or directory", ex.Message);
- }
- }
- }
- finally
- {
- Directory.Delete(localDirectory, true);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadDirectoryInfoExistingFileData), DynamicDataSourceType.Method)]
- public void Scp_Download_DirectoryInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
- string remotePath)
- {
- var content = CreateMemoryStream(100);
- content.Position = 0;
- var localDirectory = Path.GetTempFileName();
- File.Delete(localDirectory);
- Directory.CreateDirectory(localDirectory);
- var localFile = Path.Combine(localDirectory, PosixPath.GetFileName(remotePath));
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- client.UploadFile(content, remotePath);
- }
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Download(remotePath, new DirectoryInfo(localDirectory));
- }
- Assert.IsTrue(File.Exists(localFile));
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- using (var downloaded = new MemoryStream())
- {
- client.DownloadFile(remotePath, downloaded);
- downloaded.Position = 0;
- Assert.AreEqual(CreateFileHash(localFile), CreateHash(downloaded));
- }
- }
- }
- finally
- {
- Directory.Delete(localDirectory, true);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePath))
- {
- client.DeleteFile(remotePath);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadDirectoryInfoExistingDirectoryData), DynamicDataSourceType.Method)]
- public void Scp_Download_DirectoryInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
- string remotePath)
- {
- var localDirectory = Path.GetTempFileName();
- File.Delete(localDirectory);
- Directory.CreateDirectory(localDirectory);
- var localPathFile1 = Path.Combine(localDirectory, "file1 23");
- var remotePathFile1 = CombinePaths(remotePath, "file1 23");
- var contentFile1 = CreateMemoryStream(1024);
- contentFile1.Position = 0;
- var localPathFile2 = Path.Combine(localDirectory, "file2 #$%");
- var remotePathFile2 = CombinePaths(remotePath, "file2 #$%");
- var contentFile2 = CreateMemoryStream(2048);
- contentFile2.Position = 0;
- var localPathSubDirectory = Path.Combine(localDirectory, "subdir $1%#");
- var remotePathSubDirectory = CombinePaths(remotePath, "subdir $1%#");
- var localPathFile3 = Path.Combine(localPathSubDirectory, "file3 %$#");
- var remotePathFile3 = CombinePaths(remotePathSubDirectory, "file3 %$#");
- var contentFile3 = CreateMemoryStream(256);
- contentFile3.Position = 0;
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePathFile1))
- {
- client.DeleteFile(remotePathFile1);
- }
- if (client.Exists(remotePathFile2))
- {
- client.DeleteFile(remotePathFile2);
- }
- if (client.Exists(remotePathFile3))
- {
- client.DeleteFile(remotePathFile3);
- }
- if (client.Exists(remotePathSubDirectory))
- {
- client.DeleteDirectory(remotePathSubDirectory);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (!client.Exists(remotePath))
- {
- client.CreateDirectory(remotePath);
- }
- client.UploadFile(contentFile1, remotePathFile1);
- client.UploadFile(contentFile1, remotePathFile2);
- client.CreateDirectory(remotePathSubDirectory);
- client.UploadFile(contentFile3, remotePathFile3);
- }
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Download(remotePath, new DirectoryInfo(localDirectory));
- }
- var localFiles = Directory.GetFiles(localDirectory);
- Assert.AreEqual(2, localFiles.Length);
- Assert.IsTrue(localFiles.Contains(localPathFile1));
- Assert.IsTrue(localFiles.Contains(localPathFile2));
- var localSubDirecties = Directory.GetDirectories(localDirectory);
- Assert.AreEqual(1, localSubDirecties.Length);
- Assert.AreEqual(localPathSubDirectory, localSubDirecties[0]);
- var localFilesSubDirectory = Directory.GetFiles(localPathSubDirectory);
- Assert.AreEqual(1, localFilesSubDirectory.Length);
- Assert.AreEqual(localPathFile3, localFilesSubDirectory[0]);
- Assert.AreEqual(0, Directory.GetDirectories(localPathSubDirectory).Length);
- }
- finally
- {
- Directory.Delete(localDirectory, true);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePathFile1))
- {
- client.DeleteFile(remotePathFile1);
- }
- if (client.Exists(remotePathFile2))
- {
- client.DeleteFile(remotePathFile2);
- }
- if (client.Exists(remotePathFile3))
- {
- client.DeleteFile(remotePathFile3);
- }
- if (client.Exists(remotePathSubDirectory))
- {
- client.DeleteDirectory(remotePathSubDirectory);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadFileInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Download_FileInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- var fileInfo = new FileInfo(Path.GetTempFileName());
- try
- {
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(completeRemotePath, fileInfo);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
- }
- }
- }
- finally
- {
- fileInfo.Delete();
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadFileInfoFileDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Download_FileInfo_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- client.CreateDirectory(remotePath);
- }
- }
- var fileInfo = new FileInfo(Path.GetTempFileName());
- try
- {
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(completeRemotePath, fileInfo);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {completeRemotePath}: No such file or directory", ex.Message);
- }
- }
- }
- finally
- {
- fileInfo.Delete();
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadFileInfoExistingDirectoryData), DynamicDataSourceType.Method)]
- public void Scp_Download_FileInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
- string remotePath)
- {
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- client.CreateDirectory(remotePath);
- }
- }
- var fileInfo = new FileInfo(Path.GetTempFileName());
- fileInfo.Delete();
- try
- {
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(remotePath, fileInfo);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remotePath}: not a regular file", ex.Message);
- }
- Assert.IsFalse(fileInfo.Exists);
- }
- }
- finally
- {
- fileInfo.Delete();
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadFileInfoExistingFileData), DynamicDataSourceType.Method)]
- public void Scp_Download_FileInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile,
- int size)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- client.CreateDirectory(remotePath);
- }
- }
- var fileInfo = new FileInfo(Path.GetTempFileName());
- try
- {
- var content = CreateMemoryStream(size);
- content.Position = 0;
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(content, completeRemotePath);
- }
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Download(completeRemotePath, fileInfo);
- }
- using (var fs = fileInfo.OpenRead())
- {
- var downloadedBytes = new byte[fs.Length];
- Assert.AreEqual(downloadedBytes.Length, fs.Read(downloadedBytes, 0, downloadedBytes.Length));
- content.Position = 0;
- Assert.AreEqual(CreateHash(content), CreateHash(downloadedBytes));
- }
- }
- finally
- {
- fileInfo.Delete();
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadStreamExistingDirectoryData), DynamicDataSourceType.Method)]
- public void Scp_Download_Stream_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
- string remotePath)
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- client.CreateDirectory(remotePath);
- }
- }
- var file = Path.GetTempFileName();
- File.Delete(file);
- try
- {
- using (var fs = File.OpenWrite(file))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Download(remotePath, fs);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remotePath}: not a regular file", ex.Message);
- }
- Assert.AreEqual(0, fs.Length);
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpDownloadStreamExistingFileData), DynamicDataSourceType.Method)]
- public void Scp_Download_Stream_ExistingFile(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile,
- int size)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- // remove complete directory if it's not the home directory of the user
- // or else remove the remote file
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- client.CreateDirectory(remotePath);
- }
- }
- var file = CreateTempFile(size);
- try
- {
- using (var fs = File.OpenRead(file))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(fs, completeRemotePath);
- }
- using (var fs = File.OpenRead(file))
- using (var downloaded = new MemoryStream(size))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Download(completeRemotePath, downloaded);
- downloaded.Position = 0;
- Assert.AreEqual(CreateHash(fs), CreateHash(downloaded));
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileStreamDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileStream_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- var file = CreateTempFile(1000);
- try
- {
- using (var fs = File.OpenRead(file))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Upload(fs, completeRemotePath);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remotePath}: No such file or directory", ex.Message);
- }
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileStreamExistingDirectoryData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileStream_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
- string remoteFile)
- {
- using (var client = new SshClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteFile)))
- {
- command.Execute();
- }
- }
- var file = CreateTempFile(1000);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- client.CreateDirectory(remoteFile);
- }
- using (var fs = File.OpenRead(file))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Upload(fs, remoteFile);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remoteFile}: Is a directory", ex.Message);
- }
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remoteFile))
- {
- client.DeleteDirectory(remoteFile);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(ScpUploadFileStreamExistingFileData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileStream_ExistingFile(IRemotePathTransformation remotePathTransformation,
- string remoteFile)
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remoteFile))
- {
- client.DeleteFile(remoteFile);
- }
- }
- // original content is bigger than new content to ensure file is fully overwritten
- var originalContent = CreateMemoryStream(2000);
- var file = CreateTempFile(1000);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- originalContent.Position = 0;
- client.UploadFile(originalContent, remoteFile);
- }
- using (var fs = File.OpenRead(file))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(fs, remoteFile);
- }
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- using (var downloaded = new MemoryStream(1000))
- {
- client.DownloadFile(remoteFile, downloaded);
- downloaded.Position = 0;
- Assert.AreEqual(CreateFileHash(file), CreateHash(downloaded));
- }
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remoteFile))
- {
- client.DeleteFile(remoteFile);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileStreamFileDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileStream_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile,
- int size)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- // remove complete directory if it's not the home directory of the user
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- var file = CreateTempFile(size);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- // create directory if it's not the home directory of the user
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (!client.Exists((remotePath)))
- {
- client.CreateDirectory(remotePath);
- }
- }
- }
- using (var fs = File.OpenRead(file))
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(fs, completeRemotePath);
- }
- using (var fs = File.OpenRead(file))
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- var sftpFile = client.Get(completeRemotePath);
- Assert.AreEqual(GetAbsoluteRemotePath(client, remotePath, remoteFile), sftpFile.FullName);
- Assert.AreEqual(size, sftpFile.Length);
- var downloaded = client.ReadAllBytes(completeRemotePath);
- Assert.AreEqual(CreateHash(fs), CreateHash(downloaded));
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- // remove complete directory if it's not the home directory of the user
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- /// <summary>
- /// https://github.com/sshnet/SSH.NET/issues/289
- /// </summary>
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- var file = CreateTempFile(1000);
- try
- {
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Upload(new FileInfo(file), completeRemotePath);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remotePath}: No such file or directory", ex.Message);
- }
- }
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- Assert.IsFalse(client.Exists(completeRemotePath));
- Assert.IsFalse(client.Exists(remotePath));
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- /// <summary>
- /// https://github.com/sshnet/SSH.NET/issues/286
- /// </summary>
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileInfoExistingDirectoryData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
- string remoteFile)
- {
- using (var client = new SshClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteFile)))
- {
- command.Execute();
- }
- }
- var file = CreateTempFile(1000);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- client.CreateDirectory(remoteFile);
- }
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Upload(new FileInfo(file), remoteFile);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remoteFile}: Is a directory", ex.Message);
- }
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SshClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteFile)))
- {
- command.Execute();
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileInfoExistingFileData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
- string remoteFile)
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remoteFile))
- {
- client.DeleteFile(remoteFile);
- }
- }
- // original content is bigger than new content to ensure file is fully overwritten
- var originalContent = CreateMemoryStream(2000);
- var file = CreateTempFile(1000);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- originalContent.Position = 0;
- client.UploadFile(originalContent, remoteFile);
- }
- var fileInfo = new FileInfo(file)
- {
- LastAccessTimeUtc = new DateTime(1973, 8, 13, 20, 15, 33, DateTimeKind.Utc),
- LastWriteTimeUtc = new DateTime(1974, 1, 24, 3, 55, 12, DateTimeKind.Utc)
- };
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(fileInfo, remoteFile);
- }
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- var uploadedFile = client.Get(remoteFile);
- Assert.AreEqual(fileInfo.LastAccessTimeUtc, uploadedFile.LastAccessTimeUtc);
- Assert.AreEqual(fileInfo.LastWriteTimeUtc, uploadedFile.LastWriteTimeUtc);
- using (var downloaded = new MemoryStream(1000))
- {
- client.DownloadFile(remoteFile, downloaded);
- downloaded.Position = 0;
- Assert.AreEqual(CreateFileHash(file), CreateHash(downloaded));
- }
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remoteFile))
- {
- client.DeleteFile(remoteFile);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadFileInfoFileDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Upload_FileInfo_FileDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remotePath,
- string remoteFile,
- int size)
- {
- var completeRemotePath = CombinePaths(remotePath, remoteFile);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.DeleteFile(completeRemotePath);
- }
- // remove complete directory if it's not the home directory of the user
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- var file = CreateTempFile(size);
- try
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- // create directory if it's not the home directory of the user
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (!client.Exists(remotePath))
- {
- client.CreateDirectory(remotePath);
- }
- }
- }
- var fileInfo = new FileInfo(file)
- {
- LastAccessTimeUtc = new DateTime(1973, 8, 13, 20, 15, 33, DateTimeKind.Utc),
- LastWriteTimeUtc = new DateTime(1974, 1, 24, 3, 55, 12, DateTimeKind.Utc)
- };
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(fileInfo, completeRemotePath);
- }
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- var uploadedFile = client.Get(completeRemotePath);
- Assert.AreEqual(fileInfo.LastAccessTimeUtc, uploadedFile.LastAccessTimeUtc);
- Assert.AreEqual(fileInfo.LastWriteTimeUtc, uploadedFile.LastWriteTimeUtc);
- Assert.AreEqual(size, uploadedFile.Length);
- using (var downloaded = new MemoryStream(size))
- {
- client.DownloadFile(completeRemotePath, downloaded);
- downloaded.Position = 0;
- Assert.AreEqual(CreateFileHash(file), CreateHash(downloaded));
- }
- }
- }
- finally
- {
- File.Delete(file);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(completeRemotePath))
- {
- client.Delete(completeRemotePath);
- }
- // remove complete directory if it's not the home directory of the user
- if (remotePath.Length > 0 && remotePath != client.WorkingDirectory)
- {
- if (client.Exists(remotePath))
- {
- client.DeleteDirectory(remotePath);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadDirectoryInfoDirectoryDoesNotExistData), DynamicDataSourceType.Method)]
- public void Scp_Upload_DirectoryInfo_DirectoryDoesNotExist(IRemotePathTransformation remotePathTransformation,
- string remoteDirectory)
- {
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists((remoteDirectory)))
- {
- client.DeleteDirectory(remoteDirectory);
- }
- }
- var localDirectory = Path.GetTempFileName();
- File.Delete(localDirectory);
- Directory.CreateDirectory(localDirectory);
- try
- {
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- try
- {
- client.Upload(new DirectoryInfo(localDirectory), remoteDirectory);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remoteDirectory}: No such file or directory", ex.Message);
- }
- }
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- Assert.IsFalse(client.Exists(remoteDirectory));
- }
- }
- finally
- {
- Directory.Delete(localDirectory, true);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists((remoteDirectory)))
- {
- client.DeleteDirectory(remoteDirectory);
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadDirectoryInfoExistingDirectoryData), DynamicDataSourceType.Method)]
- public void Scp_Upload_DirectoryInfo_ExistingDirectory(IRemotePathTransformation remotePathTransformation,
- string remoteDirectory)
- {
- string absoluteRemoteDirectory = GetAbsoluteRemotePath(_connectionInfoFactory, remoteDirectory);
- var remotePathFile1 = CombinePaths(remoteDirectory, "file1");
- var remotePathFile2 = CombinePaths(remoteDirectory, "file2");
- var absoluteremoteSubDirectory1 = CombinePaths(absoluteRemoteDirectory, "sub1");
- var remoteSubDirectory1 = CombinePaths(remoteDirectory, "sub1");
- var remotePathSubFile1 = CombinePaths(remoteSubDirectory1, "file1");
- var remotePathSubFile2 = CombinePaths(remoteSubDirectory1, "file2");
- var absoluteremoteSubDirectory2 = CombinePaths(absoluteRemoteDirectory, "sub2");
- var remoteSubDirectory2 = CombinePaths(remoteDirectory, "sub2");
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePathSubFile1))
- {
- client.DeleteFile(remotePathSubFile1);
- }
- if (client.Exists(remotePathSubFile2))
- {
- client.DeleteFile(remotePathSubFile2);
- }
- if (client.Exists(remoteSubDirectory1))
- {
- client.DeleteDirectory(remoteSubDirectory1);
- }
- if (client.Exists(remoteSubDirectory2))
- {
- client.DeleteDirectory(remoteSubDirectory2);
- }
- if (client.Exists(remotePathFile1))
- {
- client.DeleteFile(remotePathFile1);
- }
- if (client.Exists(remotePathFile2))
- {
- client.DeleteFile(remotePathFile2);
- }
- if (remoteDirectory.Length > 0 && remoteDirectory != "." && remoteDirectory != client.WorkingDirectory)
- {
- if (client.Exists(remoteDirectory))
- {
- client.DeleteDirectory(remoteDirectory);
- }
- client.CreateDirectory(remoteDirectory);
- }
- }
- var localDirectory = Path.GetTempFileName();
- File.Delete(localDirectory);
- Directory.CreateDirectory(localDirectory);
- var localPathFile1 = Path.Combine(localDirectory, "file1");
- var localPathFile2 = Path.Combine(localDirectory, "file2");
- var localSubDirectory1 = Path.Combine(localDirectory, "sub1");
- var localPathSubFile1 = Path.Combine(localSubDirectory1, "file1");
- var localPathSubFile2 = Path.Combine(localSubDirectory1, "file2");
- var localSubDirectory2 = Path.Combine(localDirectory, "sub2");
- try
- {
- CreateFile(localPathFile1, 2000);
- File.SetLastWriteTimeUtc(localPathFile1, new DateTime(2015, 8, 24, 5, 32, 16, DateTimeKind.Utc));
- CreateFile(localPathFile2, 1000);
- File.SetLastWriteTimeUtc(localPathFile2, new DateTime(2012, 3, 27, 23, 2, 54, DateTimeKind.Utc));
- // create subdirectory with two files
- Directory.CreateDirectory(localSubDirectory1);
- CreateFile(localPathSubFile1, 1000);
- File.SetLastWriteTimeUtc(localPathSubFile1, new DateTime(2013, 4, 12, 16, 54, 22, DateTimeKind.Utc));
- CreateFile(localPathSubFile2, 2000);
- File.SetLastWriteTimeUtc(localPathSubFile2, new DateTime(2015, 8, 4, 12, 43, 12, DateTimeKind.Utc));
- Directory.SetLastWriteTimeUtc(localSubDirectory1,
- new DateTime(2014, 6, 12, 13, 2, 44, DateTimeKind.Utc));
- // create empty subdirectory
- Directory.CreateDirectory(localSubDirectory2);
- Directory.SetLastWriteTimeUtc(localSubDirectory2,
- new DateTime(2011, 5, 14, 1, 5, 12, DateTimeKind.Utc));
- Directory.SetLastWriteTimeUtc(localDirectory, new DateTime(2015, 10, 14, 22, 45, 11, DateTimeKind.Utc));
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- client.Upload(new DirectoryInfo(localDirectory), remoteDirectory);
- }
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- Assert.IsTrue(client.Exists(remoteDirectory));
- var remoteSftpDirectory = client.Get(remoteDirectory);
- Assert.IsNotNull(remoteSftpDirectory);
- Assert.AreEqual(absoluteRemoteDirectory, remoteSftpDirectory.FullName);
- Assert.IsTrue(remoteSftpDirectory.IsDirectory);
- Assert.IsFalse(remoteSftpDirectory.IsRegularFile);
- // Due to CVE-2018-20685, we can no longer set the times or modes on a file or directory
- // that refers to the current directory ('.'), the parent directory ('..') or a directory
- // containing a forward slash ('/').
- Assert.AreNotEqual(Directory.GetLastWriteTimeUtc(localDirectory), remoteSftpDirectory.LastWriteTimeUtc);
- Assert.IsTrue(client.Exists(remotePathFile1));
- Assert.AreEqual(CreateFileHash(localPathFile1), CreateRemoteFileHash(client, remotePathFile1));
- var remoteSftpFile = client.Get(remotePathFile1);
- Assert.IsNotNull(remoteSftpFile);
- Assert.IsFalse(remoteSftpFile.IsDirectory);
- Assert.IsTrue(remoteSftpFile.IsRegularFile);
- Assert.AreEqual(File.GetLastWriteTimeUtc(localPathFile1), remoteSftpFile.LastWriteTimeUtc);
- Assert.IsTrue(client.Exists(remotePathFile2));
- Assert.AreEqual(CreateFileHash(localPathFile2), CreateRemoteFileHash(client, remotePathFile2));
- remoteSftpFile = client.Get(remotePathFile2);
- Assert.IsNotNull(remoteSftpFile);
- Assert.IsFalse(remoteSftpFile.IsDirectory);
- Assert.IsTrue(remoteSftpFile.IsRegularFile);
- Assert.AreEqual(File.GetLastWriteTimeUtc(localPathFile2), remoteSftpFile.LastWriteTimeUtc);
- Assert.IsTrue(client.Exists(remoteSubDirectory1));
- remoteSftpDirectory = client.Get(remoteSubDirectory1);
- Assert.IsNotNull(remoteSftpDirectory);
- Assert.AreEqual(absoluteremoteSubDirectory1, remoteSftpDirectory.FullName);
- Assert.IsTrue(remoteSftpDirectory.IsDirectory);
- Assert.IsFalse(remoteSftpDirectory.IsRegularFile);
- Assert.AreEqual(Directory.GetLastWriteTimeUtc(localSubDirectory1), remoteSftpDirectory.LastWriteTimeUtc);
- Assert.IsTrue(client.Exists(remotePathSubFile1));
- Assert.AreEqual(CreateFileHash(localPathSubFile1), CreateRemoteFileHash(client, remotePathSubFile1));
- Assert.IsTrue(client.Exists(remotePathSubFile2));
- Assert.AreEqual(CreateFileHash(localPathSubFile2), CreateRemoteFileHash(client, remotePathSubFile2));
- Assert.IsTrue(client.Exists(remoteSubDirectory2));
- remoteSftpDirectory = client.Get(remoteSubDirectory2);
- Assert.IsNotNull(remoteSftpDirectory);
- Assert.AreEqual(absoluteremoteSubDirectory2, remoteSftpDirectory.FullName);
- Assert.IsTrue(remoteSftpDirectory.IsDirectory);
- Assert.IsFalse(remoteSftpDirectory.IsRegularFile);
- Assert.AreEqual(Directory.GetLastWriteTimeUtc(localSubDirectory2), remoteSftpDirectory.LastWriteTimeUtc);
- }
- }
- finally
- {
- Directory.Delete(localDirectory, true);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePathSubFile1))
- {
- client.DeleteFile(remotePathSubFile1);
- }
- if (client.Exists(remotePathSubFile2))
- {
- client.DeleteFile(remotePathSubFile2);
- }
- if (client.Exists(remoteSubDirectory1))
- {
- client.DeleteDirectory(remoteSubDirectory1);
- }
- if (client.Exists(remoteSubDirectory2))
- {
- client.DeleteDirectory(remoteSubDirectory2);
- }
- if (client.Exists(remotePathFile1))
- {
- client.DeleteFile(remotePathFile1);
- }
- if (client.Exists(remotePathFile2))
- {
- client.DeleteFile(remotePathFile2);
- }
- if (remoteDirectory.Length > 0 && remoteDirectory != "." && remoteDirectory != client.WorkingDirectory)
- {
- if (client.Exists(remoteDirectory))
- {
- client.DeleteDirectory(remoteDirectory);
- }
- }
- }
- }
- }
- [DataTestMethod]
- [DynamicData(nameof(GetScpUploadDirectoryInfoExistingFileData), DynamicDataSourceType.Method)]
- public void Scp_Upload_DirectoryInfo_ExistingFile(IRemotePathTransformation remotePathTransformation,
- string remoteDirectory)
- {
- var remotePathFile1 = CombinePaths(remoteDirectory, "file1");
- var remotePathFile2 = CombinePaths(remoteDirectory, "file2");
- using (var client = new SshClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- Console.WriteLine(client.ConnectionInfo.CurrentKeyExchangeAlgorithm);
- using (var command = client.CreateCommand("rm -Rf " + _remotePathTransformation.Transform(remoteDirectory)))
- {
- command.Execute();
- }
- }
- var localDirectory = Path.GetTempFileName();
- File.Delete(localDirectory);
- Directory.CreateDirectory(localDirectory);
- var localPathFile1 = Path.Combine(localDirectory, "file1");
- var localPathFile2 = Path.Combine(localDirectory, "file2");
- try
- {
- CreateFile(localPathFile1, 50);
- CreateFile(localPathFile2, 50);
- using (var client = new ScpClient(_connectionInfoFactory.Create()))
- {
- if (remotePathTransformation != null)
- {
- client.RemotePathTransformation = remotePathTransformation;
- }
- client.Connect();
- CreateRemoteFile(client, remoteDirectory, 10);
- try
- {
- client.Upload(new DirectoryInfo(localDirectory), remoteDirectory);
- Assert.Fail();
- }
- catch (ScpException ex)
- {
- Assert.IsNull(ex.InnerException);
- Assert.AreEqual($"scp: {remoteDirectory}: Not a directory", ex.Message);
- }
- }
- }
- finally
- {
- Directory.Delete(localDirectory, true);
- using (var client = new SftpClient(_connectionInfoFactory.Create()))
- {
- client.Connect();
- if (client.Exists(remotePathFile1))
- {
- client.DeleteFile(remotePathFile1);
- }
- if (client.Exists(remotePathFile2))
- {
- client.DeleteFile(remotePathFile2);
- }
- if (client.Exists((remoteDirectory)))
- {
- client.DeleteFile(remoteDirectory);
- }
- }
- }
- }
- private static IEnumerable<object[]> GetScpDownloadStreamDirectoryDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-directorydoesnotexist", "scp-file" };
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-directorydoesnotexist", "scp-file" };
- }
- private static IEnumerable<object[]> GetScpUploadFileInfoFileDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "test123", 0 };
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "test123", 5 * 1024 * 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
- yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
- yield return new object[] { null, "", "scp-issue280", 1024 };
- }
- private static IEnumerable<object[]> GetScpUploadFileStreamFileDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 0 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
- yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
- yield return new object[] { RemotePathTransformation.None, "", "scp-issue280", 1024 };
- }
- private static IEnumerable<object[]> GetScpUploadDirectoryInfoExistingDirectoryData()
- {
- yield return new object[] { RemotePathTransformation.None, "scp-directorydoesnotexist" };
- yield return new object[] { RemotePathTransformation.None, "." };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%" };
- }
- private static IEnumerable<object[]> GetScpUploadDirectoryInfoExistingFileData()
- {
- yield return new object[] { RemotePathTransformation.None, "scp-upload-file" };
- }
- private static IEnumerable<object[]> ScpUploadFileStreamExistingFileData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-upload-file" };
- }
- private static IEnumerable<object[]> GetScpDownloadStreamFileDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "scp-filedoesnotexist" };
- }
- private static IEnumerable<object[]> GetScpDownloadDirectoryInfoDirectoryDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-download" };
- }
- private static IEnumerable<object[]> GetScpDownloadDirectoryInfoExistingFileData()
- {
- yield return new object[] { RemotePathTransformation.None, "scp-download" };
- }
- private static IEnumerable<object[]> GetScpDownloadDirectoryInfoExistingDirectoryData()
- {
- yield return new object[] { RemotePathTransformation.None, "scp-download" };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'space \\tab\tlf*?[#~=%" };
- }
- private static IEnumerable<object[]> GetScpDownloadFileInfoDirectoryDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-directorydoesnotexist", "scp-file" };
- }
- private static IEnumerable<object[]> GetScpDownloadFileInfoFileDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet", "scp-filedoesnotexist" };
- }
- private static IEnumerable<object[]> GetScpDownloadFileInfoExistingDirectoryData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-test" };
- }
- private static IEnumerable<object[]> GetScpDownloadFileInfoExistingFileData()
- {
- yield return new object[] { null, "", "file 123", 0 };
- yield return new object[] { null, "", "file 123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
- yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
- }
- private static IEnumerable<object[]> GetScpDownloadStreamExistingDirectoryData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-test" };
- }
- private static IEnumerable<object[]> GetScpDownloadStreamExistingFileData()
- {
- yield return new object[] { null, "", "file 123", 0 };
- yield return new object[] { null, "", "file 123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
- yield return new object[] { null, "/home/sshnet/scp test", "file 123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/dir|&;<>()$`\"'sp\u0100ce \\tab\tlf\n*?[#~=%", "file123", 1024 };
- yield return new object[] { RemotePathTransformation.ShellQuote, "/home/sshnet/scp-test", "file|&;<>()$`\"'sp\u0100ce \\tab\tlf*?[#~=%", 1024 };
- }
- private static IEnumerable<object[]> GetScpUploadFileStreamDirectoryDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue289", "file123" };
- }
- private static IEnumerable<object[]> GetScpUploadFileStreamExistingDirectoryData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue286" };
- }
- private static IEnumerable<object[]> GetScpUploadFileInfoDirectoryDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue289", "file123" };
- }
- private static IEnumerable<object[]> GetScpUploadFileInfoExistingDirectoryData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-issue286" };
- }
- private static IEnumerable<object[]> GetScpUploadFileInfoExistingFileData()
- {
- yield return new object[] { RemotePathTransformation.None, "/home/sshnet/scp-upload-file" };
- }
- private static IEnumerable<object[]> GetScpUploadDirectoryInfoDirectoryDoesNotExistData()
- {
- yield return new object[] { RemotePathTransformation.None, "scp-directorydoesnotexist" };
- }
- private static void CreateRemoteFile(ScpClient client, string remoteFile, int size)
- {
- var file = CreateTempFile(size);
- try
- {
- using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- client.Upload(fs, remoteFile);
- }
- }
- finally
- {
- File.Delete(file);
- }
- }
- private static string GetAbsoluteRemotePath(SftpClient client, string directoryName, string fileName)
- {
- var absolutePath = string.Empty;
- if (directoryName.Length == 0)
- {
- absolutePath += client.WorkingDirectory;
- }
- else
- {
- if (directoryName[0] != '/')
- {
- absolutePath += client.WorkingDirectory + "/" + directoryName;
- }
- else
- {
- absolutePath = directoryName;
- }
- }
- return absolutePath + "/" + fileName;
- }
- private static string GetAbsoluteRemotePath(IConnectionInfoFactory connectionInfoFactory, string directoryName)
- {
- var absolutePath = string.Empty;
- if (directoryName.Length == 0 || directoryName == ".")
- {
- using (var client = new SftpClient(connectionInfoFactory.Create()))
- {
- client.Connect();
- absolutePath += client.WorkingDirectory;
- }
- }
- else
- {
- if (directoryName[0] != '/')
- {
- using (var client = new SftpClient(connectionInfoFactory.Create()))
- {
- client.Connect();
- absolutePath += client.WorkingDirectory + "/" + directoryName;
- }
- }
- else
- {
- absolutePath = directoryName;
- }
- }
- return absolutePath;
- }
- private static string CreateRemoteFileHash(SftpClient client, string remotePath)
- {
- using (var fs = client.OpenRead(remotePath))
- {
- return CreateHash(fs);
- }
- }
- private static string CombinePaths(string path1, string path2)
- {
- if (path1.Length == 0)
- {
- return path2;
- }
- if (path2.Length == 0)
- {
- return path1;
- }
- return path1 + "/" + path2;
- }
- }
- }
|