Converts an xml log file of the Android Studio format into the SARIF format
Inheritance: ToolFileConverterBase
        public void AndroidStudioConverter_ConvertSarifResult_CanRecordSourceFileAndModule()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
            builder.File = "File Goes Here";
            builder.Package = null;
            builder.Module = "LastResortModule";
            builder.EntryPointName = null;

            var expectedLocation = new Location
            {
                ResultFile = new PhysicalLocation
                {
                    Uri = new Uri("File Goes Here", UriKind.RelativeOrAbsolute),
                },
                FullyQualifiedLogicalName = "LastResortModule"
            };

            var expectedLogicalLocations = new Dictionary<string, LogicalLocation>
            {
                {
                    "LastResortModule", new LogicalLocation { ParentKey = null, Name = "LastResortModule", Kind = LogicalLocationKind.Module }
                }
               };

            var converter = new AndroidStudioConverter();
            Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
        public void AndroidStudioConverter_ConvertToSarifResult_SeverityIsPersistedInProperties()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();

            builder.Severity = "warning";
            var    uut    = new AndroidStudioProblem(builder);
            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);

            result.Properties.Should().Equal(new Dictionary <string, string> {
                { "severity", "warning" }
            });
        }
        public void AndroidStudioConverter_ConvertToSarifIssue_AttributeKeyIsPersistedInProperties()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();

            builder.AttributeKey = "key";
            var    uut    = new AndroidStudioProblem(builder);
            Result result = AndroidStudioConverter.ConvertProblemToSarifIssue(uut);

            result.Properties.Should().Equal(new Dictionary <string, string> {
                { "attributeKey", "key" }
            });
        }
        public void AndroidStudioConverter_ConvertToSarifResult_MultiplePropertiesArePersisted()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();

            builder.AttributeKey = "key";
            builder.Severity     = "warning";
            var    uut    = new AndroidStudioProblem(builder);
            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);

            result.PropertyNames.Count.Should().Be(2);
            result.GetProperty("severity").Should().Be("warning");
            result.GetProperty("attributeKey").Should().Be("key");
        }
        public Result ConvertProblemToSarifResult(AndroidStudioProblem problem)
        {
            var result = new Result();

            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);

            if (problem.Hints.IsEmpty)
            {
                result.Message = new Message {
                    Text = description
                };
            }
            else
            {
                result.Message = new Message {
                    Text = GenerateFullMessage(description, problem.Hints)
                };
            }

            SetSarifResultPropertiesForProblem(result, problem);
            var location = new Location();

            location.FullyQualifiedLogicalName = CreateFullyQualifiedLogicalName(problem);

            Uri    uri;
            string file = problem.File;

            if (!String.IsNullOrEmpty(file))
            {
                location.PhysicalLocation = new PhysicalLocation
                {
                    FileLocation = new FileLocation(),
                    Region       = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                };

                if (RemoveBadRoot(file, out uri))
                {
                    location.PhysicalLocation.FileLocation.UriBaseId = PROJECT_DIR;
                }
                location.PhysicalLocation.FileLocation.Uri = uri;
            }

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

            return(result);
        }
        /// <summary>Processes an Android Studio log and writes issues therein to an instance of
        /// <see cref="IResultLogWriter"/>.</summary>
        /// <param name="xmlReader">The XML reader from which AndroidStudio format shall be read.</param>
        /// <param name="output">The <see cref="IResultLogWriter"/> to write the output to.</param>
        private void ProcessAndroidStudioLog(XmlReader xmlReader, IResultLogWriter output)
        {
            int problemsDepth = xmlReader.Depth;

            xmlReader.ReadStartElement(_strings.Problems);

            while (xmlReader.Depth > problemsDepth)
            {
                var problem = AndroidStudioProblem.Parse(xmlReader, _strings);
                if (problem != null)
                {
                    output.WriteResult(AndroidStudioConverter.ConvertProblemToSarifIssue(problem));
                }
            }

            xmlReader.ReadEndElement(); // </problems>
        }
        public void AndroidStudioConverter_ConvertToSarifResult_HintsAreStapledToDescription()
        {
            var builder = new AndroidStudioProblem.Builder
            {
                ProblemClass = "Unused",
                File         = "Unused",
                Description  = "hungry EVIL zombies",
                Hints        = ImmutableArray.Create("comment", "delete")
            };

            var    uut    = new AndroidStudioProblem(builder);
            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);

            Assert.AreEqual(@"hungry EVIL zombies
Possible resolution: comment
Possible resolution: delete", result.Message);
        }
        private static LocationInfo GetLocationInfoForBuilder(AndroidStudioProblem.Builder builder)
        {
            var    converter = new AndroidStudioConverter();
            Result result    = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            Location location = result.Locations.First();

            string logicalLocationKey = converter.LogicalLocationsDictionary.Keys.SingleOrDefault();
            IList <LogicalLocationComponent> logicalLocationComponents = logicalLocationKey != null
                ? converter.LogicalLocationsDictionary[logicalLocationKey]
                : new List <LogicalLocationComponent>(0);

            return(new LocationInfo
            {
                Location = location,
                LogicalLocationComponents = logicalLocationComponents
            });
        }
        public void AndroidStudioConverter_ConvertSarifResult_CanRecordSourceFileAndModule()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();

            builder.File           = "File Goes Here";
            builder.Package        = null;
            builder.Module         = "LastResortModule";
            builder.EntryPointName = null;

            var expectedLocation = new Location
            {
                PhysicalLocation = new PhysicalLocation
                {
                    FileLocation = new FileLocation
                    {
                        Uri = new Uri("File Goes Here", UriKind.RelativeOrAbsolute)
                    },
                },
                FullyQualifiedLogicalName = "LastResortModule"
            };

            var expectedLogicalLocations = new Dictionary <string, LogicalLocation>
            {
                {
                    "LastResortModule", new LogicalLocation {
                        ParentKey = null, Kind = LogicalLocationKind.Module
                    }
                }
            };

            var    converter = new AndroidStudioConverter();
            Result result    = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
        public void AndroidStudioConverter_ConvertSarifResult_GeneratesLocationWithMethodEntryPointAndPackage()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();

            builder.File           = null;
            builder.Package        = "FancyPackageName";
            builder.Module         = null;
            builder.EntryPointType = "method";
            builder.EntryPointName = "my_method";

            var expectedLocation = new Location
            {
                FullyQualifiedLogicalName = "FancyPackageName\\my_method"
            };

            var expectedLogicalLocations = new Dictionary <string, LogicalLocation>
            {
                {
                    "FancyPackageName", new LogicalLocation {
                        ParentKey = null, Kind = LogicalLocationKind.Package
                    }
                },
                {
                    @"FancyPackageName\my_method", new LogicalLocation {
                        ParentKey = "FancyPackageName", Name = "my_method", Kind = LogicalLocationKind.Member
                    }
                },
            };

            var    converter = new AndroidStudioConverter();
            Result result    = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
        public void AndroidStudioConverter_ConvertSarifResult_RecordsModuleAsTopLevelIfPresent()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();

            builder.File           = null;
            builder.Module         = "my_fancy_binary";
            builder.EntryPointType = "method";
            builder.EntryPointName = "my_method";

            var expectedLocation = new Location
            {
                FullyQualifiedLogicalName = "my_fancy_binary\\my_method",
            };

            var expectedLogicalLocations = new Dictionary <string, LogicalLocation>
            {
                {
                    "my_fancy_binary", new LogicalLocation {
                        ParentKey = null, Kind = LogicalLocationKind.Module
                    }
                },
                {
                    @"my_fancy_binary\my_method",
                    new LogicalLocation {
                        ParentKey = "my_fancy_binary", Name = "my_method", Kind = LogicalLocationKind.Member
                    }
                },
            };

            var    converter = new AndroidStudioConverter();
            Result result    = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
 public AndroidStudioConverterTests()
 {
     _converter = new AndroidStudioConverter();
 }
        private static LocationInfo GetLocationInfoForBuilder(AndroidStudioProblem.Builder builder)
        {
            var converter = new AndroidStudioConverter();
            Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            Location location = result.Locations.First();

            string logicalLocationKey = converter.LogicalLocationsDictionary.Keys.SingleOrDefault();
            LogicalLocation logicalLocation = logicalLocationKey != null
                ? converter.LogicalLocationsDictionary[logicalLocationKey]
                : null;

            return new LocationInfo
            {
                Location = location,
                LogicalLocation = logicalLocation
            };
        }
        public void AndroidStudioConverter_ConvertToSarifResult_HintsAreStapledToDescription()
        {
            var builder = new AndroidStudioProblem.Builder
            {
                ProblemClass = "Unused",
                File = "Unused",
                Description = "hungry EVIL zombies",
                Hints = ImmutableArray.Create("comment", "delete")
            };

            var uut = new AndroidStudioProblem(builder);
            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
            Assert.AreEqual(@"hungry EVIL zombies
            Possible resolution: comment
            Possible resolution: delete", result.Message);
        }
 public void AndroidStudioConverter_ConvertToSarifResult_MultiplePropertiesArePersisted()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.AttributeKey = "key";
     builder.Severity = "warning";
     var uut = new AndroidStudioProblem(builder);
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     result.PropertyNames.Count.Should().Be(2);
     result.GetProperty("severity").Should().Be("warning");
     result.GetProperty("attributeKey").Should().Be("key");
 }
 public void Initialize()
 {
     _converter = new AndroidStudioConverter();
 }
 public void AndroidStudioConverter_ConvertToSarifResult_AttributeKeyIsPersistedInProperties()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.AttributeKey = "key";
     var uut = new AndroidStudioProblem(builder);
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     result.PropertyNames.Count.Should().Be(1);
     result.GetProperty("attributeKey").Should().Be("key");
 }
 private static Location GetLocationForBuilder(AndroidStudioProblem.Builder builder)
 {
     return(AndroidStudioConverter.ConvertProblemToSarifIssue(new AndroidStudioProblem(builder)).Locations[0]);
 }
 public void AndroidStudioConverter_ConvertToSarifResult_SeverityIsPersistedInProperties()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.Severity = "warning";
     var uut = new AndroidStudioProblem(builder);
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     result.PropertyNames.Count.Should().Be(1);
     result.GetProperty("severity").Should().Be("warning");
 }
        public void AndroidStudioConverter_ConvertSarifResult_GeneratesLocationWithMethodEntryPointAndPackage()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
            builder.File = null;
            builder.Package = "FancyPackageName";
            builder.Module = null;
            builder.EntryPointType = "method";
            builder.EntryPointName = "my_method";

            var expectedLocation = new Location
            {
                FullyQualifiedLogicalName = "FancyPackageName\\my_method"
            };

            var expectedLogicalLocations = new Dictionary<string, LogicalLocation>
            {
                {
                    "FancyPackageName", new LogicalLocation { ParentKey = null, Name = "FancyPackageName", Kind = LogicalLocationKind.Package }
                },
                {
                    @"FancyPackageName\my_method", new LogicalLocation { ParentKey = "FancyPackageName", Name = "my_method", Kind = LogicalLocationKind.Member }
                },
               };

            var converter = new AndroidStudioConverter();
            Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
 public void AndroidStudioConverter_ConvertToSarifResult_UsesProblemClassForRuleId()
 {
     var uut = AndroidStudioProblemTests.GetDefaultProblem();
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     Assert.AreEqual("A Problematic Problem", result.RuleId);
 }
 public void AndroidStudioConverter_ConvertToSarifResult_HasNoPropertiesIfAttributeKeyAndSeverity()
 {
     var uut = AndroidStudioProblemTests.GetDefaultProblem();
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     Assert.IsNull(result.Properties);
 }
        public static Result ConvertProblemToSarifIssue(AndroidStudioProblem problem)
        {
            var result = new Result();

            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);

            if (problem.Hints.IsEmpty)
            {
                result.FullMessage = description;
            }
            else
            {
                result.ShortMessage = description;
                result.FullMessage  = GenerateFullMessage(description, problem.Hints);
            }

            result.Properties = GetSarifIssuePropertiesForProblem(problem);
            var location = new Location();

            result.Locations = new[] { location };
            var logicalLocation = new List <LogicalLocationComponent>();

            if (!String.IsNullOrWhiteSpace(problem.Module))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.Module,
                    Kind = LogicalLocationKind.AndroidModule
                });
            }

            if (!String.IsNullOrWhiteSpace(problem.Package))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.Package,
                    Kind = LogicalLocationKind.JvmPackage
                });
            }

            if ("class".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.EntryPointName,
                    Kind = LogicalLocationKind.JvmType
                });
            }

            if ("method".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.EntryPointName,
                    Kind = LogicalLocationKind.JvmFunction
                });
            }

            if (logicalLocation.Count != 0)
            {
                location.LogicalLocation           = logicalLocation;
                location.FullyQualifiedLogicalName = String.Join("\\", location.LogicalLocation.Select(x => x.Name));
            }

            string file = problem.File;

            if (!String.IsNullOrEmpty(file))
            {
                location.ResultFile = new[]
                {
                    new PhysicalLocationComponent
                    {
                        Uri      = RemoveBadRoot(file),
                        MimeType = MimeType.Java,
                        Region   = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                    }
                };
            }

            if ("file".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                if (location.AnalysisTarget != null)
                {
                    location.ResultFile = location.AnalysisTarget;
                }

                location.AnalysisTarget = new[]
                {
                    new PhysicalLocationComponent
                    {
                        Uri      = RemoveBadRoot(problem.EntryPointName),
                        MimeType = MimeType.Java
                    }
                };
            }

            return(result);
        }
        public void AndroidStudioConverter_ConvertToSarifResult_EmptyHintsDoNotAffectDescription()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
            builder.Description = "hungry EVIL zombies";
            var uut = new AndroidStudioProblem(builder);

            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
            Assert.AreEqual("hungry EVIL zombies", result.Message);
        }
        public Result ConvertProblemToSarifResult(AndroidStudioProblem problem)
        {
            var result = new Result();

            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);

            if (problem.Hints.IsEmpty)
            {
                result.Message = description;
            }
            else
            {
                result.Message = GenerateFullMessage(description, problem.Hints);
            }

            SetSarifResultPropertiesForProblem(result, problem);
            var location = new Location();

            location.FullyQualifiedLogicalName = CreateSignature(problem);

            string logicalLocationKey = CreateLogicalLocation(problem);

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

            Uri    uri;
            string file = problem.File;

            if (!String.IsNullOrEmpty(file))
            {
                location.ResultFile = new PhysicalLocation
                {
                    Region = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                };

                if (RemoveBadRoot(file, out uri))
                {
                    location.ResultFile.UriBaseId = PROJECT_DIR;
                }
                location.ResultFile.Uri = uri;
            }

            if ("file".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                if (location.AnalysisTarget != null)
                {
                    location.ResultFile = location.AnalysisTarget;
                }

                location.AnalysisTarget = new PhysicalLocation();

                if (RemoveBadRoot(problem.EntryPointName, out uri))
                {
                    location.AnalysisTarget.UriBaseId = PROJECT_DIR;
                }
                location.AnalysisTarget.Uri = uri;
            }

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

            return(result);
        }
        public void AndroidStudioConverter_ConvertSarifResult_RecordsModuleAsTopLevelIfPresent()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
            builder.File = null;
            builder.Module = "my_fancy_binary";
            builder.EntryPointType = "method";
            builder.EntryPointName = "my_method";

            var expectedLocation = new Location
            {
                FullyQualifiedLogicalName = "my_fancy_binary\\my_method",
            };

            var expectedLogicalLocations = new Dictionary<string, LogicalLocation>
            {
                {
                    "my_fancy_binary", new LogicalLocation { ParentKey = null, Name = "my_fancy_binary", Kind = LogicalLocationKind.Module }
                },
                {
                    @"my_fancy_binary\my_method",
                    new LogicalLocation { ParentKey = "my_fancy_binary", Name = "my_method", Kind = LogicalLocationKind.Member }
                },
               };

            var converter = new AndroidStudioConverter();
            Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            result.Locations[0].ValueEquals(expectedLocation).Should().BeTrue();

            foreach (string key in expectedLogicalLocations.Keys)
            {
                expectedLogicalLocations[key].ValueEquals(converter.LogicalLocationsDictionary[key]).Should().BeTrue();
            }
            converter.LogicalLocationsDictionary.Count.Should().Be(expectedLogicalLocations.Count);
        }
 public void Initialize()
 {
     _converter = new AndroidStudioConverter();
 }