Skip to content

Commit 27976bb

Browse files
authored
Analyzer fixes round 3. (#1135)
1 parent c04cdbc commit 27976bb

File tree

81 files changed

+554
-528
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+554
-528
lines changed

Directory.Build.props

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
<!--
3838
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0-preview1.23165.1" PrivateAssets="all" />
3939
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="all" />
40-
<PackageReference Include="Meziantou.Analyzer" Version="2.0.52" PrivateAssets="all" />
41-
-->
42-
<!--
40+
<PackageReference Include="Meziantou.Analyzer" Version="2.0.54" PrivateAssets="all" />
4341
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544" PrivateAssets="all" />
44-
-->
42+
-->
4543
</ItemGroup>
4644
</Project>

src/Renci.SshNet.Tests/Classes/Sftp/SftpSessionTest_Connected_RequestStatVfs.cs

+42-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Text;
3+
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
46
using Moq;
7+
58
using Renci.SshNet.Channels;
69
using Renci.SshNet.Common;
710
using Renci.SshNet.Sftp;
@@ -97,33 +100,47 @@ private void SetupMocks()
97100

98101
#region SftpSession.Connect()
99102

100-
_sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
101-
_channelSessionMock.InSequence(sequence).Setup(p => p.Open());
102-
_channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
103-
_channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
104-
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpInitRequestBytes)).Callback(
105-
() =>
106-
{
107-
_channelSessionMock.Raise(c => c.DataReceived += null,
108-
new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
109-
});
110-
_channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
111-
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpRealPathRequestBytes)).Callback(
112-
() =>
113-
{
114-
_channelSessionMock.Raise(c => c.DataReceived += null,
115-
new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
116-
});
103+
_ = _sessionMock.InSequence(sequence)
104+
.Setup(p => p.CreateChannelSession())
105+
.Returns(_channelSessionMock.Object);
106+
_ = _channelSessionMock.InSequence(sequence)
107+
.Setup(p => p.Open());
108+
_ = _channelSessionMock.InSequence(sequence)
109+
.Setup(p => p.SendSubsystemRequest("sftp"))
110+
.Returns(true);
111+
_ = _channelSessionMock.InSequence(sequence)
112+
.Setup(p => p.IsOpen)
113+
.Returns(true);
114+
_ = _channelSessionMock.InSequence(sequence)
115+
.Setup(p => p.SendData(_sftpInitRequestBytes))
116+
.Callback(() =>
117+
{
118+
_channelSessionMock.Raise(c => c.DataReceived += null,
119+
new ChannelDataEventArgs(0, _sftpVersionResponse.GetBytes()));
120+
});
121+
_ = _channelSessionMock.InSequence(sequence)
122+
.Setup(p => p.IsOpen)
123+
.Returns(true);
124+
_ = _channelSessionMock.InSequence(sequence)
125+
.Setup(p => p.SendData(_sftpRealPathRequestBytes))
126+
.Callback(() =>
127+
{
128+
_channelSessionMock.Raise(c => c.DataReceived += null,
129+
new ChannelDataEventArgs(0, _sftpNameResponse.GetBytes()));
130+
});
117131

118132
#endregion SftpSession.Connect()
119133

120-
_channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
121-
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(_sftpStatVfsRequestBytes)).Callback(
122-
() =>
123-
{
124-
_channelSessionMock.Raise(c => c.DataReceived += null,
125-
new ChannelDataEventArgs(0, _sftpStatVfsResponse.GetBytes()));
126-
});
134+
_ = _channelSessionMock.InSequence(sequence)
135+
.Setup(p => p.IsOpen)
136+
.Returns(true);
137+
_ = _channelSessionMock.InSequence(sequence)
138+
.Setup(p => p.SendData(_sftpStatVfsRequestBytes))
139+
.Callback(() =>
140+
{
141+
_channelSessionMock.Raise(c => c.DataReceived += null,
142+
new ChannelDataEventArgs(0, _sftpStatVfsResponse.GetBytes()));
143+
});
127144
}
128145

