175 lines
5.5 KiB
C#
175 lines
5.5 KiB
C#
using System.Reflection;
|
|
|
|
namespace StellaOps.BinaryIndex.GroundTruth.Buildinfo.Tests.Fixtures;
|
|
|
|
/// <summary>
|
|
/// Provides access to deterministic test fixtures for offline testing.
|
|
/// </summary>
|
|
public static class FixtureProvider
|
|
{
|
|
private static readonly string FixturesPath;
|
|
|
|
static FixtureProvider()
|
|
{
|
|
var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
|
|
FixturesPath = Path.Combine(assemblyDir, "Fixtures");
|
|
|
|
// Also try the source directory for development
|
|
if (!Directory.Exists(FixturesPath))
|
|
{
|
|
var sourceDir = FindSourceFixturesDirectory();
|
|
if (sourceDir is not null)
|
|
{
|
|
FixturesPath = sourceDir;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get sample .buildinfo file content for curl package.
|
|
/// </summary>
|
|
public static string GetSampleBuildinfoCurl()
|
|
{
|
|
var path = Path.Combine(FixturesPath, "curl_7.88.1-10_amd64.buildinfo");
|
|
if (!File.Exists(path))
|
|
{
|
|
// Return inline fixture if file doesn't exist
|
|
return SampleBuildinfoContent;
|
|
}
|
|
return File.ReadAllText(path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get sample signed .buildinfo file content.
|
|
/// </summary>
|
|
public static string GetSampleSignedBuildinfo()
|
|
{
|
|
var path = Path.Combine(FixturesPath, "openssl_3.0.11-1_amd64.buildinfo.asc");
|
|
if (!File.Exists(path))
|
|
{
|
|
return SampleSignedBuildinfoContent;
|
|
}
|
|
return File.ReadAllText(path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a fixture file as a stream.
|
|
/// </summary>
|
|
public static Stream GetFixtureStream(string name)
|
|
{
|
|
var path = Path.Combine(FixturesPath, name);
|
|
if (!File.Exists(path))
|
|
{
|
|
throw new FileNotFoundException($"Fixture not found: {path}");
|
|
}
|
|
return File.OpenRead(path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if a fixture exists.
|
|
/// </summary>
|
|
public static bool FixtureExists(string name)
|
|
{
|
|
var path = Path.Combine(FixturesPath, name);
|
|
return File.Exists(path);
|
|
}
|
|
|
|
private static string? FindSourceFixturesDirectory()
|
|
{
|
|
var dir = Directory.GetCurrentDirectory();
|
|
while (dir is not null)
|
|
{
|
|
var candidate = Path.Combine(dir, "src", "BinaryIndex", "__Tests",
|
|
"StellaOps.BinaryIndex.GroundTruth.Buildinfo.Tests", "Fixtures");
|
|
if (Directory.Exists(candidate))
|
|
{
|
|
return candidate;
|
|
}
|
|
dir = Directory.GetParent(dir)?.FullName;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inline sample buildinfo content for deterministic testing.
|
|
/// </summary>
|
|
private const string SampleBuildinfoContent = """
|
|
Format: 1.0
|
|
Source: curl
|
|
Binary: curl libcurl4 libcurl4-openssl-dev
|
|
Architecture: amd64 source
|
|
Version: 7.88.1-10
|
|
Checksums-Sha256:
|
|
abc123def456789012345678901234567890123456789012345678901234 12345 curl_7.88.1-10_amd64.deb
|
|
def456abc789012345678901234567890123456789012345678901234567 23456 libcurl4_7.88.1-10_amd64.deb
|
|
Build-Origin: debian
|
|
Build-Architecture: amd64
|
|
Build-Date: Mon, 15 Jan 2024 10:30:00 +0000
|
|
Build-Path: /build/curl-xyz123
|
|
Installed-Build-Depends:
|
|
gcc (= 12.2.0-14),
|
|
libc6 (= 2.36-9),
|
|
libssl-dev (= 3.0.11-1),
|
|
zlib1g-dev (= 1:1.2.13.dfsg-1)
|
|
Environment:
|
|
DEB_BUILD_OPTIONS="parallel=8"
|
|
LANG="C.UTF-8"
|
|
SOURCE_DATE_EPOCH="1705315800"
|
|
""";
|
|
|
|
/// <summary>
|
|
/// Inline sample signed buildinfo content.
|
|
/// </summary>
|
|
private const string SampleSignedBuildinfoContent = """
|
|
-----BEGIN PGP SIGNED MESSAGE-----
|
|
Hash: SHA512
|
|
|
|
Format: 1.0
|
|
Source: openssl
|
|
Binary: openssl libssl3 libssl-dev
|
|
Architecture: amd64 source
|
|
Version: 3.0.11-1
|
|
Checksums-Sha256:
|
|
fedcba9876543210fedcba9876543210fedcba9876543210fedcba98765 45678 openssl_3.0.11-1_amd64.deb
|
|
012345abcdef6789012345abcdef6789012345abcdef6789012345abcdef 56789 libssl3_3.0.11-1_amd64.deb
|
|
Build-Origin: debian
|
|
Build-Architecture: amd64
|
|
Build-Date: Tue, 16 Jan 2024 14:00:00 +0000
|
|
Build-Path: /build/openssl-abc456
|
|
Installed-Build-Depends:
|
|
gcc (= 12.2.0-14),
|
|
libc6 (= 2.36-9),
|
|
perl (= 5.36.0-7)
|
|
Environment:
|
|
DEB_BUILD_OPTIONS="nocheck"
|
|
LANG="C.UTF-8"
|
|
-----BEGIN PGP SIGNATURE-----
|
|
|
|
iQIzBAEBCgAdFiEE1234567890abcdef1234567890abcdef12345FiQI
|
|
ZABC123/ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC1
|
|
23ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC1
|
|
=wxYz
|
|
-----END PGP SIGNATURE-----
|
|
""";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test fixture constants for buildinfo tests.
|
|
/// </summary>
|
|
public static class FixtureConstants
|
|
{
|
|
// Sample package info
|
|
public const string SampleSourcePackageCurl = "curl";
|
|
public const string SampleVersionCurl = "7.88.1-10";
|
|
public const string SampleArchitectureAmd64 = "amd64";
|
|
|
|
public const string SampleSourcePackageOpenssl = "openssl";
|
|
public const string SampleVersionOpenssl = "3.0.11-1";
|
|
|
|
// Expected binary names
|
|
public const string ExpectedBinaryCurl = "curl";
|
|
public const string ExpectedBinaryLibcurl = "libcurl4";
|
|
public const string ExpectedBinaryOpenssl = "openssl";
|
|
public const string ExpectedBinaryLibssl = "libssl3";
|
|
}
|