Skip to content

Commit

Permalink
Merge pull request #57 from FrendsPlatform/ISSUE-56
Browse files Browse the repository at this point in the history
Fixed issue-56: Server fingerprint, documentation and Bug with SourceOperation.Move
  • Loading branch information
jefim authored Sep 14, 2022
2 parents 5a0720e + b99fcfc commit 7945209
Show file tree
Hide file tree
Showing 18 changed files with 1,023 additions and 344 deletions.
18 changes: 18 additions & 0 deletions Frends.SFTP.UploadFiles/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

#[2.1.0] - 2022-09-08
### Fixed
- [Beaking] Removed UTF-16 and Unicode FileEncoding because their implementation didn't work. These were used as a parameter so autoupdate won't work.
- Fixed how the Encoding on windows-1252 is handled. Added NuGet System.Text.Encoding.CodePages which can handle that encoding.
- Fixed error handling by adding catch to FileTransporter to catch SftpPathNotFoundException and general Exception.
- Added HostKeyAlgorithm parameter which enables users to change the host key algorithm used in the task. Before task defaults to ED25519.
- Added tests to test the file name and content encoding.
- Updated the document to state that Ssh.Net only supports private keys in OpenSSH and ssh.com formats.
- Added documentation on the private key formatingm from putty.ppk to OpenSSH.
- Moved all the SourceOperation tests to their own test class.
- Fixed issue with server fingerprint given by user in SHA256 hex format was not accepted: Added conversion to the fingerprint given by user.
- Fixed issue when using invalid server fingerprint in MD5 string format throws wrong error message: Added more specific error messages.
- Changed how MD5 string is handled. MD5 can now be given without ':' or '-' characters.
- Fixed issue that Sha256 was only accepted in Base64 format: Added support for Sha256 in hex format.
- Added selector for host key algorithm which when enabled will force the task to use specific algorithm.
- Added more tests for using server fingerprints.
- Fixed issue where when using SourceOperation.Move the source file cannot be restored when exception occurs.

# [2.0.0] - 2022-08-08
### Fixed
- [Breaking] Changed the implementation to work similar to Cobalt by moving the source file to local Temp folder before transferring to destination.
Expand Down
204 changes: 204 additions & 0 deletions Frends.SFTP.UploadFiles/Frends.SFTP.UploadFiles.Tests/EncodingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using NUnit.Framework;
using System.IO;
using System;
using System.Threading;
using Frends.SFTP.UploadFiles.Definitions;

namespace Frends.SFTP.UploadFiles.Tests;

[TestFixture]
class EncodingTests : UploadFilesTestBase
{
[Test]
public void UploadFiles_TransferWithANSIFileNameEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileNameEncoding = FileEncoding.ANSI
};

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

[Test]
public void UploadFiles_TransferWithASCIIFileNameEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileNameEncoding = FileEncoding.ASCII
};

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

[Test]
public void UploadFiles_TransferWithUTF8WithoutBomFileNameEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileNameEncoding = FileEncoding.UTF8,
EnableBomForFileName = false
};

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

[Test]
public void UploadFiles_TransferWithUTF8WithBomFileNameEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileNameEncoding = FileEncoding.UTF8,
EnableBomForFileName = true
};

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

[Test]
public void UploadFiles_TransferWithWin1252FileNameEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileNameEncoding = FileEncoding.WINDOWS1252
};

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

[Test]
public void UploadFiles_TransferWithOtherFileNameEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileNameEncoding = FileEncoding.Other,
FileNameEncodingInString = "windows-1252"
};

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

[Test]
public void UploadFiles_TransferWithASCIIFileContentEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileContentEncoding = FileEncoding.ASCII,
Action = DestinationAction.Append
};

Helpers.UploadSingleTestFile(destination.Directory, Path.Combine(_workDir, _source.FileName));

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

[Test]
public void UploadFiles_TransferWithANSIFileContentEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileContentEncoding = FileEncoding.ANSI,
Action = DestinationAction.Append
};

Helpers.UploadSingleTestFile(destination.Directory, Path.Combine(_workDir, _source.FileName));

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

[Test]
public void UploadFiles_TransferWithUTF8WithBomFileContentEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileContentEncoding = FileEncoding.UTF8,
EnableBomForContent = true,
Action = DestinationAction.Append
};