129146
protected void Arrange()
@@ -153,4 +170,4 @@ public void AvailableBlocksInReturnedValueShouldMatchValueInSftpResponse()
153170
Assert.AreEqual(_bAvail, _actual.AvailableBlocks);
154171
}
155172
}
156-
}
173+
}

src/Renci.SshNet.Tests/Classes/Sftp/SftpStatVfsResponseBuilder.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Renci.SshNet.Tests.Classes.Sftp
55
{
6-
internal class SftpStatVfsResponseBuilder
6+
internal sealed class SftpStatVfsResponseBuilder
77
{
88
private uint _protocolVersion;
99
private uint _responseId;
@@ -141,8 +141,13 @@ public StatVfsResponse Build()
141141
}
142142
}
143143

144-
internal class StatVfsResponse : SftpExtendedReplyResponse
144+
internal sealed class StatVfsResponse : SftpResponse
145145
{
146+
public override SftpMessageTypes SftpMessageType
147+
{
148+
get { return SftpMessageTypes.ExtendedReply; }
149+
}
150+
146151
public SftpFileSytemInformation Information { get; set; }
147152

148153
public StatVfsResponse(uint protocolVersion)

src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464

6565

6666
<ItemGroup>
67-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
68-
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
69-
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
67+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
68+
<PackageReference Include="MSTest.TestAdapter" Version="3.0.3" />
69+
<PackageReference Include="MSTest.TestFramework" Version="3.0.3" />
7070
<PackageReference Include="Moq" Version="4.18.4" />
7171
</ItemGroup>
7272
<ItemGroup>

src/Renci.SshNet/.editorconfig

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ dotnet_diagnostic.SYSLIB1045.severity = none
1313
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md
1414
dotnet_diagnostic.SA1202.severity = none
1515

16+
#### Meziantou.Analyzer rules ####
17+
18+
# MA0053: Make class sealed
19+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0053.md
20+
MA0053.public_class_should_be_sealed = false
21+
1622
#### .NET Compiler Platform analysers rules ####
1723

1824
# CA1031: Do not catch general exception types

src/Renci.SshNet/Abstractions/CryptoAbstraction.cs

+16
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ public static System.Security.Cryptography.RandomNumberGenerator CreateRandomNum
3939

4040
public static System.Security.Cryptography.MD5 CreateMD5()
4141
{
42+
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
4243
return System.Security.Cryptography.MD5.Create();
44+
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
4345
}
4446

4547
public static System.Security.Cryptography.SHA1 CreateSHA1()
4648
{
49+
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
4750
return System.Security.Cryptography.SHA1.Create();
51+
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
4852
}
4953

5054
public static System.Security.Cryptography.SHA256 CreateSHA256()
@@ -66,7 +70,9 @@ public static System.Security.Cryptography.SHA512 CreateSHA512()
6670
public static System.Security.Cryptography.RIPEMD160 CreateRIPEMD160()
6771
{
6872
#if FEATURE_HASH_RIPEMD160_CREATE
73+
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
6974
return System.Security.Cryptography.RIPEMD160.Create();
75+
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
7076
#else
7177
return new System.Security.Cryptography.RIPEMD160Managed();
7278
#endif
@@ -80,22 +86,30 @@ public static System.Security.Cryptography.RIPEMD160 CreateRIPEMD160()
8086

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

8694
public static HMACMD5 CreateHMACMD5(byte[] key, int hashSize)
8795
{
96+
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
8897
return new HMACMD5(key, hashSize);
98+
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
8999
}
90100

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

96108
public static HMACSHA1 CreateHMACSHA1(byte[] key, int hashSize)
97109
{
110+
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
98111
return new HMACSHA1(key, hashSize);
112+
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
99113
}
100114

101115
public static System.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key)
@@ -131,7 +145,9 @@ public static HMACSHA512 CreateHMACSHA512(byte[] key, int hashSize)
131145
#if FEATURE_HMAC_RIPEMD160
132146
public static System.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key)
133147
{
148+
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
134149
return new System.Security.Cryptography.HMACRIPEMD160(key);
150+
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
135151
}
136152
#else
137153
public static global::SshNet.Security.Cryptography.HMACRIPEMD160 CreateHMACRIPEMD160(byte[] key)

