Skip to content

Update SonarAnalyzer.CSharp #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ dotnet_diagnostic.S101.severity = none
# This is a duplicate of CA2201 and MA0012.
dotnet_diagnostic.S112.severity = none

# S127: "for" loop stop conditions should be invariant
# https://rules.sonarsource.com/csharp/RSPEC-127
#
# Limited use.
dotnet_diagnostic.S127.severity = none

# S907: Remove use of 'goto'
# https://rules.sonarsource.com/csharp/RSPEC-907
#
Expand Down Expand Up @@ -128,6 +134,12 @@ dotnet_diagnostic.S2259.severity = none
# This is a duplicate of IDE0032.
dotnet_diagnostic.S2292.severity = none

# S2325: Methods and properties that don't access instance data should be static
# https://rules.sonarsource.com/csharp/RSPEC-2325
#
# This is a duplicate of CA1822
dotnet_diagnostic.S2325.severity = none

# S2445: Blocks should be synchronized on read-only fields
# https://rules.sonarsource.com/csharp/RSPEC-2445
#
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.7.70-alpha" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.19.0.84025" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.32.0.97167" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />
<PackageVersion Include="Testcontainers" Version="3.10.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading;
using System.Threading.Tasks;

namespace Renci.SshNet.Abstractions
{
internal static class CancellationTokenSourceExtensions
{
#if !NET8_OR_GREATER
public static Task CancelAsync(this CancellationTokenSource cancellationTokenSource)
{
cancellationTokenSource.Cancel();
return Task.CompletedTask;
}
#endif
}
}
18 changes: 18 additions & 0 deletions src/Renci.SshNet/Abstractions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if !NET && !NETSTANDARD2_1_OR_GREATER
using System.IO;
using System.Threading.Tasks;
#endif

