Current context of the result
The context accumulates in memory during the streaming, but the information we gather is very limited, and there is only one context object per input file currently constructed
示例#1
0
        public void FxCopConverter_CreateResult_FakeContext_NoModule_Member()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineTarget(@"mybinary.dll");
            context.RefineNamespace("mynamespace");
            context.RefineType("mytype");
            context.RefineMember("mymember(string)");
            context.RefineMessage("CA0000", "VeryUsefulCheck", null, null, null, null);
            context.RefineIssue("hello!", null, null, null, null, null, null);

            var expectedLogicalLocations = new List <LogicalLocation>
            {
                new LogicalLocation {
                    ParentIndex = -1, Name = "mynamespace", Kind = LogicalLocationKind.Namespace
                },
                new LogicalLocation {
                    ParentIndex = 0, Name = "mytype", FullyQualifiedName = "mynamespace.mytype", Kind = LogicalLocationKind.Type
                },
                new LogicalLocation {
                    ParentIndex = 1, Name = "mymember(string)", FullyQualifiedName = "mynamespace.mytype.mymember(string)", Kind = LogicalLocationKind.Member
                }
            };

            var    converter = new FxCopConverter();
            Result result    = converter.CreateResult(context);

            ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
        }
        /// <summary>
        /// Convert FxCop log to SARIF format stream
        /// </summary>
        /// <param name="input">FxCop log stream</param>
        /// <param name="output">output stream</param>
        public void Convert(Stream input, IResultLogWriter output)
        {
            if (input == null)
            {
                throw (new ArgumentNullException("input"));
            }

            if (output == null)
            {
                throw (new ArgumentNullException("output"));
            }

            ToolInfo toolInfo = new ToolInfo();
            RunInfo  runInfo  = new RunInfo();

            toolInfo.Name = "FxCop";
            output.WriteToolAndRunInfo(toolInfo, runInfo);

            var context = new FxCopLogReader.Context();

            var reader = new FxCopLogReader();

            reader.IssueRead += (FxCopLogReader.Context current) => { output.WriteResult(CreateIssue(current)); };
            reader.Read(context, input);
        }
示例#3
0
        public void FxCopLogReader_Context_RefineType()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineType("mytype");
            Assert.Equal("mytype", context.Type);
        }
示例#4
0
        public void FxCopLogReader_Context_RefineMember()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineMember("mymember(string)");
            Assert.Equal("mymember(string)", context.Member);
        }
示例#5
0
        public void FxCopLogReader_Context_RefineModule()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineModule("mybinary.dll");
            Assert.Equal("mybinary.dll", context.Module);
        }
示例#6
0
        public void FxCopLogReader_Context_RefineNamespace()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineNamespace("mynamespace");
            Assert.Equal("mynamespace", context.Namespace);
        }
示例#7
0
        public void FxCopLogReader_Context_RefineInnerExceptionType()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineInnerExceptionType(@"myinnertype");
            Assert.Equal(@"myinnertype", context.InnerExceptionType);
        }
示例#8
0
        public void FxCopLogReader_Context_RefineInnerStackTrace()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineInnerStackTrace(@"myinnertrace");
            Assert.Equal(@"myinnertrace", context.InnerStackTrace);
        }
示例#9
0
        public void FxCopLogReader_Context_RefineStackTrace()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineStackTrace(@"trace\n trace");
            context.StackTrace.Should().BeCrossPlatformEquivalentStrings(@"trace\n trace");
        }
示例#10
0
        public void FxCopLogReader_Context_RefineResource()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineResource("myresource.resx");
            Assert.Equal("myresource.resx", context.Resource);
        }
示例#11
0
        public void FxCopLogReader_Context_RefineExceptionMessage()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineExceptionMessage("mymessage");
            Assert.Equal("mymessage", context.ExceptionMessage);
        }
示例#12
0
        public void FxCopLogReader_Context_RefineReport()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineReport("myreport");
            Assert.Equal("myreport", context.Report);
        }
