Beispiel #1
0
        protected override TestAnalysisContext CreateContext(
            TestAnalyzeOptions options,
            IAnalysisLogger logger,
            RuntimeConditions runtimeErrors,
            PropertiesDictionary policy = null,
            string filePath             = null)
        {
            TestAnalysisContext context = base.CreateContext(options, logger, runtimeErrors, policy, filePath);

            if (context.Policy == null)
            {
                context.Policy = new PropertiesDictionary();
                context.Policy.SetProperty(TestRule.Behaviors, options.TestRuleBehaviors.AccessibleWithinContextOnly());
            }

            TestRuleBehaviors behaviors = context.Policy.GetProperty(TestRule.Behaviors);

            context.IsValidAnalysisTarget = !behaviors.HasFlag(TestRuleBehaviors.RegardAnalysisTargetAsInvalid);

            context.TargetLoadException =
                behaviors.HasFlag(TestRuleBehaviors.RegardAnalysisTargetAsCorrupted)
               ? new InvalidOperationException()
               : null;

            context.Options = options;

            return(context);
        }
Beispiel #2
0
        protected override TestAnalysisContext DetermineApplicabilityAndAnalyze(TestAnalysisContext context, IEnumerable <Skimmer <TestAnalysisContext> > skimmers, ISet <string> disabledSkimmers)
        {
            TestRuleBehaviors behaviors = context.Policy.GetProperty(TestRule.Behaviors);

            TestRule.s_testRuleBehaviors = behaviors.AccessibleOutsideOfContextOnly();

            return(base.DetermineApplicabilityAndAnalyze(context, skimmers, disabledSkimmers));
        }
Beispiel #3
0
 public static TestRuleBehaviors AccessibleWithinContextOnly(this TestRuleBehaviors behaviors)
 {
     // These are the only test behavior flags that *must* be retrieved via
     // a context object. For example, if a skimmer needs to raise an exception
     // when it is initialized, it should retrieve the test rule behavior that
     // specifies this from the context object that parameterizes the call.
     return(behaviors & ~behaviors.AccessibleOutsideOfContextOnly());
 }
Beispiel #4
0
 public static TestRuleBehaviors AccessibleOutsideOfContextOnly(this TestRuleBehaviors behaviors)
 {
     // These are the only test behavior flags that may need to be retrieved outside of
     // a context object (because the data is accessed when a context object isn't at
     // hand). For example, if a skimmer needs to raise an exception in its constructor,
     // which isn't paramterized with a context object, the test rule behavior that
     // specifies this can be retrieved from a thread static property.
     return(behaviors &
            (
                TestRuleBehaviors.RaiseExceptionAccessingId |
                TestRuleBehaviors.RaiseExceptionAccessingName |
                TestRuleBehaviors.RaiseExceptionInvokingConstructor |
                TestRuleBehaviors.TreatPlatformAsInvalid
            ));
 }
Beispiel #5
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);
        }
Beispiel #6
0
        public override void Analyze(TestAnalysisContext context)
        {
            TestRuleBehaviors testRuleBehaviors = context.Policy.GetProperty(Behaviors);

            // Currently, we only allow test rule behavior either be passed by context
            // or injected via static data, not by both mechanisms.
            (s_testRuleBehaviors == 0 || testRuleBehaviors == 0).Should().BeTrue();

            if (testRuleBehaviors == TestRuleBehaviors.None)
            {
                testRuleBehaviors = s_testRuleBehaviors;
            }
            ;

            switch (testRuleBehaviors)
            {
            case TestRuleBehaviors.RaiseExceptionInvokingAnalyze:
            {
                throw new InvalidOperationException(nameof(TestRuleBehaviors.RaiseExceptionInvokingAnalyze));
            }

            case TestRuleBehaviors.RaiseTargetParseError:
            {
                Errors.LogTargetParseError(
                    context,
                    new Region
                    {
                        StartLine   = 42,
                        StartColumn = 54
                    },
                    "Could not parse target.");
                break;
            }

            case TestRuleBehaviors.LogError:
            {
                context.Logger.Log(this,
                                   new Result
                    {
                        RuleId  = this.Id,
                        Level   = FailureLevel.Error,
                        Message = new Message {
                            Text = "Simple test rule message."
                        }
                    });
                break;
            }

            default:
            {
                break;
            }
            }

            string fileName = Path.GetFileName(context.TargetUri.LocalPath);

            if (fileName.Contains(nameof(FailureLevel.Error)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(FailureLevel.Error, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Failed),
                                                             context.TargetUri.GetFileName()));
            }
            if (fileName.Contains(nameof(FailureLevel.Warning)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(FailureLevel.Warning, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Failed),
                                                             context.TargetUri.GetFileName()));
            }
            if (fileName.Contains(nameof(FailureLevel.Note)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(FailureLevel.Note, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Note),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Pass)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Pass, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Pass),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Review)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Review, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Review),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Open)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Open, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Open),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Informational)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Informational, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Information),
                                                             context.TargetUri.GetFileName()));
            }
        }
        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;
        }
        public override void Analyze(TestAnalysisContext context)
        {
            // We do not access the static test rule behaviors here. We also want to
            // ensure this data is only set with flags (if any) that are legal for
            // this property.
            (s_testRuleBehaviors & s_testRuleBehaviors.AccessibleOutsideOfContextOnly())
            .Should().Be(s_testRuleBehaviors);

            // Now we'll make sure the context test rule behaviors are restricted
            // to settings that are legal to pass in a context object.
            TestRuleBehaviors testRuleBehaviors = context.Policy.GetProperty(Behaviors);

            (testRuleBehaviors & testRuleBehaviors.AccessibleWithinContextOnly())
            .Should().Be(testRuleBehaviors);

            switch (testRuleBehaviors)
            {
            case TestRuleBehaviors.RaiseExceptionInvokingAnalyze:
            {
                throw new InvalidOperationException(nameof(TestRuleBehaviors.RaiseExceptionInvokingAnalyze));
            }

            case TestRuleBehaviors.RaiseTargetParseError:
            {
                Errors.LogTargetParseError(
                    context,
                    new Region
                    {
                        StartLine   = 42,
                        StartColumn = 54
                    },
                    "Could not parse target.");
                break;
            }

            case TestRuleBehaviors.LogError:
            {
                context.Logger.Log(this,
                                   new Result
                    {
                        RuleId  = this.Id,
                        Level   = FailureLevel.Error,
                        Message = new Message {
                            Text = "Simple test rule message."
                        }
                    });
                break;
            }

            default:
            {
                break;
            }
            }

            string fileName = Path.GetFileName(context.TargetUri.LocalPath);

            if (fileName.Contains(nameof(FailureLevel.Error)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(FailureLevel.Error, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Failed),
                                                             context.TargetUri.GetFileName()));
            }
            if (fileName.Contains(nameof(FailureLevel.Warning)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(FailureLevel.Warning, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Failed),
                                                             context.TargetUri.GetFileName()));
            }
            if (fileName.Contains(nameof(FailureLevel.Note)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(FailureLevel.Note, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Note),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Pass)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Pass, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Pass),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Review)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Review, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Review),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Open)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Open, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Open),
                                                             context.TargetUri.GetFileName()));
            }
            else if (fileName.Contains(nameof(ResultKind.Informational)))
            {
                context.Logger.Log(this,
                                   RuleUtilities.BuildResult(ResultKind.Informational, context, null,
                                                             nameof(SkimmerBaseTestResources.TEST1001_Information),
                                                             context.TargetUri.GetFileName()));
            }
        }