コード例 #1
0
        private static SarifLog RunAnalyzeCommand(TestAnalyzeOptions options, IFileSystem fileSystem, int expectedReturnCode)
        {
            // If no log file is specified, we will convert the console output into a log file
            bool captureConsoleOutput = string.IsNullOrEmpty(options.OutputFilePath);

            var command = new TestAnalyzeCommand(fileSystem)
            {
                _captureConsoleOutput = captureConsoleOutput
            };

            command.DefaultPlugInAssemblies = new Assembly[] { typeof(AnalyzeCommandBaseTests).Assembly };

            try
            {
                HashUtilities.FileSystem = fileSystem;
                command.Run(options).Should().Be(expectedReturnCode);
            }
            finally
            {
                HashUtilities.FileSystem = null;
            }

            return(captureConsoleOutput
                ? ConvertConsoleOutputToSarifLog(command._consoleLogger.CapturedOutput)
                : JsonConvert.DeserializeObject <SarifLog>(File.ReadAllText(options.OutputFilePath)));
        }
コード例 #2
0
        public void AnalyzeCommand_LoadConfigurationFile(string configValue, bool defaultFileExists, string expectedFileName)
        {
            var options = new TestAnalyzeOptions
            {
                TargetFileSpecifiers = new string[] { "" },
                Verbose               = true,
                Statistics            = true,
                Quiet                 = true,
                ComputeFileHashes     = true,
                ConfigurationFilePath = configValue,
                Recurse               = true,
                OutputFilePath        = "",
            };

            var command = new TestAnalyzeCommand();

            string fileName = command.GetConfigurationFileName(options, defaultFileExists);

            if (string.IsNullOrEmpty(expectedFileName))
            {
                fileName.Should().BeNull();
            }
            else
            {
                fileName.Should().EndWith(expectedFileName);
            }
        }
コード例 #3
0
        public void AnalyzeCommandBase_LoadConfigurationFile(string configValue, bool defaultFileExists, string expectedFileName)
        {
            var options = new TestAnalyzeOptions
            {
                TargetFileSpecifiers = new string[] { "" },
                Verbose               = true,
                Quiet                 = true,
                DataToInsert          = new OptionallyEmittedData[] { OptionallyEmittedData.Hashes },
                ConfigurationFilePath = configValue,
                Recurse               = true,
                OutputFilePath        = "",
            };

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(x => x.FileExists(It.IsAny <string>())).Returns(defaultFileExists);

            var command = new TestAnalyzeCommand(mockFileSystem.Object);

            string fileName = command.GetConfigurationFileName(options);

            if (string.IsNullOrEmpty(expectedFileName))
            {
                fileName.Should().BeNull();
            }
            else
            {
                fileName.Should().EndWith(expectedFileName);
            }
        }
コード例 #4
0
        private void ExceptionTestHelper(
            ExceptionCondition exceptionCondition,
            RuntimeConditions runtimeConditions,
            ExitReason expectedExitReason     = ExitReason.None,
            TestAnalyzeOptions analyzeOptions = null)
        {
            ExceptionRaisingRule.s_exceptionCondition = exceptionCondition;
            analyzeOptions = analyzeOptions ?? new TestAnalyzeOptions()
            {
                TargetFileSpecifiers = new string[0]
            };

            analyzeOptions.Quiet = true;

            var command = new TestAnalyzeCommand();

            Assembly[] plugInAssemblies = null;

            if (analyzeOptions.DefaultPlugInFilePaths != null)
            {
                var assemblies = new List <Assembly>();
                foreach (string plugInFilePath in analyzeOptions.DefaultPlugInFilePaths)
                {
                    assemblies.Add(Assembly.LoadFrom(plugInFilePath));
                }
                plugInAssemblies = new Assembly[assemblies.Count];
                assemblies.CopyTo(plugInAssemblies, 0);
            }
            else
            {
                plugInAssemblies = new Assembly[] { typeof(ExceptionRaisingRule).Assembly };
            }

            command.DefaultPlugInAssemblies = plugInAssemblies;

            int result = command.Run(analyzeOptions);

            int expectedResult =
                (runtimeConditions & ~RuntimeConditions.Nonfatal) == RuntimeConditions.None ?
                TestAnalyzeCommand.SUCCESS : TestAnalyzeCommand.FAILURE;

            Assert.Equal(runtimeConditions, command.RuntimeErrors);
            Assert.Equal(expectedResult, result);

            if (expectedExitReason != ExitReason.None)
            {
                Assert.NotNull(command.ExecutionException);

                if (expectedExitReason != ExitReason.UnhandledExceptionInEngine)
                {
                    var eax = command.ExecutionException as ExitApplicationException <ExitReason>;
                    Assert.NotNull(eax);
                }
            }
            else
            {
                Assert.Null(command.ExecutionException);
            }
            ExceptionRaisingRule.s_exceptionCondition = ExceptionCondition.None;
        }