示例#13
0
        public void FxCopConverter_CreateResult_FakeContext_Resource()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineTarget(@"mybinary.dll");
            context.RefineModule("mybinary.dll");
            context.RefineResource("myresource.resx");
            context.RefineMessage("CA0000", "VeryUsefulCheck", null, null, null, null);
            context.RefineIssue("hello!", "test", null, null, @"source", "myfile.cs", 13);

            var expectedLogicalLocations = new List <LogicalLocation>
            {
                new LogicalLocation {
                    Kind = LogicalLocationKind.Module, Name = "mybinary.dll"
                },
                new LogicalLocation {
                    ParentIndex = 0, Name = "myresource.resx", FullyQualifiedName = "mybinary.dll!myresource.resx", Kind = LogicalLocationKind.Resource
                }
            };

            var    converter = new FxCopConverter();
            Result result    = converter.CreateResult(context);

            ValidateLogicalLocations(expectedLogicalLocations, converter.LogicalLocations);
        }
示例#14
0
        internal Result CreateResult(FxCopLogReader.Context context)
        {
            Result result = new Result();

            string uniqueId = context.GetUniqueId();

            if (!String.IsNullOrWhiteSpace(uniqueId))
            {
                result.ToolFingerprint = uniqueId;
            }

            result.RuleId  = context.CheckId;
            result.Message = context.Message;
            var location = new Location();

            if (!String.IsNullOrEmpty(context.Target))
            {
                location.AnalysisTarget = new PhysicalLocation
                {
                    Uri = new Uri(context.Target, UriKind.RelativeOrAbsolute)
                };
            }

            string sourceFile = GetFilePath(context);

            if (!String.IsNullOrWhiteSpace(sourceFile))
            {
                location.ResultFile = new PhysicalLocation
                {
                    Uri    = new Uri(sourceFile, UriKind.RelativeOrAbsolute),
                    Region = context.Line == null ? null : Extensions.CreateRegion(context.Line.Value)
                };
            }

            location.FullyQualifiedLogicalName = CreateSignature(context);

            IList <LogicalLocationComponent> logicalLocationComponents = CreateLogicalLocationComponents(context);

            if (logicalLocationComponents.Any())
            {
                AddLogicalLocation(location, logicalLocationComponents);
            }

            result.Locations = new HashSet <Location> {
                location
            };

            var properties = new Dictionary <string, string>();

            TryAddProperty(properties, context.Level, "Level");
            TryAddProperty(properties, context.Category, "Category");
            TryAddProperty(properties, context.FixCategory, "FixCategory");
            if (properties.Count != 0)
            {
                result.Properties = properties;
            }

            return(result);
        }
 public void FxCopLogReader_Read_BadXml()
 {
     using (MemoryStream input = Utilities.CreateStreamFromString(FxCopTestData.FxCopReportBadXml))
     {
         var context = new FxCopLogReader.Context();
         new FxCopLogReader().Read(context, input);
     }
 }
 public void FxCopLogReader_Read_BadXml()
 {
     using (MemoryStream input = Utilities.CreateStreamFromString(FxCopTestData.FxCopReportBadXml))
     {
         var context = new FxCopLogReader.Context();
         Assert.Throws <XmlException>(() => new FxCopLogReader().Read(context, input));
     }
 }
示例#17
0
        public void FxCopLogReader_Context_RefineException()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineException(true, "CA0001", "mytarget");
            Assert.True(context.Exception);
            Assert.Equal("CA0001", context.CheckId);
            Assert.Equal("mytarget", context.ExceptionTarget);
        }
示例#18
0
        public void FxCopLogReader_Context_RefineMessage()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineMessage("CA0000", "VeryUsefulCheck", "1", "MyCategory", "Breaking", "ExcludedInSource");
            Assert.Equal("CA0000", context.CheckId);
            Assert.Equal("1", context.MessageId);
            Assert.Equal("MyCategory", context.Category);
            Assert.Equal("VeryUsefulCheck", context.Typename);
            Assert.Equal("Breaking", context.FixCategory);
            Assert.Equal("ExcludedInSource", context.Status);
        }
示例#19
0
        private IList <LogicalLocationComponent> CreateLogicalLocationComponents(FxCopLogReader.Context context)
        {
            var logicalLocationComponents = new List <LogicalLocationComponent>();

            TryAddLogicalLocationComponent(logicalLocationComponents, context.Module, LogicalLocationKind.Module);
            TryAddLogicalLocationComponent(logicalLocationComponents, context.Resource, LogicalLocationKind.Resource);
            TryAddLogicalLocationComponent(logicalLocationComponents, context.Namespace, LogicalLocationKind.Namespace);
            TryAddLogicalLocationComponent(logicalLocationComponents, context.Type, LogicalLocationKind.Type);
            TryAddLogicalLocationComponent(logicalLocationComponents, context.Member, LogicalLocationKind.Member);

            return(logicalLocationComponents);
        }
