public void MissingOutputFile() { string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); path = Path.Combine(path, Guid.NewGuid().ToString()); try { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, OutputFilePath = path, Verbose = true, }; // A missing output file is a good condition. :) ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.NoErrors, expectedExitReason: ExitReason.None, analyzeOptions: options); } finally { if (File.Exists(path)) { File.Delete(path); } } }
public void IOExceptionRaisedCreatingSarifLog() { string path = Path.GetTempFileName(); try { using (var stream = File.OpenWrite(path)) { // our log file is locked for write // causing exceptions at analysis time var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, OutputFilePath = path, Verbose = true, }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.ExceptionCreatingLogfile, expectedExitReason: ExitReason.ExceptionCreatingLogFile, analyzeOptions: options); } } finally { File.Delete(path); } }
public void UnauthorizedAccessExceptionCreatingSarifLog() { string path = Environment.GetFolderPath(Environment.SpecialFolder.Windows); path = Path.Combine(path, Guid.NewGuid().ToString()); try { // attempt to persist to unauthorized location will raise exception var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, OutputFilePath = path, Verbose = true, }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.ExceptionCreatingLogfile, expectedExitReason: ExitReason.ExceptionCreatingLogFile, analyzeOptions: options); } finally { File.Delete(path); } }
private void ExceptionTestHelper( ExceptionCondition exceptionCondition, RuntimeConditions runtimeConditions, ExitReason expectedExitReason = ExitReason.None, TestAnalyzeOptions analyzeOptions = null) { ExceptionRaisingRule.s_exceptionCondition = exceptionCondition; analyzeOptions = analyzeOptions ?? new TestAnalyzeOptions() { TargetFileSpecifiers = new string[0] }; var command = new TestAnalyzeCommand(); Assembly[] plugInAssemblies = null; if (analyzeOptions.DefaultPlugInFilePaths != null) { var assemblies = new List <Assembly>(); foreach (string plugInFilePath in analyzeOptions.DefaultPlugInFilePaths) { assemblies.Add(Assembly.LoadFrom(plugInFilePath)); } plugInAssemblies = new Assembly[assemblies.Count]; assemblies.CopyTo(plugInAssemblies, 0); } else { plugInAssemblies = new Assembly[] { typeof(ExceptionRaisingRule).Assembly }; } command.DefaultPlugInAssemblies = plugInAssemblies; int result = command.Run(analyzeOptions); int expectedResult = (runtimeConditions & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors ? TestAnalyzeCommand.SUCCESS : TestAnalyzeCommand.FAILURE; Assert.Equal(runtimeConditions, command.RuntimeErrors); Assert.Equal(expectedResult, result); if (expectedExitReason != ExitReason.None) { Assert.NotNull(command.ExecutionException); if (expectedExitReason != ExitReason.UnhandledExceptionInEngine) { var eax = command.ExecutionException as ExitApplicationException <ExitReason>; Assert.NotNull(eax); } } else { Assert.Null(command.ExecutionException); } ExceptionRaisingRule.s_exceptionCondition = ExceptionCondition.None; }
public void ExceptionRaisedInstantiatingSkimmers() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.InvokingConstructor, RuntimeConditions.ExceptionInstantiatingSkimmers, ExitReason.UnhandledExceptionInstantiatingSkimmers, analyzeOptions: options); }
public void ExceptionLoadingTarget() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, RegardAnalysisTargetAsCorrupted = true }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.ExceptionLoadingTargetFile, analyzeOptions: options); }
public void MissingRequiredConfiguration() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, RegardRequiredConfigurationAsMissing = true }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.RuleMissingRequiredConfiguration, analyzeOptions: options); }
public void InvalidTarget() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, RegardAnalysisTargetAsValid = false }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.TargetNotValidToAnalyze, analyzeOptions: options); }
public void ExceptionRaisedInvokingCanAnalyze() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.InvokingCanAnalyze, RuntimeConditions.ExceptionRaisedInSkimmerCanAnalyze, analyzeOptions: options ); }
public void LoadPdbException() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.LoadingPdb, RuntimeConditions.ExceptionLoadingPdb, analyzeOptions: options ); }
public void ExceptionRaisedInvokingAnalyze() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.InvokingAnalyze, RuntimeConditions.ExceptionInSkimmerAnalyze, analyzeOptions: options ); }
public void NotApplicableToTarget() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, RegardAnalysisTargetAsNotApplicable = true }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.RuleNotApplicableToTarget, analyzeOptions: options); }
public void ParseTargetException() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.ParsingTarget, RuntimeConditions.TargetParseError, analyzeOptions: options ); }
public void InvalidCommandLineOption() { var options = new TestAnalyzeOptions { RegardOptionsAsInvalid = true }; ExceptionTestHelper( ExceptionCondition.ValidatingOptions, RuntimeConditions.InvalidCommandLineOption, ExitReason.InvalidCommandLineOption, options); }
public void FileUri() { Uri uri = new Uri(this.GetType().Assembly.Location); var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { uri.ToString() }, }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.NoErrors, analyzeOptions: options ); }
public void NoRulesLoaded() { var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, DefaultPlugInFilePaths = new string[] { typeof(string).Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.NoRulesLoaded, ExitReason.NoRulesLoaded, analyzeOptions: options ); }
public void ExceptionRaisedInEngine() { TestAnalyzeCommand.RaiseUnhandledExceptionInDriverCode = true; var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.ExceptionInEngine, ExitReason.UnhandledExceptionInEngine); TestAnalyzeCommand.RaiseUnhandledExceptionInDriverCode = false; }
public void MissingPlugInFile() { string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); path = Path.Combine(path, Guid.NewGuid().ToString()); var options = new TestAnalyzeOptions() { TargetFileSpecifiers = new string[] { this.GetType().Assembly.Location }, PlugInFilePaths = new string[] { path }, Verbose = true, }; ExceptionTestHelper( ExceptionCondition.None, RuntimeConditions.MissingFile, expectedExitReason: ExitReason.InvalidCommandLineOption, analyzeOptions: options); }
public RunLog AnalyzeFile(string fileName) { string path = Path.GetTempFileName(); RunLog runLog = null; try { var options = new TestAnalyzeOptions { TargetFileSpecifiers = new string[] { fileName }, Verbose = true, Statistics = true, ComputeTargetsHash = true, PolicyFilePath = "default", Recurse = true, OutputFilePath = path, }; var command = new TestAnalyzeCommand(); command.DefaultPlugInAssemblies = new Assembly[] { this.GetType().Assembly }; int result = command.Run(options); Assert.Equal(TestAnalyzeCommand.SUCCESS, result); JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = SarifContractResolver.Instance }; ResultLog log = JsonConvert.DeserializeObject <ResultLog>(File.ReadAllText(path), settings); Assert.NotNull(log); Assert.Equal <int>(1, log.RunLogs.Count); runLog = log.RunLogs[0]; } finally { File.Delete(path); } return(runLog); }
private void ExceptionTestHelper( ExceptionCondition exceptionCondition, RuntimeConditions runtimeConditions, ExitReason expectedExitReason = ExitReason.None, TestAnalyzeOptions analyzeOptions = null) { ExceptionRaisingRule.s_exceptionCondition = exceptionCondition; analyzeOptions = analyzeOptions ?? new TestAnalyzeOptions() { TargetFileSpecifiers = new string[0] }; var command = new TestAnalyzeCommand(); command.DefaultPlugInAssemblies = new Assembly[] { typeof(ExceptionRaisingRule).Assembly }; int result = command.Run(analyzeOptions); int expectedResult = (runtimeConditions & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors ? TestAnalyzeCommand.SUCCESS : TestAnalyzeCommand.FAILURE; Assert.Equal(runtimeConditions, command.RuntimeErrors); Assert.Equal(expectedResult, result); if (expectedExitReason != ExitReason.None) { Assert.NotNull(command.ExecutionException); if (expectedExitReason != ExitReason.UnhandledExceptionInEngine) { var eax = command.ExecutionException as ExitApplicationException <ExitReason>; Assert.NotNull(eax); } } else { Assert.Null(command.ExecutionException); } ExceptionRaisingRule.s_exceptionCondition = ExceptionCondition.None; }
private void ExceptionTestHelper( ExceptionCondition exceptionCondition, RuntimeConditions runtimeConditions, ExitReason expectedExitReason = ExitReason.None, TestAnalyzeOptions analyzeOptions = null) { ExceptionRaisingRule.s_exceptionCondition = exceptionCondition; analyzeOptions = analyzeOptions ?? new TestAnalyzeOptions() { TargetFileSpecifiers = new string[0] }; var command = new TestAnalyzeCommand(); command.DefaultPlugInAssemblies = new Assembly[] { typeof(ExceptionRaisingRule).Assembly }; int result = command.Run(analyzeOptions); int expectedResult = (runtimeConditions & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors ? TestAnalyzeCommand.SUCCESS : TestAnalyzeCommand.FAILURE; Assert.Equal(runtimeConditions, command.RuntimeErrors); Assert.Equal(expectedResult, result); if (expectedExitReason != ExitReason.None) { Assert.NotNull(command.ExecutionException); if (expectedExitReason != ExitReason.UnhandledExceptionInEngine) { var eax = command.ExecutionException as ExitApplicationException<ExitReason>; Assert.NotNull(eax); } } else { Assert.Null(command.ExecutionException); } ExceptionRaisingRule.s_exceptionCondition = ExceptionCondition.None; }
public RunLog AnalyzeFile(string fileName) { string path = Path.GetTempFileName(); RunLog runLog = null; try { var options = new TestAnalyzeOptions { TargetFileSpecifiers = new string[] { fileName }, Verbose = true, Statistics = true, ComputeTargetsHash = true, PolicyFilePath = "default", Recurse = true, OutputFilePath = path, }; var command = new TestAnalyzeCommand(); command.DefaultPlugInAssemblies = new Assembly[] { this.GetType().Assembly }; int result = command.Run(options); Assert.Equal(TestAnalyzeCommand.SUCCESS, result); JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = SarifContractResolver.Instance }; ResultLog log = JsonConvert.DeserializeObject<ResultLog>(File.ReadAllText(path), settings); Assert.NotNull(log); Assert.Equal<int>(1, log.RunLogs.Count); runLog = log.RunLogs[0]; } finally { File.Delete(path); } return runLog; }