Skip to content

Analyzer fixes round 3. #1135

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 1 commit into from
May 31, 2023
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
6 changes: 2 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
<!--
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0-preview1.23165.1" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="all" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.52" PrivateAssets="all" />
-->
<!--
<PackageReference Include="Meziantou.Analyzer" Version="2.0.54" PrivateAssets="all" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544" PrivateAssets="all" />
-->
-->
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;

using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
Expand Down Expand Up @@ -97,33 +100,47 @@ private void SetupMocks()

#region SftpSession.Connect()

_sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
_channelSessionMock.InSequence(sequence).Setup(p => p.Open());
_channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
_channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpInitRequestBytes)).Callback(
() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
});
_channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes)).Callback(
() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
});
_ = _sessionMock.InSequence(sequence)
.Setup(p => p.CreateChannelSession())
.Returns(_channelSessionMock.Object);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.Open());
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendSubsystemRequest("sftp"))
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.IsOpen)
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendData(_sftpInitRequestBytes))
.Callback(() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
});
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.IsOpen)
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendData(_sftpRealPathRequestBytes))
.Callback(() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
});

#endregion SftpSession.Connect()

_channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpStatVfsRequestBytes)).Callback(
() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpStatVfsResponse.GetBytes()));
});
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.IsOpen)
.Returns(true);
_ = _channelSessionMock.InSequence(sequence)
.Setup(p => p.SendData(_sftpStatVfsRequestBytes))
.Callback(() =>
{
_channelSessionMock.Raise(c => c.DataReceived += null,
new ChannelDataEventArgs(0, _sftpStatVfsResponse.GetBytes()));
});
}

protected void Arrange()
Expand Down Expand Up @@ -153,4 +170,4 @@ public void AvailableBlocksInReturnedValueShouldMatchValueInSftpResponse()
Assert.AreEqual(_bAvail, _actual.AvailableBlocks);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Renci.SshNet.Tests.Classes.Sftp
{
internal class SftpStatVfsResponseBuilder
internal sealed class SftpStatVfsResponseBuilder
{
private uint _protocolVersion;
private uint _responseId;
Expand Down Expand Up @@ -141,8 +141,13 @@ public StatVfsResponse Build()
}
}

internal class StatVfsResponse : SftpExtendedReplyResponse
internal sealed class StatVfsResponse : SftpResponse
{
public override SftpMessageTypes SftpMessageType
{
get { return SftpMessageTypes.ExtendedReply; }
}

public SftpFileSytemInformation Information { get; set; }

public StatVfsResponse(uint protocolVersion)
Expand Down
6 changes: 3 additions & 3 deletions src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@


<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.3" />
<PackageReference Include="Moq" Version="4.18.4" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Renci.SshNet/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ dotnet_diagnostic.SYSLIB1045.severity = none
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md
dotnet_diagnostic.SA1202.severity = none

#### Meziantou.Analyzer rules ####

# MA0053: Make class sealed
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0053.md
MA0053.public_class_should_be_sealed = false

#### .NET Compiler Platform analysers rules ####

# CA1031: Do not catch general exception types
Expand Down
16 changes: 16 additions & 0 deletions src/Renci.SshNet/Abstractions/CryptoAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public static System.Security.Cryptography.RandomNumberGenerator CreateRandomNum

public static System.Security.Cryptography.MD5 CreateMD5()
{
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
return System.Security.Cryptography.MD5.Create();
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
}

public static System.Security.Cryptography.SHA1 CreateSHA1()
{
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
return System.Security.Cryptography.SHA1.Create();
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
}

public static System.Security.Cryptography.SHA256 CreateSHA256()
Expand All @@ -66,7 +70,9 @@ public static System.Security.Cryptography.SHA512 CreateSHA512()
public static System.Security.Cryptography.RIPEMD160 CreateRIPEMD160()
{
#if FEATURE_HASH_RIPEMD160_CREATE
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
return System.Security.Cryptography.RIPEMD160.Create();
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
#else
return new System.Security.Cryptography.RIPEMD160Managed();
#endif
Expand All @@ -80,22 +86,30 @@ public static System.Security.Cryptography.RIPEMD160 CreateRIPEMD160()

public static System.Security.Cryptography.HMACMD5 CreateHMACMD5(byte[] key)
{
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
return new System.Security.Cryptography.HMACMD5(key);
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
}

public static HMACMD5 CreateHMACMD5(byte[] key, int hashSize)
{
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
return new HMACMD5(key, hashSize);
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
}

public static System.Security.Cryptography.HMACSHA1 CreateHMACSHA1(byte[] key)
{
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
return new System.Security.Cryptography.HMACSHA1(key);
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
}

public static HMACSHA1 CreateHMACSHA1(byte[] key, int hashSize)
{
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
return new HMACSHA1(key, hashSize);
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
}

public static System.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key)
Expand Down Expand Up @@ -131,7 +145,9 @@ public static HMACSHA512 CreateHMACSHA512(byte[] key, int hashSize)
#if FEATURE_HMAC_RIPEMD160
public static System.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key)
{
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
return new System.Security.Cryptography.HMACRIPEMD160(key);
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
}
#else
public static global::SshNet.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key)
Expand Down
9 changes: 7 additions & 2 deletions src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Threading;

