public async Task Check_Sarif(string purl, string targetResult) { // for initialization FindSourceTool tool = new FindSourceTool(); RepoSearch searchTool = new RepoSearch(); var results = await searchTool.ResolvePackageLibraryAsync(new PackageURL(purl)); List <Result> sarifResults = new List <Result>(); foreach (var result in results) { var confidence = result.Value * 100.0; Result sarifResult = new Result() { Message = new Message() { Text = $"https://github.com/{result.Key.Namespace}/{result.Key.Name}" }, Kind = ResultKind.Informational, Level = FailureLevel.None, Rank = confidence, Locations = SarifOutputBuilder.BuildPurlLocation(new PackageURL(purl)) }; sarifResults.Add(sarifResult); } IOutputBuilder outputBuilder = OutputBuilderFactory.CreateOutputBuilder("sarifv2"); outputBuilder.AppendOutput(sarifResults); string sarifJSON = outputBuilder.GetOutput(); SarifLog sarif = JsonConvert.DeserializeObject <SarifLog>(sarifJSON); Assert.IsNotNull(sarif); var sarifRun = sarif.Runs.FirstOrDefault(); Assert.IsNotNull(sarifRun?.Tool.Driver.Name); // make sure atleast one of the result repos match the actual one bool found = false; if (sarifRun != null) { foreach (var result in sarifRun.Results) { if (result.Message.Text == targetResult) { found = true; } } } Assert.IsTrue(found); }
public async Task <string> DiffProjects(Options options) { var extractor = new Extractor(); var diffObjs = new List <Diff>(); var outputBuilder = OutputBuilderFactory.CreateOutputBuilder(options.Format); if (outputBuilder is null) { Logger.Error($"Format {options.Format} is not supported."); return(string.Empty); } // Map relative location in package to actual location on disk Dictionary <string, (string, string)> files = new Dictionary <string, (string, string)>(); IEnumerable <string> locations = Array.Empty <string>(); IEnumerable <string> locations2 = Array.Empty <string>(); try { PackageURL purl1 = new PackageURL(options.Targets.First()); var manager = ProjectManagerFactory.CreateProjectManager(purl1, options.DownloadDirectory ?? Path.GetTempPath()); if (manager is not null) { locations = await manager.DownloadVersion(purl1, true, options.UseCache); } } catch (Exception) { var tmpDir = Path.GetTempFileName(); File.Delete(tmpDir); try { extractor.ExtractToDirectory(tmpDir, options.Targets.First()); locations = new string[] { tmpDir }; } catch (Exception e) { Logger.Error($"{e.Message}:{e.StackTrace}"); Environment.Exit(-1); } } foreach (var directory in locations) { foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) { files.Add(string.Join(Path.DirectorySeparatorChar, file[directory.Length..].Split(Path.DirectorySeparatorChar)[2..]), (file, string.Empty));
public async Task <IOutputBuilder> DiffProjects(Options options) { Extractor? extractor = new Extractor(); IOutputBuilder?outputBuilder = OutputBuilderFactory.CreateOutputBuilder(options.Format); if (outputBuilder is null) { Logger.Error($"Format {options.Format} is not supported."); throw new ArgumentOutOfRangeException("options.Format", $"Format {options.Format} is not supported."); } // Map relative location in package to actual location on disk ConcurrentDictionary <string, (string, string)> files = new ConcurrentDictionary <string, (string, string)>(); IEnumerable <string> locations = Array.Empty <string>(); IEnumerable <string> locations2 = Array.Empty <string>(); try { PackageURL purl1 = new PackageURL(options.Targets.First()); BaseProjectManager?manager = ProjectManagerFactory.CreateProjectManager(purl1, options.DownloadDirectory ?? Path.GetTempPath()); if (manager is not null) { locations = await manager.DownloadVersionAsync(purl1, true, options.UseCache); } } catch (Exception) { string?tmpDir = Path.GetTempFileName(); File.Delete(tmpDir); try { extractor.ExtractToDirectory(tmpDir, options.Targets.First()); locations = new string[] { tmpDir }; } catch (Exception e) { Logger.Error($"{e.Message}:{e.StackTrace}"); Environment.Exit(-1); } } foreach (string?directory in locations) { foreach (string?file in System.IO.Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) { files[string.Join(Path.DirectorySeparatorChar, file[directory.Length..].Split(Path.DirectorySeparatorChar)[2..])] = (file, string.Empty);