namespace Renci.SshNet.Abstractions
{
internal static class StreamExtensions
{
#if !NET && !NETSTANDARD2_1_OR_GREATER
public static ValueTask DisposeAsync(this Stream stream)
{
stream.Dispose();
return default;
}
#endif
}
}
2 changes: 2 additions & 0 deletions src/Renci.SshNet/Security/Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/// <summary>
/// Represents the abstract base class from which all implementations of algorithms must inherit.
/// </summary>
#pragma warning disable S1694 // An abstract class should have both abstract and concrete methods
public abstract class Algorithm
#pragma warning restore S1694 // An abstract class should have both abstract and concrete methods
{
/// <summary>
/// Gets the algorithm name.
Expand Down
2 changes: 2 additions & 0 deletions src/Renci.SshNet/Security/Cryptography/DigitalSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/// <summary>
/// Base class for signature implementations.
/// </summary>
#pragma warning disable S1694 // An abstract class should have both abstract and concrete methods
public abstract class DigitalSignature
#pragma warning restore S1694 // An abstract class should have both abstract and concrete methods
{
/// <summary>
/// Verifies the signature.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Renci.SshNet.Common;

namespace Renci.SshNet.Sftp.Responses
{
/// <summary>
/// Extended Reply Info.
/// </summary>
internal interface IExtendedReplyInfo
{
/// <summary>
/// Loads the data from the stream into the instance.
/// </summary>
/// <param name="stream">The stream.</param>
void LoadData(SshDataStream stream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Renci.SshNet.Sftp.Responses
{
internal sealed class StatVfsReplyInfo : ExtendedReplyInfo
internal sealed class StatVfsReplyInfo : IExtendedReplyInfo
{
public SftpFileSystemInformation Information { get; private set; }

public override void LoadData(SshDataStream stream)
public void LoadData(SshDataStream stream)
{
Information = new SftpFileSystemInformation(stream.ReadUInt64(), // FileSystemBlockSize
stream.ReadUInt64(), // BlockSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public SftpExtendedReplyResponse(uint protocolVersion)
}

public T GetReply<T>()
where T : ExtendedReplyInfo, new()
where T : IExtendedReplyInfo, new()
{
var result = new T();
result.LoadData(DataStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseA
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
}

public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithEmptyPassPhraseAuthenticationMethod()
{
var privateKeyFile = GetPrivateKey("Data.Key.RSA.Encrypted.Aes.256.CBC.12345.txt", null);
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
}

public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethodWithBadKey()
{
string unauthorizedKey = """
Expand Down
15 changes: 0 additions & 15 deletions test/Renci.SshNet.IntegrationTests/AuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@ public void Multifactor_PublicKeyWithPassPhrase()
}
}

[TestMethod]
[ExpectedException(typeof(SshPassPhraseNullOrEmptyException))]
public void Multifactor_PublicKeyWithEmptyPassPhrase()
{
_remoteSshdConfig.WithAuthenticationMethods(Users.Regular.UserName, "publickey")
.Update()
.Restart();

var connectionInfo = _connectionInfoFactory.Create(_authenticationMethodFactory.CreateRegularUserPrivateKeyWithEmptyPassPhraseAuthenticationMethod());
using (var client = new SftpClient(connectionInfo))
{
client.Connect();
}
}

[TestMethod]
public void Multifactor_PublicKey_MultiplePrivateKey()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics;

#if !NET8_0_OR_GREATER
using Renci.SshNet.Abstractions;
#endif
using Renci.SshNet.Common;

namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
Expand Down Expand Up @@ -130,7 +133,7 @@ public async Task Test_ExecuteAsync_CancellationToken()

Task executeTask = cmd.ExecuteAsync(cts.Token);

cts.Cancel();
await cts.CancelAsync();

var tce = await Assert.ThrowsExceptionAsync<TaskCanceledException>(() => executeTask);
Assert.AreSame(executeTask, tce.Task);
Expand Down
4 changes: 2 additions & 2 deletions test/Renci.SshNet.IntegrationTests/SshTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public async Task Ssh_Command_IntermittentOutput_OutputStream()
{
using (var cmd = sshClient.CreateCommand("chmod 777 " + remoteFile))
{
cmd.Execute();
await cmd.ExecuteAsync();

Assert.AreEqual(0, cmd.ExitStatus, cmd.Error);
}
Expand All @@ -333,7 +333,7 @@ public async Task Ssh_Command_IntermittentOutput_OutputStream()
{
var lines = new List<string>();
string line = null;
while ((line = reader.ReadLine()) != null)
while ((line = await reader.ReadLineAsync()) != null)
{
lines.Add(line);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Images;

#if !NET && !NETSTANDARD2_1_OR_GREATER
using Renci.SshNet.Abstractions;
#endif

namespace Renci.SshNet.IntegrationTests.TestsFixtures
{
public sealed class InfrastructureFixture : IDisposable
Expand Down Expand Up @@ -80,8 +84,8 @@ public async Task DisposeAsync()
await _sshServerImage.DisposeAsync();
}

_fsOut.Dispose();
_fsErr.Dispose();
await _fsOut.DisposeAsync();
await _fsErr.DisposeAsync();
}

public void Dispose()
Expand Down
10 changes: 8 additions & 2 deletions test/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#if !NET && !NETSTANDARD2_1_OR_GREATER
using Renci.SshNet.Abstractions;
#endif
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;

Expand Down Expand Up @@ -93,7 +96,7 @@ public async Task Read_NonEmptyArray_OnlyReturnsZeroAfterDispose()

Assert.IsFalse(readTask.IsCompleted);

pipeStream.Dispose();
await pipeStream.DisposeAsync();

Assert.AreEqual(0, await readTask);
}
Expand All @@ -111,7 +114,7 @@ public async Task Read_EmptyArray_OnlyReturnsZeroAfterDispose()

Assert.IsFalse(readTask.IsCompleted);

pipeStream.Dispose();
await pipeStream.DisposeAsync();

Assert.AreEqual(0, await readTask);
}
Expand All @@ -130,7 +133,10 @@ public async Task Read_EmptyArray_OnlyReturnsZeroWhenDataAvailable()

Assert.IsFalse(readTask.IsCompleted);

// not using WriteAsync here because it deadlocks the test
#pragma warning disable S6966 // Awaitable method should be used
pipeStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
#pragma warning restore S6966 // Awaitable method should be used

Assert.AreEqual(0, await readTask);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,17 @@ public void AsTimeout_ValidTimeSpan_ReturnsExpectedMilliseconds()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void AsTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
{
var timeSpan = TimeSpan.FromSeconds(-1);

timeSpan.AsTimeout();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void AsTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
{
var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);

timeSpan.AsTimeout();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.AsTimeout());
}

[TestMethod]
Expand Down Expand Up @@ -64,21 +60,17 @@ public void EnsureValidTimeout_ValidTimeSpan_DoesNotThrow()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void EnsureValidTimeout_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
{
var timeSpan = TimeSpan.FromSeconds(-1);

timeSpan.EnsureValidTimeout();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void EnsureValidTimeout_TimeSpanExceedingMaxValue_ThrowsArgumentOutOfRangeException()
{
var timeSpan = TimeSpan.FromMilliseconds((double)int.MaxValue + 1);

timeSpan.EnsureValidTimeout();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => timeSpan.EnsureValidTimeout());
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Renci.SshNet.Tests.Common
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class TestMethodForPlatformAttribute : TestMethodAttribute
{
public TestMethodForPlatformAttribute(string platform)
Expand Down