private static void ExecuteTests(ReadOnlyCollection <TestCaseData> testCases, FileRegionsCache fileRegionsCache, Uri uri) { foreach (TestCaseData testCase in testCases) { Region inputRegion = testCase.InputRegion; Region expectedRegion = testCase.OutputRegion.DeepClone(); ArtifactContent snippet = expectedRegion.Snippet; expectedRegion.Snippet = null; Region actualRegion = fileRegionsCache.PopulateTextRegionProperties(inputRegion, uri, populateSnippet: false); actualRegion.ValueEquals(expectedRegion).Should().BeTrue(); actualRegion.Snippet.Should().BeNull(); expectedRegion.Snippet = snippet; actualRegion = fileRegionsCache.PopulateTextRegionProperties(inputRegion, uri, populateSnippet: true); actualRegion.ValueEquals(expectedRegion).Should().BeTrue(); if (snippet == null) { actualRegion.Snippet.Should().BeNull(); } else { actualRegion.Snippet.Text.Should().Be(snippet.Text); } } }
internal ReplacementVersionOne CreateReplacementVersionOne(Replacement v2Replacement, Encoding encoding) { ReplacementVersionOne replacement = null; if (v2Replacement != null) { replacement = new ReplacementVersionOne(); ArtifactContent insertedContent = v2Replacement.InsertedContent; if (insertedContent != null) { if (insertedContent.Binary != null) { replacement.InsertedBytes = insertedContent.Binary; } else if (insertedContent.Text != null) { if (encoding != null) { replacement.InsertedBytes = SarifUtilities.GetBase64String(insertedContent.Text, encoding); } else { // The encoding is null or not supported on the current platform throw new UnknownEncodingException(); } } } replacement.DeletedLength = v2Replacement.DeletedRegion.ByteLength; replacement.Offset = v2Replacement.DeletedRegion.ByteOffset; } return(replacement); }
public override PhysicalLocation VisitPhysicalLocation(PhysicalLocation node) { if (node.Region == null || node.Region.IsBinaryRegion) { goto Exit; } bool insertRegionSnippets = _dataToInsert.HasFlag(OptionallyEmittedData.RegionSnippets); bool overwriteExistingData = _dataToInsert.HasFlag(OptionallyEmittedData.OverwriteExistingData); bool insertContextCodeSnippets = _dataToInsert.HasFlag(OptionallyEmittedData.ContextRegionSnippets); bool populateRegionProperties = _dataToInsert.HasFlag(OptionallyEmittedData.ComprehensiveRegionProperties); if (insertRegionSnippets || populateRegionProperties || insertContextCodeSnippets) { Region expandedRegion; _fileRegionsCache = _fileRegionsCache ?? new FileRegionsCache(_run); // If we can resolve a file location to a newly constructed // absolute URI, we will prefer that if (!node.ArtifactLocation.TryReconstructAbsoluteUri(_run.OriginalUriBaseIds, out Uri resolvedUri)) { resolvedUri = node.ArtifactLocation.Uri; } if (!resolvedUri.IsAbsoluteUri) { goto Exit; } expandedRegion = _fileRegionsCache.PopulateTextRegionProperties(node.Region, resolvedUri, populateSnippet: insertRegionSnippets); ArtifactContent originalSnippet = node.Region.Snippet; if (populateRegionProperties) { node.Region = expandedRegion; } if (originalSnippet == null || overwriteExistingData) { node.Region.Snippet = expandedRegion.Snippet; } else { node.Region.Snippet = originalSnippet; } if (insertContextCodeSnippets && (node.ContextRegion == null || overwriteExistingData)) { node.ContextRegion = _fileRegionsCache.ConstructMultilineContextSnippet(expandedRegion, resolvedUri); } } Exit: return(base.VisitPhysicalLocation(node)); }
public ArtifactDetailsModel(Artifact artifact) { string sha256Hash; if (artifact.Hashes.TryGetValue("sha-256", out sha256Hash)) { this.Sha256Hash = sha256Hash; } this._artifactContent = artifact.Contents; this._decodedContents = new Lazy <string>(this.DecodeContents); }
private string DecodeContents() { string content = this._artifactContent.Text; if (content == null) { byte[] data = Convert.FromBase64String(this._artifactContent.Binary); content = Encoding.UTF8.GetString(data); } this._artifactContent = null; // Clear _fileContent to save memory. return(content); }
public override PhysicalLocation VisitPhysicalLocation(PhysicalLocation node) { if (node.Region == null || node.Region.IsBinaryRegion) { goto Exit; } bool insertRegionSnippets = _dataToInsert.HasFlag(OptionallyEmittedData.RegionSnippets); bool overwriteExistingData = _dataToInsert.HasFlag(OptionallyEmittedData.OverwriteExistingData); bool insertContextCodeSnippets = _dataToInsert.HasFlag(OptionallyEmittedData.ContextRegionSnippets); bool populateRegionProperties = _dataToInsert.HasFlag(OptionallyEmittedData.ComprehensiveRegionProperties); if (insertRegionSnippets || populateRegionProperties || insertContextCodeSnippets) { Region expandedRegion; ArtifactLocation artifactLocation = node.ArtifactLocation; _fileRegionsCache = _fileRegionsCache ?? new FileRegionsCache(_run); if (artifactLocation.Uri == null && artifactLocation.Index >= 0) { // Uri is not stored at result level, but we have an index to go look in run.Artifacts // we must pick the ArtifactLocation details from run.artifacts array Artifact artifactFromRun = _run.Artifacts[artifactLocation.Index]; artifactLocation = artifactFromRun.Location; } // If we can resolve a file location to a newly constructed // absolute URI, we will prefer that if (!artifactLocation.TryReconstructAbsoluteUri(_run.OriginalUriBaseIds, out Uri resolvedUri)) { resolvedUri = artifactLocation.Uri; } if (!resolvedUri.IsAbsoluteUri) { goto Exit; } expandedRegion = _fileRegionsCache.PopulateTextRegionProperties(node.Region, resolvedUri, populateSnippet: insertRegionSnippets); ArtifactContent originalSnippet = node.Region.Snippet; if (populateRegionProperties) { node.Region = expandedRegion; } if (originalSnippet == null || overwriteExistingData) { node.Region.Snippet = expandedRegion.Snippet; } else { node.Region.Snippet = originalSnippet; } if (insertContextCodeSnippets && (node.ContextRegion == null || overwriteExistingData)) { node.ContextRegion = _fileRegionsCache.ConstructMultilineContextSnippet(expandedRegion, resolvedUri); } } Exit: return(base.VisitPhysicalLocation(node)); }