Beispiel #1
0
        public void AnalysisStopped(RuntimeConditions runtimeConditions)
        {
            RuntimeConditions fatalConditions = (runtimeConditions & ~RuntimeConditions.Nonfatal);

            if (fatalConditions == RuntimeConditions.None)
            {
                Console.WriteLine(SdkResources.MSG_AnalysisCompletedSuccessfully);
            }

            Console.WriteLine();

            if ((runtimeConditions & RuntimeConditions.RuleNotApplicableToTarget) != 0)
            {
                Console.WriteLine(SdkResources.MSG_OneOrMoreNotApplicable);
                Console.WriteLine();
            }

            if ((runtimeConditions & RuntimeConditions.TargetNotValidToAnalyze) != 0)
            {
                Console.WriteLine(SdkResources.MSG_OneOrMoreInvalidTargets);
                Console.WriteLine();
            }

            if (fatalConditions != 0)
            {
                // One or more fatal conditions observed at runtime,
                // so we'll report a catastrophic exit.
                Console.WriteLine(SdkResources.MSG_UnexpectedApplicationExit);
                Console.WriteLine(SdkResources.UnexpectedFatalRuntimeConditions + fatalConditions.ToString());
                Console.WriteLine();
            }
        }
Beispiel #2
0
        public void Analyze(SqlFileContext context)
        {
            var visitor = new AllNodesVisitor(
                (node) =>
                {
                    List<ISkimmer<SqlFileContext>> skimmersList;

                    if (node == null) { return; }

                    if (this.typeToSkimmersMap.TryGetValue(node.GetType(), out skimmersList))
                    {
                        foreach (ISqlSkimmer skimmer in skimmersList)
                        {
                            context.Fragment = node;
                            try
                            {
                                skimmer.Analyze(context);
                            }
                            catch (Exception ex)
                            {
                                RuntimeErrors |= AnalyzeCommand.LogUnhandledRuleExceptionAnalyzingTarget(
                                    this.disabledSkimmers,
                                    context,
                                    skimmer,
                                    ex);
                            }
                        }
                    }
                }
                );
            context.Fragment.Accept(visitor);
        }
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
     Console.WriteLine();
     Console.WriteLine("# valid targets: " + _targetsCount.ToString());
     Console.WriteLine("# invalid targets: " + _invalidTargetsCount.ToString());
     Console.WriteLine("Time elapsed: " + _stopwatch.Elapsed.ToString());
 }
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
     foreach (IAnalysisLogger logger in Loggers)
     {
         logger.AnalysisStopped(runtimeConditions);
     }
 }
Beispiel #5
0
        protected virtual TContext CreateContext(
            TOptions options,
            IAnalysisLogger logger,
            RuntimeConditions runtimeErrors,
            string filePath = null)
        {
            var context = new TContext();

            context.Logger        = logger;
            context.RuntimeErrors = runtimeErrors;

            if (filePath != null)
            {
                context.TargetUri = new Uri(filePath);
            }

            return(context);
        }
Beispiel #6
0
        private void ExceptionTestHelper(
            RuntimeConditions runtimeConditions,
            ExitReason expectedExitReason     = ExitReason.None,
            TestAnalyzeOptions analyzeOptions = null)
        {
            analyzeOptions ??= new TestAnalyzeOptions()
            {
                TargetFileSpecifiers = new string[] { }
            };

            ExceptionTestHelperImplementation(
                runtimeConditions, expectedExitReason,
                analyzeOptions,
                multithreaded: false);

            ExceptionTestHelperImplementation(
                runtimeConditions, expectedExitReason,
                analyzeOptions,
                multithreaded: true);
        }
Beispiel #7
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]
            };

            var command = new TestAnalyzeCommand();

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

            int result = command.Run(analyzeOptions);

            int expectedResult =
                (runtimeConditions & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors ?
                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;
        }
        protected virtual TContext CreateContext(
            TOptions options,
            IAnalysisLogger logger,
            RuntimeConditions runtimeErrors,
            PropertiesDictionary policy = null,
            string filePath             = null)
        {
            var context = new TContext
            {
                Logger        = logger,
                RuntimeErrors = runtimeErrors,
                Policy        = policy
            };

            if (filePath != null)
            {
                context.TargetUri = new Uri(filePath);
            }

            return(context);
        }
        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]
            };

            var command = new TestAnalyzeCommand();
            command.DefaultPlugInAssemblies = new Assembly[] { typeof(ExceptionRaisingRule).Assembly };

            int result = command.Run(analyzeOptions);

            int expectedResult =
                (runtimeConditions & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors ?
                    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;
        }
