public void ExpectedTagCountNoDupsAllowed_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, AllowDupTags = false, SimpleTagsOnly = true, OutputFileFormat = "json" }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); string contentResult = command.GetResult(); if (String.IsNullOrEmpty(contentResult)) { exitCode = AnalyzeCommand.ExitCode.NoMatches; } else { var file1Tags = JsonConvert.DeserializeObject <TagsFile>(contentResult); exitCode = file1Tags.Tags.Length == 21 ? AnalyzeCommand.ExitCode.Success : AnalyzeCommand.ExitCode.NoMatches; } } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void ExclusionFilter_Fail() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"), //FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, OutputFileFormat = "text" }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); string contentResult = command.GetResult(); if (String.IsNullOrEmpty(contentResult)) { exitCode = AnalyzeCommand.ExitCode.NoMatches; } else if (contentResult.Contains("Data.Parse.JSON")) //from default and custom rules { exitCode = AnalyzeCommand.ExitCode.Success; } } catch (Exception) { //check for specific error if desired } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.NoMatches); }
public void ExpectedTagCountDupsAllowed_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, AllowDupTags = true, OutputFileFormat = "text", OutputFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt") }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); if (exitCode == AnalyzeCommand.ExitCode.Success) { string[] lines = File.ReadAllLines(options.OutputFilePath); List <string> tags = Helper.GetTagsFromFile(lines); exitCode = tags.Count == 34 ? AnalyzeCommand.ExitCode.Success : AnalyzeCommand.ExitCode.NoMatches; } } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void DefaultAndCustomRulesMatched_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { 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"), SuppressBrowserOpen = true, SimpleTagsOnly = true, OutputFileFormat = "text" }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); string contentResult = command.GetResult(); if (String.IsNullOrEmpty(contentResult)) { exitCode = AnalyzeCommand.ExitCode.NoMatches; } else if (contentResult.Contains("Custom1") && contentResult.Contains("Authentication.General")) //from default and custom rules { exitCode = AnalyzeCommand.ExitCode.Success; } } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void LogDebugLevel_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, LogFileLevel = "debug", LogFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logdebug.txt"), }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); string testLogContent = File.ReadAllText(options.LogFilePath); if (String.IsNullOrEmpty(testLogContent)) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } else if (testLogContent.ToLower().Contains("debug")) { exitCode = AnalyzeCommand.ExitCode.Success; } } catch (Exception) { //check for specific error if desired } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void NoOutputSelected_Fail() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"), FilePathExclusions = "none", //allow source under unittest path OutputFileFormat = "text", SuppressBrowserOpen = true, //OutputFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"), ConsoleVerbosityLevel = "none" //together with no output file = no output at all which is a fail }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { //check for specific error if desired } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.CriticalError); }
public void SimpleTagsJsonOut_JSSrc_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\onetag.js"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, SimpleTagsOnly = true, OutputFileFormat = "json" }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); string contentResult = command.GetResult(); if (String.IsNullOrEmpty(contentResult)) { exitCode = AnalyzeCommand.ExitCode.NoMatches; } else if (contentResult.Contains("Data.Parsing.JSON")) { exitCode = AnalyzeCommand.ExitCode.Success; } } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void LogErrorLevel_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\badfile.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, LogFileLevel = "error", LogFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"logerror.txt"), }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); command.Run(); } catch (Exception) { string testLogContent = File.ReadAllText(options.LogFilePath); if (!String.IsNullOrEmpty(testLogContent) && testLogContent.ToLower().Contains("error")) { exitCode = AnalyzeCommand.ExitCode.Success; } else { exitCode = AnalyzeCommand.ExitCode.CriticalError; } } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
private static int RunAnalyzeCommand(AnalyzeCommandOptions opts) { Logger = Utils.SetupLogging(opts); WriteOnce.Log = Logger; opts.Log = Logger; return(new AnalyzeCommand(opts).Run()); }
public void NoConsoleOutput_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"), FilePathExclusions = "none", //allow source under unittest path OutputFileFormat = "text", OutputFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"output.txt"), SuppressBrowserOpen = true, ConsoleVerbosityLevel = "none" }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.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); exitCode = (AnalyzeCommand.ExitCode)command.Run(); try { string testContent = File.ReadAllText(Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"consoleout.txt")); if (String.IsNullOrEmpty(testContent)) { exitCode = AnalyzeCommand.ExitCode.Success; } else { exitCode = AnalyzeCommand.ExitCode.NoMatches; } } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.Success;//no console output file found } } } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } //reset to normal var standardOutput = new StreamWriter(Console.OpenStandardOutput()); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void BasicZipRead_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"zipped\mainx.zip"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
public void InvalidSourcePath_Fail() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"baddir\main.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = false }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { //check for specific error if desired } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.CriticalError); }
public void NoMatchesFound_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.NoMatches); }
public void DupTagsHTMLOut_Fail() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"), FilePathExclusions = "none", //allow source under unittest path AllowDupTags = true, SuppressBrowserOpen = true }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { //check for specific error if desired } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.CriticalError); }
public void InvalidLogPath_Fail() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { SourcePath = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\main.cpp"), FilePathExclusions = "none", //allow source under unittest path SuppressBrowserOpen = true, LogFilePath = Path.Combine(Helper.GetPath(Helper.AppPath.testOutput), @"baddir\logdebug.txt"), }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.CriticalError);//test fails even when values match unless this case run individually -mstest bug? }
public void DefaultWithCustomRules_Pass() { AnalyzeCommandOptions options = new AnalyzeCommandOptions() { 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"), SuppressBrowserOpen = true }; AnalyzeCommand.ExitCode exitCode = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand command = new AnalyzeCommand(options); exitCode = (AnalyzeCommand.ExitCode)command.Run(); } catch (Exception) { exitCode = AnalyzeCommand.ExitCode.CriticalError; } Assert.IsTrue(exitCode == AnalyzeCommand.ExitCode.Success); }
private static int RunAnalyzeCommand(AnalyzeCommandOptions opts) { SetupLogging(opts); return(new AnalyzeCommand(opts).Run()); }
/// <summary> /// Analyzes a directory of files. /// </summary> /// <param name="directory">directory to analyze.</param> /// <returns>List of tags identified</returns> public async Task <List <string> > AnalyzeDirectory(string directory) { Logger.Trace("AnalyzeDirectory({0})", directory); var analysisResults = new List <string>(); // Call Application Inspector using the NuGet package var options = new AnalyzeCommandOptions() { ConsoleVerbosityLevel = "None", LogFileLevel = "Off", SourcePath = directory, IgnoreDefaultRules = (bool)Options["disable-default-rules"], CustomRulesPath = (string)Options["custom-rule-directory"] }; try { var analyzeCommand = new AnalyzeCommand(options); var resultsJson = analyzeCommand.GetResult(); Logger.Debug("Operation Complete: {0}", resultsJson?.Length); if (resultsJson != default) { var results = await JsonDocument.ParseAsync(new MemoryStream(Encoding.UTF8.GetBytes(resultsJson))); foreach (var matchDetail in results.RootElement.GetProperty("FormattedMatchList").EnumerateArray()) { var sb = new StringBuilder(); sb.AppendFormat("{0}:\n", matchDetail.GetProperty("ruleName").GetString()); sb.AppendFormat("{0}\n", matchDetail.GetProperty("fileName").GetString()); sb.AppendFormat("Excerpt:\n"); var excerpt = Encoding.UTF8.GetString(Convert.FromBase64String(matchDetail.GetProperty("excerpt").GetString())); foreach (var excerptLine in excerpt.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)) { if (!string.IsNullOrEmpty(excerptLine.TrimEnd())) { sb.AppendFormat(" {0}\n", excerptLine.TrimEnd()); } } sb.AppendLine(); foreach (var tag in matchDetail.GetProperty("tags").EnumerateArray()) { analysisResults.Add(tag.GetString()); } //Logger.Info(sb.ToString()); Console.WriteLine(sb.ToString()); // Workaround due to https://github.com/microsoft/ApplicationInspector/issues/179 } } else { throw new Exception("No output retrieved from Application Inspector."); } } catch (Exception ex) { Logger.Warn("Error analyzing {0}: {1}", directory, ex.Message); } return(analysisResults); }