src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics;
2-
using System.Threading;
32

43
namespace Renci.SshNet.Abstractions
54
{
@@ -22,7 +21,13 @@ public static bool IsEnabled(TraceEventType traceEventType)
2221
[Conditional("DEBUG")]
2322
public static void Log(string text)
2423
{
25-
Loggging.TraceEvent(TraceEventType.Verbose, Thread.CurrentThread.ManagedThreadId, text);
24+
Loggging.TraceEvent(TraceEventType.Verbose,
25+
#if NET6_0_OR_GREATER
26+
System.Environment.CurrentManagedThreadId,
27+
#else
28+
System.Threading.Thread.CurrentThread.ManagedThreadId,
29+
#endif // NET6_0_OR_GREATER
30+
text);
2631
}
2732
}
2833
}

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public static bool CanWrite(Socket socket)
4343
public static Socket Connect(IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
4444
{
4545
var socket = new Socket(remoteEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
46-
ConnectCore(socket, remoteEndpoint, connectTimeout, true);
46+
ConnectCore(socket, remoteEndpoint, connectTimeout, ownsSocket: true);
4747
return socket;
4848
}
4949

5050
public static void Connect(Socket socket, IPEndPoint remoteEndpoint, TimeSpan connectTimeout)
5151
{
52-
ConnectCore(socket, remoteEndpoint, connectTimeout, false);
52+
ConnectCore(socket, remoteEndpoint, connectTimeout, ownsSocket: false);
5353
}
5454

5555
public static async Task ConnectAsync(Socket socket, IPEndPoint remoteEndpoint, CancellationToken cancellationToken)
@@ -60,7 +60,7 @@ public static async Task ConnectAsync(Socket socket, IPEndPoint remoteEndpoint,
6060
private static void ConnectCore(Socket socket, IPEndPoint remoteEndpoint, TimeSpan connectTimeout, bool ownsSocket)
6161
{
6262
#if FEATURE_SOCKET_EAP
63-
var connectCompleted = new ManualResetEvent(false);
63+
var connectCompleted = new ManualResetEvent(initialState: false);
6464
var args = new SocketAsyncEventArgs
6565
{
6666
UserToken = connectCompleted,

src/Renci.SshNet/Abstractions/SocketExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void SetCompleted()
3535
{
3636
IsCompleted = true;
3737

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

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

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

110-
using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, false))
110+
using (cancellationToken.Register(o => ((SocketAsyncEventArgsAwaitable)o).SetCancelled(), args, useSynchronizationContext: false))
111111
{
112112
await args.ExecuteAsync(socket.ReceiveAsync);
113113
}

src/Renci.SshNet/BaseClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
180180
/// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
181181
/// connection info will be disposed when this instance is disposed.
182182
/// </remarks>
183-
internal BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
183+
private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
184184
{
185185
if (connectionInfo is null)
186186
{

src/Renci.SshNet/Channels/ChannelDirectTcpip.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Renci.SshNet.Channels
1111
/// <summary>
1212
/// Implements "direct-tcpip" SSH channel.
1313
/// </summary>
14-
internal class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
14+
internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
1515
{
1616
private readonly object _socketLock = new object();
1717

src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Renci.SshNet.Channels
1010
/// <summary>
1111
/// Implements "forwarded-tcpip" SSH channel.
1212
/// </summary>
13-
internal class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
13+
internal sealed class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
1414
{
1515
private readonly object _socketShutdownAndCloseLock = new object();
1616
private Socket _socket;

src/Renci.SshNet/ClientAuthentication.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Renci.SshNet
66
{
7-
internal class ClientAuthentication : IClientAuthentication
7+
internal sealed class ClientAuthentication : IClientAuthentication
88
{
99
private readonly int _partialSuccessLimit;
1010

@@ -151,7 +151,7 @@ private bool TryAuthenticate(ISession session,
151151
return false;
152152
}
153153

154-
private class AuthenticationState
154+
private sealed class AuthenticationState
155155
{
156156
private readonly IList<IAuthenticationMethod> _supportedAuthenticationMethods;
157157

0 commit comments

Comments
 (0)