Beispiel #10
0
        public void Analyze(SqlFileContext context)
        {
            var visitor = new AllNodesVisitor(
                (node) =>
            {
                List <ISkimmer <SqlFileContext> > skimmersList;

                if (node == null)
                {
                    return;
                }

                if (this.typeToSkimmersMap.TryGetValue(node.GetType(), out skimmersList))
                {
                    foreach (ISqlSkimmer skimmer in skimmersList)
                    {
                        context.Fragment = node;
                        try
                        {
                            skimmer.Analyze(context);
                        }
                        catch (Exception ex)
                        {
                            RuntimeErrors |= AnalyzeCommand.LogUnhandledRuleExceptionAnalyzingTarget(
                                this.disabledSkimmers,
                                context,
                                skimmer,
                                ex);
                        }
                    }
                }
            }
                );

            context.Fragment.Accept(visitor);
        }
Beispiel #11
0
        protected override BinaryAnalyzerContext CreateContext(AnalyzeOptions options, IAnalysisLogger logger, RuntimeConditions runtimeErrors, string filePath = null)
        {
            BinaryAnalyzerContext binaryAnalyzerContext = base.CreateContext(options, logger, runtimeErrors, filePath);

            binaryAnalyzerContext.SymbolPath             = options.SymbolsPath;
            binaryAnalyzerContext.LocalSymbolDirectories = options.LocalSymbolDirectories;
            _plugInFilePaths = options.PluginFilePaths;
            return(binaryAnalyzerContext);
        }
        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;

            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();
            }
            ExceptionRaisingRule.s_exceptionCondition = ExceptionCondition.None;
        }
Beispiel #13
0
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
 }
Beispiel #14
0
        private void ExceptionTestHelperImplementation(
            RuntimeConditions runtimeConditions,
            ExitReason expectedExitReason,
            TestAnalyzeOptions analyzeOptions,
            bool multithreaded)
        {
            TestRule.s_testRuleBehaviors = analyzeOptions.TestRuleBehaviors.AccessibleOutsideOfContextOnly();

            analyzeOptions = analyzeOptions ?? new TestAnalyzeOptions()
            {
                TestRuleBehaviors    = analyzeOptions.TestRuleBehaviors.AccessibleWithinContextOnly(),
                TargetFileSpecifiers = new string[0]
            };

            analyzeOptions.Quiet = true;

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

            ITestAnalyzeCommand command = multithreaded
                ? (ITestAnalyzeCommand) new TestMultithreadedAnalyzeCommand()
                : (ITestAnalyzeCommand) new TestAnalyzeCommand();

            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;
        }
        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;
        }
Beispiel #16
0
        private void NodeAction(TSqlFragment node)
        {
            List<ISkimmer<SqlFileContext>> skimmersList;

            if (node == null) { return; }

            if (this.typeToSkimmersMap.TryGetValue(node.GetType(), out skimmersList))
            {
                foreach (ISqlSkimmer skimmer in skimmersList)
                {
                    this.context.Fragment = node;
                    try
                    {
                        skimmer.Analyze(this.context);
                    }
                    catch (Exception ex)
                    {
                        RuntimeErrors |= AnalyzeCommand.LogUnhandledRuleExceptionAnalyzingTarget(
                            this.disabledSkimmers,
                            this.context,
                            skimmer,
                            ex);
                    }
                }
            }
        }
Beispiel #17
0
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
     _run.Invocation.EndTime = DateTime.UtcNow;
 }
Beispiel #18
0
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
     RuntimeErrors = runtimeConditions;
 }
Beispiel #19
0
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
     RuntimeErrors = runtimeConditions;
 }
Beispiel #20
0
        protected override SarifValidationContext CreateContext(ValidateOptions options, IAnalysisLogger logger, RuntimeConditions runtimeErrors, string filePath = null)
        {
            SarifValidationContext context = base.CreateContext(options, logger, runtimeErrors, filePath);

            context.SchemaFilePath             = options.SchemaFilePath;
            context.UpdateInputsToCurrentSarif = options.UpdateInputsToCurrentSarif;
            return(context);
        }
Beispiel #21
0
 public void AnalysisStopped(RuntimeConditions runtimeConditions)
 {
     _run.Invocation.EndTime = DateTime.UtcNow;
 }