Beispiel #1
0
        public void OrderSensitiveValueComparisonListTests_DefaultObjectComparer()
        {
            var equalityComparer = new DefaultObjectComparer <ArtifactChange>();

            var listOne = CreateTestList(equalityComparer);

            // Populate the second list with references from the first.
            var listTwo = new OrderSensitiveValueComparisonList <ArtifactChange>(equalityComparer);

            for (int i = 0; i < listOne.Count; i++)
            {
                listTwo.Add(listOne[i]);
            }

            // Every list s/be equal to itself.
            listOne.Equals(listOne).Should().Be(true);

            // Two lists with shared objects, by reference, in the same
            // order should be regarded as equivalent.
            listOne.Equals(listTwo).Should().Be(true);

            ArtifactChange toSwap = listTwo[0];

            listTwo[0] = listTwo[1];
            listTwo[1] = toSwap;

            // We have reordered two objects that are themselves identical.
            // The comparison should fail as the order of references changed.
            listOne.Equals(listTwo).Should().Be(false);
        }
Beispiel #2
0
        public static ArtifactChangeModel ToArtifactChangeModel(this ArtifactChange fileChange, IDictionary <string, ArtifactLocation> originalUriBaseIds, FileRegionsCache fileRegionsCache)
        {
            if (fileChange == null)
            {
                return(null);
            }

            var model = new ArtifactChangeModel();

            // don't resolve filepath at model creation phase.
            // it will be resolved by TryResolveFilePath later.
            model.FilePath = fileChange.ArtifactLocation.Uri.IsAbsoluteUri ?
                             fileChange.ArtifactLocation.Uri.LocalPath :
                             fileChange.ArtifactLocation.Uri.OriginalString;

            if (fileChange.Replacements != null)
            {
                if (!fileChange.ArtifactLocation.TryReconstructAbsoluteUri(originalUriBaseIds, out Uri resolvedUri))
                {
                    resolvedUri = fileChange.ArtifactLocation.Uri;
                }

                foreach (Replacement replacement in fileChange.Replacements)
                {
                    model.Replacements.Add(replacement.ToReplacementModel(fileRegionsCache, resolvedUri));
                }
            }

            return(model);
        }
        internal FileChangeVersionOne CreateFileChangeVersionOne(ArtifactChange v2FileChange)
        {
            FileChangeVersionOne fileChange = null;

            if (v2FileChange != null)
            {
                string   encodingName = GetFileEncodingName(v2FileChange.ArtifactLocation?.Uri);
                Encoding encoding     = GetFileEncoding(encodingName);

                try
                {
                    fileChange = new FileChangeVersionOne
                    {
                        Replacements = v2FileChange.Replacements?.Select(r => CreateReplacementVersionOne(r, encoding)).ToList(),
                        Uri          = v2FileChange.ArtifactLocation?.Uri,
                        UriBaseId    = v2FileChange.ArtifactLocation?.UriBaseId
                    };
                }
                catch (UnknownEncodingException ex)
                {
                    // Set the unknown encoding name so the caller can provide useful reporting
                    ex.EncodingName = encodingName;
                    throw ex;
                }
            }

            return(fileChange);
        }
Beispiel #4
0
        public void OrderSensitiveValueComparisonListTests_ValueComparer()
        {
            // Two identical lists with elements that are
            // distinct objects, by reference.
            var listOne = CreateTestList(ArtifactChange.ValueComparer);
            var listTwo = CreateTestList(ArtifactChange.ValueComparer);

            // Every list s/be equal to itself
            listOne.Equals(listOne).Should().Be(true);

            // As initialized, these objects are different, due
            // to a unique GUID property on each list
            listOne.Equals(listTwo).Should().Be(false);

            // Make the two lists equivalent, by value
            listTwo[2].SetProperty(DIFFERENTIATING_PROPERTY_NAME, listOne[2].GetProperty <Guid>(DIFFERENTIATING_PROPERTY_NAME));
            listOne.Equals(listTwo).Should().Be(true);

            ArtifactChange toSwap = listTwo[0];

            listTwo[0] = listTwo[1];
            listTwo[1] = toSwap;

            // We have reordered two objects that are themselves identical.
            // by value. The comparison should still succeed.
            listOne.Equals(listTwo).Should().Be(true);
        }
        private void Visit(ArtifactChange artifactChange, string artifactChangePointer)
        {
            Analyze(artifactChange, artifactChangePointer);

            if (artifactChange.ArtifactLocation != null)
            {
                Visit(artifactChange.ArtifactLocation, artifactChangePointer.AtProperty(SarifPropertyName.ArtifactLocation));
            }
        }
