Skip to content
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

Sarif converters fixups #1218

Merged
2 commits merged into from
Jan 17, 2019
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
4 changes: 2 additions & 2 deletions src/Sarif.Converters.UnitTests/ConverterTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Microsoft.CodeAnalysis.Sarif
{
public class ConverterTestsBase<T> where T : ToolFileConverterBase, new()
{
public SarifLog RunTestCase(string inputData, string expectedResult, bool prettyPrint = true)
public SarifLog RunTestCase(string inputData, string expectedResult, bool prettyPrint = true, bool forceV2Transform = false)
{
PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(expectedResult, forceUpdate: false, formatting: Formatting.Indented, out expectedResult);
PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(expectedResult, forceUpdate: forceV2Transform, formatting: Formatting.Indented, out expectedResult);
var converter = new T();

// First retrieve converter JSON. This code will raise appropriate exceptions
Expand Down
4 changes: 2 additions & 2 deletions src/Sarif.Converters.UnitTests/SemmleQLConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void SemmleQLConverter_SimpleCsv()
}
]
}";
RunTestCase(semmleCsvInput, expected, prettyPrint: true);
RunTestCase(semmleCsvInput, expected, prettyPrint: true, forceV2Transform: true);
}

[Fact]
Expand Down Expand Up @@ -153,7 +153,7 @@ public void SemmleQLConvert_EmbeddedLocations()
]
}";

RunTestCase(semmleCsvInput, expected, prettyPrint: true);
RunTestCase(semmleCsvInput, expected, prettyPrint: true, forceV2Transform: true);
}

private class SemmleCsvRecord
Expand Down
17 changes: 12 additions & 5 deletions src/Sarif.Converters/FortifyFprConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class FortifyFprConverter : ToolFileConverterBase
private string _automationId;
private string _originalUriBasePath;
private List<Result> _results = new List<Result>();
private Dictionary<string, FileData> _fileDictionary;
private HashSet<FileData> _files;
private Dictionary<string, Rule> _ruleDictionary;
private Dictionary<ThreadFlowLocation, string> _tflToNodeIdDictionary;
private Dictionary<ThreadFlowLocation, string> _tflToSnippetIdDictionary;
Expand All @@ -49,7 +49,7 @@ public FortifyFprConverter()
_strings = new FortifyFprStrings(_nameTable);

_results = new List<Result>();
_fileDictionary = new Dictionary<string, FileData>();
_files = new HashSet<FileData>(FileData.ValueComparer);
_ruleDictionary = new Dictionary<string, Rule>();
_tflToNodeIdDictionary = new Dictionary<ThreadFlowLocation, string>();
_tflToSnippetIdDictionary = new Dictionary<ThreadFlowLocation, string>();
Expand Down Expand Up @@ -89,7 +89,7 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm
_invocation = new Invocation();
_invocation.ToolNotifications = new List<Notification>();
_results.Clear();
_fileDictionary.Clear();
_files.Clear();
_ruleDictionary.Clear();
_tflToNodeIdDictionary.Clear();
_tflToSnippetIdDictionary.Clear();
Expand All @@ -113,6 +113,7 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm
InstanceGuid = _runId,
InstanceId = _automationId + "/"
},
Files = new List<FileData>(_files),
Tool = tool,
Invocations = new[] { _invocation },
Resources = new Resources { Rules = _ruleDictionary}
Expand Down Expand Up @@ -293,14 +294,20 @@ private void ParseFile()

if (!string.IsNullOrEmpty(fileName))
{
Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);
var fileData = new FileData
{
Encoding = encoding,
MimeType = MimeType.DetermineFromFileExtension(fileName),
Length = length
Length = length,
FileLocation = new FileLocation
{
Uri = uri,
UriBaseId = uri.IsAbsoluteUri ? null : FileLocationUriBaseId
}
};

