public void FxCopConverter_CreateResult_FakeContext_Member()
        {
            var 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", "FakeCategory", "Breaking", "ExcludedInSource");
            context.RefineIssue("hello!", "test", "uncertain", "error", @"source", "myfile.cs", 13);

            string expectedLogicalLocation = "mynamespace.mytype.mymember(string)";

            var expectedResult = new Result
            {
                RuleId = "CA0000",
                Message = "hello!",
                ToolFingerprintContribution = "1#test",
                SuppressionStates = SuppressionStates.SuppressedInSource,
                Locations = new List<Location>
                {
                    new Location
                    {
                        AnalysisTarget = new PhysicalLocation
                        {
                            Uri = new Uri("mybinary.dll", UriKind.RelativeOrAbsolute),
                        },
                        ResultFile = new PhysicalLocation
                        {
                            Uri = new Uri("source\\myfile.cs", UriKind.RelativeOrAbsolute),
                            Region = new Region { StartLine = 13 }
                        },
                        FullyQualifiedLogicalName = expectedLogicalLocation,
                    }
                }
            };

            expectedResult.SetProperty("Level", "error");
            expectedResult.SetProperty("Category", "FakeCategory");
            expectedResult.SetProperty("FixCategory", "Breaking");

            var expectedLogicalLocations = new Dictionary<string, LogicalLocation>
            {
                {
                    "mybinary.dll", new LogicalLocation { ParentKey = null, Name = "mybinary.dll", Kind = LogicalLocationKind.Module }
                },
                {
                    "mybinary.dll!mynamespace",
                    new LogicalLocation { ParentKey = "mybinary.dll", Name = "mynamespace", Kind = LogicalLocationKind.Namespace }
                },
                {
                    "mybinary.dll!mynamespace.mytype",
                    new LogicalLocation { ParentKey = "mybinary.dll!mynamespace", Name = "mytype", Kind = LogicalLocationKind.Type }
                },
                {
                    "mybinary.dll!mynamespace.mytype.mymember(string)",
                    new LogicalLocation { ParentKey = "mybinary.dll!mynamespace.mytype", Name = "mymember(string)", Kind = LogicalLocationKind.Member }
                }            };

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

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
示例#2
0
 private static void TryAddProperty(Result result, string value, string key)
 {
     if (!String.IsNullOrWhiteSpace(value))
     {
         result.SetProperty(key, value);
     }
 }
示例#3
0
        /// <summary>Converts a Fortify result to a static analysis results interchange format result.</summary>
        /// <param name="fortify">The Fortify result convert.</param>
        /// <returns>
        /// A SARIF result <see cref="Result"/> containing the same content as the supplied
        /// <see cref="FortifyIssue"/>.
        /// </returns>
        public static Result ConvertFortifyIssueToSarifIssue(FortifyIssue fortify)
        {
            var result = new Result();
            result.RuleId = fortify.Category;
            result.ToolFingerprintContribution = fortify.InstanceId;
            List<string> messageComponents = new List<string>();
            if (fortify.Abstract != null)
            {
                messageComponents.Add(fortify.Abstract);
            }

            if (fortify.AbstractCustom != null)
            {
                messageComponents.Add(fortify.AbstractCustom);
            }

            if (messageComponents.Count == 0)
            {
                result.Message = String.Format(CultureInfo.InvariantCulture, ConverterResources.FortifyFallbackMessage, result.RuleId);
            }
            else
            {
                result.Message = String.Join(Environment.NewLine, messageComponents);
            }

            result.SetProperty("kingdom", fortify.Kingdom);
            if (fortify.Priority != null)
            {
                result.SetProperty("priority", fortify.Priority);
            }

            if (!fortify.CweIds.IsDefaultOrEmpty)
            {
                result.SetProperty("cwe", String.Join(", ",
                    fortify.CweIds.Select(id => id.ToString(CultureInfo.InvariantCulture))));
            }

            if (fortify.RuleId != null)
            {
                result.SetProperty("fortifyRuleId", fortify.RuleId);
            }

            PhysicalLocation primaryOrSink = ConvertFortifyLocationToPhysicalLocation(fortify.PrimaryOrSink);
            result.Locations = new List<Location>
            {
                new Location
                {
                    ResultFile = primaryOrSink
                }
            };

            if (fortify.Source != null)
            {
                PhysicalLocation source = ConvertFortifyLocationToPhysicalLocation(fortify.Source);
                result.CodeFlows = new List<CodeFlow>
                {
                    new CodeFlow
                    {
                        Locations = new List<AnnotatedCodeLocation>
                        {
                            new AnnotatedCodeLocation
                            {
                                PhysicalLocation = source
                            },

                            new AnnotatedCodeLocation
                            {
                                PhysicalLocation = primaryOrSink
                            }
                        }
                    }
                };
            }

            return result;
        }
示例#4
0
        /// <summary>Converts this instance to <see cref="Result"/>.</summary>
        /// <returns>This instance as an <see cref="Result"/>.</returns>
        public Result ToSarifIssue()
        {
            if (this.Locations.Length == 0)
            {
                throw new InvalidOperationException("At least one location must be present in a SARIF result.");
            }

            var result = new Result
            {
                RuleId = this.Id,
            };

            result.SetProperty("Severity", this.Severity);

            if (!string.IsNullOrEmpty(this.VerboseMessage))
            {
                result.Message = this.VerboseMessage;
            }
            else
            {
                result.Message = this.Message;
            }

            PhysicalLocation lastLocationConverted;
            if (this.Locations.Length == 1)
            {
                lastLocationConverted = this.Locations[0].ToSarifPhysicalLocation();
            }
            else
            {
                var locations = new List<AnnotatedCodeLocation>
                {
                    Capacity = this.Locations.Length
                };

                foreach (CppCheckLocation loc in this.Locations)
                {
                    locations.Add(new AnnotatedCodeLocation
                    {
                        PhysicalLocation = loc.ToSarifPhysicalLocation(),
                        Importance = AnnotatedCodeLocationImportance.Essential
                    });
                }

                var flow = new CodeFlow
                {
                    Locations = locations
                };

                result.CodeFlows = new List<CodeFlow> { flow };

                // In the N != 1 case, set the overall location's location to
                // the last entry in the execution flow.
                lastLocationConverted = locations[locations.Count - 1].PhysicalLocation;
            }

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

            return result;
        }
        private Result CreateResult(IDictionary<string, object> issueData)
        {
            if (issueData != null)
            {
                // Used for Result.FullMessage
                string description = FindString(issueData, "description");

                // Used as rule id.
                string issueType = FindString(issueData, "type");

                // This data persisted to result property bag
                string category = FindString(issueData, "category");
                string issueContextKind = FindString(issueData, "issue_context_kind");
                string issueContext = FindString(issueData, "issue_context");
                string issueHash = FindString(issueData, "issue_hash");

                int issueLine = 0;
                int issueColumn = 0;
                string fileName = null;

                IDictionary<string, object> location = FindDictionary(issueData, "location");
                if (location != null)
                {
                    issueLine = FindInt(location, "line");
                    issueColumn = FindInt(location, "col");
                    int fileNumber = FindInt(location, "file");
                    if (_files != null && fileNumber < _files.Count)
                    {
                        fileName = _files[fileNumber] as string;
                    }
                }

                var result = new Result
                {
                    RuleId = issueType,
                    Message = description,
                    Locations = new List<Location>
                    {
                        new Location
                        {
                            AnalysisTarget = new PhysicalLocation
                            {
                                Uri = new Uri(fileName, UriKind.RelativeOrAbsolute),
                                Region = new Region()
                                {
                                    StartLine = issueLine,
                                    StartColumn = issueColumn
                                }
                            }
                        }
                    },
                };

                result.SetProperty("category", category);
                result.SetProperty("issue_context_kind", issueContextKind);
                result.SetProperty("issueContext", issueContext);
                result.SetProperty("issueHash", issueHash);

                return result;
            }
            else
            {
                return null;
            }
        }
        private static void SetSarifResultPropertiesForProblem(Result result, AndroidStudioProblem problem)
        {
            if (problem.Severity != null)
            {
                result.SetProperty("severity", problem.Severity);
            }

            if (problem.AttributeKey != null)
            {
                result.SetProperty("attributeKey", problem.AttributeKey);
            }
        }