Skip to content

Commit

Permalink
Merge pull request #152 from FrendsPlatform/issue-151
Browse files Browse the repository at this point in the history
SFTP.DownloadFiles - Bug fixes and new features
  • Loading branch information
Svenskapojkarna authored Jun 12, 2023
2 parents 49e76bf + 3bf7744 commit 8ad9e2d
Show file tree
Hide file tree
Showing 15 changed files with 1,070 additions and 881 deletions.
8 changes: 8 additions & 0 deletions Frends.SFTP.DownloadFiles/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [2.7.0] - 2023-06-07
### Added
- [Breaking] Added new parameter for keyboard-interactive authentication where users can add prompts and responses.
- Modified operations log to list current system and sftp server information.
### Fixed
- Modified private key passphrase to be visible when all private key authentication options were enabled.
- Fixed operations log to show case exceptions more precisely.

## [2.6.1] - 2023-05-17
### Fixed
- Fixed issue with TransferredFileNames was incorrect when FilePaths parameter was used.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,128 +1,162 @@
using System;
using System.IO;
using System.Threading;
using NUnit.Framework;
using Frends.SFTP.DownloadFiles.Definitions;

namespace Frends.SFTP.DownloadFiles.Tests
{
[TestFixture]
public class ConnectivityTests : DownloadFilesTestBase
{
[Test]
public void DownloadFiles_TestWithLargerBuffer()
{
Helpers.UploadLargeTestFiles(_source.Directory, 1);

var connection = Helpers.GetSftpConnection();
connection.BufferSize = 256;

var source = new Source
{
Directory = _source.Directory,
FileName = "LargeTestFile1.bin",
Action = SourceAction.Error,
Operation = SourceOperation.Nothing,
};

var result = SFTP.DownloadFiles(source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestTransferThatThrowsWithIncorrectCredentials()
{
var connection = Helpers.GetSftpConnection();
connection.ConnectionTimeout = 10;
connection.UserName = "demo";
connection.Password = "demo";

var result = Assert.Throws<Exception>(() => SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken()));
Assert.That(result.Message.StartsWith("SFTP transfer failed: Authentication of SSH session failed: Permission denied (password)"));
}

[Test]
public void DownloadFiles_TestPrivateKeyFileRsa()
{
var connection = Helpers.GetSftpConnection();
connection.Authentication = AuthenticationType.UsernamePasswordPrivateKeyFile;
connection.PrivateKeyFilePassphrase = "passphrase";
connection.PrivateKeyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Volumes/ssh_host_rsa_key");

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestPrivateKeyFileRsaFromString()
{
var key = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Volumes/ssh_host_rsa_key"));

var connection = Helpers.GetSftpConnection();
connection.Authentication = AuthenticationType.UsernamePasswordPrivateKeyString;
connection.PrivateKeyFilePassphrase = "passphrase";
connection.PrivateKeyString = key;

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestWithInteractiveKeyboardAuthentication()
{
var connection = Helpers.GetSftpConnection();
connection.UseKeyboardInteractiveAuthentication = true;

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestKeepAliveIntervalWithDefault()
{
Helpers.UploadLargeTestFiles(_source.Directory, 1);

var connection = Helpers.GetSftpConnection();

var source = new Source
{
Directory = _source.Directory,
FileName = "*.bin",
Action = SourceAction.Error,
Operation = SourceOperation.Nothing,
};

var result = SFTP.DownloadFiles(source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestKeepAliveIntervalWith1ms()
{
Helpers.UploadLargeTestFiles(_source.Directory, 1);

var connection = Helpers.GetSftpConnection();
connection.KeepAliveInterval = 1;
connection.BufferSize = 256;

var source = new Source
{
Directory = _source.Directory,
FileName = "*.bin",
Action = SourceAction.Error,
Operation = SourceOperation.Nothing,
};

var result = SFTP.DownloadFiles(source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}
}
}


using System;
using System.IO;
using System.Threading;
using NUnit.Framework;
using Frends.SFTP.DownloadFiles.Definitions;

namespace Frends.SFTP.DownloadFiles.Tests
{
[TestFixture]
public class ConnectivityTests : DownloadFilesTestBase
{
[Test]
public void DownloadFiles_TestWithLargerBuffer()
{
Helpers.UploadLargeTestFiles(_source.Directory, 1);

var connection = Helpers.GetSftpConnection();
connection.BufferSize = 256;

var source = new Source
{
Directory = _source.Directory,
FileName = "LargeTestFile1.bin",
Action = SourceAction.Error,
Operation = SourceOperation.Nothing,
};

var result = SFTP.DownloadFiles(source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestTransferThatThrowsWithIncorrectCredentials()
{
var connection = Helpers.GetSftpConnection();
connection.ConnectionTimeout = 10;
connection.UserName = "demo";
connection.Password = "demo";

var result = Assert.Throws<Exception>(() => SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken()));
Assert.IsTrue(result.Message.StartsWith("SFTP transfer failed: Authentication of SSH session failed: Permission denied (password)"));
}

[Test]
public void DownloadFiles_TestPrivateKeyFileRsa()
{
var connection = Helpers.GetSftpConnection();
connection.Authentication = AuthenticationType.UsernamePasswordPrivateKeyFile;
connection.PrivateKeyPassphrase = "passphrase";
connection.PrivateKeyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Volumes/ssh_host_rsa_key");

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestPrivateKeyFileRsaFromString()
{
var key = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Volumes/ssh_host_rsa_key"));

var connection = Helpers.GetSftpConnection();
connection.HostKeyAlgorithm = HostKeyAlgorithms.RSA;
connection.Authentication = AuthenticationType.UsernamePasswordPrivateKeyString;
connection.PrivateKeyPassphrase = "passphrase";
connection.PrivateKeyString = key;

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestWithInteractiveKeyboardAuthentication()
{
var connection = Helpers.GetSftpConnection();
connection.UseKeyboardInteractiveAuthentication = true;

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestWithInteractiveKeyboardAuthenticationAndPrivateKey()
{
var connection = Helpers.GetSftpConnection();
connection.Authentication = AuthenticationType.UsernamePrivateKeyFile;
connection.PrivateKeyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Volumes/ssh_host_rsa_key");
connection.Password = null;
connection.PrivateKeyPassphrase = "passphrase";
connection.UseKeyboardInteractiveAuthentication = true;
connection.PromptAndResponse = new PromptResponse[] { new PromptResponse { Prompt = "Password", Response = "pass" } };

var result = SFTP.DownloadFiles(_source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);

connection.Authentication = AuthenticationType.UsernamePrivateKeyString;
connection.PrivateKeyFile = null;
connection.PrivateKeyString = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Volumes/ssh_host_rsa_key"));
connection.PrivateKeyPassphrase = "passphrase";

var destination = new Destination
{
Directory = Path.Combine(_workDir, "destination"),
Action = DestinationAction.Overwrite,
FileNameEncoding = FileEncoding.UTF8,
EnableBomForFileName = true
};

result = SFTP.DownloadFiles(_source, destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestKeepAliveIntervalWithDefault()
{
Helpers.UploadLargeTestFiles(_source.Directory, 1);

var connection = Helpers.GetSftpConnection();

var source = new Source
{
Directory = _source.Directory,
FileName = "*.bin",
Action = SourceAction.Error,
Operation = SourceOperation.Nothing,
};

var result = SFTP.DownloadFiles(source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}

[Test]
public void DownloadFiles_TestKeepAliveIntervalWith1ms()
{
Helpers.UploadLargeTestFiles(_source.Directory, 1);

var connection = Helpers.GetSftpConnection();
connection.KeepAliveInterval = 1;
connection.BufferSize = 256;

var source = new Source
{
Directory = _source.Directory,
FileName = "*.bin",
Action = SourceAction.Error,
Operation = SourceOperation.Nothing,
};

var result = SFTP.DownloadFiles(source, _destination, connection, _options, _info, new CancellationToken());
Assert.IsTrue(result.Success);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void DownloadFiles_TestWithSubDirNameAsFileMask()

var ex = Assert.Throws<Exception>(() => SFTP.DownloadFiles(source, _destination, _connection, _options, _info, new CancellationToken()));
Assert.IsTrue(ex.Message.StartsWith("SFTP transfer failed: 1 Errors: No source files found from directory"));

Helpers.DeleteSubDirectory(path);
}

Expand All @@ -78,7 +77,7 @@ public void DownloadFiles_TestThrowsWithSourceMoveToNonExistingDirectoryShouldRe
};

var ex = Assert.Throws<Exception>(() => SFTP.DownloadFiles(source, _destination, _connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.Contains($"Operation failed: Source file {_source.FileName} couldn't be moved to given directory {source.DirectoryToMoveAfterTransfer} because the directory didn't exist."));
Assert.IsTrue(ex.Message.Contains($"Operation failed: Source file {_source.FileName} couldn't be moved to given directory {source.DirectoryToMoveAfterTransfer} because the directory didn't exist."));
Assert.IsTrue(Helpers.SourceFileExists(_source.Directory + "/" + _source.FileName));
}

Expand Down Expand Up @@ -117,7 +116,7 @@ public void DownloadFiles_TestThrowsSourceMoveToDestinationFileExists()
};

var ex = Assert.Throws<Exception>(() => SFTP.DownloadFiles(source, destination, _connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.StartsWith($"SFTP transfer failed: 1 Errors: Failure in CheckIfDestination"));
Assert.IsTrue(ex.Message.StartsWith($"SFTP transfer failed: 1 Errors: Failure in CheckIfDestination"));
Assert.IsTrue(Helpers.SourceFileExists(Path.Combine(_source.Directory, _source.FileName).Replace("\\", "/")));
}

Expand Down Expand Up @@ -146,7 +145,7 @@ public void DownloadFiles_TestThrowsSourceMoveToDestinationFileExistsWithRenameS
};

var ex = Assert.Throws<Exception>(() => SFTP.DownloadFiles(source, destination, _connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.StartsWith($"SFTP transfer failed: 1 Errors: Failure in CheckIfDestination"));
Assert.IsTrue(ex.Message.StartsWith($"SFTP transfer failed: 1 Errors: Failure in CheckIfDestination"));
Assert.IsTrue(Helpers.SourceFileExists(Path.Combine(_source.Directory, _source.FileName).Replace("\\", "/")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,6 @@ internal static string[] UploadLargeTestFiles(string destination, int count = 3,
return filePaths.ToArray();
}

internal static void DeleteRemoteFiles(int count, string directory)
{
using (var client = new SftpClient(_dockerAddress, 2222, _dockerUsername, _dockerPassword))
{
client.Connect();
var files = client.ListDirectory(directory);
var i = 0;
foreach (var file in files)
{
if (i == count)
break;
client.DeleteFile(file.FullName);
i++;
}
}
}

internal static List<string> CreateDummyFiles(int count, List<string> filenames = null)
{
Directory.CreateDirectory(_workDir);
Expand Down Expand Up @@ -184,6 +167,7 @@ internal static void DeleteDummyFiles()
{
if (Directory.Exists(file))
Directory.Delete(file, true);

File.Delete(file);
}
}
Expand Down
Loading

0 comments on commit 8ad9e2d

Please sign in to comment.