public void LogDebugLevel_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath            = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions    = "none", //allow source under unittest path
                LogFileLevel          = "debug",
                LogFilePath           = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logdebug.txt"),
                CloseLogOnCommandExit = true
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  result  = command.GetResult();
                exitCode = result.ResultCode;
                string testLogContent = File.ReadAllText(options.LogFilePath);
                if (String.IsNullOrEmpty(testLogContent))
                {
                    exitCode = AnalyzeResult.ExitCode.CriticalError;
                }
                else if (testLogContent.ToLower().Contains("debug"))
                {
                    exitCode = AnalyzeResult.ExitCode.Success;
                }
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
示例#2
0
        public void DefaultAndCustomRulesMatched_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json")
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  result  = command.GetResult();
                if (result.Metadata.UniqueTags.Any(v => v.Contains("Authentication.General")) &&
                    result.Metadata.UniqueTags.Any(v => v.Contains("Data.Custom1")))
                {
                    exitCode = AnalyzeResult.ExitCode.Success;
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.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 == AnalyzeResult.ExitCode.Success);
        }
        public void ExclusionFilter_Pass()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\project\one")
                },
                FilePathExclusions = new string[] { "main.cpp" },
                TagsOnly           = true
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.NoMatches);
        }
示例#4
0
        public void BasicZipRead_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"zipped\main.zip"),
                FilePathExclusions = "none", //allow source under unittest path
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  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 == AnalyzeResult.ExitCode.Success);
        }
示例#5
0
        private static int RunAnalyzeCommand(CLIAnalyzeCmdOptions cliOptions)
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;

            AnalyzeCommand command = new AnalyzeCommand(new AnalyzeOptions()
            {
                SourcePath            = cliOptions.SourcePath ?? "",
                CustomRulesPath       = cliOptions.CustomRulesPath ?? "",
                IgnoreDefaultRules    = cliOptions.IgnoreDefaultRules,
                AllowDupTags          = cliOptions.AllowDupTags,
                ConfidenceFilters     = cliOptions.ConfidenceFilters,
                MatchDepth            = cliOptions.MatchDepth,
                FilePathExclusions    = cliOptions.FilePathExclusions,
                ConsoleVerbosityLevel = cliOptions.ConsoleVerbosityLevel,
                Log          = cliOptions.Log,
                SingleThread = cliOptions.SingleThread
            });;

            AnalyzeResult analyzeResult = command.GetResult();

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

            return((int)exitCode);
        }
示例#6
0
        public void InsecureLogPath_Fail()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"),
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  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 == AnalyzeResult.ExitCode.CriticalError);
        }
        public void ExpectedTagCountNoDupsAllowed_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                AllowDupTags       = false
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  result  = command.GetResult();
                exitCode = result.ResultCode;
                if (exitCode == AnalyzeResult.ExitCode.Success)
                {
                    exitCode = result.Metadata.TotalMatchesCount == 7 && result.Metadata.UniqueMatchesCount == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void NoDefaultNoCustomRules_Fail()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp")
                },
                FilePathExclusions = Array.Empty <string>(), //allow source under unittest path
                IgnoreDefaultRules = true,
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.CriticalError);
        }
示例#9
0
        public void LogErrorLevel_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\badfile.cpp"),
                FilePathExclusions = "none", //allow source under unittest path

                LogFileLevel = "error",
                LogFilePath  = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logerror.txt"),
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
            }
            catch (Exception)
            {
                string testLogContent = File.ReadAllText(options.LogFilePath);
                if (!String.IsNullOrEmpty(testLogContent) && testLogContent.ToLower().Contains("error"))
                {
                    exitCode = AnalyzeResult.ExitCode.Success;
                }
                else
                {
                    exitCode = AnalyzeResult.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 == AnalyzeResult.ExitCode.Success);
        }
示例#10
0
        public void NoDefaultCustomRules_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                IgnoreDefaultRules = true,
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  result  = command.GetResult();
                exitCode = result.ResultCode;
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.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 == AnalyzeResult.ExitCode.Success);
        }
        public void NoConsoleOutput_Pass()
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string appInspectorPath = Helper.GetPath(Helper.AppPath.appInspectorCLI);

                string args = string.Format(@"analyze -s {0} -x none -f text -k none -o {1}",
                                            Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                                            Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"log.txt"));


                exitCode = (AnalyzeResult.ExitCode)Helper.RunProcess(appInspectorPath, args, out string testContent);

                if (exitCode == AnalyzeResult.ExitCode.Success)
                {
                    exitCode = String.IsNullOrEmpty(testContent) ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.CriticalError;
                }
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void LogErrorLevel_Pass()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\badfile.cpp")
                },
                FilePathExclusions = Array.Empty <string>(),
                TagsOnly           = true,
                LogFileLevel       = "error",
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logerror.txt"),
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
示例#13
0
        public void InvalidLogPath_Fail()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\log.txt")
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  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 == AnalyzeResult.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug?
        }
        public void ExpectedTagCountNoDupsAllowed_Pass()
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string appInspectorPath = Helper.GetPath(Helper.AppPath.appInspectorCLI);
                string args             = String.Format(@"analyze -s {0} -f json -o {1} -k none",
                                                        Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp"),
                                                        Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));

                exitCode = (AnalyzeResult.ExitCode)Helper.RunProcess(appInspectorPath, args);

                if (exitCode == AnalyzeResult.ExitCode.Success)
                {
                    string content = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));
                    var    result  = JsonConvert.DeserializeObject <AnalyzeResult>(content);
                    exitCode = result.Metadata.TotalMatchesCount == 7 && result.Metadata.UniqueMatchesCount == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void InsecureLogPath_Fail()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp")
                },
                FilePathExclusions = Array.Empty <string>(),
                TagsOnly           = true,
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"),
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.CriticalError);
        }
        public void SimpleTagsJsonOutput_CPPSrc_Pass()
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string appInspectorPath = Helper.GetPath(Helper.AppPath.appInspectorCLI);
                string args             = String.Format(@"analyze -s {0} -f json -k none -o {1} -t",
                                                        Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                                                        Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));

                if (File.Exists(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt")))
                {
                    File.Delete(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));
                }

                exitCode = (AnalyzeResult.ExitCode)Helper.RunProcess(appInspectorPath, args);
                string testContent = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));
                exitCode = testContent.Contains("Authentication.General") ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.CriticalError;
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void InvalidLogPath_Fail()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp")
                },
                FilePathExclusions = Array.Empty <string>(), //allow source under unittest path
                LogFilePath        = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\log.txt"),
                TagsOnly           = true
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug?
        }
        public void DefaultWithCustomRules_Pass()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp")
                },
                FilePathExclusions = Array.Empty <string>(),
                TagsOnly           = true,
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json"),
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void NoMatchesFound_Pass()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp")
                },
                FilePathExclusions = Array.Empty <string>(),
                TagsOnly           = true
            };

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

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.NoMatches);
        }
示例#20
0
        public void ExpectedTagCountNoDupsAllowed_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                AllowDupTags       = false
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  result  = command.GetResult();
                exitCode = result.ResultCode;
                if (exitCode == AnalyzeResult.ExitCode.Success)
                {
                    exitCode = result.Metadata.TotalMatchesCount == 7 && result.Metadata.UniqueMatchesCount == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.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 == AnalyzeResult.ExitCode.Success);
        }
        public void DefaultAndCustomRulesMatched_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"),
                FilePathExclusions = "none", //allow source under unittest path
                CustomRulesPath    = Path.Combine(Helper.GetPath(Helper.AppPath.testRules), @"myrule.json")
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new AnalyzeCommand(options);
                AnalyzeResult  result  = command.GetResult();
                if (result.Metadata.UniqueTags.Keys.Any(v => v.Contains("Authentication.General")) &&
                    result.Metadata.UniqueTags.Keys.Any(v => v.Contains("Data.Custom1")))
                {
                    exitCode = AnalyzeResult.ExitCode.Success;
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void NoConsoleOutput_Pass()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp")
                },
                FilePathExclusions    = Array.Empty <string>(),
                TagsOnly              = true,
                ConsoleVerbosityLevel = "none"
            };

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

                AnalyzeCommand command = new(options);
                AnalyzeResult  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 = AnalyzeResult.ExitCode.Success;
                    }
                    else
                    {
                        exitCode = AnalyzeResult.ExitCode.NoMatches;
                    }
                }
                catch (Exception)
                {
                    exitCode = AnalyzeResult.ExitCode.Success;//no console output file found
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.ExitCode.CriticalError;
            }

            //reset to normal
            StreamWriter standardOutput = new(Console.OpenStandardOutput())
            {
                AutoFlush = true
            };

            Console.SetOut(standardOutput);

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
    }
}
示例#23
0
        public void NoConsoleOutput_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath            = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"),
                FilePathExclusions    = "none", //allow source under unittest path
                ConsoleVerbosityLevel = "none"
            };

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

                    AnalyzeCommand command = new AnalyzeCommand(options);
                    AnalyzeResult  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 = AnalyzeResult.ExitCode.Success;
                        }
                        else
                        {
                            exitCode = AnalyzeResult.ExitCode.NoMatches;
                        }
                    }
                    catch (Exception)
                    {
                        exitCode = AnalyzeResult.ExitCode.Success;//no console output file found
                    }
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.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 == AnalyzeResult.ExitCode.Success);
        }
示例#24
0
        public void InvalidLogPath_Fail()
        {
            string args = string.Format(@"analyze -s {0} -f json -l {1} -g none",
                                        Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\badfile.cpp"),
                                        Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"badir\log.txt"));

            AnalyzeResult.ExitCode exitCode = (AnalyzeResult.ExitCode)Microsoft.ApplicationInspector.CLI.Program.Main(args.Split(' '));

            Assert.AreEqual(AnalyzeResult.ExitCode.CriticalError, exitCode);//test fails even when values match unless this case run individually -mstest bug?
        }
        public void BasicHTMLOutput_Pass()
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string args = string.Format(@"analyze -b -s {0} -f html -k none", Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"));
                exitCode = (AnalyzeResult.ExitCode)Microsoft.ApplicationInspector.CLI.Program.Main(args.Split(' '));
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void ExpectedTagCount()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp")
                },
                FilePathExclusions = Array.Empty <string>(), //allow source under unittest path
                SingleThread       = true,
                TagsOnly           = true
            };

            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                AnalyzeCommand command = new(options);
                AnalyzeResult  result  = command.GetResult();
                exitCode = result.ResultCode;
                if (exitCode == AnalyzeResult.ExitCode.Success)
                {
                    exitCode = result.Metadata.TotalMatchesCount == 0 && result.Metadata.UniqueMatchesCount == 0 && result.Metadata.UniqueTags.Count == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);

            AnalyzeResult.ExitCode exitCodeMultiThread = AnalyzeResult.ExitCode.CriticalError;
            options.SingleThread = false;
            try
            {
                AnalyzeCommand command = new(options);
                AnalyzeResult  result  = command.GetResult();
                exitCodeMultiThread = result.ResultCode;
                if (exitCodeMultiThread == AnalyzeResult.ExitCode.Success)
                {
                    exitCodeMultiThread = result.Metadata.TotalMatchesCount == 0 && result.Metadata.UniqueMatchesCount == 0 && result.Metadata.UniqueTags.Count == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
                exitCodeMultiThread = AnalyzeResult.ExitCode.CriticalError;
            }

            Assert.IsTrue(exitCodeMultiThread == AnalyzeResult.ExitCode.Success);
        }
        public void BasicHTMLOutput_Pass()
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string appInspectorPath = Helper.GetPath(Helper.AppPath.appInspectorCLI);
                string args             = String.Format(@"analyze -b -s {0} -f html -k none", Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"));
                exitCode = (AnalyzeResult.ExitCode)Helper.RunProcess(appInspectorPath, args);
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.Success);
        }
        public void UnknownFormat_Fail() //dupliacte tags not supported for html format
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string args = string.Format(@"analyze -b -s {0} -f unknown -k none", Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"));
                exitCode = (AnalyzeResult.ExitCode)Microsoft.ApplicationInspector.CLI.Program.Main(args.Split(' '));
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

            Assert.IsTrue(exitCode == AnalyzeResult.ExitCode.CriticalError);
        }
        public void ExpectedTagCountDupsAllowed_Pass()
        {
            AnalyzeResult.ExitCode exitCodeSingleThread = AnalyzeResult.ExitCode.CriticalError;

            try
            {
                string args = string.Format(@"analyze -s {0} -d -f json -o {1} -k none --single-threaded",
                                            Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp"),
                                            Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));

                exitCodeSingleThread = (AnalyzeResult.ExitCode)Microsoft.ApplicationInspector.CLI.Program.Main(args.Split(' '));

                if (exitCodeSingleThread == AnalyzeResult.ExitCode.Success)
                {
                    string content = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));
                    var    result  = JsonConvert.DeserializeObject <AnalyzeResult>(content);
                    exitCodeSingleThread = result.Metadata.TotalMatchesCount == 11 && result.Metadata.UniqueMatchesCount == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCodeSingleThread == AnalyzeResult.ExitCode.Success);

            AnalyzeResult.ExitCode exitCodeMultiThread = AnalyzeResult.ExitCode.CriticalError;

            try
            {
                string args = string.Format(@"analyze -s {0} -d -f json -o {1} -k none",
                                            Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp"),
                                            Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));

                exitCodeMultiThread = (AnalyzeResult.ExitCode)Microsoft.ApplicationInspector.CLI.Program.Main(args.Split(' '));

                if (exitCodeMultiThread == AnalyzeResult.ExitCode.Success)
                {
                    string content = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"));
                    var    result  = JsonConvert.DeserializeObject <AnalyzeResult>(content);
                    exitCodeMultiThread = result.Metadata.TotalMatchesCount == 11 && result.Metadata.UniqueMatchesCount == 7 ? AnalyzeResult.ExitCode.Success : AnalyzeResult.ExitCode.NoMatches;
                }
            }
            catch (Exception)
            {
            }

            Assert.IsTrue(exitCodeMultiThread == AnalyzeResult.ExitCode.Success);
        }
        public void UnknownFormat_Fail() //dupliacte tags not supported for html format
        {
            AnalyzeResult.ExitCode exitCode = AnalyzeResult.ExitCode.CriticalError;
            try
            {
                string appInspectorPath = Helper.GetPath(Helper.AppPath.appInspectorCLI);
                string args             = String.Format(@"analyze -b -s {0} -f unknown -k none", Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"));
                exitCode = (AnalyzeResult.ExitCode)Helper.RunProcess(appInspectorPath, args);
            }
            catch (Exception)
            {
                //check for specific error if desired
            }

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