From 5a4593039a112f38577daab3f0924463d17f6f7e Mon Sep 17 00:00:00 2001 From: bvilela Date: Tue, 22 Oct 2024 17:08:44 +0100 Subject: [PATCH 1/2] Exclude pdb files --- Checkmarx.API/Checkmarx.API.csproj | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Checkmarx.API/Checkmarx.API.csproj b/Checkmarx.API/Checkmarx.API.csproj index 70e82ad..d517799 100644 --- a/Checkmarx.API/Checkmarx.API.csproj +++ b/Checkmarx.API/Checkmarx.API.csproj @@ -52,6 +52,15 @@ It also provides access to the Access Control API. VSSpell001 + + None + false + + + + + + From 3d0edfe6b9689a0af77fc3cb3c2c91cec4c2a675 Mon Sep 17 00:00:00 2001 From: bvilela Date: Wed, 23 Oct 2024 17:26:56 +0100 Subject: [PATCH 2/2] Embedded asset files + tests --- Checkmarx.API.Tests/CxClientUnitTests.cs | 3 +- Checkmarx.API.Tests/ProcessTests.cs | 3 +- Checkmarx.API.Tests/ScanTests.cs | 4 +- Checkmarx.API.Tests/Utils/TestUtils.cs | 62 ++++++ Checkmarx.API/Checkmarx.API.csproj | 240 +++++++++++------------ 5 files changed, 185 insertions(+), 127 deletions(-) create mode 100644 Checkmarx.API.Tests/Utils/TestUtils.cs diff --git a/Checkmarx.API.Tests/CxClientUnitTests.cs b/Checkmarx.API.Tests/CxClientUnitTests.cs index 5f1f130..4c33410 100644 --- a/Checkmarx.API.Tests/CxClientUnitTests.cs +++ b/Checkmarx.API.Tests/CxClientUnitTests.cs @@ -14,6 +14,7 @@ using System.Xml.Linq; using Checkmarx.API; using Checkmarx.API.Exceptions; +using Checkmarx.API.Tests.Utils; using Microsoft.Extensions.Configuration; using Microsoft.OData.Client; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -225,7 +226,7 @@ public void SuggestExclusionsTest() // unzip ZipFile.ExtractToDirectory(zipPath, extractPath); - var exclusions = Exclusions.FromJson("exclusions.json"); + var exclusions = Exclusions.FromJson(TestUtils.ReadEmbeddedFile("exclusions.json")); Regex[] filesRegex = exclusions.Files.Select(x => new Regex(x, RegexOptions.Compiled)).ToArray(); Regex[] foldersRegex = exclusions.Folders.Select(x => new Regex(x, RegexOptions.Compiled)).ToArray(); diff --git a/Checkmarx.API.Tests/ProcessTests.cs b/Checkmarx.API.Tests/ProcessTests.cs index fb43eb2..8ee067a 100644 --- a/Checkmarx.API.Tests/ProcessTests.cs +++ b/Checkmarx.API.Tests/ProcessTests.cs @@ -14,6 +14,7 @@ using System.Xml.Linq; using Checkmarx.API; using Checkmarx.API.Exceptions; +using Checkmarx.API.Tests.Utils; using Microsoft.Extensions.Configuration; using Microsoft.OData.Client; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -157,7 +158,7 @@ public void SuggestExclusionsTest() // unzip ZipFile.ExtractToDirectory(zipPath, extractPath, true); - var exclusions = Exclusions.FromJson(File.ReadAllText("Assets/exclusions.json")); + var exclusions = Exclusions.FromJson(TestUtils.ReadEmbeddedFile("exclusions.json")); Regex[] filesRegex = exclusions.Files.Select(x => new Regex(x, RegexOptions.Compiled)).ToArray(); Regex[] foldersRegex = exclusions.Folders.Select(x => new Regex(x, RegexOptions.Compiled)).ToArray(); diff --git a/Checkmarx.API.Tests/ScanTests.cs b/Checkmarx.API.Tests/ScanTests.cs index f73f08a..35f1432 100644 --- a/Checkmarx.API.Tests/ScanTests.cs +++ b/Checkmarx.API.Tests/ScanTests.cs @@ -607,7 +607,7 @@ public void GetNotExploitableResultsAudit() similarityIdResult.Add(resultPath.SimilarityId); - var uri = Utils.GetLink(resultPath, clientV93, project.Key, scan.Id); + var uri = Checkmarx.API.Utils.GetLink(resultPath, clientV93, project.Key, scan.Id); stringBuilder.AppendLine("
  • "); stringBuilder.AppendLine($"Result [{toResultStateToString((ResultState)resultPath.State)}]"); @@ -644,7 +644,7 @@ public void GetODataCommentsTest() { foreach (var item in clientV93.GetODataResults(1031805).Where(x => x.PathId == 38)) { - var uri = Utils.GetLink(item, clientV93, item.Scan.ProjectId, item.ScanId); + var uri = Checkmarx.API.Utils.GetLink(item, clientV93, item.Scan.ProjectId, item.ScanId); Trace.WriteLine($"{uri.AbsoluteUri}"); diff --git a/Checkmarx.API.Tests/Utils/TestUtils.cs b/Checkmarx.API.Tests/Utils/TestUtils.cs new file mode 100644 index 0000000..1763911 --- /dev/null +++ b/Checkmarx.API.Tests/Utils/TestUtils.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Checkmarx.API.Tests.Utils +{ + public static class TestUtils + { + public static string ReadEmbeddedFile(string fileName) + { + if (string.IsNullOrWhiteSpace(fileName)) + throw new ArgumentNullException(nameof(fileName)); + + var assemblyAndFile = GetFileAssembly(fileName); + + Assembly detectedAssembly = assemblyAndFile.Item1; + string resourceName = assemblyAndFile.Item2; + using (Stream stream = detectedAssembly.GetManifestResourceStream(resourceName)) + { + if (stream == null) + throw new FileNotFoundException($"Embedded resource '{resourceName}' not found."); + + // Read the embedded file's content as text + using (StreamReader reader = new StreamReader(stream)) + { + string content = reader.ReadToEnd(); + return content; + } + } + + // If the resource is not found, you could either return default(T) or throw an exception + throw new FileNotFoundException($"Embedded resource '{resourceName}' not found."); + } + + public static Tuple GetFileAssembly(string fileName) + { + if (string.IsNullOrWhiteSpace(fileName)) + throw new ArgumentNullException(nameof(fileName)); + + string resourceName = string.Empty; + Assembly detectedAssembly = null; + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + resourceName = assembly.GetManifestResourceNames().SingleOrDefault(x => x.EndsWith(fileName)); + if (!string.IsNullOrWhiteSpace(resourceName)) + { + detectedAssembly = assembly; + break; + } + } + + if (string.IsNullOrWhiteSpace(resourceName) || detectedAssembly == null) + throw new Exception($"No resource found with the name \"{fileName}\""); + + return new Tuple(detectedAssembly, resourceName); + } + } +} diff --git a/Checkmarx.API/Checkmarx.API.csproj b/Checkmarx.API/Checkmarx.API.csproj index d517799..d6a95bf 100644 --- a/Checkmarx.API/Checkmarx.API.csproj +++ b/Checkmarx.API/Checkmarx.API.csproj @@ -1,127 +1,121 @@  - - net8.0 - 6.1 - Pedro Portilha - Checkmarx - Checkmarx API (Unofficial) -This SDK provides an API wrapper for accessing REST, SOAP and OData API for the Checkmarx SAST and OSA products. -It also provides access to the Access Control API. - Copyright ©2021 - true - CxIcon.png - false - MIT - 9629655e-7523-42f3-b285-1cf568d14b45 - https://github.com/portilha/Checkmarx.API - git - README.md - Debug;Release;Publish - AnyCPU;x64 - - - - - True - VSSpell001 - - - - True - VSSpell001 - - - - True - VSSpell001 - - - - True - VSSpell001 - - - - True - VSSpell001 - - - - True - VSSpell001 - - - - None - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - True - - - - Always - - - True - - - - - - - - + + net8.0 + 6.1 + Pedro Portilha + Checkmarx + + Checkmarx API (Unofficial) + This SDK provides an API wrapper for accessing REST, SOAP and OData API for the Checkmarx SAST and OSA products. + It also provides access to the Access Control API. + + Copyright ©2021 + true + CxIcon.png + false + MIT + 9629655e-7523-42f3-b285-1cf568d14b45 + https://github.com/portilha/Checkmarx.API + git + README.md + Debug;Release;Publish + AnyCPU;x64 + + + + + True + VSSpell001 + + + + True + VSSpell001 + + + + True + VSSpell001 + + + + True + VSSpell001 + + + + True + VSSpell001 + + + + True + VSSpell001 + + + + + + + + + + + + + + + + + + + + + + + Never + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + True + + + + True + + + + + + + +