Skip to content

Commit

Permalink
SBOM UT added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumanth K B committed Aug 1, 2023
1 parent 707c1f3 commit 2d24d4a
Show file tree
Hide file tree
Showing 17 changed files with 825 additions and 6 deletions.
60 changes: 58 additions & 2 deletions src/LCT.PackageIdentifier.UTest/DebianParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using LCT.Common;
using LCT.Common.Model;
using LCT.Common.Constants;

namespace PackageIdentifier.UTest
{
Expand Down Expand Up @@ -97,7 +98,8 @@ public void ParsePackageConfig_GivenAInputFilePath_ReturnsSourceDetails()
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string OutFolder = Path.GetDirectoryName(exePath);
DebianProcessor DebianProcessor = new DebianProcessor();
string[] Includes = { "SourceDetails_Cyclonedx.json" };
string[] Includes = { "SourceDetails_Cyclonedx.cdx.json" };

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = OutFolder + @"\PackageIdentifierUTTestFiles",
Expand All @@ -110,7 +112,61 @@ public void ParsePackageConfig_GivenAInputFilePath_ReturnsSourceDetails()
Bom listofcomponents = DebianProcessor.ParsePackageFile(appSettings);

//Assert
Assert.AreEqual(sourceName, listofcomponents.Components[0].Name + "_" + listofcomponents.Components[0].Version, "Checks componet name and version");
Assert.AreEqual(sourceName, listofcomponents.Components[0].Name + "_" + listofcomponents.Components[0].Version, "Checks component name and version");
}

[Test]
public void ParsePackageConfig_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTotalComponentsList()
{
//Arrange
int expectednoofcomponents = 5;
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string OutFolder = Path.GetDirectoryName(exePath);
DebianProcessor DebianProcessor = new DebianProcessor();
string[] Includes = { "CycloneDX_Debian.cdx.json" };
string packagefilepath = OutFolder + @"\PackageIdentifierUTTestFiles";

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = packagefilepath,
ProjectType = "DEBIAN",
RemoveDevDependency = true,
Debian = new Config() { Include = Includes },
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Debian.cdx.json"
};

//Act
Bom listofcomponents = DebianProcessor.ParsePackageFile(appSettings);

//Assert
Assert.That(expectednoofcomponents, Is.EqualTo(listofcomponents.Components.Count), "Checks for no of components");
}

[Test]
public void ParsePackageConfig_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUpdatedComponents()
{
//Arrange
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string OutFolder = Path.GetDirectoryName(exePath);
DebianProcessor DebianProcessor = new DebianProcessor();
string[] Includes = { "CycloneDX_Debian.cdx.json" };
string packagefilepath = OutFolder + @"\PackageIdentifierUTTestFiles";

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = packagefilepath,
ProjectType = "DEBIAN",
RemoveDevDependency = true,
Debian = new Config() { Include = Includes },
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Debian.cdx.json",
};

//Act
Bom listofcomponents = DebianProcessor.ParsePackageFile(appSettings);
bool isUpdated = listofcomponents.Components.Exists(x => x.Properties != null && x.Properties.Exists(x => x.Name == Dataconstant.Cdx_IdentifierType && x.Value == "TemplateAdded"));

//Assert
Assert.IsTrue(isUpdated, "Checks For Updated Property In List ");
}
}
}
15 changes: 15 additions & 0 deletions src/LCT.PackageIdentifier.UTest/LCT.PackageIdentifier.UTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@
<None Update="PackageIdentifierUTTestFiles\project.assets.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PackageIdentifierUTTestFiles\SBOMTemplates\SBOMTemplate_Debian.cdx.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PackageIdentifierUTTestFiles\SBOMTemplates\SBOMTemplate_Maven.cdx.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PackageIdentifierUTTestFiles\SBOMTemplates\SBOMTemplate_Npm.cdx.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PackageIdentifierUTTestFiles\SBOMTemplates\SBOMTemplate_Nuget.cdx.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PackageIdentifierUTTestFiles\SBOMTemplates\SBOMTemplate_Python.cdx.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PackageIdentifierUTTestFiles\SourceDetails_Cyclonedx.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
62 changes: 62 additions & 0 deletions src/LCT.PackageIdentifier.UTest/MavenParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using LCT.Services.Interface;
using Moq;
using System.Threading.Tasks;
using LCT.Common.Constants;