_fileDictionary.Add(fileName, fileData);
_files.Add(fileData);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Sarif.UnitTests/Sarif.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
<None Remove="TestData\InsertOptionalDataVisitor\ExpectedOutputs\CoreTests_RegionSnippets.sarif" />
<None Remove="TestData\InsertOptionalDataVisitor\ExpectedOutputs\CoreTests_TextFiles.sarif" />
<None Remove="TestData\InsertOptionalDataVisitor\Inputs\CoreTests.sarif" />
<None Remove="TestData\PrereleaseCompatibilityTransformer\ExpectedOutputs\ComprehensiveFileProperties.sarif" />
<None Remove="TestData\PrereleaseCompatibilityTransformer\ExpectedOutputs\NestedFiles.sarif" />
<None Remove="TestData\PrereleaseCompatibilityTransformer\Inputs\ComprehensiveFileProperties.sarif" />
<None Remove="TestData\PrereleaseCompatibilityTransformer\Inputs\NestedFiles.sarif" />
<None Remove="TestData\SarifCurrentToVersionOneVisitor\ExpectedOutputs\Minimum.sarif" />
<None Remove="TestData\SarifCurrentToVersionOneVisitor\ExpectedOutputs\MinimumWithTwoRuns.sarif" />
Expand Down Expand Up @@ -106,7 +108,9 @@
<EmbeddedResource Include="TestData\InsertOptionalDataVisitor\ExpectedOutputs\CoreTests_RegionSnippets.sarif" />
<EmbeddedResource Include="TestData\InsertOptionalDataVisitor\ExpectedOutputs\CoreTests_TextFiles.sarif" />
<EmbeddedResource Include="TestData\InsertOptionalDataVisitor\Inputs\CoreTests.sarif" />
<EmbeddedResource Include="TestData\PrereleaseCompatibilityTransformer\ExpectedOutputs\ComprehensiveFileProperties.sarif" />
<EmbeddedResource Include="TestData\PrereleaseCompatibilityTransformer\ExpectedOutputs\NestedFiles.sarif" />
<EmbeddedResource Include="TestData\PrereleaseCompatibilityTransformer\Inputs\ComprehensiveFileProperties.sarif" />
<EmbeddedResource Include="TestData\PrereleaseCompatibilityTransformer\Inputs\NestedFiles.sarif" />
<EmbeddedResource Include="TestData\SarifCurrentToVersionOneVisitor\ExpectedOutputs\Minimum.sarif" />
<EmbeddedResource Include="TestData\SarifCurrentToVersionOneVisitor\ExpectedOutputs\MinimumWithTwoRuns.sarif" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"$schema": "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-11-28",
"version": "2.0.0-csd.2.beta.2018-11-28",
"runs": [
{
"tool": {
"name": "CodeScanner",
"semanticVersion": "2.1.0"
},
"files": [
{
"fileLocation": {
"uri": "file.txt",
"uriBaseId": "SUB",
"fileIndex": 0
},
"length": 3945,
"roles": ["addedFile"],
"mimeType": "text/plain"
},
{
"fileLocation": {
"uri": "file.txt",
"fileIndex": 1
},
"mimeType": "text/plain",
"encoding": "utf8"
}
],
"results": [
{
"ruleId": "C1",
"message": {
"messageId": "a"
},
"locations": [
{
"physicalLocation": {
"fileLocation": {
"uri": "file.txt",
"fileIndex": 1
}
},
"properties": {
"key": "file.txt"
}
}
]
},
{
"ruleId": "C1",
"message": {
"messageId": "a"
},
"locations": [
{
"physicalLocation": {
"fileLocation": {
"uri": "file.txt",
"uriBaseId": "SUB",
"fileIndex": 0,
"properties": {
"key": "#SUB#file.txt"
Copy link

@ghost ghost Jan 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"key": "#SUB#file.txt" [](start = 20, length = 22)

I didn't realized the transformer saved the key. #ByDesign

}
}
}
}
]
}
],
"resources": {
"rules": {
"C1": {
"messageStrings": {
"a": "Review all plaintext content for geopolitically sensitive terms."
}
}
}
},
"columnKind": "utf16CodeUnits"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-10-10",
"version": "2.0.0-csd.2.beta.2018-10-10",
"runs": [
{
"tool": {
"name": "CodeScanner",
"semanticVersion": "2.1.0"
},
"files": {
"#SUB#file.txt": { "mimeType": "text/plain", "length": 3945, "roles": ["addedFile"] },
"file.txt": { "mimeType": "text/plain", "encoding": "utf8" }
},
"results": [
{ "ruleId": "C1", "message": {"messageId": "a"}, "locations": [{"physicalLocation": {"fileLocation": {"uri": "file.txt"}}, "properties": {"key": "file.txt"}}]},
{ "ruleId": "C1", "message": {"messageId": "a"}, "locations": [{"physicalLocation": {"fileLocation": {"uri": "file.txt", "uriBaseId": "SUB", "properties": {"key": "#SUB#file.txt"}}}}]}
],
"resources": { "rules": { "C1": { "messageStrings": { "a": "Review all plaintext content for geopolitically sensitive terms." } } } }
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public void PrereleaseCompatibilityTransformer_NestedFiles()
{
RunTest("NestedFiles.sarif");
}

[Fact]
public void PrereleaseCompatibilityTransformer_ComprehensiveFileProperties()
{
RunTest("ComprehensiveFileProperties.sarif");
}
}
}
13 changes: 13 additions & 0 deletions src/Sarif/Core/Invocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,18 @@ public bool ShouldSerializeArguments()
return this.Arguments != null &&
(this.Arguments.Where((e) => { return e != null; }).Count() == this.Arguments.Count);
}

public bool ShouldSerializeToolNotifications()
{
return this.ToolNotifications != null &&
this.ToolNotifications.Where((n) => { return n != null; }).Any();
}

public bool ShouldSerializeConfigurationNotifications()
{
return this.ConfigurationNotifications != null &&
this.ConfigurationNotifications.Where((n) => { return n != null; }).Any();
}

}
}
4 changes: 2 additions & 2 deletions src/Sarif/Core/Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ private void InitializeFileToIndexMap()

var fileLocation = new FileLocation
{
Uri = fileData.FileLocation.Uri,
UriBaseId = fileData.FileLocation.UriBaseId,
Uri = fileData.FileLocation?.Uri,
UriBaseId = fileData.FileLocation?.UriBaseId,
};

_fileToIndexMap[fileLocation] = i;
Expand Down