namespace Renci.SshNet.Abstractions
{
Expand All @@ -22,7 +21,13 @@ public static bool IsEnabled(TraceEventType traceEventType)
[Conditional("DEBUG")]
public static void Log(string text)
{
Loggging.TraceEvent(TraceEventType.Verbose, Thread.CurrentThread.ManagedThreadId, text);
Loggging.TraceEvent(TraceEventType.Verbose,
#if NET6_0_OR_GREATER
System.Environment.CurrentManagedThreadId,
#else
System.Threading.Thread.CurrentThread.ManagedThreadId,
#endif // NET6_0_OR_GREATER
text);
}
}
}
6 changes: 3 additions & 3 deletions src/Renci.SshNet/Abstractions/SocketAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public static bool CanWrite(Socket socket)
public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
{
var socket = new Socket(remoteEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
ConnectCore(socket, remoteEndpoint, connectTimeout, true);
ConnectCore(socket, remoteEndpoint, connectTimeout, ownsSocket: true);
return socket;
}

public static void Connect(Socket socket, IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
{
ConnectCore(socket, remoteEndpoint, connectTimeout, false);
ConnectCore(socket, remoteEndpoint, connectTimeout, ownsSocket: false);
}

public static async Task ConnectAsync(Socket socket, IPEndPoint remoteEndpoint, CancellationToken cancellationToken)
Expand All @@ -60,7 +60,7 @@ public static async Task ConnectAsync(Socket socket, IPEndPoint remoteEndpoint,
private static void ConnectCore(Socket socket, IPEndPoint remoteEndpoint, TimeSpan connectTimeout, bool ownsSocket)
{
#if FEATURE_SOCKET_EAP
var connectCompleted = new ManualResetEvent(false);
var connectCompleted = new ManualResetEvent(initialState: false);
var args = new SocketAsyncEventArgs
{
UserToken = connectCompleted,
Expand Down
8 changes: 4 additions & 4 deletions src/Renci.SshNet/Abstractions/SocketExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void SetCompleted()
{
IsCompleted = true;

var continuation = _continuationAction ?? Interlocked.CompareExchange(ref _continuationAction, SENTINEL, null);
var continuation = _continuationAction ?? Interlocked.CompareExchange(ref _continuationAction, SENTINEL, comparand: null);
if (continuation is not null)
{
continuation();
Expand All @@ -57,7 +57,7 @@ public SocketAsyncEventArgsAwaitable GetAwaiter()

void INotifyCompletion.OnCompleted(Action continuation)
{
if (_continuationAction == SENTINEL || Interlocked.CompareExchange(ref _continuationAction, continuation, null) == SENTINEL)
if (_continuationAction == SENTINEL || Interlocked.CompareExchange(ref _continuationAction, continuation, comparand: null) == SENTINEL)
{
// We have already completed; run continuation asynchronously
_ = Task.Run(continuation);
Expand Down Expand Up @@ -92,7 +92,7 @@ public static async Task ConnectAsync(this Socket socket, IPEndPoint remoteEndpo
{
args.RemoteEndPoint = remoteEndpoint;

using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, false))
using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, useSynchronizationContext: false))
{
await args.ExecuteAsync(socket.ConnectAsync);
}
Expand All @@ -107,7 +107,7 @@ public static async Task<int> ReceiveAsync(this Socket socket, byte[] buffer, in
{
args.SetBuffer(buffer, offset, length);

using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, false))
using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, useSynchronizationContext: false))
{
await args.ExecuteAsync(socket.ReceiveAsync);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
/// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
/// connection info will be disposed when this instance is disposed.
/// </remarks>
internal BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
{
if (connectionInfo is null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Channels/ChannelDirectTcpip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Renci.SshNet.Channels
/// <summary>
/// Implements "direct-tcpip" SSH channel.
/// </summary>
internal class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
{
private readonly object _socketLock = new object();

Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Renci.SshNet.Channels
/// <summary>
/// Implements "forwarded-tcpip" SSH channel.
/// </summary>
internal class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
internal sealed class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
{
private readonly object _socketShutdownAndCloseLock = new object();
private Socket _socket;
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/ClientAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Renci.SshNet
{
internal class ClientAuthentication : IClientAuthentication
internal sealed class ClientAuthentication : IClientAuthentication
{
private readonly int _partialSuccessLimit;

Expand Down Expand Up @@ -151,7 +151,7 @@ private bool TryAuthenticate(ISession session,
return false;
}

private class AuthenticationState
private sealed class AuthenticationState
{
private readonly IList<IAuthenticationMethod> _supportedAuthenticationMethods;

Expand Down
Loading