Beispiel #6
0
        public void ToArtifactChangeModel_ArtifactChangeIsNull()
        {
            ArtifactChange artifactChange = null;

            var model = artifactChange.ToArtifactChangeModel(new Dictionary <string, ArtifactLocation>(), new FileRegionsCache());

            model.Should().BeNull();

            model = artifactChange.ToArtifactChangeModel(new Dictionary <string, Uri>(), new FileRegionsCache());
            model.Should().BeNull();
        }
        internal ArtifactChange CreateFileChange(FileChangeVersionOne v1FileChange)
        {
            ArtifactChange fileChange = null;

            if (v1FileChange != null)
            {
                fileChange = new ArtifactChange
                {
                    ArtifactLocation = CreateFileLocation(v1FileChange),
                    Replacements     = v1FileChange.Replacements?.Select(CreateReplacement).ToList()
                };
            }

            return(fileChange);
        }
        public void ArtifactChangeModel_FromArtifactChange_RelativePath()
        {
            ArtifactChange change = new ArtifactChange
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri = new Uri(@"\src\tools\util.cs", UriKind.RelativeOrAbsolute)
                },
                Replacements = new List <Replacement>()
            };

            ArtifactChangeModel model = change.ToArtifactChangeModel();

            model.FilePath.Should().Be(@"\src\tools\util.cs");
        }
        public void ArtifactChangeModel_FromArtifactChange_RelativePath()
        {
            var change = new ArtifactChange
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri = new Uri(@"\src\tools\util.cs", UriKind.RelativeOrAbsolute),
                },
                Replacements = new List <Replacement>(),
            };

            var model = change.ToArtifactChangeModel(s_emptyOriginalUriBaseIds, s_emptyFileRegionsCache);

            model.FilePath.Should().Be(@"\src\tools\util.cs");
        }
Beispiel #10
0
        public void ToArtifactChangeModel_ArtifactLocationRelativePath()
        {
            string         relativePath   = "path/to/file1";
            ArtifactChange artifactChange = new ArtifactChange
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri = new Uri(relativePath, UriKind.Relative),
                }
            };

            var model = artifactChange.ToArtifactChangeModel(new Dictionary <string, ArtifactLocation>(), new FileRegionsCache());

            model.Should().NotBeNull();
            model.FilePath.Should().BeEquivalentTo(relativePath);
            model.FileName.Should().BeEquivalentTo("file1");
        }
Beispiel #11
0
        private OrderSensitiveValueComparisonList <ArtifactChange> CreateTestList(IEqualityComparer <ArtifactChange> equalityComparer)
        {
            // Test list. First two elements are identical. The third element is unique.
            var fileChangeOne = new ArtifactChange();
            var fileChangeTwo = new ArtifactChange();

            var  fileChangeThree         = new ArtifactChange();
            Guid differentiatingProperty = Guid.NewGuid();

            fileChangeThree.SetProperty(DIFFERENTIATING_PROPERTY_NAME, differentiatingProperty);

            var list = new OrderSensitiveValueComparisonList <ArtifactChange>(equalityComparer);

            list.AddRange(new[] { fileChangeOne, fileChangeTwo, fileChangeThree });

            return(list);
        }
Beispiel #12
0
        private List <Fix> GetFixits(IssueRecord issue)
        {
            List <Fix> fixes = new List <Fix>();

            if (issue.Issue.Rule.Fixes != null)
            {
                foreach (CodeFix fix in issue.Issue.Rule.Fixes)
                {
                    List <Replacement> replacements = new List <Replacement>();
                    replacements.Add(new Replacement(new Region()
                    {
                        CharOffset = issue.Issue.Boundary.Index,
                        CharLength = issue.Issue.Boundary.Length,
                    }, new ArtifactContent()
                    {
                        Text = RuleProcessor.Fix(issue.TextSample, fix)
                    }, null));

                    var path    = Path.GetFullPath(issue.Filename);
                    var changes = new ArtifactChange[]
                    {
                        new ArtifactChange(
                            new ArtifactLocation()
                        {
                            Uri = new Uri(path)
                        },
                            replacements,
                            null)
                    };

                    fixes.Add(new Fix()
                    {
                        ArtifactChanges = changes,
                        Description     = new Message()
                        {
                            Text = issue.Issue.Rule.Description
                        }
                    });
                }
            }
            return(fixes);
        }
Beispiel #13
0
        public static ArtifactChangeModel ToArtifactChangeModel(this ArtifactChange fileChange)
        {
            if (fileChange == null)
            {
                return(null);
            }

            ArtifactChangeModel model = new ArtifactChangeModel();

            if (fileChange.Replacements != null)
            {
                model.FilePath = fileChange.ArtifactLocation.Uri.IsAbsoluteUri ?
                                 fileChange.ArtifactLocation.Uri.LocalPath :
                                 fileChange.ArtifactLocation.Uri.OriginalString;

                foreach (Replacement replacement in fileChange.Replacements)
                {
                    model.Replacements.Add(replacement.ToReplacementModel());
                }
            }

            return(model);
        }
