Skip to content

Commit

Permalink
Merge pull request #35 from bmrvilela/master
Browse files Browse the repository at this point in the history
Embedded asset files + tests
  • Loading branch information
portilha authored Oct 23, 2024
2 parents 0a69dec + 3d0edfe commit f09b8a3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 118 deletions.
3 changes: 2 additions & 1 deletion Checkmarx.API.Tests/CxClientUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion Checkmarx.API.Tests/ProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Checkmarx.API.Tests/ScanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<li>");
stringBuilder.AppendLine($"<a href=\"{uri.AbsoluteUri}\">Result [{toResultStateToString((ResultState)resultPath.State)}]</a>");
Expand Down Expand Up @@ -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}");

Expand Down
62 changes: 62 additions & 0 deletions Checkmarx.API.Tests/Utils/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -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<Assembly, string> 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<Assembly, string>(detectedAssembly, resourceName);
}
}
}
231 changes: 117 additions & 114 deletions Checkmarx.API/Checkmarx.API.csproj
Original file line number Diff line number Diff line change
@@ -1,118 +1,121 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>6.1</Version>
<Authors>Pedro Portilha</Authors>
<Company>Checkmarx</Company>
<Description>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.</Description>
<Copyright>Copyright ©2021</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIcon>CxIcon.png</PackageIcon>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<UserSecretsId>9629655e-7523-42f3-b285-1cf568d14b45</UserSecretsId>
<RepositoryUrl>https://github.com/portilha/Checkmarx.API</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Configurations>Debug;Release;Publish</Configurations>
<Platforms>AnyCPU;x64</Platforms>

</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Publish|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Publish|x64'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Remove="AST\**" />
<EmbeddedResource Remove="AST\**" />
<None Remove="AST\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="output.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="Connected Services\CxSAST OData Service V94\CxSAST OData Service V94Csdl.xml" />
<None Remove="Connected Services\CxSAST OData Service\CxSAST OData ServiceCsdl.xml" />
<None Remove="Connected Services\CxSAST V9 OData Service\CxSAST V9 OData ServiceCsdl.xml" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Connected Services\CxSAST OData Service V94\CxSAST OData Service V94Csdl.xml" />
<EmbeddedResource Include="Connected Services\CxSAST OData Service\CxSAST OData ServiceCsdl.xml" />
<EmbeddedResource Include="Connected Services\CxSAST V9 OData Service\CxSAST V9 OData ServiceCsdl.xml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Capri" Version="0.0.1" />
<PackageReference Include="Checkmarx.API.AccessControl" Version="3.0.0" />
<PackageReference Include="Checkmarx.API.SCA" Version="4.0.0" />
<PackageReference Include="Microsoft.Data.OData" Version="5.8.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.OData.Client" Version="7.21.3" />
<PackageReference Include="Microsoft.OData.Core" Version="7.21.3" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.3" />
<PackageReference Include="Microsoft.Spatial" Version="7.21.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.3" />
<PackageReference Include="System.ServiceModel.Duplex" Version="6.0.0" />
<PackageReference Include="System.ServiceModel.Federation" Version="8.0.0" />
<PackageReference Include="System.ServiceModel.Http" Version="8.0.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="8.0.0" />
<PackageReference Include="System.ServiceModel.Security" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
<None Update="Assets\CxIcon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Update="Assets\exclusions.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Assets\CxIcon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
<WCFMetadata Include="Connected Services" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>6.1</Version>
<Authors>Pedro Portilha</Authors>
<Company>Checkmarx</Company>
<Description>
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.
</Description>
<Copyright>Copyright ©2021</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIcon>CxIcon.png</PackageIcon>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<UserSecretsId>9629655e-7523-42f3-b285-1cf568d14b45</UserSecretsId>
<RepositoryUrl>https://github.com/portilha/Checkmarx.API</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Configurations>Debug;Release;Publish</Configurations>
<Platforms>AnyCPU;x64</Platforms>

</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Publish|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Publish|x64'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>VSSpell001</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Remove="AST\**" />
<EmbeddedResource Remove="AST\**" />
<None Remove="AST\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="output.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="Assets\exclusions.json" />
<None Remove="Connected Services\CxSAST OData Service V94\CxSAST OData Service V94Csdl.xml" />
<None Remove="Connected Services\CxSAST OData Service\CxSAST OData ServiceCsdl.xml" />
<None Remove="Connected Services\CxSAST V9 OData Service\CxSAST V9 OData ServiceCsdl.xml" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Assets\exclusions.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Connected Services\CxSAST OData Service V94\CxSAST OData Service V94Csdl.xml" />
<EmbeddedResource Include="Connected Services\CxSAST OData Service\CxSAST OData ServiceCsdl.xml" />
<EmbeddedResource Include="Connected Services\CxSAST V9 OData Service\CxSAST V9 OData ServiceCsdl.xml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Capri" Version="0.0.1" />
<PackageReference Include="Checkmarx.API.AccessControl" Version="3.0.0" />
<PackageReference Include="Checkmarx.API.SCA" Version="4.0.0" />
<PackageReference Include="Microsoft.Data.OData" Version="5.8.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.OData.Client" Version="7.21.3" />
<PackageReference Include="Microsoft.OData.Core" Version="7.21.3" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.3" />
<PackageReference Include="Microsoft.Spatial" Version="7.21.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.3" />
<PackageReference Include="System.ServiceModel.Duplex" Version="6.0.0" />
<PackageReference Include="System.ServiceModel.Federation" Version="8.0.0" />
<PackageReference Include="System.ServiceModel.Http" Version="8.0.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="8.0.0" />
<PackageReference Include="System.ServiceModel.Security" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Assets\CxIcon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</EmbeddedResource>
<EmbeddedResource Include="Assets\CxIcon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
<WCFMetadata Include="Connected Services" />
</ItemGroup>

</Project>

0 comments on commit f09b8a3

Please sign in to comment.