77 lines
2.2 KiB
C#
77 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using CycloneDX.Models;
|
|
using StellaOps.Scanner.Emit.Spdx.Conversion;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Scanner.Emit.Tests.Composition;
|
|
|
|
public sealed class SpdxCycloneDxConversionTests
|
|
{
|
|
[Fact]
|
|
public void Converts_CycloneDx_To_Spdx()
|
|
{
|
|
var bom = BuildBom();
|
|
|
|
var spdx = SpdxCycloneDxConverter.FromCycloneDx(bom);
|
|
|
|
var packages = spdx.Elements.OfType<StellaOps.Scanner.Emit.Spdx.Models.SpdxPackage>().ToArray();
|
|
Assert.Equal(2, packages.Length);
|
|
Assert.Contains(packages, pkg => pkg.Name == "demo-app");
|
|
Assert.Contains(spdx.Relationships, rel => rel.Type == StellaOps.Scanner.Emit.Spdx.Models.SpdxRelationshipType.DependsOn);
|
|
}
|
|
|
|
[Fact]
|
|
public void Converts_Spdx_To_CycloneDx()
|
|
{
|
|
var bom = BuildBom();
|
|
var spdx = SpdxCycloneDxConverter.FromCycloneDx(bom);
|
|
|
|
var converted = SpdxCycloneDxConverter.ToCycloneDx(spdx);
|
|
|
|
Assert.NotNull(converted.Metadata);
|
|
Assert.NotNull(converted.Components);
|
|
Assert.Contains(converted.Components!, component => component.Name == "dependency");
|
|
}
|
|
|
|
private static Bom BuildBom()
|
|
{
|
|
var root = new Component
|
|
{
|
|
BomRef = "root",
|
|
Name = "demo-app",
|
|
Version = "1.0.0",
|
|
Type = Component.Classification.Application
|
|
};
|
|
|
|
var dependency = new Component
|
|
{
|
|
BomRef = "dep",
|
|
Name = "dependency",
|
|
Version = "2.0.0",
|
|
Type = Component.Classification.Library
|
|
};
|
|
|
|
return new Bom
|
|
{
|
|
SpecVersion = SpecificationVersion.v1_7,
|
|
Version = 1,
|
|
Metadata = new Metadata
|
|
{
|
|
Timestamp = new DateTime(2025, 10, 20, 0, 0, 0, DateTimeKind.Utc),
|
|
Component = root
|
|
},
|
|
Components = new List<Component> { dependency },
|
|
Dependencies = new List<Dependency>
|
|
{
|
|
new()
|
|
{
|
|
Ref = root.BomRef,
|
|
Dependencies = new List<Dependency> { new() { Ref = dependency.BomRef } }
|
|
}
|
|
}
|
|
};
|
|
}
|
|
}
|