Pluggable FxCop log reader
        /// <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);
        }
        /// <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();
        }
        /// <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();
        }
Exemple #4
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);
        }
Exemple #5
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();
        }
        public void FxCopLogReader_Context_RefineProjectToExceptionIssue()
        {
            var context = TestHelper.CreateProjectContext();

            context.RefineException(true, "CA0001", "binary.dll#namespace#member(string)");
            context.RefineExceptionType("type");
            context.RefineExceptionMessage("message");
            context.RefineStackTrace("trace");
            context.RefineInnerExceptionType("innertype");
            context.RefineInnerExceptionMessage("innermessage");
            context.RefineInnerStackTrace("innertrace");

            string exception = FxCopLogReader.MakeExceptionMessage("Rule", "CA1000", context.ExceptionType, context.ExceptionMessage, context.StackTrace, context.InnerExceptionType, context.InnerExceptionMessage, context.InnerStackTrace);

            context.RefineIssue(exception, null, null, null, null, null, null);

            Assert.Equal("Rule CA1000 exception: type: message trace. Inner Exception: innertype: innermessage innertrace", context.Message);
            Assert.Equal("binary.dll#namespace#member(string)", context.ExceptionTarget);

            context.ClearException();
            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);

            Assert.False(context.Exception);
            Assert.Null(context.ExceptionType);
            Assert.Null(context.ExceptionMessage);
            Assert.Null(context.StackTrace);
            Assert.Null(context.InnerExceptionType);
            Assert.Null(context.InnerExceptionMessage);
            Assert.Null(context.InnerStackTrace);
        }
        internal Result CreateResult(FxCopLogReader.Context context)
        {
            Result result = new Result();

            string uniqueId = context.GetUniqueId();
            if (!String.IsNullOrWhiteSpace(uniqueId))
            {
                result.ToolFingerprintContribution = uniqueId;
            }

            string status = context.Status;

            if ("ExcludedInSource".Equals(status))
            {
                result.SuppressionStates = SuppressionStates.SuppressedInSource;
            }
            else if ("ExcludedInProject".Equals(status))
            {
                result.BaselineState = BaselineState.Existing;
            }

            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);

            string logicalLocationKey = CreateLogicalLocation(context);

            if (logicalLocationKey != location.FullyQualifiedLogicalName)
            {
                location.LogicalLocationKey = logicalLocationKey;
            }

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

            bool mapsDirectlyToSarifName;

            result.Level = ConvertFxCopLevelToResultLevel(context.Level, out mapsDirectlyToSarifName);

            if (!mapsDirectlyToSarifName)
            {
                // We will not recapitulate FxCop MessageLevel names (such as
                // "Error" and "Warning") as a property. For names that differ
                // (such as "CriticalWarning" and "Information"), we will also
                // include the FxCop-specific values in the property bag.
                TryAddProperty(result, context.Level, "Level");
            }

            TryAddProperty(result, context.Category, "Category");
            TryAddProperty(result, context.FixCategory, "FixCategory");

            return result;
        }
        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;
        }
 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);
     }
 }
Exemple #10
0
        private static string CreateSignature(FxCopLogReader.Context context)
        {
            string[] parts = new string[] { context.Resource, context.Namespace, context.Type, context.Member };
            var updated = parts
                    .Where(part => !String.IsNullOrEmpty(part))
                    .Select(part => part.TrimStart('#'));

            string joinedParts = String.Join(".", updated);

            if (String.IsNullOrEmpty(joinedParts))
            {
                return context.Module;
            }

            if (!String.IsNullOrEmpty(context.Module))
            {
                return context.Module + "!" + joinedParts;
            }
            else
            {
                return joinedParts;
            }
        }