Beispiel #14
0
        public void ToArtifactChangeModel_WithReplacement()
        {
            string         uriId          = "SRCROOT";
            string         relativePath   = "path/to/file1";
            ArtifactChange artifactChange = new ArtifactChange
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri       = new Uri(relativePath, UriKind.Relative),
                    UriBaseId = uriId,
                },
                Replacements = new[]
                {
                    new Replacement {
                        DeletedRegion = new Region {
                            CharOffset = 0, CharLength = 10
                        }
                    },
                }
            };

            var uriBaseIds = new Dictionary <string, ArtifactLocation>
            {
                { uriId, new ArtifactLocation {
                      Uri = new Uri("file:///etc/", UriKind.Absolute)
                  } }
            };

            var model = artifactChange.ToArtifactChangeModel(uriBaseIds, new FileRegionsCache());

            model.Should().NotBeNull();
            model.FilePath.Should().BeEquivalentTo(relativePath);
            model.FileName.Should().BeEquivalentTo("file1");
            model.Replacements.Should().NotBeEmpty();
            model.Replacements.Count.Should().Be(1);
        }
Beispiel #15
0
        public static ArtifactChangeModel ToArtifactChangeModel(this ArtifactChange fileChange, IDictionary <string, Uri> originalUriBaseIds, FileRegionsCache fileRegionsCache)
        {
            if (fileChange == null)
            {
                return(null);
            }

            Dictionary <string, ArtifactLocation> uriToArtifactLocationMap = null;

            if (originalUriBaseIds != null)
            {
                // convert IDictionary<string, Uri> to IDictionary<string, ArtifactLocation> which
                // is needed by Sarif.SDK extension method ArtifactLocation.TryReconstructAbsoluteUri()
                uriToArtifactLocationMap = new Dictionary <string, ArtifactLocation>(originalUriBaseIds.Count);
                foreach (KeyValuePair <string, Uri> entry in originalUriBaseIds)
                {
                    uriToArtifactLocationMap.Add(entry.Key, new ArtifactLocation {
                        Uri = entry.Value
                    });
                }
            }

            return(fileChange.ToArtifactChangeModel(uriToArtifactLocationMap, fileRegionsCache));
        }
Beispiel #16
0
        internal Result CreateResult(TSLintLogEntry entry)
        {
            entry = entry ?? throw new ArgumentNullException(nameof(entry));

            Result result = new Result()
            {
                RuleId  = entry.RuleName,
                Message = new Message {
                    Text = entry.Failure
                },
                Kind = ResultKind.Fail
            };

            switch (entry.RuleSeverity)
            {
            case "ERROR":
                result.Level = FailureLevel.Error;
                break;

            case "WARN":
            case "WARNING":
                result.Level = FailureLevel.Warning;
                break;

            case "DEFAULT":
            default:
                result.Level = FailureLevel.Note;
                break;
            }

            Region region = new Region()
            {
                // The TSLint logs have line and column start at 0, Sarif has them starting at 1, so add 1 to each
                StartLine   = entry.StartPosition.Line + 1,
                StartColumn = entry.StartPosition.Character + 1,
                EndLine     = entry.EndPosition.Line + 1,
                EndColumn   = entry.EndPosition.Character + 1,

                CharOffset = entry.StartPosition.Position
            };

            int length = entry.EndPosition.Position - entry.StartPosition.Position;

            region.CharLength = length > 0 ? length : 0;

            Uri analysisTargetUri = new Uri(entry.Name, UriKind.Relative);

            var physicalLocation = new PhysicalLocation
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri = analysisTargetUri
                },
                Region = region
            };

            Location location = new Location()
            {
                PhysicalLocation = physicalLocation
            };

            result.Locations = new List <Location>()
            {
                location
            };

            if (entry.Fixes?.Any() == true)
            {
                IList <Replacement> replacements = new List <Replacement>();

                foreach (TSLintLogFix fix in entry.Fixes)
                {
                    Replacement replacement = new Replacement();

                    replacement.DeletedRegion = new Region
                    {
                        CharLength = fix.InnerLength,
                        CharOffset = fix.InnerStart
                    };

                    if (!string.IsNullOrEmpty(fix.InnerText))
                    {
                        replacement.InsertedContent = new ArtifactContent
                        {
                            Text = fix.InnerText
                        };
                    }

                    replacements.Add(replacement);
                }

                var sarifFileChange = new ArtifactChange
                {
                    ArtifactLocation = new ArtifactLocation
                    {
                        Uri = analysisTargetUri
                    },
                    Replacements = replacements
                };

                Fix sarifFix = new Fix(description: null, artifactChanges: new List <ArtifactChange>()
                {
                    sarifFileChange
                }, properties: null);
                result.Fixes = new List <Fix> {
                    sarifFix
                };
            }

            return(result);
        }
 protected virtual void Analyze(ArtifactChange artifactChange, string artifactChangePointer)
 {
 }