コード例 #5
0
        public Run AnalyzeFile(
            string fileName,
            string configFileName = null,
            RuntimeConditions runtimeConditions = RuntimeConditions.None,
            int expectedReturnCode = TestAnalyzeCommand.SUCCESS)
        {
            string path = Path.GetTempFileName();
            Run    run  = null;

            try
            {
                var options = new TestAnalyzeOptions
                {
                    TargetFileSpecifiers = new string[] { fileName },
                    Verbose               = true,
                    Statistics            = true,
                    Quiet                 = true,
                    ComputeFileHashes     = true,
                    ConfigurationFilePath = configFileName ?? TestAnalyzeCommand.DefaultPolicyName,
                    Recurse               = true,
                    OutputFilePath        = path,
                    SarifVersion          = SarifVersion.TwoZeroZero
                };

                var command = new TestAnalyzeCommand();
                command.DefaultPlugInAssemblies = new Assembly[] { this.GetType().Assembly };
                int result = command.Run(options);

                result.Should().Be(expectedReturnCode);

                command.RuntimeErrors.Should().Be(runtimeConditions);

                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ContractResolver = SarifContractResolver.Instance
                };

                SarifLog log = JsonConvert.DeserializeObject <SarifLog>(File.ReadAllText(path), settings);
                Assert.NotNull(log);
                Assert.Equal <int>(1, log.Runs.Count);

                run = log.Runs.First();
            }
            finally
            {
                File.Delete(path);
            }

            return(run);
        }
コード例 #6
0
        public Run AnalyzeFile(
            string fileName,
            TestRuleBehaviors behaviors         = TestRuleBehaviors.None,
            string configFileName               = null,
            RuntimeConditions runtimeConditions = RuntimeConditions.None,
            int expectedReturnCode              = TestAnalyzeCommand.SUCCESS)
        {
            string path = Path.GetTempFileName();
            Run    run  = null;

            try
            {
                var options = new TestAnalyzeOptions
                {
                    TargetFileSpecifiers = new string[] { fileName },
                    Verbose = true,
                    Quiet   = true,
                    ConfigurationFilePath = configFileName ?? TestAnalyzeCommand.DefaultPolicyName,
                    Recurse            = true,
                    OutputFilePath     = path,
                    SarifOutputVersion = SarifVersion.Current,
                    Force             = true,
                    TestRuleBehaviors = behaviors
                };

                var command = new TestAnalyzeCommand();
                command.DefaultPlugInAssemblies = new Assembly[] { this.GetType().Assembly };
                int result = command.Run(options);

                result.Should().Be(expectedReturnCode);

                command.RuntimeErrors.Should().Be(runtimeConditions);

                SarifLog log = JsonConvert.DeserializeObject <SarifLog>(File.ReadAllText(path));
                Assert.NotNull(log);
                Assert.Equal <int>(1, log.Runs.Count);

                run = log.Runs.First();
            }
            finally
            {
                File.Delete(path);
            }

            return(run);
        }
コード例 #7
0
        private static SarifLog RunAnalyzeCommand(TestAnalyzeOptions options, IFileSystem fileSystem = null, int expectedReturnCode = 0)
        {
            // If no log file is specified, we will convert the console output into a log file
            bool captureConsoleOutput = string.IsNullOrEmpty(options.OutputFilePath);

            var command = new TestAnalyzeCommand(fileSystem)
            {
                _captureConsoleOutput = captureConsoleOutput
            };

            command.DefaultPlugInAssemblies = new Assembly[] { typeof(AnalyzeCommandBaseTests).Assembly };

            try
            {
                HashUtilities.FileSystem = fileSystem;
                command.Run(options).Should().Be(expectedReturnCode);
            }
            finally
            {
                HashUtilities.FileSystem = null;
            }

            SarifLog sarifLog = null;

            if (captureConsoleOutput)
            {
                var converter = new MSBuildConverter(verbose: true);

                var sb = new StringBuilder();

                using (var input = new MemoryStream(Encoding.UTF8.GetBytes(command._consoleLogger.CapturedOutput)))
                    using (var outputTextWriter = new StringWriter(sb))
                        using (var outputJson = new JsonTextWriter(outputTextWriter))
                            using (var output = new ResultLogJsonWriter(outputJson))
                            {
                                converter.Convert(input, output, OptionallyEmittedData.None);
                            }
                sarifLog = JsonConvert.DeserializeObject <SarifLog>(sb.ToString());
            }
            else
            {
                sarifLog = JsonConvert.DeserializeObject <SarifLog>(File.ReadAllText(options.OutputFilePath));
            }

            return(sarifLog);
        }
