92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using FluentAssertions;
|
|
using StellaOps.Tools.GoldenPairs.Schema;
|
|
using StellaOps.Tools.GoldenPairs.Serialization;
|
|
using StellaOps.Tools.GoldenPairs.Services;
|
|
|
|
namespace StellaOps.Tools.GoldenPairs.Tests;
|
|
|
|
public sealed class GoldenPairLoaderTests
|
|
{
|
|
[Fact]
|
|
public async Task LoadAsync_ReturnsMetadataAndNormalizes()
|
|
{
|
|
var metadata = TestData.CreateMetadata();
|
|
using var temp = new TempDirectory();
|
|
|
|
var datasetRoot = Path.Combine(temp.Path, "datasets");
|
|
var pairDir = Path.Combine(datasetRoot, metadata.Cve);
|
|
Directory.CreateDirectory(pairDir);
|
|
|
|
var metadataPath = Path.Combine(pairDir, "metadata.json");
|
|
await File.WriteAllTextAsync(metadataPath, GoldenPairsJsonSerializer.Serialize(metadata));
|
|
|
|
var loader = CreateLoader(datasetRoot);
|
|
var result = await loader.LoadAsync(metadata.Cve);
|
|
|
|
result.IsValid.Should().BeTrue();
|
|
result.Metadata.Should().NotBeNull();
|
|
result.Metadata!.Cve.Should().Be("CVE-2022-0847");
|
|
result.Metadata!.Advisories[0].Source.Should().Be("nvd");
|
|
result.Metadata!.Advisories[1].Source.Should().Be("ubuntu");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LoadAsync_MissingMetadata_ReturnsError()
|
|
{
|
|
using var temp = new TempDirectory();
|
|
var loader = CreateLoader(temp.Path);
|
|
|
|
var result = await loader.LoadAsync("CVE-2099-9999");
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().NotBeEmpty();
|
|
}
|
|
|
|
private static GoldenPairLoader CreateLoader(string datasetRoot)
|
|
{
|
|
var repoRoot = FindRepoRoot();
|
|
var schemaProvider = new GoldenPairsSchemaProvider(
|
|
Path.Combine(repoRoot, "docs", "schemas", "golden-pair-v1.schema.json"),
|
|
Path.Combine(repoRoot, "docs", "schemas", "golden-pairs-index.schema.json"));
|
|
|
|
var layout = new GoldenPairLayout(datasetRoot);
|
|
return new GoldenPairLoader(schemaProvider, layout);
|
|
}
|
|
|
|
private static string FindRepoRoot()
|
|
{
|
|
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (current is not null)
|
|
{
|
|
var solutionPath = Path.Combine(current.FullName, "src", "StellaOps.sln");
|
|
if (File.Exists(solutionPath))
|
|
{
|
|
return current.FullName;
|
|
}
|
|
|
|
current = current.Parent;
|
|
}
|
|
|
|
throw new InvalidOperationException("Repository root not found.");
|
|
}
|
|
|
|
private sealed class TempDirectory : IDisposable
|
|
{
|
|
public TempDirectory()
|
|
{
|
|
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"golden-pairs-{Guid.NewGuid():N}");
|
|
Directory.CreateDirectory(Path);
|
|
}
|
|
|
|
public string Path { get; }
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(Path))
|
|
{
|
|
Directory.Delete(Path, recursive: true);
|
|
}
|
|
}
|
|
}
|
|
}
|