Skip to content

Commit

Permalink
Add support for .NET 8.0 (#1255)
Browse files Browse the repository at this point in the history
* Add support for .NET 8.0

* Fix CA1512: Use 'ArgumentOutOfRangeException.ThrowIfNegative' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1512)

* Use `ThrowIfGreaterThan` instead of `ThrowIfNegative`+calculation, to supply the correct parameter name.

* .NET 8 is faster :-)

* Use the explicit version, in case newer version SDK breaks our tests
  • Loading branch information
scott-xu authored Dec 1, 2023
1 parent d3641a0 commit 4c7bd35
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 23 deletions.
14 changes: 7 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ for:
- image: Ubuntu2204

install:
- sh: sudo apt-get update && sudo apt-get install -y dotnet-sdk-7.0=7.0.403-1
- sh: sudo apt-get update && sudo apt-get install -y dotnet-sdk-8.0=8.0.100-1

before_build:
- sh: mkdir artifacts -p

build_script:
- echo build
- dotnet build Renci.SshNet.sln -c Debug -f net7.0
- dotnet build Renci.SshNet.sln -c Debug -f net8.0

test_script:
- sh: echo "Run unit tests"
- sh: dotnet test -f net7.0 -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=linux_unit_test_net_7_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/linux_unit_test_net_7_coverage.xml test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
- sh: dotnet test -f net8.0 -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=linux_unit_test_net_8_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/linux_unit_test_net_8_coverage.xml test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
- sh: echo "Run integration tests"
- sh: dotnet test -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=linux_integration_test_net_7_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/linux_integration_test_net_7_coverage.xml test/Renci.SshNet.IntegrationTests/Renci.SshNet.IntegrationTests.csproj
- sh: dotnet test -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=linux_integration_test_net_8_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/linux_integration_test_net_8_coverage.xml test/Renci.SshNet.IntegrationTests/Renci.SshNet.IntegrationTests.csproj

# on_failure:
# - sh: appveyor PushArtifact artifacts/tcpdump.pcap
Expand All @@ -36,7 +36,7 @@ for:
- image: Visual Studio 2022

install:
- ps: choco install dotnet-7.0-sdk --version=7.0.403
- ps: choco install dotnet-8.0-sdk --version=8.0.100

before_build:
- ps: mkdir artifacts -f
Expand All @@ -46,8 +46,8 @@ for:
- dotnet build Renci.SshNet.sln -c Debug

test_script:
- ps: echo "Run unit tests for .NET 7.0"
- ps: dotnet test -f net7.0 -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=windows_unit_test_net_7_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/windows_unit_test_net_7_coverage.xml test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
- ps: echo "Run unit tests for .NET 8.0"
- ps: dotnet test -f net8.0 -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=windows_unit_test_net_8_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/windows_unit_test_net_8_coverage.xml test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
- ps: echo "Run unit tests for .NET Framework 4.6.2"
- ps: dotnet test -f net462 -c Debug --no-restore --no-build --results-directory artifacts --logger Appveyor --logger "console;verbosity=normal" --logger "liquid.md;LogFileName=windows_unit_test_net_4_6_2_report.md" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=../../artifacts/windows_unit_test_net_4_6_2_coverage.xml test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

Expand Down
5 changes: 4 additions & 1 deletion build/nuget/SSH.NET.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<group targetFramework="net7.0">
<dependency id="SshNet.Security.Cryptography" version="[1.3.0]" />
</group>
<group targetFramework="net8.0">
<dependency id="SshNet.Security.Cryptography" version="[1.3.0]" />
</group>
</dependencies>
</metadata>
</package>
</package>
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.403",
"version": "8.0.100",
"rollForward": "latestMajor"
}
}
}
4 changes: 4 additions & 0 deletions src/Renci.SshNet/Common/SshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,14 @@ protected byte[] ReadBytes(int length)
var data = new byte[length];
var bytesRead = _stream.Read(data, 0, length);

#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, bytesRead);
#else
if (bytesRead < length)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
#endif

return data;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Renci.SshNet/Renci.SshNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AssemblyName>Renci.SshNet</AssemblyName>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
<DefineConstants>$(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_SOCKET_EAP;FEATURE_SOCKET_APM;FEATURE_DNS_SYNC;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="SshNet.Security.Cryptography" Version="[1.3.0]" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">
<DefineConstants>$(DefineConstants);FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
</PropertyGroup>
</Project>
28 changes: 24 additions & 4 deletions src/Renci.SshNet/Sftp/SftpFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ public override int Read(byte[] buffer, int offset, int count)
throw new ArgumentNullException(nameof(buffer));
}

#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(count);
#else
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset));
Expand All @@ -528,7 +532,7 @@ public override int Read(byte[] buffer, int offset, int count)
{
throw new ArgumentOutOfRangeException(nameof(count));
}

#endif
if ((buffer.Length - offset) < count)
{
throw new ArgumentException("Invalid array range.");
Expand Down Expand Up @@ -660,6 +664,10 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
throw new ArgumentNullException(nameof(buffer));
}

#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(count);
#else
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset));
Expand All @@ -669,7 +677,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
{
throw new ArgumentOutOfRangeException(nameof(count));
}

#endif
if ((buffer.Length - offset) < count)
{
throw new ArgumentException("Invalid array range.");
Expand Down Expand Up @@ -951,10 +959,14 @@ public override long Seek(long offset, SeekOrigin origin)
/// </remarks>
public override void SetLength(long value)
{
#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfNegative(value);
#else
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
#endif

// Lock down the file stream while we do this.
lock (_lock)
Expand Down Expand Up @@ -1005,6 +1017,10 @@ public override void Write(byte[] buffer, int offset, int count)
throw new ArgumentNullException(nameof(buffer));
}

#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(count);
#else
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset));
Expand All @@ -1014,7 +1030,7 @@ public override void Write(byte[] buffer, int offset, int count)
{
throw new ArgumentOutOfRangeException(nameof(count));
}

#endif
if ((buffer.Length - offset) < count)
{
throw new ArgumentException("Invalid array range.");
Expand Down Expand Up @@ -1104,6 +1120,10 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
throw new ArgumentNullException(nameof(buffer));
}

#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(count);
#else
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset));
Expand All @@ -1113,7 +1133,7 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
{
throw new ArgumentOutOfRangeException(nameof(count));
}

#endif
if ((buffer.Length - offset) < count)
{
throw new ArgumentException("Invalid array range.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);SYSLIB0021;SYSLIB1045</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ protected override void Act()
{
_client.KeepAliveInterval = _keepAliveInterval;

// allow keep-alive to be sent a few times. .NET 7 is faster and
// allow keep-alive to be sent a few times. .NET 8 is faster and
// we need to wait less because we want exactly three messages in a session.
#if NETFRAMEWORK
Thread.Sleep(195);
#else
Thread.Sleep(180);
Thread.Sleep(170);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net462;net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 4c7bd35

Please sign in to comment.