forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSftpClientTests.cs
More file actions
209 lines (164 loc) · 8.84 KB
/
SftpClientTests.cs
File metadata and controls
209 lines (164 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Renci.SshNet.Common;
namespace Renci.SshNet.IntegrationTests
{
/// <summary>
/// The SFTP client integration tests
/// </summary>
[TestClass]
public class SftpClientTests : IntegrationTestBase
{
private readonly SftpClient _sftpClient;
public SftpClientTests()
{
_sftpClient = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password);
}
[TestInitialize]
public async Task InitializeAsync()
{
await _sftpClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
}
[TestCleanup]
public void Cleanup()
{
_sftpClient.Disconnect();
_sftpClient.Dispose();
}
[TestMethod]
public void Create_directory_with_contents_and_list_it()
{
var testDirectory = "/home/sshnet/sshnet-test";
var testFileName = "test-file.txt";
var testFilePath = $"{testDirectory}/{testFileName}";
var testContent = "file content";
// Create new directory and check if it exists
_sftpClient.CreateDirectory(testDirectory);
Assert.IsTrue(_sftpClient.Exists(testDirectory));
// Upload file and check if it exists
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
_sftpClient.UploadFile(fileStream, testFilePath);
Assert.IsTrue(_sftpClient.Exists(testFilePath));
// Check if ListDirectory works
var expectedFiles = new List<(string FullName, bool IsRegularFile, bool IsDirectory)>()
{
("/home/sshnet/sshnet-test/.", IsRegularFile: false, IsDirectory: true),
("/home/sshnet/sshnet-test/..", IsRegularFile: false, IsDirectory: true),
("/home/sshnet/sshnet-test/test-file.txt", IsRegularFile: true, IsDirectory: false),
};
var actualFiles = _sftpClient.ListDirectory(testDirectory)
.Select(f => (f.FullName, f.IsRegularFile, f.IsDirectory))
.ToList();
_sftpClient.DeleteFile(testFilePath);
_sftpClient.DeleteDirectory(testDirectory);
CollectionAssert.AreEquivalent(expectedFiles, actualFiles);
}
[TestMethod]
public async Task Create_directory_with_contents_and_list_it_async()
{
var testDirectory = "/home/sshnet/sshnet-test";
var testFileName = "test-file.txt";
var testFilePath = $"{testDirectory}/{testFileName}";
var testContent = "file content";
// Create new directory and check if it exists
await _sftpClient.CreateDirectoryAsync(testDirectory, CancellationToken.None).ConfigureAwait(false);
Assert.IsTrue(await _sftpClient.ExistsAsync(testDirectory));
// Upload file and check if it exists
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
await _sftpClient.UploadFileAsync(fileStream, testFilePath).ConfigureAwait(false);
Assert.IsTrue(await _sftpClient.ExistsAsync(testFilePath));
// Check if ListDirectory works
var expectedFiles = new List<(string FullName, bool IsRegularFile, bool IsDirectory)>()
{
("/home/sshnet/sshnet-test/.", IsRegularFile: false, IsDirectory: true),
("/home/sshnet/sshnet-test/..", IsRegularFile: false, IsDirectory: true),
("/home/sshnet/sshnet-test/test-file.txt", IsRegularFile: true, IsDirectory: false),
};
var actualFiles = new List<(string FullName, bool IsRegularFile, bool IsDirectory)>();
await foreach (var file in _sftpClient.ListDirectoryAsync(testDirectory, CancellationToken.None))
{
actualFiles.Add((file.FullName, file.IsRegularFile, file.IsDirectory));
}
await _sftpClient.DeleteFileAsync(testFilePath, CancellationToken.None);
await _sftpClient.DeleteDirectoryAsync(testDirectory, CancellationToken.None);
CollectionAssert.AreEquivalent(expectedFiles, actualFiles);
}
[TestMethod]
public void Test_Sftp_ListDirectory_Permission_Denied()
{
Assert.ThrowsException<SftpPermissionDeniedException>(() => _sftpClient.ListDirectory("/root"));
}
[TestMethod]
public async Task Create_directory_and_delete_it_async()
{
var testDirectory = "/home/sshnet/sshnet-test";
// Create new directory and check if it exists
await _sftpClient.CreateDirectoryAsync(testDirectory);
Assert.IsTrue(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
await _sftpClient.DeleteDirectoryAsync(testDirectory, CancellationToken.None).ConfigureAwait(false);
Assert.IsFalse(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
}
[TestMethod]
public async Task Create_directory_with_contents_and_delete_contents_then_directory_async()
{
var testDirectory = "/home/sshnet/sshnet-test";
var testFileName = "test-file.txt";
var testFilePath = $"{testDirectory}/{testFileName}";
var testContent = "file content";
// Create new directory and check if it exists
await _sftpClient.CreateDirectoryAsync(testDirectory).ConfigureAwait(false);
Assert.IsTrue(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
// Upload file and check if it exists
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
await _sftpClient.UploadFileAsync(fileStream, testFilePath).ConfigureAwait(false);
Assert.IsTrue(await _sftpClient.ExistsAsync(testFilePath).ConfigureAwait(false));
await _sftpClient.DeleteFileAsync(testFilePath, CancellationToken.None).ConfigureAwait(false);
Assert.IsFalse(await _sftpClient.ExistsAsync(testFilePath).ConfigureAwait(false));
Assert.IsTrue(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
await _sftpClient.DeleteDirectoryAsync(testDirectory, CancellationToken.None).ConfigureAwait(false);
Assert.IsFalse(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
}
[TestMethod]
public async Task Create_directory_and_delete_it_using_DeleteAsync()
{
var testDirectory = "/home/sshnet/sshnet-test";
// Create new directory and check if it exists
await _sftpClient.CreateDirectoryAsync(testDirectory).ConfigureAwait(false);
Assert.IsTrue(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
await _sftpClient.DeleteAsync(testDirectory, CancellationToken.None).ConfigureAwait(false);
Assert.IsFalse(await _sftpClient.ExistsAsync(testDirectory).ConfigureAwait(false));
}
[TestMethod]
public async Task Create_file_and_delete_using_DeleteAsync()
{
var testFileName = "test-file.txt";
var testContent = "file content";
// Upload file and check if it exists
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
await _sftpClient.UploadFileAsync(fileStream, testFileName).ConfigureAwait(false);
Assert.IsTrue(await _sftpClient.ExistsAsync(testFileName).ConfigureAwait(false));
await _sftpClient.DeleteAsync(testFileName, CancellationToken.None).ConfigureAwait(false);
Assert.IsFalse(await _sftpClient.ExistsAsync(testFileName).ConfigureAwait(false));
}
[TestMethod]
public void Create_file_and_use_GetAttributes()
{
var testFileName = "test-file.txt";
var testContent = "file content";
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
_sftpClient.UploadFile(fileStream, testFileName);
var attributes = _sftpClient.GetAttributes(testFileName);
Assert.IsNotNull(attributes);
Assert.IsTrue(attributes.IsRegularFile);
}
[TestMethod]
public async Task Create_file_and_use_GetAttributesAsync()
{
var testFileName = "test-file.txt";
var testContent = "file content";
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
await _sftpClient.UploadFileAsync(fileStream, testFileName).ConfigureAwait(false);
var attributes = await _sftpClient.GetAttributesAsync(testFileName, CancellationToken.None).ConfigureAwait(false);
Assert.IsNotNull(attributes);
Assert.IsTrue(attributes.IsRegularFile);
}
}
}