示例#20
0
        internal ReportingDescriptor CreateRule(FxCopLogReader.Context context)
        {
            var rule = new ReportingDescriptor
            {
                Id             = context.CheckId,
                Name           = context.RuleTypeName,
                MessageStrings = context.Resolutions.ConvertToMultiformatMessageStringsDictionary()
            };

            rule.SetProperty("Category", context.RuleCategory);

            return(rule);
        }
示例#21
0
        public void FxCopLogReader_Context_RefineIssue()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineIssue("hello!", "test", "25", "error", "source", "myfile.cs", 13);
            Assert.Equal("hello!", context.Message);
            Assert.Equal("test", context.ResolutionName);
            Assert.Equal("25", context.Certainty);
            Assert.Equal("error", context.Level);
            Assert.Equal("source", context.Path);
            Assert.Equal("myfile.cs", context.File);
            Assert.Equal(13, context.Line.Value);
        }
示例#22
0
        internal Rule CreateRule(FxCopLogReader.Context context)
        {
            var rule = new Rule
            {
                Id = context.CheckId,
                Name = context.RuleTypeName.ToMessage(),
                MessageStrings = context.Resolutions
            };

            rule.SetProperty("Category", context.RuleCategory);

            return rule;
        }
示例#23
0
        /// <summary>
        /// Convert FxCop log to SARIF format stream
        /// </summary>
        /// <param name="input">FxCop log stream</param>
        /// <param name="output">output stream</param>
        /// <param name="dataToInsert">Optionally emitted properties that should be written to log.</param>
        public override void Convert(Stream input, IResultLogWriter output, OptionallyEmittedData dataToInsert)
        {
            if (input == null)
            {
                throw (new ArgumentNullException(nameof(input)));
            }

            if (output == null)
            {
                throw (new ArgumentNullException(nameof(output)));
            }

            LogicalLocationsDictionary.Clear();

            var context = new FxCopLogReader.Context();

            var results = new List <Result>();
            var reader  = new FxCopLogReader();

            reader.ResultRead += (FxCopLogReader.Context current) => { results.Add(CreateResult(current)); };
            reader.Read(context, input);

            Tool tool = new Tool
            {
                Name = "FxCop"
            };

            var fileInfoFactory = new FileInfoFactory(MimeType.DetermineFromFileExtension, dataToInsert);
            Dictionary <string, FileData> fileDictionary = fileInfoFactory.Create(results);

            var run = new Run()
            {
                Tool = tool
            };

            output.Initialize(run);

            if (fileDictionary != null && fileDictionary.Any())
            {
                output.WriteFiles(fileDictionary);
            }

            if (LogicalLocationsDictionary != null && LogicalLocationsDictionary.Any())
            {
                output.WriteLogicalLocations(LogicalLocationsDictionary);
            }

            output.OpenResults();
            output.WriteResults(results);
            output.CloseResults();
        }
示例#24
0
        public void FxCopConverter_CreateResult_FakeContext_NoModule_Resource()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineTarget(@"mybinary.dll");
            context.RefineResource("myresource.resx");
            context.RefineMessage("CA0000", "VeryUsefulCheck", null, null, null, null);
            context.RefineIssue("hello!", "test", null, null, null, null, null);

            var    converter = new FxCopConverter();
            Result result    = converter.CreateResult(context);

            result.Locations.First().LogicalLocation.FullyQualifiedName.Should().Be(@"myresource.resx");
        }
示例#25
0
 private static string GetFilePath(FxCopLogReader.Context context)
 {
     if (context.Path == null)
     {
         return(context.File);
     }
     else if (context.File == null)
     {
         Debug.Fail("FxCop with path set but file unset.");
         return(context.Path);
     }
     else
     {
         return(Path.Combine(context.Path, context.File));
     }
 }
