Skip to content

Commit 82dc77a

Browse files
Use correct SSH.NET
1 parent 77b27e5 commit 82dc77a

File tree

13 files changed

+148
-60
lines changed

13 files changed

+148
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net7.0</TargetFramework>
@@ -7,6 +7,7 @@
77

88
<IsPackable>false</IsPackable>
99
<IsTestProject>true</IsTestProject>
10+
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
1011
<!--
1112
Even though we're not interested in producing XML docs for test projects, we have to enable this in order to have the .NET Compiler
1213
Platform analyzers produce the IDE0005 (Remove unnecessary import) diagnostic.
@@ -17,17 +18,30 @@
1718
https://github.com/dotnet/roslyn/issues/41640.
1819
-->
1920
<NoWarn>$(NoWarn);CS1591</NoWarn>
21+
2022
</PropertyGroup>
2123

2224
<ItemGroup>
23-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
24-
<PackageReference Include="MSTest.TestAdapter" Version="3.0.3" />
25-
<PackageReference Include="MSTest.TestFramework" Version="3.0.3" />
26-
<PackageReference Include="Testcontainers" Version="3.2.0" />
27-
<PackageReference Include="coverlet.collector" Version="6.0.0">
25+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
26+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
27+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
28+
<PackageReference Include="Testcontainers" Version="3.4.0" />
29+
<PackageReference Include="coverlet.collector" Version="6.0.0">
2830
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2931
<PrivateAssets>all</PrivateAssets>
3032
</PackageReference>
3133
</ItemGroup>
3234

35+
<ItemGroup>
36+
<ProjectReference Include="..\Renci.SshNet\Renci.SshNet.csproj">
37+
<Aliases>LocalSshNet</Aliases>
38+
</ProjectReference>
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<None Update="app.config">
43+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
44+
</None>
45+
</ItemGroup>
46+
3347
</Project>

