Esempio n. 1
0
        public void RulesNotPresent_Pass()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myfakerule.json"),
                FilePathExclusions = "none", //allow source under unittest path
                TestType           = "RulesNotPresent"
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;

            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }
Esempio n. 2
0
        public void LogDebugLevel_Pass()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none", //allow source under unittest path
                LogFileLevel       = "debug",
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logdebug.txt"),
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;

                string testLogContent = File.ReadAllText(options.LogFilePath);
                if (String.IsNullOrEmpty(testLogContent))
                {
                    exitCode = TagTestResult.ExitCode.CriticalError;
                }
                else if (testLogContent.ToLower().Contains("debug"))
                {
                    exitCode = TagTestResult.ExitCode.TestPassed;
                }
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }
        public void BasicZipReadDiff_Pass()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"zipped\main.zip"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none", //allow source under unittest path
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            //because these are static and each test is meant to be indpendent null assign the references to create the log
            WriteOnce.Log = null;
            Utils.Logger  = null;

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }
        public void RulesPresent_Fail()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myfakerule.json"),
                FilePathExclusions = "none", //allow source under unittest path
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            //because these are static and each test is meant to be indpendent null assign the references to create the log
            WriteOnce.Log = null;
            Utils.Logger  = null;

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestFailed);
        }
        public void InsecureLogPath_Fail()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none",
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
            }

            //because these are static and each test is meant to be indpendent null assign the references to create the log
            WriteOnce.Log = null;
            Utils.Logger  = null;

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.CriticalError);
        }
        public void InvalidLogPath_Fail()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none", //allow source under unittest path
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\logdebug.txt"),
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            //because these are static and each test is meant to be indpendent null assign the references to create the log
            WriteOnce.Log = null;
            Utils.Logger  = null;

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug?
        }
        public void NoConsoleOutput_Pass()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath            = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath       = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions    = "none",
                ConsoleVerbosityLevel = "none"
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                // Attempt to open output file.
                using (var writer = new StreamWriter(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"consoleout.txt")))
                {
                    // Redirect standard output from the console to the output file.
                    Console.SetOut(writer);

                    TagTestCommand command = new TagTestCommand(options);
                    TagTestResult  result  = command.GetResult();
                    exitCode = result.ResultCode;
                    try
                    {
                        string testContent = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"consoleout.txt"));
                        if (String.IsNullOrEmpty(testContent))
                        {
                            exitCode = TagTestResult.ExitCode.TestPassed;
                        }
                        else
                        {
                            exitCode = TagTestResult.ExitCode.TestFailed;
                        }
                    }
                    catch (Exception)
                    {
                        exitCode = TagTestResult.ExitCode.TestPassed;//no console output file found
                    }
                }
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            //reset to normal
            var standardOutput = new StreamWriter(Console.OpenStandardOutput());

            standardOutput.AutoFlush = true;
            Console.SetOut(standardOutput);

            //because these are static and each test is meant to be indpendent null assign the references to create the log
            WriteOnce.Log = null;
            Utils.Logger  = null;

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }
        public override void WriteResults(Result result, CLICommandOptions commandOptions, bool autoClose = true)
        {
            CLITagTestCmdOptions cLITagTestCmdOptions = (CLITagTestCmdOptions)commandOptions;
            TagTestResult        tagTestResult        = (TagTestResult)result;

            //For console output, update write once for same results to console or file
            WriteOnce.TextWriter = TextWriter;

            if (string.IsNullOrEmpty(commandOptions.OutputFilePath))
            {
                WriteOnce.Result("Results");
            }

            WriteOnce.General(MsgHelp.FormatString(MsgHelp.ID.TAGTEST_RESULTS_TEST_TYPE, cLITagTestCmdOptions.TestType), false, WriteOnce.ConsoleVerbosity.Low);

            if (tagTestResult.ResultCode == TagTestResult.ExitCode.TestFailed)
            {
                WriteOnce.Any(MsgHelp.GetString(MsgHelp.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low);
            }
            else
            {
                WriteOnce.Any(MsgHelp.GetString(MsgHelp.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low);
            }

            if (tagTestResult.TagsStatusList.Count > 0)
            {
                WriteOnce.Result("Test results:");

                foreach (TagStatus tag in tagTestResult.TagsStatusList)
                {
                    WriteOnce.General(string.Format("Tag: {0}, Detected: {1}", tag.Tag, tag.Detected));
                }
            }

            WriteOnce.NewLine();

            if (autoClose)
            {
                FlushAndClose();
            }
        }
Esempio n. 9
0
        private static int RunTagTestCommand(CLITagTestCmdOptions cliOptions)
        {
            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;

            TagTestCommand command = new TagTestCommand(new TagTestOptions()
            {
                SourcePath            = cliOptions.SourcePath,
                CustomRulesPath       = cliOptions.CustomRulesPath,
                FilePathExclusions    = cliOptions.FilePathExclusions,
                TestType              = cliOptions.TestType,
                ConsoleVerbosityLevel = cliOptions.ConsoleVerbosityLevel,
                Log = cliOptions.Log
            });

            TagTestResult tagTestCommand = command.GetResult();

            exitCode = tagTestCommand.ResultCode;
            ResultsWriter.Write(tagTestCommand, cliOptions);

            return((int)exitCode);
        }
Esempio n. 10
0
        public void NoRules_Fail()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.CriticalError);
        }
        public void LogTraceLevel_Pass()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none", //allow source under unittest path
                LogFileLevel       = "trace",
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logtrace.txt"),
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;

                string testLogContent = File.ReadAllText(options.LogFilePath);
                if (String.IsNullOrEmpty(testLogContent))
                {
                    exitCode = TagTestResult.ExitCode.CriticalError;
                }
                else if (testLogContent.ToLower().Contains("trace"))
                {
                    exitCode = TagTestResult.ExitCode.TestPassed;
                }
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            //because these are static and each test is meant to be indpendent null assign the references to create the log
            WriteOnce.Log = null;
            Utils.Logger  = null;

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }
Esempio n. 12
0
        public void BasicZipReadDiff_Pass()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"zipped\main.zip"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none", //allow source under unittest path
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }
Esempio n. 13
0
        public void InsecureLogPath_Fail()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none",
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.CriticalError);
        }
Esempio n. 14
0
        public void InvalidLogPath_Fail()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                FilePathExclusions = "none", //allow source under unittest path
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\logdebug.txt"),
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = TagTestResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug?
        }
Esempio n. 15
0
        public void RulesNotPresentNoResults_Success()
        {
            TagTestOptions options = new TagTestOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"),
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                TestType           = "RulesNotPresent",
                FilePathExclusions = "none", //allow source under unittest path
            };

            TagTestResult.ExitCode exitCode = TagTestResult.ExitCode.CriticalError;
            try
            {
                TagTestCommand command = new TagTestCommand(options);
                TagTestResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == TagTestResult.ExitCode.TestPassed);
        }