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 LogTraceLevel_Pass()
        {
            AnalyzeOptions options = new AnalyzeOptions()
            {
                SourcePath         = Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\empty.cpp"),
                FilePathExclusions = "none", //allow source under unittest path

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

            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("trace"))
                {
                    exitCode = AnalyzeResult.ExitCode.Success;
                }
            }
            catch (Exception)
            {
                exitCode = AnalyzeResult.ExitCode.CriticalError;
            }

            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);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Analyzes a directory of files.
        /// </summary>
        /// <param name="directory"> directory to analyze. </param>
        /// <returns> List of tags identified </returns>
        public async Task <AnalyzeResult?> AnalyzeDirectory(Options options, string directory)
        {
            Logger.Trace("AnalyzeDirectory({0})", directory);

            AnalyzeResult?analysisResult = null;

            // Call Application Inspector using the NuGet package
            AnalyzeOptions?analyzeOptions = new AnalyzeOptions()
            {
                ConsoleVerbosityLevel    = "None",
                LogFileLevel             = "Off",
                SourcePath               = new[] { directory },
                IgnoreDefaultRules       = options.DisableDefaultRules,
                CustomRulesPath          = options.CustomRuleDirectory,
                ConfidenceFilters        = "high,medium,low",
                ScanUnknownTypes         = true,
                AllowAllTagsInBuildFiles = options.AllowTagsInBuildFiles,
                SingleThread             = false,
                FilePathExclusions       = options.FilePathExclusions?.Split(',') ?? Array.Empty <string>()
            };

            try
            {
                AnalyzeCommand?analyzeCommand = new AnalyzeCommand(analyzeOptions);
                analysisResult = analyzeCommand.GetResult();
                Logger.Debug("Operation Complete: {0} files analyzed.", analysisResult?.Metadata?.TotalFiles);
            }
            catch (Exception ex)
            {
                Logger.Warn("Error analyzing {0}: {1}", directory, ex.Message);
            }

            return(analysisResult);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Analyzes a directory of files.
        /// </summary>
        /// <param name="directory"> directory to analyze. </param>
        /// <returns> List of tags identified </returns>
        public async Task <AnalyzeResult?> AnalyzeDirectory(Options options, string directory)
        {
            Logger.Trace("AnalyzeDirectory({0})", directory);

            AnalyzeResult?analysisResult = null;

            // Call Application Inspector using the NuGet package
            var analyzeOptions = new AnalyzeOptions()
            {
                ConsoleVerbosityLevel = "None",
                LogFileLevel          = "Off",
                SourcePath            = directory,
                IgnoreDefaultRules    = options.DisableDefaultRules == true,
                CustomRulesPath       = options.CustomRuleDirectory,
                ConfidenceFilters     = "high,medium,low",
                TreatEverythingAsCode = options.TreatEverythingAsCode,
                SingleThread          = true
            };

            try
            {
                var analyzeCommand = new AnalyzeCommand(analyzeOptions);
                analysisResult = analyzeCommand.GetResult();
                Logger.Debug("Operation Complete: {0} files analyzed.", analysisResult?.Metadata?.TotalFiles);
            }
            catch (Exception ex)
            {
                Logger.Warn("Error analyzing {0}: {1}", directory, ex.Message);
            }

            return(analysisResult);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        private void NodeAction(TSqlFragment node)
        {
            List <ISkimmer <SqlFileContext> > skimmersList;

            if (node == null)
            {
                return;
            }

            if (this.typeToSkimmersMap.TryGetValue(node.GetType(), out skimmersList))
            {
                foreach (ISqlSkimmer skimmer in skimmersList)
                {
                    this.context.Fragment = node;
                    try
                    {
                        skimmer.Analyze(this.context);
                    }
                    catch (Exception ex)
                    {
                        RuntimeErrors |= AnalyzeCommand.LogUnhandledRuleExceptionAnalyzingTarget(
                            this.disabledSkimmers,
                            this.context,
                            skimmer,
                            ex);
                    }
                }
            }
        }
Ejemplo n.º 19
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?
        }
Ejemplo n.º 20
0
 public AnalyzeViewModel()
 {
     this.currentModel             = new AnalyzeTableModel();
     command                       = new AnalyzeCommand(ViewModel: this);
     this.currentConexao.xLogin    = "******";
     this.currentConexao.xPassword = "******";
 }
Ejemplo n.º 21
0
        public void ExpectedTagCountDupsAllowed_Pass()
        {
            AnalyzeOptions options = new()
            {
                SourcePath = new string[1] {
                    Path.Combine(Helper.GetPath(Helper.AppPath.testSource), @"unzipped\simple\mainduptags.cpp")
                },
                FilePathExclusions = Array.Empty <string>(),
                SingleThread       = true
            };

            AnalyzeCommand command = new(options);
            AnalyzeResult  result  = command.GetResult();

            Assert.AreEqual(AnalyzeResult.ExitCode.Success, result.ResultCode);
            Assert.AreEqual(11, result.Metadata.TotalMatchesCount);
            Assert.AreEqual(7, result.Metadata.UniqueMatchesCount);


            options.SingleThread = false;
            command = new AnalyzeCommand(options);
            result  = command.GetResult();
            Assert.AreEqual(AnalyzeResult.ExitCode.Success, result.ResultCode);
            Assert.AreEqual(11, result.Metadata.TotalMatchesCount);
            Assert.AreEqual(7, result.Metadata.UniqueMatchesCount);
        }
Ejemplo n.º 22
0
        private async Task AnalyzeImagesTest(ICollection images)
        {
            ObservableCollection <StatusResult> tempUploadResult = new ObservableCollection <StatusResult>(UploadResult);

            UploadResult.Clear();
            AnalyzeCommand.RaiseCanExecuteChanged();

            AnalyzeMyImgService  analyzeImageService = new AnalyzeMyImgService();
            ImageDatabaseService imageDbService      = new ImageDatabaseService();

            foreach (StatusResult imageStatusResult in tempUploadResult)
            {
                foreach (ImageFromResult image in imageStatusResult.Images)
                {
                    ImageModel tmpImg = imageDbService.GetImageByImageData(image.OriginalImage.ImageData);
                    if (tmpImg == null)
                    {
                        tmpImg           = image.OriginalImage;
                        tmpImg.ImageTags = await analyzeImageService.RunAsyncAnalyzeImage(image.Id);

                        tmpImg.IsProcessed = true;
                        imageDbService.SaveImageToDatabase(tmpImg);
                    }

                    SortImageInApropriateCollection(tmpImg);
                }
            }
        }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
0
        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);
        }
