/// <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 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 = MiniMagic.DetectFileType(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"));
            }

            return(_appProfile.MatchList.Count() == 0 ? (int)ExitCode.NoMatches :
                   (int)ExitCode.MatchesFound);
        }
        /// <summary>
        /// Wrapper for files that are on disk and ready to read vs unzipped files which are not to allow separation of core
        /// scan evaluation for use by decompression methods as well
        /// </summary>
        /// <param name="filename"></param>
        void ProcessAsFile(string filename)
        {
            //check for supported language
            LanguageInfo languageInfo = new LanguageInfo();

            if (FileChecksPassed(filename, ref languageInfo))
            {
                LastUpdated = File.GetLastWriteTime(filename);
                _appProfile.MetaData.PackageTypes.Add(ErrMsg.GetString(ErrMsg.ID.ANALYZE_UNCOMPRESSED_FILETYPE));

                string fileText = File.ReadAllText(filename);
                ProcessInMemory(filename, fileText, languageInfo);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Wrapper for files that are on disk and ready to read to allow separation of core
        /// scan evaluation for use by decompression methods as well
        /// </summary>
        /// <param name="filename"></param>
        void ProcessAsFile(string filename)
        {
            if (File.Exists(filename))
            {
                _appProfile.MetaData.FileNames.Add(filename);
                _appProfile.MetaData.PackageTypes.Add(ErrMsg.GetString(ErrMsg.ID.ANALYZE_UNCOMPRESSED_FILETYPE));

                string fileText = File.ReadAllText(filename);
                ProcessInMemory(filename, fileText);
            }
            else
            {
                throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_FILE_OR_DIR, filename));
            }
        }
Beispiel #4
0
 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));
         }
     }
 }
Beispiel #5
0
        void UnZipAndProcess(string filename, ArchiveFileType archiveFileType)
        {
            //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 for supported language
                        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
                {
                    throw new OpException(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_COMPRESSED_ERROR, filename));
                }
            }
            catch (Exception e)
            {
                string errmsg = ErrMsg.FormatString(ErrMsg.ID.ANALYZE_COMPRESSED_ERROR, filename);
                WriteOnce.Error(errmsg);
                throw new Exception(errmsg + e.Message + "\n" + e.StackTrace);
            }
        }
        void ConfigRules()
        {
            _rulePaths = new List <string>();
            if (!_arg_ignoreDefaultRules)
            {
                _rulePaths.Add(Utils.GetPath(Utils.AppPath.defaultRules));
            }

            if (!string.IsNullOrEmpty(_arg_customRulesPath))
            {
                _rulePaths.Add(_arg_customRulesPath);
            }

            if (_rulePaths.Count == 0)
            {
                throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_NORULES_SPECIFIED));
            }

            //validation of paths is delayed for Run in this cmd
        }
        bool CompareTags(string[] fileTags1, string[] fileTags2)
        {
            bool found = true;

            //are all tags in file1 found in file2
            foreach (string s1 in fileTags1)
            {
                if (!fileTags2.Contains(s1))
                {
                    found = false;
                    WriteOnce.Result(s1, true, WriteOnce.ConsoleVerbosity.High);
                }
            }

            //none missing
            if (found)
            {
                WriteOnce.Result(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_NONE), true, WriteOnce.ConsoleVerbosity.High);
            }

            return(found);
        }
        /// <summary>
        /// Wrapper for files that are on disk and ready to read to allow separation of core
        /// scan evaluation for use by decompression methods as well
        /// </summary>
        /// <param name="filename"></param>
        void ProcessAsFile(string filename)
        {
            try
            {
                if (new FileInfo(filename).Length > MAX_FILESIZE)
                {
                    WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILESIZE_SKIPPED, filename), LogLevel.Warn);
                    _appProfile.MetaData.FilesSkipped++;
                    return;
                }
            }
            catch (Exception)
            {
                throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_FILE_OR_DIR, filename));
            }


            _appProfile.MetaData.FileNames.Add(filename);
            _appProfile.MetaData.PackageTypes.Add(ErrMsg.GetString(ErrMsg.ID.ANALYZE_UNCOMPRESSED_FILETYPE));
            string fileText = File.ReadAllText(filename);

            ProcessInMemory(filename, fileText);
        }
        void ConfigRules()
        {
            List <string> rulePaths = new List <string>();

            if (!_arg_ignoreDefaultRules)
            {
                rulePaths.Add(Utils.GetPath(Utils.AppPath.defaultRules));
            }

            if (!string.IsNullOrEmpty(_arg_customRulesPath))
            {
                rulePaths.Add(_arg_customRulesPath);
            }

            foreach (string rulePath in rulePaths)
            {
                if (Directory.Exists(rulePath))
                {
                    _rules.AddDirectory(rulePath);
                }
                else if (File.Exists(rulePath))
                {
                    _rules.AddFile(rulePath);
                }
                else
                {
                    throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_RULE_PATH, rulePath));
                }
            }

            //error check based on ruleset not path enumeration
            if (_rules.Count() == 0)
            {
                throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_NORULES_SPECIFIED));
            }
        }