コード例 #8
0
        public void AnalyzeCommandBase_PersistsSarifOneZeroZero()
        {
            string fileName = GetThisTestAssemblyFilePath();
            string path     = Path.GetTempFileName();

            try
            {
                var options = new TestAnalyzeOptions
                {
                    TargetFileSpecifiers = new string[] { fileName },
                    Verbose               = true,
                    Statistics            = true,
                    Quiet                 = true,
                    ComputeFileHashes     = true,
                    ConfigurationFilePath = TestAnalyzeCommand.DefaultPolicyName,
                    Recurse               = true,
                    OutputFilePath        = path,
                    PrettyPrint           = true,
                    Force                 = true,
                    SarifOutputVersion    = SarifVersion.OneZeroZero
                };

                var command = new TestAnalyzeCommand();
                command.DefaultPlugInAssemblies = new Assembly[] { this.GetType().Assembly };
                int returnValue = command.Run(options);

                returnValue.Should().Be(0);

                command.RuntimeErrors.Should().Be(RuntimeConditions.None);

                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ContractResolver = SarifContractResolverVersionOne.Instance
                };

                SarifLogVersionOne log = JsonConvert.DeserializeObject <SarifLogVersionOne>(File.ReadAllText(path), settings);
                log.Should().NotBeNull();
                log.Runs.Count.Should().Be(1);
            }
            finally
            {
                File.Delete(path);
            }
        }
コード例 #9
0
        private void ExceptionTestHelper(
            TestRuleBehaviors testRuleBehaviors,
            RuntimeConditions runtimeConditions,
            ExitReason expectedExitReason     = ExitReason.None,
            TestAnalyzeOptions analyzeOptions = null)
        {
            TestRule.s_testRuleBehaviors = testRuleBehaviors;
            analyzeOptions = analyzeOptions ?? new TestAnalyzeOptions()
            {
                TargetFileSpecifiers = new string[0]
            };

            analyzeOptions.Quiet = true;

            var command = new TestAnalyzeCommand();

            Assembly[] plugInAssemblies = null;

            if (analyzeOptions.DefaultPlugInFilePaths != null)
            {
                var assemblies = new List <Assembly>();
                foreach (string plugInFilePath in analyzeOptions.DefaultPlugInFilePaths)
                {
                    assemblies.Add(Assembly.LoadFrom(plugInFilePath));
                }
                plugInAssemblies = new Assembly[assemblies.Count];
                assemblies.CopyTo(plugInAssemblies, 0);
            }
            else
            {
                plugInAssemblies = new Assembly[] { typeof(TestRule).Assembly };
            }

            command.DefaultPlugInAssemblies = plugInAssemblies;

            int result = command.Run(analyzeOptions);

            int expectedResult =
                (runtimeConditions & ~RuntimeConditions.Nonfatal) == RuntimeConditions.None ?
                TestAnalyzeCommand.SUCCESS : TestAnalyzeCommand.FAILURE;

            command.RuntimeErrors.Should().Be(runtimeConditions);
            result.Should().Be(expectedResult);

            if (expectedExitReason != ExitReason.None)
            {
                command.ExecutionException.Should().NotBeNull();

                if (expectedExitReason != ExitReason.UnhandledExceptionInEngine)
                {
                    var eax = command.ExecutionException as ExitApplicationException <ExitReason>;
                    eax.Should().NotBeNull();
                    eax.ExitReason.Should().Be(expectedExitReason);
                }
            }
            else
            {
                command.ExecutionException.Should().BeNull();
            }
            TestRule.s_testRuleBehaviors = TestRuleBehaviors.None;
        }