Helpers.UploadSingleTestFile(destination.Directory, Path.Combine(_workDir, _source.FileName));

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

[Test]
public void UploadFiles_TransferWithUTF8WithoutBomFileContentEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileContentEncoding = FileEncoding.UTF8,
EnableBomForContent = false,
Action = DestinationAction.Append
};

Helpers.UploadSingleTestFile(destination.Directory, Path.Combine(_workDir, _source.FileName));

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

[Test]
public void UploadFiles_TransferWithWIN1252FileContentEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileContentEncoding = FileEncoding.WINDOWS1252,
Action = DestinationAction.Append
};

Helpers.UploadSingleTestFile(destination.Directory, Path.Combine(_workDir, _source.FileName));

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

[Test]
public void UploadFiles_TransferWithOtherFileContentEncoding()
{
var destination = new Destination
{
Directory = "/upload/Upload",
FileContentEncoding = FileEncoding.Other,
FileContentEncodingInString = "Windows-1252",
Action = DestinationAction.Append
};

Helpers.UploadSingleTestFile(destination.Directory, Path.Combine(_workDir, _source.FileName));

var result = SFTP.UploadFiles(_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 @@ -9,7 +9,6 @@ namespace Frends.SFTP.UploadFiles.Tests;
[TestFixture]
class ErrorTesting : UploadFilesTestBase
{

[Test]
public void UploadFiles_TestTransferThatThrowsIfFileNotExist()
{
Expand Down Expand Up @@ -42,7 +41,64 @@ public void UploadFiles_TestThrowsWithWrongPort()
connection.Port = 51651;

var ex = Assert.Throws<Exception>(() => SFTP.UploadFiles(_source, _destination, connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.StartsWith("SFTP transfer failed: Unable to establish the socket: No such host is known"));
Assert.That(ex.Message.StartsWith("SFTP transfer failed: Unable to establish the socket:"));
}

[Test]
public void UploadFiles_TestThrowsWithWrongAddress()
{
var connection = Helpers.GetSftpConnection();
connection.Address = "local";

var ex = Assert.Throws<Exception>(() => SFTP.UploadFiles(_source, _destination, connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.StartsWith("SFTP transfer failed: Unable to establish the socket:"));
}

[Test]
public void UploadFiles_TestThrowsMovedSourceFileIsRestored()
{
Helpers.UploadSingleTestFile(_destination.Directory, Path.Combine(_workDir, _source.FileName));

var connection = Helpers.GetSftpConnection();
var source = new Source
{
Directory = _workDir,
FileName = "SFTPUploadTestFile1.txt",
Action = SourceAction.Error,
Operation = SourceOperation.Move,
DirectoryToMoveAfterTransfer = Path.Combine(_workDir, "moved")
};
Directory.CreateDirectory(source.DirectoryToMoveAfterTransfer);

var ex = Assert.Throws<Exception>(() => SFTP.UploadFiles(source, _destination, connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.StartsWith($"SFTP transfer failed: 1 Errors: Failure in CheckIfDestination"));
Assert.IsTrue(File.Exists(Path.Combine(_source.Directory, _source.FileName)));

Directory.Delete(source.DirectoryToMoveAfterTransfer, true);
}

[Test]
public void UploadFiles_TestSourceMoveWithFileAlreadyInMovedFolder()
{
Helpers.UploadSingleTestFile(_destination.Directory, Path.Combine(_workDir, _source.FileName));

var connection = Helpers.GetSftpConnection();
var source = new Source
{
Directory = _workDir,
FileName = "SFTPUploadTestFile1.txt",
Action = SourceAction.Error,
Operation = SourceOperation.Move,
DirectoryToMoveAfterTransfer = Path.Combine(_workDir, "moved")
};
Directory.CreateDirectory(source.DirectoryToMoveAfterTransfer);
File.Copy(Path.Combine(source.Directory, source.FileName), Path.Combine(source.DirectoryToMoveAfterTransfer, source.FileName));

var ex = Assert.Throws<Exception>(() => SFTP.UploadFiles(source, _destination, connection, _options, _info, new CancellationToken()));
Assert.That(ex.Message.Contains($"Error: Failure in source operation:"));
Assert.IsTrue(File.Exists(Path.Combine(_source.Directory, _source.FileName)));

Directory.Delete(source.DirectoryToMoveAfterTransfer, true);
}
}

Loading

0 comments on commit 7945209

Please sign in to comment.