|
| 1 | +namespace IntegrationTests |
| 2 | +{ |
| 3 | + /// <summary> |
| 4 | + /// The SFTP client integration tests |
| 5 | + /// </summary> |
| 6 | + [TestClass] |
| 7 | + public class SftpClientTests : IntegrationTestBase, IDisposable |
| 8 | + { |
| 9 | + private readonly SftpClient _sftpClient; |
| 10 | + |
| 11 | + public SftpClientTests() |
| 12 | + { |
| 13 | + _sftpClient = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password); |
| 14 | + _sftpClient.Connect(); |
| 15 | + } |
| 16 | + |
| 17 | + [TestMethod] |
| 18 | + public void Create_directory_with_contents_and_list_it() |
| 19 | + { |
| 20 | + var testDirectory = "/home/sshnet/sshnet-test"; |
| 21 | + var testFileName = "test-file.txt"; |
| 22 | + var testFilePath = $"{testDirectory}/{testFileName}"; |
| 23 | + var testContent = "file content"; |
| 24 | + |
| 25 | + // Create new directory and check if it exists |
| 26 | + _sftpClient.CreateDirectory(testDirectory); |
| 27 | + Assert.IsTrue(_sftpClient.Exists(testDirectory)); |
| 28 | + |
| 29 | + // Upload file and check if it exists |
| 30 | + using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent)); |
| 31 | + _sftpClient.UploadFile(fileStream, testFilePath); |
| 32 | + Assert.IsTrue(_sftpClient.Exists(testFilePath)); |
| 33 | + |
| 34 | + // Check if ListDirectory works |
| 35 | + var files = _sftpClient.ListDirectory(testDirectory); |
| 36 | + |
| 37 | + _sftpClient.DeleteFile(testFilePath); |
| 38 | + _sftpClient.DeleteDirectory(testDirectory); |
| 39 | + |
| 40 | + var builder = new StringBuilder(); |
| 41 | + foreach (var file in files) |
| 42 | + { |
| 43 | + builder.AppendLine($"{file.FullName} {file.IsRegularFile} {file.IsDirectory}"); |
| 44 | + } |
| 45 | + |
| 46 | + Assert.AreEqual(@"/home/sshnet/sshnet-test/. False True |
| 47 | +/home/sshnet/sshnet-test/.. False True |
| 48 | +/home/sshnet/sshnet-test/test-file.txt True False |
| 49 | +", builder.ToString()); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public async Task Create_directory_with_contents_and_list_it_async() |
| 54 | + { |
| 55 | + var testDirectory = "/home/sshnet/sshnet-test"; |
| 56 | + var testFileName = "test-file.txt"; |
| 57 | + var testFilePath = $"{testDirectory}/{testFileName}"; |
| 58 | + var testContent = "file content"; |
| 59 | + |
| 60 | + // Create new directory and check if it exists |
| 61 | + _sftpClient.CreateDirectory(testDirectory); |
| 62 | + Assert.IsTrue(_sftpClient.Exists(testDirectory)); |
| 63 | + |
| 64 | + // Upload file and check if it exists |
| 65 | + using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent)); |
| 66 | + _sftpClient.UploadFile(fileStream, testFilePath); |
| 67 | + Assert.IsTrue(_sftpClient.Exists(testFilePath)); |
| 68 | + |
| 69 | + // Check if ListDirectory works |
| 70 | + var files = await _sftpClient.ListDirectoryAsync(testDirectory, CancellationToken.None); |
| 71 | + |
| 72 | + _sftpClient.DeleteFile(testFilePath); |
| 73 | + _sftpClient.DeleteDirectory(testDirectory); |
| 74 | + |
| 75 | + var builder = new StringBuilder(); |
| 76 | + foreach (var file in files) |
| 77 | + { |
| 78 | + builder.AppendLine($"{file.FullName} {file.IsRegularFile} {file.IsDirectory}"); |
| 79 | + } |
| 80 | + |
| 81 | + Assert.AreEqual(@"/home/sshnet/sshnet-test/. False True |
| 82 | +/home/sshnet/sshnet-test/.. False True |
| 83 | +/home/sshnet/sshnet-test/test-file.txt True False |
| 84 | +", builder.ToString()); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + [ExpectedException(typeof(SftpPermissionDeniedException), "Permission denied")] |
| 89 | + public void Test_Sftp_ListDirectory_Permission_Denied() |
| 90 | + { |
| 91 | + _sftpClient.ListDirectory("/root"); |
| 92 | + } |
| 93 | + |
| 94 | + public void Dispose() |
| 95 | + { |
| 96 | + _sftpClient.Disconnect(); |
| 97 | + _sftpClient.Dispose(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments