public void CustomRules_Pass()
        {
            VerifyRulesOptions options = new VerifyRulesOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
            };

            VerifyRulesResult.ExitCode exitCode = VerifyRulesResult.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                VerifyRulesResult  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 == VerifyRulesResult.ExitCode.Verified);
        }
        public void InsecureLogPath_Fail()
        {
            VerifyRulesOptions options = new VerifyRulesOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp")
            };

            VerifyRulesResult.ExitCode exitCode = VerifyRulesResult.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                VerifyRulesResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = VerifyRulesResult.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 == VerifyRulesResult.ExitCode.CriticalError);
        }
        [Ignore] //default option won't find rules unless run from CLI; todo to look at addressed
        public void DefaultRules_Pass()
        {
            VerifyRulesOptions options = new VerifyRulesOptions()
            {
                VerifyDefaultRules = true
            };

            VerifyRulesResult.ExitCode exitCode = VerifyRulesResult.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                VerifyRulesResult  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 == VerifyRulesResult.ExitCode.Verified);
        }
        public void LogErrorLevel_Pass()
        {
            VerifyRulesOptions options = new VerifyRulesOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"mybadrule.json"),
                LogFileLevel    = "error",
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logerror.txt"),
            };

            VerifyRulesResult.ExitCode exitCode = VerifyRulesResult.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                VerifyRulesResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                string testLogContent = File.ReadAllText(options.LogFilePath);
                if (!String.IsNullOrEmpty(testLogContent) && testLogContent.ToLower().Contains("error"))
                {
                    exitCode = VerifyRulesResult.ExitCode.Verified;
                }
                else
                {
                    exitCode = VerifyRulesResult.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 == VerifyRulesResult.ExitCode.Verified);
        }
        public void InvalidLogPath_Fail()
        {
            VerifyRulesOptions options = new VerifyRulesOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\logdebug.txt"),
            };

            VerifyRulesResult.ExitCode exitCode = VerifyRulesResult.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                VerifyRulesResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = VerifyRulesResult.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 == VerifyRulesResult.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug?
        }
        public void LogDebugLevel_Pass()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                OutputFilePath  = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"verifyrules.txt"),
                LogFileLevel    = "debug",
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logdebug.txt"),
            };

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

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.Verified);
        }
        public void LogErrorLevel_Pass()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                OutputFilePath  = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\verifyRules.txt"),
                LogFileLevel    = "error",
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logerror.txt"),
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                command.Run();
            }
            catch (Exception)
            {
                string testLogContent = File.ReadAllText(options.LogFilePath);
                if (!String.IsNullOrEmpty(testLogContent) && testLogContent.ToLower().Contains("error"))
                {
                    exitCode = VerifyRulesCommand.ExitCode.Verified;
                }
                else
                {
                    exitCode = VerifyRulesCommand.ExitCode.CriticalError;
                }
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.Verified);
        }
        public void NoConsoleOutputFileOutput_Pass()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath       = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                OutputFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"verifyrules.txt"),
                ConsoleVerbosityLevel = "none"
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.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);

                    VerifyRulesCommand command = new VerifyRulesCommand(options);
                    exitCode = (VerifyRulesCommand.ExitCode)command.Run();
                    try
                    {
                        string testContent = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"consoleout.txt"));
                        if (String.IsNullOrEmpty(testContent))
                        {
                            exitCode = VerifyRulesCommand.ExitCode.Verified;
                        }
                        else
                        {
                            exitCode = VerifyRulesCommand.ExitCode.NotVerified;
                        }
                    }
                    catch (Exception)
                    {
                        exitCode = VerifyRulesCommand.ExitCode.Verified;//no console output file found
                    }
                }
            }
            catch (Exception e)
            {
                exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            }

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

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

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.Verified);
        }
        public void NoDefaultNoCustomRules_Fail()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.CriticalError);
        }
        public void CustomRules_Pass()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.Verified);
        }
        [Ignore] //default option won't find rules unless run from CLI; todo to look at addressed
        public void DefaultRules_Pass()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                VerifyDefaultRules = true
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.Verified);
        }
Esempio n. 12
0
        private static int RunVerifyRulesCommand(CLIVerifyRulesCmdOptions cliOptions)
        {
            VerifyRulesResult.ExitCode exitCode = VerifyRulesResult.ExitCode.CriticalError;

            VerifyRulesCommand command = new VerifyRulesCommand(new VerifyRulesOptions()
            {
                VerifyDefaultRules    = cliOptions.VerifyDefaultRules,
                CustomRulesPath       = cliOptions.CustomRulesPath,
                ConsoleVerbosityLevel = cliOptions.ConsoleVerbosityLevel,
                Failfast = cliOptions.Failfast,
                Log      = cliOptions.Log
            });

            VerifyRulesResult exportTagsResult = command.GetResult();

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

            return((int)exitCode);
        }
        public void VerifyRulesToFile_Fail()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                OutputFilePath  = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\verifyRules.txt")
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.CriticalError);
        }
        [Ignore]//another faulty fail that passes when run individually...MSTest flaw?
        public void InsecureLogPath_Fail()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                OutputFilePath  = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"verifyrules.txt"),
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json")
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.CriticalError);
        }
        public void InvalidLogPath_Fail()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                OutputFilePath  = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"verifyrules.txt"),
                LogFilePath     = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\logdebug.txt"),
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug?
        }
        public void NoOutputSelected_Fail()
        {
            VerifyRulesCommandOptions options = new VerifyRulesCommandOptions()
            {
                CustomRulesPath       = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
                ConsoleVerbosityLevel = "none",
                //OutputFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"verifyrules.txt"),
                LogFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"log.txt")
            };

            VerifyRulesCommand.ExitCode exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            try
            {
                VerifyRulesCommand command = new VerifyRulesCommand(options);
                exitCode = (VerifyRulesCommand.ExitCode)command.Run();
            }
            catch (Exception)
            {
                exitCode = VerifyRulesCommand.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == VerifyRulesCommand.ExitCode.CriticalError);
        }