Ejemplo n.º 1
0
        public void ConvertCommand_SemmleQlExample()
        {
            // Try converting a tiny sample SemmleQl file
            string sampleFilePath = "SemmleQlSample.csv";
            string outputFilePath = Path.ChangeExtension(sampleFilePath, ".sarif");

            File.WriteAllText(sampleFilePath, Extractor.GetResourceText($"ConvertCommand.{sampleFilePath}"));

            var options = new ConvertOptions
            {
                ToolFormat     = ToolFormat.SemmleQL,
                InputFilePath  = sampleFilePath,
                OutputFilePath = outputFilePath,
                Force          = true
            };

            // Verify command returned success
            int returnCode = new ConvertCommand().Run(options);

            returnCode.Should().Be(0);

            // Verify SARIF output log exists
            File.Exists(outputFilePath).Should().BeTrue();

            // Verify log loads, has correct Result count, and spot check a Result
            SarifLog log = SarifLog.Load(outputFilePath);

            log.Runs[0].Results.Count.Should().Be(8);
            log.Runs[0].Results[7].Locations[0].PhysicalLocation.Region.StartLine.Should().Be(40);
            log.Runs[0].Results[7].Locations[0].PhysicalLocation.Region.StartColumn.Should().Be(43);
        }
Ejemplo n.º 2
0
        private static bool ValidateOptions(ConvertOptions convertOptions, IFileSystem fileSystem)
        {
            bool valid = true;

            valid &= convertOptions.ValidateOutputOptions();

            valid &= DriverUtilities.ReportWhetherOutputFileCanBeCreated(convertOptions.OutputFilePath, convertOptions.Force, fileSystem);

            return(valid);
        }