Ejemplo n.º 26
0
 public ChildCommands(DiffCommand diff, SetupComCommand setupCom, CleanCommand cleanCom, SortCommand sort, GraphCommand graph, AnalyzeCommand analyze)
 {
     Diff     = diff;
     SetupCom = setupCom;
     CleanCom = cleanCom;
     Sort     = sort;
     Graph    = graph;
     Analyze  = analyze;
 }
        protected override bool AnalyzeCore(Uri uri, string text, string solutionDirectory, SarifLogger sarifLogger, CancellationToken cancellationToken)
        {
            if (!SariferOption.Instance.ShouldAnalyzeSarifFile && uri.GetFilePath().EndsWith(".sarif", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(solutionDirectory) ||
                (this.currentSolutionDirectory?.Equals(solutionDirectory, StringComparison.OrdinalIgnoreCase) != true))
            {
                // clear older rules
                this.rules?.Clear();
                this.currentSolutionDirectory = solutionDirectory;

                if (this.currentSolutionDirectory != null)
                {
                    this.rules = LoadSearchDefinitionsFiles(this.fileSystem, this.currentSolutionDirectory);
                    Trace.WriteLine($"Rules loaded: {this.rules.Count}");
                }
            }

            if (this.rules == null || this.rules.Count == 0)
            {
                return(false);
            }

            Trace.WriteLine($"Analyzing {uri}...");

            var disabledSkimmers = new HashSet <string>();

            var context = new AnalyzeContext
            {
                TargetUri         = uri,
                FileContents      = text,
                Logger            = sarifLogger,
                DynamicValidation = true, // Enable dynamic validations.
            };

            using (context)
            {
                cancellationToken.ThrowIfCancellationRequested();

                // clear region cache make sure latest text is cached
                FileRegionsCache.Instance.ClearCache();

                // Filtering file before analyzing.
                IEnumerable <Skimmer <AnalyzeContext> > applicableSkimmers = AnalyzeCommand.DetermineApplicabilityForTargetHelper(context, this.rules, disabledSkimmers);

                Trace.WriteLine($"Applicable rules count: {applicableSkimmers.Count()}");

                AnalyzeCommand.AnalyzeTargetHelper(context, applicableSkimmers, disabledSkimmers);
            }

            Trace.WriteLine($"Analyzing {uri} completed.");

            return(true);
        }
Ejemplo n.º 28
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);
        }
