Skip to content

Commit

Permalink
Added few tests for metadata #817
Browse files Browse the repository at this point in the history
Signed-off-by: MTsfoni <[email protected]>
  • Loading branch information
mtsfoni committed Jan 5, 2024
1 parent 37167ae commit 5b82c6e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CycloneDX.Tests/CycloneDX.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<None Update="FunctionalTests\TestcaseFiles\Issue-758.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\TestcaseFiles\metadata.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\TestcaseFiles\ProjectReferenceWithPackageReferenceWithTransitivePackage.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
4 changes: 2 additions & 2 deletions CycloneDX.Tests/FunctionalTests/FunctionalTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public static async Task<Bom> Test(RunOptions options, INugetServiceFactory nuge
}
else
{
validationResult = Xml.Validator.Validate(mockBomFileStream, SpecificationVersion.v1_5);
validationResult = Xml.Validator.Validate(mockBomFileStream, SpecificationVersion.v1_5);
}
Assert.True(validationResult.Valid);

return runner.LastGeneratedBom;
}

private const string CsprojContents =
public const string CsprojContents =
"<Project Sdk=\"Microsoft.NET.Sdk\">\n\n " +
"<PropertyGroup>\n " +
"<OutputType>Exe</OutputType>\n " +
Expand Down
136 changes: 136 additions & 0 deletions CycloneDX.Tests/FunctionalTests/MetaData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CycloneDX.Models;
using Xunit;

namespace CycloneDX.Tests.FunctionalTests
{
public class MetaData
{
private MockFileSystem getMockFS()
{
return new MockFileSystem(new Dictionary<string, MockFileData>
{
{ MockUnixSupport.Path("c:/ProjectPath/obj/project.assets.json"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", "TestcaseFiles", "SimpleNET6.0Library.json"))) },
{ MockUnixSupport.Path("c:/ProjectPath/Project.csproj"), new MockFileData(FunctionalTestHelper.CsprojContents) },
{ MockUnixSupport.Path("c:/ProjectPath/metadata.xml"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", "TestcaseFiles", "metadata.xml"))) }
});
}

[Fact]
public async Task ImportedMetaDataAreInBomOutput()
{
var options = new RunOptions
{
importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml")
};

var bom = await FunctionalTestHelper.Test(options, getMockFS());

Assert.Equal("CycloneDX", bom.Metadata.Component.Name);
Assert.Equal("1.3.0", bom.Metadata.Component.Version);
Assert.Equal(Component.Classification.Application, bom.Metadata.Component.Type);
Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description));
Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name);
Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id);
Assert.Equal("pkg:nuget/[email protected]", bom.Metadata.Component.Purl);
}

[Fact]
public async Task IfNoMetadataIsImportedTimestampIsSetAutomatically()
{
var options = new RunOptions
{
};

var bom = await FunctionalTestHelper.Test(options, getMockFS());
Assert.True(bom.Metadata.Timestamp.Value > DateTime.UtcNow.AddMinutes(-10));
}

[Fact]
public async Task IfMetadataWithoutTimestampIsImportedTimestampStillGetsSet()
{
var options = new RunOptions
{
importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml")
};

var bom = await FunctionalTestHelper.Test(options, getMockFS());
Assert.True(bom.Metadata.Timestamp.Value > DateTime.UtcNow.AddMinutes(-10));
}

[Fact(Skip ="Test schlägt fehl #821")]
public async Task SetVersionOverwritesVersionWhenMetadataAreProvided()
{
var options = new RunOptions
{
importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml"),
setVersion = "3.0.4"

};

var bom = await FunctionalTestHelper.Test(options, getMockFS());

Assert.Equal("CycloneDX", bom.Metadata.Component.Name);
Assert.Equal("3.0.4", bom.Metadata.Component.Version);
Assert.Equal(Component.Classification.Application, bom.Metadata.Component.Type);
Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description));
Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name);
Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id);
Assert.Equal("pkg:nuget/[email protected]", bom.Metadata.Component.Purl);
}

[Fact(Skip = "Test schlägt fehl #821")]
public async Task SetNameOverwritesNameWhenMetadataAreProvided()
{
var options = new RunOptions
{
importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml"),
setName = "Foo"

};

var bom = await FunctionalTestHelper.Test(options, getMockFS());

Assert.Equal("Foo", bom.Metadata.Component.Name);
Assert.Equal("1.3.0", bom.Metadata.Component.Version);
Assert.Equal(Component.Classification.Application, bom.Metadata.Component.Type);
Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description));
Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name);
Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id);
Assert.Equal("pkg:nuget/[email protected]", bom.Metadata.Component.Purl);
}