src/Renci.SshNet.IntegrationTests/ScpClientTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace Renci.SshNet.IntegrationTests
1+
namespace IntegrationTests
42
{
53
/// <summary>
64
/// The SCP client integration tests
@@ -18,7 +16,7 @@ public ScpClientTests()
1816

1917
[TestMethod]
2018

21-
public void Scp_Upload_And_Download_FileStream()
19+
public void Upload_And_Download_FileStream()
2220
{
2321
var file = $"/tmp/{Guid.NewGuid()}.txt";
2422
var fileContent = "File content !@#$%^&*()_+{}:,./<>[];'\\|";

src/Renci.SshNet.IntegrationTests/SftpClientTests.cs

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System.Text;
2-
3-
using Renci.SshNet.Common;
4-
5-
namespace Renci.SshNet.IntegrationTests
1+
namespace IntegrationTests
62
{
73
/// <summary>
84
/// The SFTP client integration tests
@@ -19,35 +15,72 @@ public SftpClientTests()
1915
}
2016

2117
[TestMethod]
22-
public void Test_Sftp_ListDirectory_Home_Directory()
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()
2354
{
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+
2475
var builder = new StringBuilder();
25-
var files = _sftpClient.ListDirectory("/");
2676
foreach (var file in files)
2777
{
28-
builder.AppendLine($"{file.FullName}");
78+
builder.AppendLine($"{file.FullName} {file.IsRegularFile} {file.IsDirectory}");
2979
}
3080

31-
Assert.AreEqual(@"/usr
32-
/var
33-
/.
34-
/bin
35-
/mnt
36-
/opt
37-
/tmp
38-
/etc
39-
/root
40-
/media
41-
/..
42-
/dev
43-
/proc
44-
/sys
45-
/home
46-
/lib
47-
/sbin
48-
/run
49-
/srv
50-
/.dockerenv
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
5184
", builder.ToString());
5285
}
5386

src/Renci.SshNet.IntegrationTests/SshClientTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace Renci.SshNet.IntegrationTests
1+
namespace IntegrationTests
42
{
53
/// <summary>
64
/// The SSH client integration tests
@@ -17,7 +15,7 @@ public SshClientTests()
1715
}
1816

1917
[TestMethod]
20-
public void Test_SSH_Echo_Command()
18+
public void Echo_Command_with_all_characters()
2119
{
2220
var builder = new StringBuilder();
2321
var response = _sshClient.RunCommand("echo $'test !@#$%^&*()_+{}:,./<>[];\\|'");

src/Renci.SshNet.IntegrationTests/TestInitializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Renci.SshNet.IntegrationTests
1+
namespace IntegrationTests
22
{
33
[TestClass]
44
public class TestInitializer
@@ -7,7 +7,6 @@ public class TestInitializer
77
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "MSTests requires context parameter")]
88
public static async Task Initialize(TestContext context)
99
{
10-
1110
await InfrastructureFixture.Instance.InitializeAsync();
1211
}
1312

src/Renci.SshNet.IntegrationTests/TestsFixtures/InfrastructureFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
using DotNet.Testcontainers.Builders;
33
using DotNet.Testcontainers.Containers;
44

5-
namespace Renci.SshNet.IntegrationTests.TestsFixtures
5+
namespace IntegrationTests.TestsFixtures
66
{
77
public sealed class InfrastructureFixture : IDisposable
88
{
99
private InfrastructureFixture()
1010
{
11-
1211
}
1312

1413
private static readonly Lazy<InfrastructureFixture> InstanceLazy = new Lazy<InfrastructureFixture>(() => new InfrastructureFixture());
@@ -48,7 +47,6 @@ public async Task InitializeAsync()
4847
_sshServer = new ContainerBuilder()
4948
.WithHostname("renci-ssh-tests-server")
5049
.WithImage(_sshServerImage)
51-
//.WithPrivileged(true)
5250
.WithPortBinding(22, true)
5351
.Build();
5452

src/Renci.SshNet.IntegrationTests/TestsFixtures/IntegrationTestBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
namespace Renci.SshNet.IntegrationTests.TestsFixtures
1+
namespace IntegrationTests.TestsFixtures
32
{
43
/// <summary>
54
/// The base class for integration tests

src/Renci.SshNet.IntegrationTests/TestsFixtures/SshUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Renci.SshNet.IntegrationTests.TestsFixtures
1+
namespace IntegrationTests.TestsFixtures
22
{
33
public class SshUser
44
{
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
1+
#pragma warning disable IDE0005
2+
3+
extern alias LocalSshNet;
4+
5+
global using System.Text;
6+
17
global using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
global using Renci.SshNet.IntegrationTests.TestsFixtures;
8+
9+
global using IntegrationTests.TestsFixtures;
10+
11+
// The testcontainers library uses SSH.NET, so we have two versions of SSH.NET in the project.
12+
// We need to explicitly choose which version we want to test.
13+
// To avoid problems, we import all namespaces.
14+
global using LocalSshNet::Renci.SshNet;
15+
global using LocalSshNet::Renci.SshNet.Abstractions;
16+
global using LocalSshNet::Renci.SshNet.Channels;
17+
global using LocalSshNet::Renci.SshNet.Common;
18+
global using LocalSshNet::Renci.SshNet.Compression;
19+
global using LocalSshNet::Renci.SshNet.Connection;
20+
global using LocalSshNet::Renci.SshNet.Messages;
21+
global using LocalSshNet::Renci.SshNet.Messages.Authentication;
22+
global using LocalSshNet::Renci.SshNet.Messages.Connection;
23+
global using LocalSshNet::Renci.SshNet.Messages.Transport;
24+
global using LocalSshNet::Renci.SshNet.NetConf;
25+
global using LocalSshNet::Renci.SshNet.Security;
26+
global using LocalSshNet::Renci.SshNet.Security.Chaos;
27+
global using LocalSshNet::Renci.SshNet.Security.Chaos.NaCl;
28+
global using LocalSshNet::Renci.SshNet.Security.Chaos.NaCl.Internal;
29+
global using LocalSshNet::Renci.SshNet.Security.Cryptography;
30+
global using LocalSshNet::Renci.SshNet.Security.Cryptography.Ciphers;
31+
global using LocalSshNet::Renci.SshNet.Security.Org;
32+
global using LocalSshNet::Renci.SshNet.Security.Org.BouncyCastle;
33+
34+
global using LocalSshNet::Renci.SshNet.Sftp;
35+
36+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<runtime>
4+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
5+
<dependentAssembly>
6+
<assemblyIdentity name="Renci.SshNet"
7+
publicKeyToken="1cee9f8bde3db106"
8+
culture="neutral" />
9+
<bindingRedirect oldVersion="2020.0.2.0-2023.0.0.0" newVersion="2023.0.0.0" />
10+
</dependentAssembly>
11+
12+
</assemblyBinding>
13+
</runtime>
14+
15+
</configuration>

0 commit comments

Comments
 (0)