-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework spm check based on roslyn pkg updates
- Loading branch information
Showing
4 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Dhgms.GripeWithRoslyn.Analyzer/Analyzers/DoNotUseSystemNetServicePointManagerAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using Dhgms.GripeWithRoslyn.Analyzer.CodeCracker.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Dhgms.GripeWithRoslyn.Analyzer.Analyzers | ||
{ | ||
/// <summary> | ||
/// Analyzer to detect the use of <see cref="System.Net.ServicePointManager"/>. | ||
/// </summary> | ||
[Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class DoNotUseSystemNetServicePointManagerAnalyzer : BaseInvocationUsingClassAnalyzer | ||
{ | ||
internal const string Title = "Do not use System.Net.ServicePointManager."; | ||
|
||
private const string MessageFormat = Title; | ||
|
||
private const string Category = SupportedCategories.Design; | ||
|
||
private const string Description = | ||
"System.Net.ServicePointManager suggests code that may not be flexible, such as making global changes to transport layer security. The recommendation is to use System.Net.HttpClient"; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DoNotUseSystemNetServicePointManagerAnalyzer"/> class. | ||
/// </summary> | ||
public DoNotUseSystemNetServicePointManagerAnalyzer() | ||
: base( | ||
DiagnosticIdsHelper.DoNotUseSystemNetServicePointManager, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
Description, | ||
DiagnosticSeverity.Warning) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override string ClassName => "global::System.Net.ServicePointManager"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...s.GripeWithRoslyn.UnitTests/Analyzers/DoNotUseSystemNetServicePointManagerAnalyzerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using Dhgms.GripeWithRoslyn.Analyzer; | ||
using Dhgms.GripeWithRoslyn.Analyzer.Analyzers; | ||
using Dhgms.GripeWithRoslyn.UnitTests.Helpers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Xunit; | ||
|
||
namespace Dhgms.GripeWithRoslyn.UnitTests.Analyzers | ||
{ | ||
/// <summary> | ||
/// Unit Tests for <see cref="DoNotUseSystemNetServicePointManagerAnalyzer"/>. | ||
/// </summary> | ||
public class DoNotUseSystemNetServicePointManagerAnalyzerTest : CodeFixVerifier | ||
{ | ||
/// <summary> | ||
/// Test to ensure bad code returns a warning for a method invocation. | ||
/// </summary> | ||
[Fact] | ||
public void ReturnsWarningForMethodInvocation() | ||
{ | ||
var test = @" | ||
namespace System.Net | ||
{ | ||
public sealed class ServicePoint | ||
{ | ||
} | ||
public sealed class System.Net.Security.RemoteCertificateValidationCallback | ||
{ | ||
} | ||
public sealed class ServicePointManager | ||
{ | ||
public static ServicePoint FindServicePoint(Uri uri) | ||
{ | ||
} | ||
public static System.Net.Security.RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; } | ||
} | ||
} | ||
namespace ConsoleApplication1 | ||
{ | ||
class TypeName | ||
{ | ||
public void MethodName() | ||
{ | ||
var servicePointManager = new System.Net.ServicePointManager(); | ||
servicePointManager.FindServicePoint(); | ||
} | ||
} | ||
}"; | ||
var methodInvoke = new DiagnosticResult | ||
{ | ||
Id = DiagnosticIdsHelper.DoNotUseSystemNetServicePointManager, | ||
Message = DoNotUseSystemNetServicePointManagerAnalyzer.Title, | ||
Severity = DiagnosticSeverity.Warning, | ||
Locations = | ||
new[] | ||
{ | ||
new DiagnosticResultLocation("Test0.cs", 29, 17) | ||
} | ||
}; | ||
|
||
VerifyCSharpDiagnostic( | ||
test, | ||
methodInvoke); | ||
} | ||
|
||
/// <summary> | ||
/// Test to ensure bad code returns a warning for a property access. | ||
/// </summary> | ||
[Fact] | ||
public void ReturnsWarningForPropertyAccess() | ||
{ | ||
var test = @" | ||
namespace System.Net | ||
{ | ||
public sealed class ServicePoint | ||
{ | ||
} | ||
public sealed class System.Net.Security.RemoteCertificateValidationCallback | ||
{ | ||
} | ||
public sealed class ServicePointManager | ||
{ | ||
public static ServicePoint FindServicePoint() | ||
{ | ||
} | ||
public static System.Net.Security.RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; } | ||
} | ||
} | ||
namespace ConsoleApplication1 | ||
{ | ||
class TypeName | ||
{ | ||
public void MethodName() | ||
{ | ||
var remoteCertificateValidationCallback = System.Net.ServicePointManager.RemoteCertificateValidationCallback; | ||
} | ||
} | ||
}"; | ||
var methodInvoke = new DiagnosticResult | ||
{ | ||
Id = DiagnosticIdsHelper.DoNotUseSystemNetServicePointManager, | ||
Message = DoNotUseSystemNetServicePointManagerAnalyzer.Title, | ||
Severity = DiagnosticSeverity.Warning, | ||
Locations = | ||
new[] | ||
{ | ||
new DiagnosticResultLocation("Test0.cs", 19, 17) | ||
} | ||
}; | ||
|
||
VerifyCSharpDiagnostic( | ||
test, | ||
methodInvoke); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() | ||
{ | ||
return new DoNotUseSystemNetServicePointManagerAnalyzer(); | ||
} | ||
} | ||
} |