Beispiel #10
0
        /// <summary>
        /// Add default and/or custom rules paths
        /// Iterate paths and add to ruleset
        /// </summary>
        void ConfigRules()
        {
            WriteOnce.SafeLog("AnalyzeCommand::ConfigRules", LogLevel.Trace);

            RuleSet       rulesSet  = new RuleSet(Program.Logger);
            List <string> rulePaths = new List <string>();

            if (!_arg_ignoreDefaultRules)
            {
                rulePaths.Add(Utils.GetPath(Utils.AppPath.defaultRules));
            }

            if (!string.IsNullOrEmpty(_arg_customRulesPath))
            {
                rulePaths.Add(_arg_customRulesPath);
            }

            foreach (string rulePath in rulePaths)
            {
                if (Directory.Exists(rulePath))
                {
                    rulesSet.AddDirectory(rulePath);
                }
                else if (File.Exists(rulePath))
                {
                    rulesSet.AddFile(rulePath);
                }
                else
                {
                    throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_RULE_PATH, rulePath));
                }
            }

            //error check based on ruleset not path enumeration
            if (rulesSet.Count() == 0)
            {
                throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_NORULES_SPECIFIED));
            }

            //instantiate a RuleProcessor with the added rules and exception for dependency
            _rulesProcessor = new RuleProcessor(rulesSet, _arg_confidence, _arg_outputUniqueTagsOnly, _arg_simpleTagsOnly, Program.Logger);

            if (_arg_outputUniqueTagsOnly)
            {
                List <TagException> tagExceptions;
                if (File.Exists(Utils.GetPath(Utils.AppPath.tagCounterPref)))
                {
                    tagExceptions = JsonConvert.DeserializeObject <List <TagException> >(File.ReadAllText(Utils.GetPath(Utils.AppPath.tagCounterPref)));
                    string[] exceptions = new string[tagExceptions.Count];
                    for (int i = 0; i < tagExceptions.Count; i++)
                    {
                        exceptions[i] = tagExceptions[i].Tag;
                    }
                    _rulesProcessor.UniqueTagExceptions = exceptions;
                }
            }

            _appProfile      = new AppProfile(_arg_sourcePath, rulePaths, false, _arg_simpleTagsOnly, _arg_outputUniqueTagsOnly);
            _appProfile.Args = "analyze -f " + _arg_fileFormat + " -u " + _arg_outputUniqueTagsOnly.ToString().ToLower() + " -v " +
                               WriteOnce.Verbosity.ToString() + " -x " + _arg_confidence + " -i " + _arg_ignoreDefaultRules.ToString().ToLower();
        }
        public int Run()
        {
            bool issues = false;

            WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Verify Rules"));

            //load [each] rules file separately to report out where a failure is happening
            RuleSet rules = new RuleSet(WriteOnce.Log);
            IEnumerable <string> fileListing = new List <string>();

            foreach (string rulePath in _rulePaths)
            {
                if (Directory.Exists(rulePath))
                {
                    fileListing = Directory.EnumerateFiles(rulePath, "*.json", SearchOption.AllDirectories);
                }
                else if (File.Exists(rulePath) && Path.GetExtension(rulePath) == ".json")
                {
                    fileListing = new List <string>()
                    {
                        new string(rulePath)
                    }
                }
                ;
                else
                {
                    throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_RULE_PATH, rulePath));
                }

                //test loading each file
                foreach (string filename in fileListing)
                {
                    try
                    {
                        rules.AddFile(filename);
                        WriteOnce.Info(string.Format("Rule file added {0}", filename), true, WriteOnce.ConsoleVerbosity.High);
                    }
                    catch (Exception e)
                    {
                        WriteOnce.Error(string.Format("Rule file add failed {0}", filename));
                        WriteOnce.SafeLog(e.Message + "\n" + e.StackTrace, NLog.LogLevel.Error);
                        issues = true;
                    }
                }
            }

            //option to write validating data
            if (_arg_consoleVerbosityLevel == WriteOnce.ConsoleVerbosity.High)
            {
                WritePartialRuleDetails(rules);
            }

            //final status report
            if (issues)
            {
                WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.VERIFY_RULES_RESULTS_FAIL), true, ConsoleColor.Red, WriteOnce.ConsoleVerbosity.Low);
            }
            else
            {
                WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.VERIFY_RULES_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low);
            }

            WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Verify Rules"));
            WriteOnce.FlushAll();
            if (!String.IsNullOrEmpty(_arg_outputFile))
            {
                WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low);
            }

            return(issues ? (int)ExitCode.NotVerified : (int)ExitCode.Verified);
        }
        /// <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 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)
            {
                //exclude sample, test or similar files by default or as specified in exclusion list
                if (!_arg_allowSampleFiles && _fileExclusionList.Any(v => filename.ToLower().Contains(v)))
                {
                    WriteOnce.SafeLog("Part of excluded list: " + filename, LogLevel.Trace);
                    WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILESIZE_SKIPPED, filename), LogLevel.Trace);
                    _appProfile.MetaData.FilesSkipped++;
                    continue;
                }

                ArchiveFileType archiveFileType = MiniMagic.DetectFileType(filename);
                if (archiveFileType == ArchiveFileType.UNKNOWN)
                {
                    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"));
            }

            return(_appProfile.MatchList.Count() == 0 ? (int)ExitCode.NoMatches :
                   (int)ExitCode.MatchesFound);
        }
        readonly int MAX_HTML_REPORT_FILE_SIZE = 1024 * 1000 * 3;  //warn about potential slow rendering

        /// <summary>
        /// Registers datatypes with html framework liquid and sets up data for use within it and used
        /// with html partial.liquid files that are embedded as resources
        /// </summary>
        /// <param name="app"></param>
        public override void WriteApp(AppProfile app)
        {
            var      htmlTemplateText = File.ReadAllText(Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "html/index.html"));
            Assembly test             = Assembly.GetEntryAssembly();

            Template.FileSystem = new EmbeddedFileSystem(Assembly.GetEntryAssembly(), "ApplicationInspector.html.partials");

            RegisterSafeType(typeof(AppProfile));
            RegisterSafeType(typeof(AppMetaData));

            var htmlTemplate = Template.Parse(htmlTemplateText);
            var data         = new Dictionary <string, object>();

            data["AppProfile"] = app;

            //matchitems rather than records created to exclude full rule/patterns/cond.
            List <MatchItems> matches = new List <MatchItems>();

            foreach (MatchRecord match in app.MatchList)
            {
                MatchItems matchItem = new MatchItems(match);
                matches.Add(matchItem);
            }

            data["matchDetails"] = matches;

            var hashData = new Hash();

            hashData["json"] = JsonConvert.SerializeObject(data, Formatting.Indented);
            hashData["application_version"] = Program.GetVersionString();

            //add dynamic sets of groups of taginfo read from preferences for Profile page
            List <TagGroup> tagGroupList = app.GetCategoryTagGroups("profile");

            hashData["groups"] = tagGroupList;

            //add summary values for sorted tags lists of taginfo
            foreach (string outerKey in app.KeyedSortedTagInfoLists.Keys)
            {
                hashData.Add(outerKey, app.KeyedSortedTagInfoLists[outerKey]);
            }

            //add summary metadata lists
            hashData["cputargets"]   = app.MetaData.CPUTargets;
            hashData["apptypes"]     = app.MetaData.AppTypes;
            hashData["packagetypes"] = app.MetaData.PackageTypes;
            hashData["ostargets"]    = app.MetaData.OSTargets;
            hashData["outputs"]      = app.MetaData.Outputs;
            hashData["filetypes"]    = app.MetaData.FileExtensions;
            hashData["tagcounters"]  = app.MetaData.TagCountersUI;

            var    htmlResult         = htmlTemplate.Render(hashData);
            string htmlOutputFilePath = Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "output.html");

            File.WriteAllText(htmlOutputFilePath, htmlResult);

            //writes out json report for convenience and linking to from report page(s)
            String jsonReportPath = Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "output.json");
            Writer jsonWriter     = WriterFactory.GetWriter("json", jsonReportPath);

            jsonWriter.TextWriter = File.CreateText(jsonReportPath);
            jsonWriter.WriteApp(app);
            jsonWriter.FlushAndClose();

            //html report size warning
            string outputHTMLPath = Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "output.html");

            if (File.Exists(outputHTMLPath) && new FileInfo(outputHTMLPath).Length > MAX_HTML_REPORT_FILE_SIZE)
            {
                WriteOnce.Info(ErrMsg.GetString(ErrMsg.ID.ANALYZE_REPORTSIZE_WARN));
            }

            Utils.OpenBrowser(htmlOutputFilePath);
        }
        public 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(Program.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);
        }
Beispiel #15
0
        public 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,
                    ConsoleVerbosityLevel = "None"
                });


                //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);
        }