示例#26
0
        /// <summary>
        /// Convert FxCop log to SARIF format stream
        /// </summary>
        /// <param name="input">FxCop log stream</param>
        /// <param name="output">output stream</param>
        public override void Convert(Stream input, IResultLogWriter output)
        {
            if (input == null)
            {
                throw (new ArgumentNullException(nameof(input)));
            }

            if (output == null)
            {
                throw (new ArgumentNullException(nameof(output)));
            }

            LogicalLocationsDictionary.Clear();

            var context = new FxCopLogReader.Context();

            var results = new List<Result>();
            var reader = new FxCopLogReader();
            reader.ResultRead += (FxCopLogReader.Context current) => { results.Add(CreateResult(current)); };
            reader.Read(context, input);

            Tool tool = new Tool
            {
                Name = "FxCop"
            };

            var fileInfoFactory = new FileInfoFactory(MimeType.DetermineFromFileExtension);
            Dictionary<string, FileData> fileDictionary = fileInfoFactory.Create(results);

            output.Initialize(id: null, correlationId: null);

            output.WriteTool(tool);

            if (fileDictionary != null && fileDictionary.Any())
            {
                output.WriteFiles(fileDictionary);
            }

            if (LogicalLocationsDictionary != null && LogicalLocationsDictionary.Any())
            {
                output.WriteLogicalLocations(LogicalLocationsDictionary);
            }

            output.OpenResults();
            output.WriteResults(results);
            output.CloseResults();
        }
示例#27
0
        public void FxCopLogReader_Context_RefineProjectToMemberIssue()
        {
            FxCopLogReader.Context context = TestHelper.CreateProjectContext();

            context.RefineTarget("mybinary.dll");
            context.RefineModule("mybinary.dll");
            context.RefineNamespace("mynamespace");
            context.RefineType("mytype");
            context.RefineMember("mymember(string)");
            context.RefineMessage("CA0000", "VeryUsefulCheck", "1", "MyCategory", "Breaking", "Excluded");
            context.RefineIssue("hello!", "test", "25", "error", "source", "myfile.cs", 13);

            Assert.Equal("mybinary.dll", context.Target);
            Assert.Equal("mybinary.dll", context.Module);
            Assert.Equal("mynamespace", context.Namespace);
            Assert.Equal("mytype", context.Type);
            Assert.Equal("mymember(string)", context.Member);
            Assert.Equal("CA0000", context.CheckId);
            Assert.Equal("1", context.MessageId);
            Assert.Equal("MyCategory", context.Category);
            Assert.Equal("VeryUsefulCheck", context.Typename);
            Assert.Equal("Breaking", context.FixCategory);
            Assert.Equal("hello!", context.Message);
            Assert.Equal("test", context.ResolutionName);
            Assert.Equal("25", context.Certainty);
            Assert.Equal("error", context.Level);
            Assert.Equal("source", context.Path);
            Assert.Equal("myfile.cs", context.File);
            Assert.Equal("Excluded", context.Status);
            Assert.Equal(13, context.Line.Value);

            context.ClearTarget();
            Assert.Null(context.Target);
            Assert.Null(context.Module);
            Assert.Null(context.Namespace);
            Assert.Null(context.Type);
            Assert.Null(context.Member);
            Assert.Null(context.Message);
            Assert.Null(context.ResolutionName);
        }
示例#28
0
        /// <summary>
        /// Convert FxCop log to SARIF format stream
        /// </summary>
        /// <param name="input">FxCop log stream</param>
        /// <param name="output">output stream</param>
        /// <param name="dataToInsert">Optionally emitted properties that should be written to log.</param>
        public override void Convert(Stream input, IResultLogWriter output, OptionallyEmittedData dataToInsert)
        {
            if (input == null)
            {
                throw (new ArgumentNullException(nameof(input)));
            }

            if (output == null)
            {
                throw (new ArgumentNullException(nameof(output)));
            }

            LogicalLocations.Clear();

            var context = new FxCopLogReader.Context();

            var results = new List <Result>();
            var rules   = new List <ReportingDescriptor>();
            var reader  = new FxCopLogReader();

            reader.RuleRead   += (FxCopLogReader.Context current) => { rules.Add(CreateRule(current)); };
            reader.ResultRead += (FxCopLogReader.Context current) => { results.Add(CreateResult(current)); };
            reader.Read(context, input);

            var run = new Run()
            {
                Tool = new Tool
                {
                    Driver = new ToolComponent
                    {
                        Name  = ToolName,
                        Rules = rules
                    }
                },
            };

            PersistResults(output, results, run);

            output.WriteLogicalLocations(LogicalLocations);
        }