Ejemplo n.º 29
0
        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 AnalyzeMultiThread()
        {
            AnalyzeCommand command = new AnalyzeCommand(new AnalyzeOptions()
            {
                SourcePath         = path,
                AllowDupTags       = false,
                SingleThread       = false,
                IgnoreDefaultRules = false
            });

            AnalyzeResult analyzeResult = command.GetResult();
        }
Ejemplo n.º 31
0
        public RunLog AnalyzeFile(string fileName)
        {
            string path = Path.GetTempFileName();
            RunLog runLog = null;

            try
            {
                var options = new AnalyzeOptions()
                {
                    BinaryFileSpecifiers = new string[] { fileName },
                    Verbose = true,
                    Statistics = true,
                    ComputeTargetsHash = true,
                    PolicyFilePath = "default",
                    Recurse = true,
                    OutputFilePath = path,
                    SymbolsPath = "SRV*http://symweb"
                };

                var command = new AnalyzeCommand();
                int result = command.Run(options);
                Assert.Equal(AnalyzeCommand.SUCCESS, result);

                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ContractResolver = SarifContractResolver.Instance
                };

                IssueLog log = JsonConvert.DeserializeObject<IssueLog>(File.ReadAllText(path), settings);
                Assert.NotNull(log);
                Assert.Equal<int>(1, log.RunLogs.Count);

                runLog = log.RunLogs[0];
            }
            finally
            {
                File.Delete(path);
            }

            return runLog;
        }