Ejemplo n.º 3
0
        public static int Run(ConvertOptions convertOptions)
        {
            try
            {
                LoggingOptions loggingOptions = LoggingOptions.None;

                OptionallyEmittedData dataToInsert = OptionallyEmittedData.None;
                if (convertOptions.DataToInsert != null)
                {
                    Array.ForEach(convertOptions.DataToInsert, data => dataToInsert |= data);
                }

                if (convertOptions.PrettyPrint)
                {
                    loggingOptions |= LoggingOptions.PrettyPrint;
                }
                ;

                if (convertOptions.Force)
                {
                    loggingOptions |= LoggingOptions.OverwriteExistingOutputFile;
                }
                ;

                if (string.IsNullOrEmpty(convertOptions.OutputFilePath))
                {
                    convertOptions.OutputFilePath = convertOptions.InputFilePath + ".sarif";
                }

                new ToolFormatConverter().ConvertToStandardFormat(
                    convertOptions.ToolFormat,
                    convertOptions.InputFilePath,
                    convertOptions.OutputFilePath,
                    loggingOptions,
                    dataToInsert,
                    convertOptions.PluginAssemblyPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(1);
            }

            return(0);
        }
Ejemplo n.º 4
0
        public static int Run(ConvertOptions convertOptions)
        {
            try
            {
                LoggingOptions loggingOptions = LoggingOptions.None;

                if (convertOptions.PrettyPrint)
                {
                    loggingOptions |= LoggingOptions.PrettyPrint;
                }
                ;

                if (convertOptions.Force)
                {
                    loggingOptions |= LoggingOptions.OverwriteExistingOutputFile;
                }
                ;

                if (convertOptions.PersistFileContents)
                {
                    loggingOptions |= LoggingOptions.PersistFileContents;
                }

                if (string.IsNullOrEmpty(convertOptions.OutputFilePath))
                {
                    convertOptions.OutputFilePath = convertOptions.InputFilePath + ".sarif";
                }

                new ToolFormatConverter().ConvertToStandardFormat(
                    convertOptions.ToolFormat,
                    convertOptions.InputFilePath,
                    convertOptions.OutputFilePath,
                    loggingOptions,
                    convertOptions.PluginAssemblyPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(1);
            }

            return(0);
        }
Ejemplo n.º 5
0
        public void Run_WhenOutputFileExistsAndForceNotSpecified_Fails()
        {
            const string InputFilePath  = @"C:\input\ToolOutput.xml";
            const string OutputFilePath = @"C:\output\ToolOutput.xml.sarif";

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(x => x.DirectoryExists(OutputFilePath)).Returns(false);
            mockFileSystem.Setup(x => x.FileExists(OutputFilePath)).Returns(true);
            IFileSystem fileSystem = mockFileSystem.Object;

            var options = new ConvertOptions
            {
                ToolFormat     = ToolFormat.FxCop,
                InputFilePath  = InputFilePath,
                OutputFilePath = OutputFilePath
            };

            int returnCode = new ConvertCommand().Run(options, fileSystem);

            returnCode.Should().Be(1);
        }
        public void Run_WhenOutputFormatOptionsAreInconsistent_Fails()
        {
            // Run on the same sample file that succeeded in the test ConvertCommand_SemmleQlExample.
            // This time we expect it to fail because of the inconsistent output format options.
            string sampleFilePath = "SemmleQlSample.csv";
            string outputFilePath = Path.ChangeExtension(sampleFilePath, ".sarif");

            File.WriteAllText(sampleFilePath, Extractor.GetResourceText($"ConvertCommand.{sampleFilePath}"));

            var options = new ConvertOptions
            {
                ToolFormat     = ToolFormat.SemmleQL,
                InputFilePath  = sampleFilePath,
                OutputFilePath = outputFilePath,
                Force          = true,
                PrettyPrint    = true,
                Minify         = true
            };

            int returnCode = new ConvertCommand().Run(options);

            returnCode.Should().Be(1);
        }
 public void ConsumeEnvVarsAndInterpretOptions(ConvertOptions convertOptions)
 {
     ConsumeEnvVarsAndInterpretOptions((SingleFileOptionsBase)convertOptions);
 }
Ejemplo n.º 8
0
        public int Run(ConvertOptions convertOptions, IFileSystem fileSystem = null)
        {
            if (fileSystem == null)
            {
                fileSystem = new FileSystem();
            }

            try
            {
                if (string.IsNullOrEmpty(convertOptions.OutputFilePath))
                {
                    convertOptions.OutputFilePath = convertOptions.InputFilePath + SarifConstants.SarifFileExtension;
                }

                if (fileSystem.DirectoryExists(convertOptions.OutputFilePath))
                {
                    Console.Error.WriteLine(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "The output path '{0}' is a directory.",
                            convertOptions.OutputFilePath));
                    return(FAILURE);
                }

                if (!ValidateOptions(convertOptions, fileSystem))
                {
                    return(FAILURE);
                }

                LoggingOptions loggingOptions = LoggingOptions.None;

                OptionallyEmittedData dataToInsert = convertOptions.DataToInsert.ToFlags();

                if (convertOptions.PrettyPrint)
                {
                    loggingOptions |= LoggingOptions.PrettyPrint;
                }
                ;

                if (convertOptions.Force)
                {
                    loggingOptions |= LoggingOptions.OverwriteExistingOutputFile;
                }
                ;

                new ToolFormatConverter().ConvertToStandardFormat(
                    convertOptions.ToolFormat,
                    convertOptions.InputFilePath,
                    convertOptions.OutputFilePath,
                    loggingOptions,
                    dataToInsert,
                    convertOptions.PluginAssemblyPath);

                if (convertOptions.NormalizeForGitHubDsp)
                {
                    SarifLog sarifLog;

                    JsonSerializer serializer = new JsonSerializer()
                    {
                        Formatting = convertOptions.PrettyPrint ? Formatting.Indented : 0,
                    };

                    using (JsonTextReader reader = new JsonTextReader(new StreamReader(convertOptions.OutputFilePath)))
                    {
                        sarifLog = serializer.Deserialize <SarifLog>(reader);
                    }

                    var visitor = new GitHubDspIngestionVisitor();
                    visitor.VisitSarifLog(sarifLog);

                    using (FileStream stream = File.Create(convertOptions.OutputFilePath))
                        using (StreamWriter streamWriter = new StreamWriter(stream))
                            using (JsonTextWriter writer = new JsonTextWriter(streamWriter))
                            {
                                serializer.Serialize(writer, sarifLog);
                            }
                }
            }
            catch (Exception ex) when(!Debugger.IsAttached)
            {
                Console.WriteLine(ex);
                return(FAILURE);
            }

            return(SUCCESS);
        }
Ejemplo n.º 9
0
        public int Run(ConvertOptions convertOptions, IFileSystem fileSystem = null)
        {
            if (fileSystem == null)
            {
                fileSystem = new FileSystem();
            }

            try
            {
                if (string.IsNullOrEmpty(convertOptions.OutputFilePath))
                {
                    convertOptions.OutputFilePath = convertOptions.InputFilePath + ".sarif";
                }

                if (fileSystem.DirectoryExists(convertOptions.OutputFilePath))
                {
                    Console.Error.WriteLine(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "The output path '{0}' is a directory.",
                            convertOptions.OutputFilePath));
                    return(FAILURE);
                }

                if (!ValidateOptions(convertOptions, fileSystem))
                {
                    return(FAILURE);
                }

                LoggingOptions loggingOptions = LoggingOptions.None;

                OptionallyEmittedData dataToInsert = convertOptions.DataToInsert.ToFlags();

                if (convertOptions.PrettyPrint)
                {
                    loggingOptions |= LoggingOptions.PrettyPrint;
                }
                ;

                if (convertOptions.Force)
                {
                    loggingOptions |= LoggingOptions.OverwriteExistingOutputFile;
                }
                ;

                new ToolFormatConverter().ConvertToStandardFormat(
                    convertOptions.ToolFormat,
                    convertOptions.InputFilePath,
                    convertOptions.OutputFilePath,
                    loggingOptions,
                    dataToInsert,
                    convertOptions.PluginAssemblyPath);
            }
            catch (Exception ex) when(!Debugger.IsAttached)
            {
                Console.WriteLine(ex);
                return(FAILURE);
            }

            return(SUCCESS);
        }