namespace LCT.PackageIdentifier.UTest
{
Expand Down Expand Up @@ -294,5 +295,66 @@ public void DevDependencyIdentificationLogic_ReturnsCountOfComponents_WithoutDev

}

[Test]
public void ParsePackageFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTotalComponentsList()
{
//Arrange
int expectednoofcomponents = 2;
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string outFolder = Path.GetDirectoryName(exePath);
string filepath = outFolder + @"\PackageIdentifierUTTestFiles";
string[] Includes = { "CycloneDX_Maven.cdx.json" };
string[] Excludes = { "lol" };

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = filepath,
ProjectType = "MAVEN",
RemoveDevDependency = true,
Maven = new Config() { Include = Includes, Exclude = Excludes },
CycloneDxSBomTemplatePath = filepath + "\\SBOMTemplates\\SBOMTemplate_Maven.cdx.json"
};

MavenProcessor MavenProcessor = new MavenProcessor();

//Act
Bom bom = MavenProcessor.ParsePackageFile(appSettings);

//Assert
Assert.That(expectednoofcomponents, Is.EqualTo(bom.Components.Count), "Checks for no of components");

}

[Test]
public void ParsePackageFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUpdatedComponents()
{
//Arrange
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string outFolder = Path.GetDirectoryName(exePath);
string filepath = outFolder + @"\PackageIdentifierUTTestFiles";
string[] Includes = { "CycloneDX_Maven.cdx.json" };
string[] Excludes = { "lol" };

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = filepath,
ProjectType = "MAVEN",
RemoveDevDependency = true,
Maven = new Config() { Include = Includes, Exclude = Excludes },
CycloneDxSBomTemplatePath = filepath + "\\SBOMTemplates\\SBOMTemplate_Maven.cdx.json"
};

MavenProcessor MavenProcessor = new MavenProcessor();

//Act
Bom bom = MavenProcessor.ParsePackageFile(appSettings);

bool isUpdated = bom.Components.Exists(x => x.Properties != null && x.Properties.Exists(x => x.Name == Dataconstant.Cdx_IdentifierType && x.Value == "TemplateAdded"));

//Assert
Assert.IsTrue(isUpdated, "Checks For Updated Property In List ");

}

}
}
56 changes: 56 additions & 0 deletions src/LCT.PackageIdentifier.UTest/NPMParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using LCT.Common.Model;
using System.Collections.Generic;
using CycloneDX.Models;
using LCT.Common.Constants;

namespace LCT.PackageIdentifier.UTest
{
Expand Down Expand Up @@ -125,5 +126,60 @@ public void ParseCycloneDXFile_GivenMultipleInputFiles_ReturnsCounts()
//Assert
Assert.That(expectednoofcomponents, Is.EqualTo(listofcomponents.Components.Count), "Checks for no of components");
}

[Test]
public void ParseCycloneDXFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTotalComponentsList()
{
//Arrange
int expectednoofcomponents = 4;
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string OutFolder = Path.GetDirectoryName(exePath);
NpmProcessor npmProcessor = new NpmProcessor();
string[] Includes = { "CycloneDX2_NPM.cdx.json" };
string packagefilepath = OutFolder + @"\PackageIdentifierUTTestFiles";

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = packagefilepath,
ProjectType = "NPM",
RemoveDevDependency = true,
Npm = new Config() { Include = Includes },
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Npm.cdx.json"
};

//Act
Bom listofcomponents = npmProcessor.ParsePackageFile(appSettings);

//Assert
Assert.That(expectednoofcomponents, Is.EqualTo(listofcomponents.Components.Count), "Checks for no of components");
}

[Test]
public void ParseCycloneDXFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUpdatedComponents()
{
//Arrange
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string OutFolder = Path.GetDirectoryName(exePath);
NpmProcessor npmProcessor = new NpmProcessor();
string[] Includes = { "CycloneDX2_NPM.cdx.json" };
string packagefilepath = OutFolder + @"\PackageIdentifierUTTestFiles";

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = packagefilepath,
ProjectType = "NPM",
RemoveDevDependency = true,
Npm = new Config() { Include = Includes },
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Npm.cdx.json"
};

