Skip to content

Commit

Permalink
GeneXusSftp client remove function implementation (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrampone authored Dec 23, 2022
1 parent 5ea83ed commit 8b3ba79
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public abstract class ISftpClientObject : SecurityAPIObject
public abstract bool Get(string remoteFilePath, string localDir);
#pragma warning restore CA1716 // Identifiers should not match keywords
public abstract void Disconnect();
public abstract bool Rm(string remoteFilePath);

public abstract string GetWorkingDirectory();
public abstract string GetWorkingDirectory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,45 @@ public override bool Get(String remoteFilePath, String localDir)
return true;
}

[SecuritySafeCritical]
[SecuritySafeCritical]
public override bool Rm(String remoteFilePath)
{
if (SecurityUtils.compareStrings("", remoteFilePath) || remoteFilePath == null || remoteFilePath.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
{
this.error.setError("SF018", "remoteFilePath cannot be empty");
return false;
}

if (this.channel == null || !this.channel.IsConnected)
{
this.error.setError("SF019", "The channel is invalid, reconect");
return false;
}
string rDir = "";
if (this.channel.WorkingDirectory.Contains("/"))
{
remoteFilePath = $"/{remoteFilePath.Replace(@"\", "/")}";

rDir += this.channel.WorkingDirectory + remoteFilePath;
}
else
{

rDir = this.channel.WorkingDirectory + remoteFilePath;
}
try
{
this.channel.DeleteFile(rDir);
}
catch (Exception e)
{
this.error.setError("SF008", e.Message);
return false;
}
return true;
}

[SecuritySafeCritical]
public override void Disconnect()
{
if (this.channel != null && this.channel.IsConnected)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using SecurityAPITest.SecurityAPICommons.commons;
using SecurityAPITest.SecurityAPICommons.commons;
using NUnit.Framework;
using Sftp.GeneXusSftp;
using System.IO;
Expand Down Expand Up @@ -61,6 +61,18 @@ private void TestGet(SftpClient client)
True(get, client);
}

private void TestRm(SftpClient client)
{
bool get = client.Rm(remoteFilePath);
True(get, client);
}

private void TestGetFalse(SftpClient client)
{
bool get = client.Get(remoteFilePath, localDir);
False(get, client);
}

[Test]
public void TestWithUserPassword()
{
Expand All @@ -75,6 +87,22 @@ public void TestWithUserPassword()
client.Disconnect();
}


[Test]
public void TestRemove()
{
SftpOptions options = new SftpOptions();
options.Host = host;
options.User = user;
options.Password = password;
options.AllowHostKeyChecking = false;
SftpClient client = TestConnection(options);
TestPut(client);
TestRm(client);
TestGetFalse(client);
client.Disconnect();
}

[Test]
public void TestWithKey()
{
Expand Down

0 comments on commit 8b3ba79

Please sign in to comment.