Skip to content

Commit 459ebc7

Browse files
Adding integration tests + unit test
1 parent 375e227 commit 459ebc7

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Renci.SshNet.Common;
2+
3+
namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
4+
{
5+
public partial class SftpClientTest
6+
{
7+
[TestMethod]
8+
[TestCategory("Sftp")]
9+
public void Test_Sftp_GetAttributes_Not_Exists()
10+
{
11+
using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
12+
{
13+
sftp.Connect();
14+
15+
Assert.ThrowsException<SftpPathNotFoundException>(() => sftp.GetAttributes("/asdfgh"));
16+
}
17+
}
18+
19+
[TestMethod]
20+
[TestCategory("Sftp")]
21+
public void Test_Sftp_GetAttributes_Null()
22+
{
23+
using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
24+
{
25+
sftp.Connect();
26+
27+
Assert.ThrowsException<ArgumentNullException>(() => sftp.GetAttributes(null));
28+
}
29+
}
30+
31+
[TestMethod]
32+
[TestCategory("Sftp")]
33+
public void Test_Sftp_GetAttributes_Current()
34+
{
35+
using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
36+
{
37+
sftp.Connect();
38+
39+
var attributes = sftp.GetAttributes(".");
40+
41+
Assert.IsNotNull(attributes);
42+
43+
sftp.Disconnect();
44+
}
45+
}
46+
}
47+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Renci.SshNet.Common;
2+
3+
namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
4+
{
5+
/// <summary>
6+
/// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
7+
/// </summary>
8+
public partial class SftpClientTest
9+
{
10+
[TestMethod]
11+
[TestCategory("Sftp")]
12+
public async Task Test_Sftp_GetAttributesAsync_Not_Exists()
13+
{
14+
using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
15+
{
16+
var cts = new CancellationTokenSource();
17+
cts.CancelAfter(TimeSpan.FromMinutes(1));
18+
19+
await sftp.ConnectAsync(cts.Token);
20+
21+
await Assert.ThrowsExceptionAsync<SftpPathNotFoundException>(async () => await sftp.GetAttributesAsync("/asdfgh", cts.Token));
22+
}
23+
}
24+
25+
[TestMethod]
26+
[TestCategory("Sftp")]
27+
public async Task Test_Sftp_GetAttributesAsync_Null()
28+
{
29+
using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
30+
{
31+
var cts = new CancellationTokenSource();
32+
cts.CancelAfter(TimeSpan.FromMinutes(1));
33+
34+
await sftp.ConnectAsync(cts.Token);
35+
36+
await Assert.ThrowsExceptionAsync<ArgumentNullException>(async () => await sftp.GetAttributesAsync(null, cts.Token));
37+
}
38+
}
39+
40+
[TestMethod]
41+
[TestCategory("Sftp")]
42+
public async Task Test_Sftp_GetAttributesAsync_Current()
43+
{
44+
using (var sftp = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
45+
{
46+
var cts = new CancellationTokenSource();
47+
cts.CancelAfter(TimeSpan.FromMinutes(1));
48+
49+
await sftp.ConnectAsync(cts.Token);
50+
51+
var fileAttributes = await sftp.GetAttributesAsync(".", cts.Token);
52+
53+
Assert.IsNotNull(fileAttributes);
54+
55+
sftp.Disconnect();
56+
}
57+
}
58+
}
59+
}

test/Renci.SshNet.IntegrationTests/SftpClientTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,33 @@ public async Task Create_file_and_delete_using_DeleteAsync()
177177

178178
Assert.IsFalse(await _sftpClient.ExistsAsync(testFileName).ConfigureAwait(false));
179179
}
180+
181+
[TestMethod]
182+
public void Create_file_and_use_GetAttributes()
183+
{
184+
var testFileName = "test-file.txt";
185+
var testContent = "file content";
186+
187+
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
188+
_sftpClient.UploadFile(fileStream, testFileName);
189+
190+
var attributes = _sftpClient.GetAttributes(testFileName);
191+
Assert.IsNotNull(attributes);
192+
Assert.IsTrue(attributes.IsRegularFile);
193+
}
194+
195+
[TestMethod]
196+
public async Task Create_file_and_use_GetAttributesAsync()
197+
{
198+
var testFileName = "test-file.txt";
199+
var testContent = "file content";
200+
201+
using var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(testContent));
202+
await _sftpClient.UploadFileAsync(fileStream, testFileName).ConfigureAwait(false);
203+
204+
var attributes = await _sftpClient.GetAttributesAsync(testFileName, CancellationToken.None).ConfigureAwait(false);
205+
Assert.IsNotNull(attributes);
206+
Assert.IsTrue(attributes.IsRegularFile);
207+
}
180208
}
181209
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Renci.SshNet.Common;
4+
using Renci.SshNet.Tests.Properties;
5+
6+
namespace Renci.SshNet.Tests.Classes;
7+
8+
public partial class SftpClientTest
9+
{
10+
[TestMethod]
11+
public void GetAttributes_Throws_WhenNotConnected()
12+
{
13+
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
14+
{
15+
Assert.ThrowsException<SshConnectionException>(() => sftp.GetAttributes("."));
16+
}
17+
}
18+
19+
[TestMethod]
20+
public void GetAttributes_Throws_WhenDisposed()
21+
{
22+
var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD);
23+
sftp.Dispose();
24+
25+
Assert.ThrowsException<ObjectDisposedException>(() => sftp.GetAttributes("."));
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Renci.SshNet.Common;
6+
using Renci.SshNet.Tests.Properties;
7+
8+
namespace Renci.SshNet.Tests.Classes;
9+
10+
public partial class SftpClientTest
11+
{
12+
[TestMethod]
13+
public async Task GetAttributesAsync_Throws_WhenNotConnected()
14+
{
15+
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
16+
{
17+
await Assert.ThrowsExceptionAsync<SshConnectionException>(() => sftp.GetAttributesAsync(".", CancellationToken.None));
18+
}
19+
}
20+
21+
[TestMethod]
22+
public async Task GetAttributesAsync_Throws_WhenDisposed()
23+
{
24+
var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD);
25+
sftp.Dispose();
26+
27+
await Assert.ThrowsExceptionAsync<ObjectDisposedException>(() => sftp.GetAttributesAsync(".", CancellationToken.None));
28+
}
29+
}

0 commit comments

Comments
 (0)