Ejemplo n.º 32
0
        private void RunRules(StringBuilder sb, string inputFileName)
        {
            string fileName = Path.GetFileName(inputFileName);
            string actualDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Actual");
            string expectedDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Expected");

            if (!Directory.Exists(actualDirectory))
            {
                Directory.CreateDirectory(actualDirectory);
            }

            string expectedFileName = Path.Combine(expectedDirectory, fileName + ".sarif");
            string actualFileName = Path.Combine(actualDirectory, fileName + ".sarif");

            AnalyzeCommand command = new AnalyzeCommand();
            AnalyzeOptions options = new AnalyzeOptions();

            options.TargetFileSpecifiers = new string[] { inputFileName };
            options.OutputFilePath = actualFileName;
            options.Verbose = true;
            options.Recurse = false;
            options.ComputeTargetsHash = true;
            options.ConfigurationFilePath = "default";

            int result = command.Run(options);

            // Note that we don't ensure a success code. That is because we
            // are running end-to-end tests for valid and invalid files

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ContractResolver = SarifContractResolver.Instance,
                Formatting = Formatting.Indented
            };

            string expectedText = File.ReadAllText(expectedFileName);
            string actualText = File.ReadAllText(actualFileName);

            // Replace repository root absolute path with Z:\ for machine and enlistment independence
            string repoRoot = Path.GetFullPath(Path.Combine(actualDirectory, "..", "..", "..", ".."));
            actualText = actualText.Replace(repoRoot.Replace(@"\", @"\\"), @"Z:");
            actualText = actualText.Replace(repoRoot.Replace(@"\", @"/"), @"Z:");

            // Remove stack traces as they can change due to inlining differences by configuration and runtime.
            actualText = Regex.Replace(actualText, @"\\r\\n   at [^""]+", "");

            // Write back the normalized actual text so that the diff command given on failure shows what was actually compared.
            File.WriteAllText(actualFileName, actualText);

            // Make sure we can successfully deserialize what was just generated
            SarifLog expectedLog = JsonConvert.DeserializeObject<SarifLog>(expectedText, settings);
            SarifLog actualLog = JsonConvert.DeserializeObject<SarifLog>(actualText, settings);

            var visitor = new ResultDiffingVisitor(expectedLog);

            if (!visitor.Diff(actualLog.Runs[0].Results))
            {
                string errorMessage = "The output of the tool did not match for input {0}.";
                sb.AppendLine(String.Format(CultureInfo.CurrentCulture, errorMessage, inputFileName));
                sb.AppendLine("Check differences with:");
                sb.AppendLine(GenerateDiffCommand(expectedFileName, actualFileName));
            }
        }
Ejemplo n.º 33
0
        private void ExceptionTestHelper(
            ExceptionCondition exceptionCondition,
            RuntimeConditions runtimeConditions,
            FailureReason expectedExitReason = FailureReason.ExceptionsDidNotHaltAnalysis,
            AnalyzeOptions analyzeOptions = null)
        {
            AnalyzeCommand.DefaultRuleAssemblies = new Assembly[] { typeof(ExceptionRaisingRule).Assembly };
            ExceptionRaisingRule.s_exceptionCondition = exceptionCondition;
            analyzeOptions = analyzeOptions ?? new AnalyzeOptions()
            {
                BinaryFileSpecifiers = new string[0]
            };

            var command = new AnalyzeCommand();
            int result = command.Run(analyzeOptions);

            int expectedResult =
                (runtimeConditions & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors ?
                    AnalyzeCommand.SUCCESS : AnalyzeCommand.FAILURE;

            Assert.Equal(runtimeConditions, command.RuntimeErrors);
            Assert.Equal(expectedResult, result);

            if (expectedExitReason != FailureReason.ExceptionsDidNotHaltAnalysis)
            {
                Assert.NotNull(command.ExecutionException);

                if (expectedExitReason != FailureReason.UnhandledExceptionInEngine)
                {
                    var eax = command.ExecutionException as ExitApplicationException<FailureReason>;
                    Assert.NotNull(eax);
                }
            }
            else
            {
                Assert.Null(command.ExecutionException);
            }
            ExceptionRaisingRule.s_exceptionCondition = ExceptionCondition.None;
            AnalyzeCommand.DefaultRuleAssemblies = null;
        }
Ejemplo n.º 34
0
        private void RunRules(StringBuilder sb, string inputFileName)
        {
            string fileName = Path.GetFileName(inputFileName);
            string actualDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Actual");
            string expectedDirectory = Path.Combine(Path.GetDirectoryName(inputFileName), "Expected");

            if (!Directory.Exists(actualDirectory))
            {
                Directory.CreateDirectory(actualDirectory);
            }

            string expectedFileName = Path.Combine(expectedDirectory, fileName + ".sarif");
            string actualFileName = Path.Combine(actualDirectory, fileName + ".sarif");

            AnalyzeCommand command = new AnalyzeCommand();
            AnalyzeOptions options = new AnalyzeOptions();

            options.TargetFileSpecifiers = new string[] { inputFileName };
            options.OutputFilePath = actualFileName;
            options.Verbose = true;
            options.Recurse = false;
            options.ConfigurationFilePath = "default";

            int result = command.Run(options);

            // Note that we don't ensure a success code. That is because we
            // are running end-to-end tests for valid and invalid files

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ContractResolver = SarifContractResolver.Instance,
                Formatting = Formatting.Indented
            };

            string expectedText = File.ReadAllText(expectedFileName);
            string actualText = File.ReadAllText(actualFileName);

            // Make sure we can successfully deserialize what was just generated
            ResultLog expectedLog = JsonConvert.DeserializeObject<ResultLog>(expectedText, settings);
            ResultLog actualLog = JsonConvert.DeserializeObject<ResultLog>(actualText, settings);

            var visitor = new ResultDiffingVisitor(expectedLog);

            if (!visitor.Diff(actualLog.RunLogs[0].Results))
            {
                string errorMessage = "The output of the tool did not match for input {0}.";
                sb.AppendLine(String.Format(CultureInfo.CurrentCulture, errorMessage, inputFileName));
                sb.AppendLine("Check differences with:");
                sb.AppendLine(GenerateDiffCommand(expectedFileName, actualFileName));
            }
        }