[Fact(Skip = "Test schlägt fehl #821")]
public async Task SetTypeOverwritesTypeWhenMetadataAreProvided()
{
var options = new RunOptions
{
importMetadataPath = MockUnixSupport.Path("c:/ProjectPath/metadata.xml"),
setType = Component.Classification.Container

};

var bom = await FunctionalTestHelper.Test(options, getMockFS());

Assert.Equal("CycloneDX", bom.Metadata.Component.Name);
Assert.Equal("1.3.0", bom.Metadata.Component.Version);
Assert.Equal(Component.Classification.Container, bom.Metadata.Component.Type);
Assert.False(string.IsNullOrEmpty(bom.Metadata.Component.Description));
Assert.Equal("Apache License 2.0", bom.Metadata.Component.Licenses.First().License.Name);
Assert.Equal("Apache-2.0", bom.Metadata.Component.Licenses.First().License.Id);
Assert.Equal("pkg:nuget/[email protected]", bom.Metadata.Component.Purl);
}
}
}
19 changes: 19 additions & 0 deletions CycloneDX.Tests/FunctionalTests/TestcaseFiles/metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<bom xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" serialNumber="urn:uuid:087d0712-f591-4995-ba76-03f1c5c48884" version="1" xmlns="http://cyclonedx.org/schema/bom/1.5">
<metadata>
<component type="application" bom-ref="pkg:nuget/[email protected]">
<name>CycloneDX</name>
<version>1.3.0</version>
<description>
<![CDATA[The [CycloneDX module](https://github.com/CycloneDX/cyclonedx-dotnet) for .NET creates a valid CycloneDX bill-of-material document containing an aggregate of all project dependencies. CycloneDX is a lightweight BOM specification that is easily created, human readable, and simple to parse.]]>
</description>
<licenses>
<license>
<name>Apache License 2.0</name>
<id>Apache-2.0</id>
</license>
</licenses>
<purl>pkg:nuget/[email protected]</purl>
</component>
</metadata>
</bom>
3 changes: 2 additions & 1 deletion CycloneDX.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Threading.Tasks;
using CycloneDX.Interfaces;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void CheckMetaDataTemplate()
{
var bom = new Bom();
string resourcePath = Path.Join(AppContext.BaseDirectory, "Resources", "metadata");
bom = Runner.ReadMetaDataFromFile(bom, Path.Join(resourcePath, "cycloneDX-metadata-template.xml"));
bom = Runner.ReadMetaDataFromFile(bom, Path.Join(resourcePath, "cycloneDX-metadata-template.xml"), new FileSystem());
Assert.NotNull(bom.Metadata);
Assert.Matches("CycloneDX", bom.Metadata.Component.Name);
Assert.NotEmpty(bom.Metadata.Tools.Tools);
Expand Down
8 changes: 4 additions & 4 deletions CycloneDX/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,14 @@ public async Task<int> HandleCommandAsync(RunOptions options)

if (!string.IsNullOrEmpty(importMetadataPath))
{
if (!File.Exists(importMetadataPath))
if (!fileSystem.File.Exists(importMetadataPath))
{
Console.Error.WriteLine($"Metadata template '{importMetadataPath}' does not exist.");
return (int)ExitCode.InvalidOptions;
}
else
{
bom = ReadMetaDataFromFile(bom, importMetadataPath);
bom = ReadMetaDataFromFile(bom, importMetadataPath, fileSystem);
}
}
SetMetadataComponentIfNecessary(bom, topLevelComponent);
Expand Down Expand Up @@ -448,11 +448,11 @@ private static void SetMetadataComponentIfNecessary(Bom bom, Component topLevelC

}

internal static Bom ReadMetaDataFromFile(Bom bom, string templatePath)
internal static Bom ReadMetaDataFromFile(Bom bom, string templatePath, IFileSystem fileSystem)
{
try
{
return Xml.Serializer.Deserialize(File.ReadAllText(templatePath));
return Xml.Serializer.Deserialize(fileSystem.File.ReadAllText(templatePath));
}
catch (IOException ex)
{
Expand Down

0 comments on commit 5b82c6e

Please sign in to comment.