Skip to content

Commit

Permalink
SLVS-1292 Use vulnerabilityProbability from SLCore instead of the har…
Browse files Browse the repository at this point in the history
…dcoded mapping (#5573)
  • Loading branch information
gabriela-trutan-sonarsource committed Aug 5, 2024
1 parent 21f2c4d commit 2f556e9
Show file tree
Hide file tree
Showing 18 changed files with 459 additions and 341 deletions.
26 changes: 21 additions & 5 deletions src/Core/Analysis/AnalysisIssue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.Generic;

namespace SonarLint.VisualStudio.Core.Analysis
{
public class AnalysisIssue : IAnalysisIssue
{
private static readonly IReadOnlyList<IAnalysisIssueFlow> EmptyFlows = Array.Empty<IAnalysisIssueFlow>();
private static readonly IReadOnlyList<IQuickFix> EmptyFixes = Array.Empty<IQuickFix>();
private static readonly IReadOnlyList<IAnalysisIssueFlow> EmptyFlows = [];
private static readonly IReadOnlyList<IQuickFix> EmptyFixes = [];

public AnalysisIssue(
string ruleKey,
Expand Down Expand Up @@ -66,6 +63,25 @@ public AnalysisIssue(
public string RuleDescriptionContextKey { get; }
}

public class AnalysisHotspotIssue : AnalysisIssue, IAnalysisHotspotIssue
{
public AnalysisHotspotIssue(string ruleKey,
AnalysisIssueSeverity severity,
AnalysisIssueType type,
SoftwareQualitySeverity? highestSoftwareQualitySeverity,
IAnalysisIssueLocation primaryLocation,
IReadOnlyList<IAnalysisIssueFlow> flows,
IReadOnlyList<IQuickFix> fixes = null,
string context = null,
HotspotPriority? hotspotPriority = null) :
base(ruleKey, severity, type, highestSoftwareQualitySeverity, primaryLocation, flows, fixes, context)
{
HotspotPriority = hotspotPriority;
}

public HotspotPriority? HotspotPriority { get; }
}

public class AnalysisIssueFlow : IAnalysisIssueFlow
{
public AnalysisIssueFlow(IReadOnlyList<IAnalysisIssueLocation> locations)
Expand Down
14 changes: 12 additions & 2 deletions src/Core/Analysis/IAnalysisIssue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Collections.Generic;

namespace SonarLint.VisualStudio.Core.Analysis
{
public interface IAnalysisIssue : IAnalysisIssueBase
Expand All @@ -33,6 +31,11 @@ public interface IAnalysisIssue : IAnalysisIssueBase
IReadOnlyList<IQuickFix> Fixes { get; }
}

public interface IAnalysisHotspotIssue : IAnalysisIssue
{
HotspotPriority? HotspotPriority { get; }
}

public interface IAnalysisIssueBase
{
string RuleKey { get; }
Expand Down Expand Up @@ -106,6 +109,13 @@ public enum AnalysisIssueType
SecurityHotspot
}

public enum HotspotPriority
{
High,
Medium,
Low
}

public static class IAnalysisIssueExtensions
{
public static bool IsFileLevel(this IAnalysisIssueBase issue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.Models;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.Hotspots;
Expand All @@ -43,11 +40,6 @@ public void MefCtor_CheckIsSingleton()
}

[DataRow("notakey", null)]
[DataRow("typescript:S4502", HotspotPriority.High)]
[DataRow("javascript:S4502", HotspotPriority.High)]
[DataRow("typescript:S1313", HotspotPriority.Low)]
[DataRow("javascript:S1313", HotspotPriority.Low)]
[DataRow("javascript:S90000000000000000", null)]
[DataRow("c:S1313", HotspotPriority.Low)]
[DataRow("cpp:S1313", HotspotPriority.Low)]
[DataRow("python:S1313", null)]
Expand All @@ -58,7 +50,7 @@ public void GetPriority_ShouldReturnAsExpected(string ruleKey, HotspotPriority?
{
new HotspotReviewPriorityProvider().GetPriority(ruleKey).Should().Be(expectedPriority);
}

[TestMethod]
public void GetPriority_NullKey_Throws()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.TestInfrastructure;
using SonarLint.VisualStudio.IssueVisualization.Editor;
using SonarLint.VisualStudio.IssueVisualization.IssueVisualizationControl.ViewModels.Commands;
using SonarLint.VisualStudio.IssueVisualization.Models;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.HotspotsList.ViewModels;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.Models;
using SonarLint.VisualStudio.IssueVisualization.Selection;

namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.Hotspots.HotspotsList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Windows.Media;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.HotspotsList;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.Models;

namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.Hotspots.HotspotsList
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarLint.VisualStudio.ConnectedMode.Hotspots;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.IssueVisualization.Models;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots;
using SonarLint.VisualStudio.IssueVisualization.Security.IssuesStore;
Expand Down Expand Up @@ -87,7 +83,7 @@ public async Task IssuesChanged_HasIssues_ContextIsSet()
{
var monitorSelection = CreateMonitorSelection(222);
var serviceProvider = CreateServiceProvider(monitorSelection.Object);
var store = CreateStore(new LocalHotspot(Mock.Of<IAnalysisIssueVisualization>(), Security.Hotspots.Models.HotspotPriority.Medium));
var store = CreateStore(new LocalHotspot(Mock.Of<IAnalysisIssueVisualization>(), HotspotPriority.Medium));

_ = await CreateInitializedTestSubject(serviceProvider.Object, store.Object);
monitorSelection.Invocations.Clear();
Expand Down
44 changes: 34 additions & 10 deletions src/IssueViz.Security.UnitTests/Hotspots/LocalHotspotStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarLint.VisualStudio.ConnectedMode.Hotspots;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.IssueVisualization.Models;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.Models;
using SonarLint.VisualStudio.IssueVisualization.Security.IssuesStore;
using SonarLint.VisualStudio.SLCore.Common.Models;
using SonarLint.VisualStudio.TestInfrastructure;
using SonarQube.Client.Models;

Expand All @@ -43,8 +39,8 @@ public void MefCtor_CheckExports_ILocalHotspotsStore()
{
MefTestHelpers.CheckTypeCanBeImported<LocalHotspotsStore, ILocalHotspotsStore>(
MefTestHelpers.CreateExport<IServerHotspotStore>(),
MefTestHelpers.CreateExport<IHotspotMatcher>(),
MefTestHelpers.CreateExport<IHotspotReviewPriorityProvider>(),
MefTestHelpers.CreateExport<IHotspotMatcher>(),
MefTestHelpers.CreateExport<IThreadHandling>());
}

Expand All @@ -53,8 +49,8 @@ public void MefCtor_CheckExports_ILocalHotspotsStoreUpdater()
{
MefTestHelpers.CheckTypeCanBeImported<LocalHotspotsStore, ILocalHotspotsStoreUpdater>(
MefTestHelpers.CreateExport<IServerHotspotStore>(),
MefTestHelpers.CreateExport<IHotspotMatcher>(),
MefTestHelpers.CreateExport<IHotspotReviewPriorityProvider>(),
MefTestHelpers.CreateExport<IHotspotMatcher>(),
MefTestHelpers.CreateExport<IThreadHandling>());
}

Expand All @@ -63,8 +59,8 @@ public void MefCtor_CheckExports_IIssuesStore()
{
MefTestHelpers.CheckTypeCanBeImported<LocalHotspotsStore, IIssuesStore>(
MefTestHelpers.CreateExport<IServerHotspotStore>(),
MefTestHelpers.CreateExport<IHotspotMatcher>(),
MefTestHelpers.CreateExport<IHotspotReviewPriorityProvider>(),
MefTestHelpers.CreateExport<IHotspotMatcher>(),
MefTestHelpers.CreateExport<IThreadHandling>());
}

Expand Down Expand Up @@ -181,6 +177,23 @@ public void UpdateForFile_NoServerHotspots_UsesReviewPriority()
new LocalHotspot(issueVis3.Object, HotspotPriority.Low));
}

[TestMethod]
[DataRow(HotspotPriority.High)]
[DataRow(HotspotPriority.Medium)]
[DataRow(HotspotPriority.Low)]
public void UpdateForFile_WithNoServerHotspots_ShouldAssignHotspotPriority(HotspotPriority priority)
{
const string rule1 = "rule:s1";
var issueVis1 = CreateIssueVisualizationWithHotspot(rule1, priority);
var reviewPriorityProviderMock = new Mock<IHotspotReviewPriorityProvider>();
var testSubject = CreateTestSubject(out _, reviewPriorityProvider: reviewPriorityProviderMock.Object);

testSubject.UpdateForFile("file1", new[] { issueVis1 });

VerifyContent(testSubject, new LocalHotspot(issueVis1, priority));
reviewPriorityProviderMock.Verify(mock => mock.GetPriority(It.IsAny<string>()), Times.Never);
}

[TestMethod]
public void UpdateForFile_ServerHotspots_MatchesCorrectly()
{
Expand Down Expand Up @@ -210,7 +223,7 @@ public void UpdateForFile_ServerHotspots_MatchesCorrectly()
}

[TestMethod]
public void UpdateForFile_ServerHotspots_UsesReviewPriority()
public void UpdateForFile_WithNoServerHotspots_ForCFamily_ShouldAssignHotspotPriority()
{
/*
* issue1 + server1 -> rule1 -> Low - could be changed to test server override once implemented
Expand Down Expand Up @@ -625,4 +638,15 @@ private void EventHandler(object sender, IssuesChangedEventArgs eventArgs)
Events.Add(eventArgs);
}
}

private static IAnalysisIssueVisualization CreateIssueVisualizationWithHotspot(string rule, HotspotPriority priority)
{
var issueVis = new Mock<IAnalysisIssueVisualization>();
var hotspotIssue = new Mock<IAnalysisHotspotIssue>();
hotspotIssue.SetupGet(x => x.HotspotPriority).Returns(priority);
issueVis.Setup(x => x.Issue).Returns(hotspotIssue.Object);
issueVis.Setup(x => x.RuleId).Returns(rule);

return issueVis.Object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.IssueVisualization.Security.Hotspots.Models;

namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.Hotspots.Models
Expand Down
Loading

0 comments on commit 2f556e9

Please sign in to comment.