Ejemplo n.º 1
0
        private void VerifyThrows <ExceptionType>(
            BinarySkimmer skimmer,
            bool useDefaultPolicy = false) where ExceptionType : Exception
        {
            var    targets  = new List <string>();
            string ruleName = skimmer.GetType().Name;

            string baseFilesDirectory = GetTestDirectoryFor(ruleName);

            baseFilesDirectory = Path.Combine(Environment.CurrentDirectory, "FunctionalTestsData", baseFilesDirectory);

            string[] testFilesDirectories =
                new string[]
            {
                Path.Combine(baseFilesDirectory, "Pass"),
                Path.Combine(baseFilesDirectory, "Fail"),
                Path.Combine(baseFilesDirectory, "NotApplicable")
            };

            foreach (string testDirectory in testFilesDirectories)
            {
                if (Directory.Exists(testDirectory))
                {
                    foreach (string target in Directory.GetFiles(testDirectory, "*", SearchOption.AllDirectories))
                    {
                        targets.Add(target);
                    }
                }
            }
            var context = new BinaryAnalyzerContext();
            var logger  = new TestMessageLogger();

            context.Logger = logger;
            PropertiesDictionary policy = null;

            if (useDefaultPolicy)
            {
                policy = new PropertiesDictionary();
            }
            context.Policy = policy;

            skimmer.Initialize(context);

            foreach (string target in targets)
            {
                context = this.CreateContext(logger, policy, target);

                context.Rule = skimmer;

                if (skimmer.CanAnalyze(context, out string reasonForNotAnalyzing) != AnalysisApplicability.ApplicableToSpecifiedTarget)
                {
                    continue;
                }
                Assert.Throws <ExceptionType>(() => skimmer.Analyze(context));
            }
        }
Ejemplo n.º 2
0
        private void Verify(
            BinarySkimmer skimmer,
            IEnumerable <string> additionalTestFiles,
            bool useDefaultPolicy,
            bool expectToPass)
        {
            var    targets            = new List <string>();
            string ruleName           = skimmer.GetType().Name;
            string testFilesDirectory = GetTestDirectoryFor(ruleName);

            testFilesDirectory = Path.Combine(Environment.CurrentDirectory, "FunctionalTestsData", testFilesDirectory);
            testFilesDirectory = Path.Combine(testFilesDirectory, expectToPass ? "Pass" : "Fail");

            Assert.True(Directory.Exists(testFilesDirectory));

            foreach (string target in Directory.GetFiles(testFilesDirectory, "*", SearchOption.AllDirectories))
            {
                if (AnalyzeCommand.ValidAnalysisFileExtensions.Contains(Path.GetExtension(target)))
                {
                    targets.Add(target);
                }
            }

            if (additionalTestFiles != null)
            {
                foreach (string additionalTestFile in additionalTestFiles)
                {
                    targets.Add(additionalTestFile);
                }
            }

            var context = new BinaryAnalyzerContext();
            var logger  = new TestMessageLogger();

            context.Logger = logger;
            PropertiesDictionary policy = null;

            if (useDefaultPolicy)
            {
                policy = new PropertiesDictionary();
            }
            context.Policy = policy;

            skimmer.Initialize(context);

            foreach (string target in targets)
            {
                context = this.CreateContext(logger, policy, target);

                if (!context.IsValidAnalysisTarget)
                {
                    continue;
                }

                context.Rule = skimmer;

                if (skimmer.CanAnalyze(context, out string reasonForNotAnalyzing) != AnalysisApplicability.ApplicableToSpecifiedTarget)
                {
                    continue;
                }

                skimmer.Analyze(context);
            }

            HashSet <string> expected     = expectToPass ? logger.PassTargets : logger.FailTargets;
            HashSet <string> other        = expectToPass ? logger.FailTargets : logger.PassTargets;
            HashSet <string> configErrors = logger.ConfigurationErrorTargets;

            string expectedText = expectToPass ? "success" : "failure";
            string actualText   = expectToPass ? "failed" : "succeeded";
            var    sb           = new StringBuilder();

            foreach (string target in targets)
            {
                if (expected.Contains(target))
                {
                    expected.Remove(target);
                    continue;
                }
                bool missingEntirely = !other.Contains(target);

                if (missingEntirely &&
                    !expectToPass &&
                    target.Contains("Pdb") &&
                    configErrors.Contains(target))
                {
                    missingEntirely = false;
                    configErrors.Remove(target);
                    continue;
                }

                if (missingEntirely)
                {
                    sb.AppendLine("Expected '" + ruleName + "' " + expectedText + " but saw no result at all for file: " + Path.GetFileName(target));
                }
                else
                {
                    other.Remove(target);
                    sb.AppendLine("Expected '" + ruleName + "' " + expectedText + " but check " + actualText + " for: " + Path.GetFileName(target));
                }
            }

            if (sb.Length > 0)
            {
                this.testOutputHelper.WriteLine(sb.ToString());
            }

            Assert.Equal(0, sb.Length);
            Assert.Empty(expected);
            Assert.Empty(other);
        }
Ejemplo n.º 3
0
        private void VerifyNotApplicable(
            BinarySkimmer skimmer,
            HashSet <string> notApplicableConditions,
            AnalysisApplicability expectedApplicability = AnalysisApplicability.NotApplicableToSpecifiedTarget,
            bool useDefaultPolicy = false)
        {
            string ruleName           = skimmer.GetType().Name;
            string testFilesDirectory = GetTestDirectoryFor(ruleName);

            testFilesDirectory = Path.Combine(Environment.CurrentDirectory, "FunctionalTestsData", testFilesDirectory);
            testFilesDirectory = Path.Combine(testFilesDirectory, "NotApplicable");

            var context = new BinaryAnalyzerContext();

            HashSet <string> targets = this.GetTestFilesMatchingConditions(notApplicableConditions);

            if (Directory.Exists(testFilesDirectory))
            {
                foreach (string target in Directory.GetFiles(testFilesDirectory, "*", SearchOption.AllDirectories))
                {
                    if (AnalyzeCommand.ValidAnalysisFileExtensions.Contains(Path.GetExtension(target)))
                    {
                        targets.Add(target);
                    }
                }
            }

            var logger = new TestMessageLogger();

            context.Logger = logger;

            var sb = new StringBuilder();

            foreach (string target in targets)
            {
                string extension = Path.GetExtension(target);

                context = this.CreateContext(logger, null, target);
                if (!context.IsValidAnalysisTarget)
                {
                    continue;
                }

                if (useDefaultPolicy)
                {
                    context.Policy = new PropertiesDictionary();
                }

                context.Rule = skimmer;

                AnalysisApplicability applicability;
                applicability = skimmer.CanAnalyze(context, out string reasonForNotAnalyzing);
                if (applicability != expectedApplicability)
                {
                    sb.AppendLine("CanAnalyze did not indicate target was invalid for analysis (return was " +
                                  applicability + "): " +
                                  Path.GetFileName(target));
                    continue;
                }
            }

            if (sb.Length > 0)
            {
                this.testOutputHelper.WriteLine(sb.ToString());
            }

            Assert.Equal(0, sb.Length);
        }