void UnZipAndProcess(string filename, ArchiveFileType archiveFileType) { // zip itself may be in excluded list i.e. sample, test or similar unless ignore filter requested if (_fileExclusionList != null && _fileExclusionList.Any(v => filename.ToLower().Contains(v))) { WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_EXCLUDED_TYPE_SKIPPED, filename), LogLevel.Warn); _appProfile.MetaData.FilesSkipped++; return; } //zip itself may be too huge for timely processing if (new FileInfo(filename).Length > WARN_ZIP_FILE_SIZE) { WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_COMPRESSED_FILESIZE_WARN)); } else { WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_COMPRESSED_PROCESSING)); } LastUpdated = File.GetLastWriteTime(filename); _appProfile.MetaData.PackageTypes.Add(ErrMsg.GetString(ErrMsg.ID.ANALYZE_COMPRESSED_FILETYPE)); try { IEnumerable <FileEntry> files = Extractor.ExtractFile(filename); if (files.Count() > 0) { _appProfile.MetaData.TotalFiles += files.Count();//additive in case additional child zip files processed foreach (FileEntry file in files) { //check uncompressed file passes standard checks LanguageInfo languageInfo = new LanguageInfo(); if (FileChecksPassed(file.FullPath, ref languageInfo, file.Content.Length)) { byte[] streamByteArray = file.Content.ToArray(); ProcessInMemory(file.FullPath, Encoding.UTF8.GetString(streamByteArray, 0, streamByteArray.Length), languageInfo); } } } else { WriteOnce.SafeLog(string.Format("Decompression found no files in {0}", filename), LogLevel.Warn);//zero results can be valid } } catch (Exception) { string errmsg = ErrMsg.FormatString(ErrMsg.ID.ANALYZE_COMPRESSED_ERROR, filename); WriteOnce.Error(errmsg); throw; } }
public static void OpenBrowser(string?url) { if (string.IsNullOrEmpty(url)) { WriteOnce.Log?.Error("Bad url for OpenBrowser method"); return; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")); WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_FAIL));//soft error } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BROWSER"))) { try { Process.Start("xdg-open", "\"" + url + "\""); WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_FAIL));//soft error } } else { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_ENVIRONMENT_VAR)); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { try { Process.Start("open", "\"" + url + "\""); WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_FAIL));//soft error } } }
public static void OpenBrowser(string url) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")); WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.BROWSER_START_FAIL)); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BROWSER"))) { try { Process.Start("xdg-open", "\"" + url + "\""); WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.SafeLog("Unable to open browser using BROWSER environment var", NLog.LogLevel.Error); } } else { WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.BROWSER_ENVIRONMENT_VAR)); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { try { Process.Start("open", "\"" + url + "\""); WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.BROWSER_START_FAIL)); } } }
/// <summary> /// Main WORKHORSE for analyzing file; called from file based or decompression functions /// </summary> /// <param name="filename"></param> /// <param name="fileText"></param> void ProcessInMemory(string filePath, string fileText, LanguageInfo languageInfo) { #region minorRollupTrackingAndProgress WriteOnce.SafeLog("Preparing to process file: " + filePath, LogLevel.Trace); _appProfile.MetaData.FilesAnalyzed++; int totalFilesReviewed = _appProfile.MetaData.FilesAnalyzed + _appProfile.MetaData.FilesSkipped; int percentCompleted = (int)((float)totalFilesReviewed / (float)_appProfile.MetaData.TotalFiles * 100); //earlier issue now resolved so app handles mixed zipped/zipped and unzipped/zipped directories but catch all for non-critical UI if (percentCompleted > 100) { percentCompleted = 100; } else if (percentCompleted < 100) //caller already reports @100% so avoid 2x for file output { WriteOnce.General("\r" + ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILES_PROCESSED_PCNT, percentCompleted), false); } #endregion //process file against rules Issue[] matches = _rulesProcessor.Analyze(fileText, languageInfo); //if any matches found for this file... if (matches.Count() > 0) { _appProfile.MetaData.FilesAffected++; _appProfile.MetaData.TotalMatchesCount += matches.Count(); // Iterate through each match issue foreach (Issue match in matches) { WriteOnce.SafeLog(string.Format("Processing pattern matches for ruleId {0}, ruleName {1} file {2}", match.Rule.Id, match.Rule.Name, filePath), LogLevel.Trace); //maintain a list of unique tags; multi-purpose but primarily for filtering -d option bool dupTagFound = false; foreach (string t in match.Rule.Tags) { dupTagFound = !_uniqueTagsControl.Add(t); } //save all unique dependencies even if Dependency tag pattern is not-unique var tagPatternRegex = new Regex("Dependency.SourceInclude", RegexOptions.IgnoreCase); String textMatch; if (match.Rule.Tags.Any(v => tagPatternRegex.IsMatch(v))) { textMatch = ExtractDependency(fileText, match.Boundary.Index, match.PatternMatch, languageInfo.Name); } else { textMatch = ExtractTextSample(fileText, match.Boundary.Index, match.Boundary.Length); } //wrap rule issue result to add metadata MatchRecord record = new MatchRecord() { Filename = filePath, Language = languageInfo, Filesize = fileText.Length, TextSample = textMatch, Excerpt = ExtractExcerpt(fileText, match.StartLocation.Line), Issue = match }; //preserve issue level characteristics as rolled up meta data of interest bool addAsFeatureMatch = _appProfile.MetaData.AddStandardProperties(ref record); //bail after extracting any dependency unique items IF user requested if (_arg_outputUniqueTagsOnly && dupTagFound) { continue; } else if (addAsFeatureMatch) { _appProfile.MatchList.Add(record); } else { _appProfile.MetaData.TotalMatchesCount -= 1;//reduce e.g. tag counters only as per preferences file } } } else { WriteOnce.SafeLog("No pattern matches detected for file: " + filePath, LogLevel.Trace); } }
/// <summary> /// Main entry point to start analysis from CLI; handles setting up rules, directory enumeration /// file type detection and handoff /// Pre: All Configure Methods have been called already and we are ready to SCAN /// </summary> /// <returns></returns> public override int Run() { WriteOnce.SafeLog("AnalyzeCommand::Run", LogLevel.Trace); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "analyze")); try { _appProfile.MetaData.TotalFiles = _srcfileList.Count();//updated for zipped files later // Iterate through all files and process against rules foreach (string filename in _srcfileList) { ArchiveFileType archiveFileType = ArchiveFileType.UNKNOWN; try //fix for #146 { archiveFileType = MiniMagic.DetectFileType(filename); } catch (Exception e) { WriteOnce.SafeLog(e.Message + "\n" + e.StackTrace, LogLevel.Error); //log details Exception f = new Exception(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILE_TYPE_OPEN, filename)); //report friendly version throw f; } if (archiveFileType == ArchiveFileType.UNKNOWN)//not a known zipped file type { ProcessAsFile(filename); } else { UnZipAndProcess(filename, archiveFileType); } } WriteOnce.General("\r" + ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILES_PROCESSED_PCNT, 100)); WriteOnce.Operation(ErrMsg.GetString(ErrMsg.ID.CMD_PREPARING_REPORT)); //Prepare report results _appProfile.MetaData.LastUpdated = LastUpdated.ToString(); _appProfile.DateScanned = DateScanned.ToString(); _appProfile.PrepareReport(); FlushAll(); //wrapup result status if (_appProfile.MetaData.TotalFiles == _appProfile.MetaData.FilesSkipped) { WriteOnce.Error(ErrMsg.GetString(ErrMsg.ID.ANALYZE_NOSUPPORTED_FILETYPES)); } else if (_appProfile.MatchList.Count == 0) { WriteOnce.Error(ErrMsg.GetString(ErrMsg.ID.ANALYZE_NOPATTERNS)); } else { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "analyze")); } } catch (Exception e) { WriteOnce.Error(e.Message); //exit normaly for CLI callers and throw for DLL callers if (Utils.CLIExecutionContext) { return((int)ExitCode.CriticalError); } else { throw; } } finally { if (_arg_close_log_on_exit) { Utils.Logger = null; WriteOnce.Log = null; } } return(_appProfile.MatchList.Count() == 0 ? (int)ExitCode.NoMatches : (int)Utils.ExitCode.Success); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public override int Run() { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagtest")); //init based on true or false present argument value bool testSuccess = true; //one file vs ruleset string tmp1 = Path.GetTempFileName(); WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeCommand.ExitCode result = AnalyzeCommand.ExitCode.CriticalError; //setup analyze call with silent option #region analyzesetup try { AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_srcPath, OutputFilePath = tmp1, OutputFileFormat = "json", CustomRulesPath = _arg_customRulesPath, IgnoreDefaultRules = _arg_ignoreDefaultRules, SimpleTagsOnly = true, AllowDupTags = false, FilePathExclusions = "sample,example,test,docs,.vs,.git", ConsoleVerbosityLevel = "None", Log = _arg_logger }); //quiet analysis commands result = (AnalyzeCommand.ExitCode)cmd1.Run(); } catch (Exception e) { WriteOnce.Verbosity = saveVerbosity; throw e; } //restore WriteOnce.Verbosity = saveVerbosity; if (result == AnalyzeCommand.ExitCode.CriticalError) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR)); } else if (result == AnalyzeCommand.ExitCode.NoMatches) { //results WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); if (_arg_tagTestType == TagTestType.RulesPresent) { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low); } else { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low); } WriteOnce.FlushAll(); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest")); return((int)ExitCode.CriticalError); } #endregion //assumed (result == AnalyzeCommand.ExitCode.MatchesFound) string file1TagsJson = File.ReadAllText(tmp1); var file1TagsObj = JsonConvert.DeserializeObject <TagsFile[]>(file1TagsJson); var file1Tags = file1TagsObj.First(); // here we have a single FileList object File.Delete(tmp1); foreach (Rule r in _rulesSet) { //supports both directions by generalizing string[] testList1 = _arg_tagTestType == TagTestType.RulesNotPresent ? r.Tags : file1Tags.Tags; string[] testList2 = _arg_tagTestType == TagTestType.RulesNotPresent ? file1Tags.Tags : r.Tags; foreach (string t in testList2) { if (TagTest(testList1, t)) { WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_FOUND, t), true, WriteOnce.ConsoleVerbosity.High); } else { testSuccess = false; WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_MISSING, t), true, WriteOnce.ConsoleVerbosity.High); } } } //results WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); if (testSuccess) { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low); } else { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low); } WriteOnce.FlushAll(); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest")); return(testSuccess ? (int)ExitCode.NoDiff : (int)ExitCode.DiffFound); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public override int Run() { WriteOnce.SafeLog("TagDiffCommand::Run", LogLevel.Trace); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagdiff")); ExitCode exitCode = ExitCode.CriticalError; //save to quiet analyze cmd and restore WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeCommand.ExitCode analyzeCmdResult1 = AnalyzeCommand.ExitCode.CriticalError; AnalyzeCommand.ExitCode analyzeCmdResult2 = AnalyzeCommand.ExitCode.CriticalError; try { #region setup analyze calls string tmp1 = Path.GetTempFileName(); string tmp2 = Path.GetTempFileName(); AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_src1, OutputFilePath = tmp1, OutputFileFormat = "json", CustomRulesPath = _arg_customRulesPath, IgnoreDefaultRules = _arg_ignoreDefault, FilePathExclusions = _arg_fileExclusionList, SimpleTagsOnly = true, ConsoleVerbosityLevel = "none", Log = _arg_logger }); AnalyzeCommand cmd2 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_src2, OutputFilePath = tmp2, OutputFileFormat = "json", CustomRulesPath = _arg_customRulesPath, IgnoreDefaultRules = _arg_ignoreDefault, FilePathExclusions = _arg_fileExclusionList, SimpleTagsOnly = true, ConsoleVerbosityLevel = "none", Log = _arg_logger }); analyzeCmdResult1 = (AnalyzeCommand.ExitCode)cmd1.Run(); analyzeCmdResult2 = (AnalyzeCommand.ExitCode)cmd2.Run(); ConfigureFileOutput(); //restore WriteOnce.Verbosity = saveVerbosity; #endregion bool equalTagsCompare1 = true; bool equalTagsCompare2 = true; //process results for each analyze call before comparing results if (analyzeCmdResult1 == AnalyzeCommand.ExitCode.CriticalError) { throw new Exception(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src1)); } else if (analyzeCmdResult2 == AnalyzeCommand.ExitCode.CriticalError) { throw new Exception(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src2)); } else if (analyzeCmdResult1 == AnalyzeCommand.ExitCode.NoMatches || analyzeCmdResult2 == AnalyzeCommand.ExitCode.NoMatches) { throw new Exception(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_NO_TAGS_FOUND)); } else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.Success) { //setup output here rather than top to avoid analyze command output in this command output ConfigureFileOutput(); ConfigureConsoleOutput();//recheck string file1TagsJson = File.ReadAllText(tmp1); string file2TagsJson = File.ReadAllText(tmp2); var file1Tags = JsonConvert.DeserializeObject <TagsFile>(file1TagsJson); var file2Tags = JsonConvert.DeserializeObject <TagsFile>(file2TagsJson); //can't simply compare counts as content may differ; must compare both in directions in two passes a->b; b->a WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src1), Path.GetFileName(_arg_src2)), true, WriteOnce.ConsoleVerbosity.High); equalTagsCompare1 = CompareTags(file1Tags.Tags, file2Tags.Tags); //reverse order for second pass WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src2), Path.GetFileName(_arg_src1)), true, WriteOnce.ConsoleVerbosity.High); equalTagsCompare2 = CompareTags(file2Tags.Tags, file1Tags.Tags); //final results bool resultsDiffer = !(equalTagsCompare1 && equalTagsCompare2); if (_arg_tagTestType == TagTestType.Inequality && !resultsDiffer) { exitCode = ExitCode.TestFailed; } else if (_arg_tagTestType == TagTestType.Equality && resultsDiffer) { exitCode = ExitCode.TestFailed; } else { exitCode = ExitCode.TestPassed; } WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_DIFFER), false); WriteOnce.Result(resultsDiffer.ToString()); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "tagdiff")); if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext) { WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false); } WriteOnce.FlushAll(); } //cleanup try { File.Delete(tmp1); File.Delete(tmp2); } catch { //no action needed; } } catch (Exception e) { WriteOnce.Verbosity = saveVerbosity; if (analyzeCmdResult1 == AnalyzeCommand.ExitCode.Success && analyzeCmdResult2 == AnalyzeCommand.ExitCode.Success) //error not previously logged { WriteOnce.Error(e.Message); } else { WriteOnce.Error(e.Message, true, WriteOnce.ConsoleVerbosity.Low, false);//console but don't log again } //exit normaly for CLI callers and throw for DLL callers if (Utils.CLIExecutionContext) { return((int)ExitCode.CriticalError); } else { throw; } } finally { if (_arg_close_log_on_exit) { Utils.Logger = null; WriteOnce.Log = null; } } return((int)exitCode); }
public override int Run() { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Tagdiff")); //setup output TextWriter outputWriter; if (!string.IsNullOrEmpty(_arg_outputFile)) { outputWriter = File.CreateText(_arg_outputFile); outputWriter.WriteLine(Utils.GetVersionString()); WriteOnce.Writer = outputWriter; } if (_arg_src1 == _arg_src2) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_SAME_FILE_ARG)); } else if (string.IsNullOrEmpty(_arg_src1) || string.IsNullOrEmpty(_arg_src2)) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_INVALID_ARG_VALUE)); } #region setup analyze calls //save to quiet analyze cmd WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; string tmp1 = Path.GetTempFileName(); string tmp2 = Path.GetTempFileName(); AnalyzeCommand.ExitCode result1 = AnalyzeCommand.ExitCode.CriticalError; AnalyzeCommand.ExitCode result2 = AnalyzeCommand.ExitCode.CriticalError; try { AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_src1, OutputFilePath = tmp1, OutputFileFormat = "json", CustomRulesPath = _arg_rulesPath, IgnoreDefaultRules = _arg_ignoreDefault, SimpleTagsOnly = true, AllowDupTags = false, FilePathExclusions = "sample,example,test,docs,.vs,.git", ConsoleVerbosityLevel = "None" }); AnalyzeCommand cmd2 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_src2, OutputFilePath = tmp2, OutputFileFormat = "json", CustomRulesPath = _arg_rulesPath, IgnoreDefaultRules = _arg_ignoreDefault, SimpleTagsOnly = true, AllowDupTags = false, FilePathExclusions = "sample,example,test,docs,.vs,.git", ConsoleVerbosityLevel = "None" }); result1 = (AnalyzeCommand.ExitCode)cmd1.Run(); result2 = (AnalyzeCommand.ExitCode)cmd2.Run(); } catch (Exception e) { //restore WriteOnce.Verbosity = saveVerbosity; throw e; } //restore WriteOnce.Verbosity = saveVerbosity; #endregion bool successResult; bool equal1 = true; bool equal2 = true; //process results for each analyze call before comparing results if (result1 == AnalyzeCommand.ExitCode.CriticalError) { throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src1)); } else if (result2 == AnalyzeCommand.ExitCode.CriticalError) { throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR, _arg_src2)); } else if (result1 == AnalyzeCommand.ExitCode.NoMatches || result2 == AnalyzeCommand.ExitCode.NoMatches) { throw new OpException(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_NO_TAGS_FOUND)); } else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.MatchesFound) { string file1TagsJson = File.ReadAllText(tmp1); string file2TagsJson = File.ReadAllText(tmp2); var file1Tags = JsonConvert.DeserializeObject <TagsFile[]>(file1TagsJson).First(); var file2Tags = JsonConvert.DeserializeObject <TagsFile[]>(file2TagsJson).First(); //can't simply compare counts as content may differ; must compare both in directions in two passes a->b; b->a //first pass WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src1), Path.GetFileName(_arg_src2)), true, WriteOnce.ConsoleVerbosity.High); equal1 = CompareTags(file1Tags.Tags, file2Tags.Tags); //reverse order for second pass WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_GAP, Path.GetFileName(_arg_src2), Path.GetFileName(_arg_src1)), true, WriteOnce.ConsoleVerbosity.High); equal2 = CompareTags(file2Tags.Tags, file1Tags.Tags); //final results bool resultsDiffer = !(equal1 && equal2); if (_arg_tagTestType == TagTestType.Inequality && resultsDiffer) { successResult = true; } else if (_arg_tagTestType == TagTestType.Equality && !resultsDiffer) { successResult = true; } else { successResult = false; } WriteOnce.General(ErrMsg.GetString(ErrMsg.ID.TAGDIFF_RESULTS_DIFFER), false); WriteOnce.Result(resultsDiffer.ToString()); } //cleanup try { File.Delete(tmp1); File.Delete(tmp2); } catch { //no action needed; } WriteOnce.FlushAll(); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagdiff")); return(successResult ? (int)ExitCode.TestPassed : (int)ExitCode.TestFailed); }
/// <summary> /// Main entry point to start analysis; handles setting up rules, directory enumeration /// file type detection and handoff /// Pre: All Configure Methods have been called already and we are ready to SCAN /// </summary> /// <returns></returns> public override int Run() { WriteOnce.SafeLog("AnalyzeCommand::Run", LogLevel.Trace); DateTime start = DateTime.Now; WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Analyze")); _appProfile.MetaData.TotalFiles = _srcfileList.Count();//updated for zipped files later // Iterate through all files and process against rules foreach (string filename in _srcfileList) { ArchiveFileType archiveFileType; try //fix for #146 { archiveFileType = MiniMagic.DetectFileType(filename); } catch (Exception e) { throw new OpException(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILE_TYPE_OPEN, filename)); } if (archiveFileType == ArchiveFileType.UNKNOWN)//not a known zipped file type { ProcessAsFile(filename); } else { UnZipAndProcess(filename, archiveFileType); } } WriteOnce.General("\r" + ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILES_PROCESSED_PCNT, 100)); WriteOnce.Operation(ErrMsg.GetString(ErrMsg.ID.CMD_PREPARING_REPORT)); //Prepare report results _appProfile.MetaData.LastUpdated = LastUpdated.ToString(); _appProfile.DateScanned = DateScanned.ToString(); _appProfile.PrepareReport(); TimeSpan timeSpan = start - DateTime.Now; WriteOnce.SafeLog(String.Format("Processing time: seconds:{0}", timeSpan.TotalSeconds * -1), LogLevel.Trace); FlushAll(); //wrapup result status if (_appProfile.MetaData.TotalFiles == _appProfile.MetaData.FilesSkipped) { WriteOnce.Error(ErrMsg.GetString(ErrMsg.ID.ANALYZE_NOSUPPORTED_FILETYPES)); } else if (_appProfile.MatchList.Count == 0) { WriteOnce.Error(ErrMsg.GetString(ErrMsg.ID.ANALYZE_NOPATTERNS)); } else { WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Analyze")); if (!_arg_autoBrowserOpen) { WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, "output.html")); } } return(_appProfile.MatchList.Count() == 0 ? (int)ExitCode.NoMatches : (int)ExitCode.Success); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public override int Run() { WriteOnce.SafeLog("TagTestCommand::Run", LogLevel.Trace); WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagtest")); //init based on true or false present argument value ExitCode exitCode = ExitCode.CriticalError; WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeCommand.ExitCode analyzeCmdResult = AnalyzeCommand.ExitCode.CriticalError; try { //one file vs ruleset string tmp1 = Path.GetTempFileName(); //setup analyze call with silent option AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeCommandOptions { SourcePath = _arg_srcPath, OutputFilePath = tmp1, OutputFileFormat = "json", IgnoreDefaultRules = true, CustomRulesPath = _arg_customRulesPath, FilePathExclusions = _arg_fileExclusionList, SimpleTagsOnly = true, ConsoleVerbosityLevel = "None", Log = _arg_logger }); //get and perform initial analyze on results analyzeCmdResult = (AnalyzeCommand.ExitCode)cmd1.Run(); //must be done here to avoid losing our handle from analyze command overwriting WriteOnce.Writer ConfigureFileOutput(); //restore WriteOnce.Verbosity = saveVerbosity; if (analyzeCmdResult == AnalyzeCommand.ExitCode.CriticalError) { throw new Exception(ErrMsg.GetString(ErrMsg.ID.CMD_CRITICAL_FILE_ERR)); } else if (analyzeCmdResult == AnalyzeCommand.ExitCode.NoMatches) { WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); if (_arg_tagTestType == TagTestType.RulesPresent) { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low); } else { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low); } WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "tagtest")); exitCode = _arg_tagTestType == TagTestType.RulesPresent ? ExitCode.TestFailed : ExitCode.TestPassed; } else //assumed (result == AnalyzeCommand.ExitCode.MatchesFound) { string file1TagsJson = File.ReadAllText(tmp1); var file1Tags = JsonConvert.DeserializeObject <TagsFile>(file1TagsJson); File.Delete(tmp1); exitCode = ExitCode.TestPassed; foreach (Rule r in _rulesSet) { //supports both directions by generalizing string[] testList1 = _arg_tagTestType == TagTestType.RulesNotPresent ? r.Tags : file1Tags.Tags; string[] testList2 = _arg_tagTestType == TagTestType.RulesNotPresent ? file1Tags.Tags : r.Tags; foreach (string t in testList2) { if (TagTest(testList1, t)) { WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_FOUND, t), true, WriteOnce.ConsoleVerbosity.High); } else { exitCode = ExitCode.TestFailed; WriteOnce.Result(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TAGS_MISSING, t), true, WriteOnce.ConsoleVerbosity.High); } if (exitCode != ExitCode.TestPassed) { break; } } if (exitCode != ExitCode.TestPassed) { break; } } //results WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); if (exitCode == ExitCode.TestFailed) { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low); } else { WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low); } WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest")); if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext) { WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false); } WriteOnce.FlushAll(); } } catch (Exception e) { WriteOnce.Verbosity = saveVerbosity; if (analyzeCmdResult == AnalyzeCommand.ExitCode.Success) //then error was not previously logged { WriteOnce.Error(e.Message); } else { WriteOnce.Error(e.Message, true, WriteOnce.ConsoleVerbosity.Low, false); } //exit normaly for CLI callers and throw for DLL callers if (Utils.CLIExecutionContext) { return((int)ExitCode.CriticalError); } else { throw; } } finally { if (_arg_close_log_on_exit) { Utils.Logger = null; WriteOnce.Log = null; } } return((int)exitCode); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public TagTestResult GetResult() { WriteOnce.SafeLog("TagTestCommand::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Tag Test")); TagTestResult tagTestResult = new TagTestResult() { AppVersion = Utils.GetVersionString() }; //init based on true or false present argument value WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeResult.ExitCode analyzeCmdResult = AnalyzeResult.ExitCode.CriticalError; try { //setup analyze call with silent option AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath, IgnoreDefaultRules = true, CustomRulesPath = _options.CustomRulesPath, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); //get and perform initial analyze on results AnalyzeResult analyze1 = cmd1.GetResult(); //restore WriteOnce.Verbosity = saveVerbosity; if (analyze1.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR)); } else if (analyzeCmdResult == AnalyzeResult.ExitCode.NoMatches) { WriteOnce.General(MsgHelp.FormatString(MsgHelp.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); tagTestResult.ResultCode = _arg_tagTestType == TagTestType.RulesPresent ? TagTestResult.ExitCode.TestFailed : TagTestResult.ExitCode.TestPassed; } else //assumed (result == AnalyzeCommand.ExitCode.MatchesFound) { tagTestResult.ResultCode = TagTestResult.ExitCode.TestPassed; int count = 0; int sizeTags = analyze1.Metadata.UniqueTags.Count; string[] tagsFound = new string[sizeTags]; foreach (string tag in analyze1.Metadata.UniqueTags.Keys.ToList <string>()) { tagsFound[count++] = tag; } foreach (Rule rule in _rulesSet) { //supports both directions by generalizing string[] testList1 = _arg_tagTestType == TagTestType.RulesNotPresent ? rule.Tags : tagsFound; string[] testList2 = _arg_tagTestType == TagTestType.RulesNotPresent ? tagsFound : rule.Tags; foreach (string t in testList2) { if (TagTest(testList1, t)) { tagTestResult.TagsStatusList.Add(new TagStatus() { Tag = t, Detected = true }); } else { tagTestResult.ResultCode = TagTestResult.ExitCode.TestFailed; tagTestResult.TagsStatusList.Add(new TagStatus() { Tag = t, Detected = false }); } } } } } catch (OpException e) { WriteOnce.Verbosity = saveVerbosity; WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(tagTestResult); }