示例#29
0
        private string CreateFullyQualifiedLogicalName(FxCopLogReader.Context context, out int index)
        {
            index = -1;
            string fullyQualifiedName = null;
            string delimiter          = string.Empty;

            if (!string.IsNullOrEmpty(context.Module))
            {
                index     = AddLogicalLocation(index, ref fullyQualifiedName, context.Module, LogicalLocationKind.Module, delimiter);
                delimiter = "!";
            }

            if (!string.IsNullOrEmpty(context.Resource))
            {
                index     = AddLogicalLocation(index, ref fullyQualifiedName, context.Resource, LogicalLocationKind.Resource, delimiter);
                delimiter = ".";
            }

            if (!string.IsNullOrEmpty(context.Namespace))
            {
                index     = AddLogicalLocation(index, ref fullyQualifiedName, context.Namespace, LogicalLocationKind.Namespace, delimiter);
                delimiter = ".";
            }

            if (!string.IsNullOrEmpty(context.Type))
            {
                index     = AddLogicalLocation(index, ref fullyQualifiedName, context.Type, LogicalLocationKind.Type, delimiter);
                delimiter = ".";
            }

            if (!string.IsNullOrEmpty(context.Member))
            {
                string member = context.Member != null?context.Member.Trim('#') : null;

                index = AddLogicalLocation(index, ref fullyQualifiedName, member, LogicalLocationKind.Member, delimiter);
            }

            return(fullyQualifiedName);
        }
示例#30
0
        private string CreateLogicalLocation(FxCopLogReader.Context context)
        {
            string parentLogicalLocationKey = null;
            string delimiter = null;

            if (!string.IsNullOrEmpty(context.Module))
            {
                parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, context.Module, LogicalLocationKind.Module);
                delimiter = "!";
            }

            if (!string.IsNullOrEmpty(context.Resource))
            {
                parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, context.Resource, LogicalLocationKind.Resource, delimiter);
                delimiter = ".";
            }


            if (!string.IsNullOrEmpty(context.Namespace))
            {
                parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, context.Namespace, LogicalLocationKind.Namespace, delimiter);
                delimiter = ".";
            }

            if (!string.IsNullOrEmpty(context.Type))
            {
                parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, context.Type, LogicalLocationKind.Type, delimiter);
                delimiter = ".";
            }

            if (!string.IsNullOrEmpty(context.Member))
            {
                string member = context.Member != null?context.Member.Trim('#') : null;

                parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, member, LogicalLocationKind.Member, delimiter);
            }

            return(parentLogicalLocationKey);
        }
示例#31
0
        /// <summary>
        /// Convert FxCop log to SARIF format stream
        /// </summary>
        /// <param name="input">FxCop log stream</param>
        /// <param name="output">output stream</param>
        public void Convert(Stream input, IResultLogWriter output)
        {
            if (input == null)
            {
                throw (new ArgumentNullException("input"));
            }

            if (output == null)
            {
                throw (new ArgumentNullException("output"));
            }

            var context = new FxCopLogReader.Context();

            var results = new List <Result>();
            var reader  = new FxCopLogReader();

            reader.IssueRead += (FxCopLogReader.Context current) => { results.Add(CreateIssue(current)); };
            reader.Read(context, input);

            Tool tool = new Tool
            {
                Name = "FxCop"
            };

            var fileInfoFactory = new FileInfoFactory(MimeType.DetermineFromFileExtension);
            Dictionary <string, IList <FileData> > fileDictionary = fileInfoFactory.Create(results);

            output.WriteTool(tool);
            if (fileDictionary != null && fileDictionary.Count > 0)
            {
                output.WriteFiles(fileDictionary);
            }

            output.OpenResults();
            output.WriteResults(results);
            output.CloseResults();
        }
示例#32
0
 public void FxCopLogReader_Read_NullInput()
 {
     var context = new FxCopLogReader.Context();
     new FxCopLogReader().Read(context, null);
 }
示例#33
0
 public void FxCopLogReader_Read_BadXml()
 {
     using (MemoryStream input = Utilities.CreateStreamFromString(FxCopTestData.FxCopReportBadXml))
     {
         var context = new FxCopLogReader.Context();
         new FxCopLogReader().Read(context, input);
     }
 }