//Act
Bom listofcomponents = npmProcessor.ParsePackageFile(appSettings);

bool isUpdated = listofcomponents.Components.Exists(x => x.Properties != null && x.Properties.Exists(x => x.Name == Dataconstant.Cdx_IdentifierType && x.Value == "TemplateAdded"));

//Assert
Assert.IsTrue(isUpdated, "Checks For Updated Property In List ");
}
}
}
64 changes: 64 additions & 0 deletions src/LCT.PackageIdentifier.UTest/NugetParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using CycloneDX.Models;
using System.Threading.Tasks;
using System.Linq;
using LCT.Common.Constants;
using Markdig.Extensions.Yaml;

namespace PackageIdentifier.UTest
{
Expand Down Expand Up @@ -465,7 +467,69 @@ public void ParseProjectAssetFile_GivenAInputFilePath_ReturnDevDependentComp()

//Assert
Assert.That(IsDev, Is.EqualTo(IsDevDependency), "Checks if Dev Dependency Component or not");
}


[TestCase]
public void ParsingInputFileForBOM_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTotalComponentsList()
{
//Arrange
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string outFolder = Path.GetDirectoryName(exePath);
string packagefilepath = outFolder + @"\PackageIdentifierUTTestFiles";
int TotalCount = 3;

string[] Includes = { "project.assets.json" };
Config config = new Config()
{
Include = Includes
};

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = packagefilepath,
Nuget = config,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Nuget.cdx.json",
ProjectType = "nuget"
};

//Act
Bom listofcomponents = new NugetProcessor().ParsePackageFile(appSettings);

//Assert
Assert.That(TotalCount, Is.EqualTo(listofcomponents.Components.Count), "Checks For Total Component Count");
}

[TestCase]
public void ParsingInputFileForBOM_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUpdatedComponents()
{
//Arrange
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string outFolder = Path.GetDirectoryName(exePath);
string packagefilepath = outFolder + @"\PackageIdentifierUTTestFiles";
bool isUpdated = false;

string[] Includes = { "project.assets.json" };
Config config = new Config()
{
Include = Includes
};

CommonAppSettings appSettings = new CommonAppSettings()
{
PackageFilePath = packagefilepath,
Nuget = config,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Nuget.cdx.json",
ProjectType = "nuget"
};

//Act
Bom listofcomponents = new NugetProcessor().ParsePackageFile(appSettings);

isUpdated = listofcomponents.Components.Exists(x => x.Properties.Exists(x => x.Name == Dataconstant.Cdx_IdentifierType && x.Value == "TemplateAdded"));

//Assert
Assert.IsTrue(isUpdated, "Checks For Updated Property In List ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"serialNumber": "urn:uuid:43858d10-328d-40d3-8184-ad4f0b5ea53c",
"version": 1,
"metadata": {
"timestamp": "2023-07-11T12:11:14Z",
"tools": [
{
"vendor": "anchore",
"name": "syft",
"version": "0.84.1"
}
],
"component": {
"bom-ref": "f8e285590963f51a",
"type": "container",
"name": "/mnt/InputImages/Buster_amd64.tar",
"version": "sha256:b75774030c2c10178aad29230221cad47aa607c72895aa4af389b3f1f01f71f4"
}
},
"components": [
{
"bom-ref": "pkg:deb/debian/[email protected]?arch=amd64&upstream=util-linux&distro=debian-10&package-id=ef0026c7016096be",
"type": "library",
"publisher": "LaMont Jones <[email protected]>",
"name": "libblkid1",
"version": "2.33.1-0.1",
"licenses": [
{
"license": {
"id": "Testing License"
}
}
],
"cpe": "cpe:2.3:a:libblkid1:libblkid1:2.33.1-0.1:*:*:*:*:*:*:*",
"purl": "pkg:deb/debian/[email protected]?arch=amd64&upstream=util-linux&distro=debian-10",
"properties": [
{
"name": "syft:package:foundBy",
"value": "Testing Properties"
}
]
}
]
}
Loading

0 comments on commit 2d24d4a

Please sign in to comment.