using System.Linq; using StellaOps.Zastava.Observer.Runtime; using StellaOps.Zastava.Observer.Tests.TestSupport; using Xunit; namespace StellaOps.Zastava.Observer.Tests.Runtime; public sealed class ElfBuildIdReaderTests { [Fact] public async Task TryReadBuildIdAsync_ReturnsExpectedHex() { using var temp = new TempDirectory(); var elfPath = Path.Combine(temp.RootPath, "bin", "example"); var buildIdBytes = Enumerable.Range(0, 20).Select(static index => (byte)(index + 1)).ToArray(); ElfTestFileBuilder.CreateElfWithBuildId(elfPath, buildIdBytes); var buildId = await ElfBuildIdReader.TryReadBuildIdAsync(elfPath, CancellationToken.None); Assert.Equal(Convert.ToHexString(buildIdBytes).ToLowerInvariant(), buildId); } [Fact] public async Task TryReadBuildIdAsync_InvalidFileReturnsNull() { using var temp = new TempDirectory(); var path = Path.Combine(temp.RootPath, "bin", "invalid"); Directory.CreateDirectory(Path.GetDirectoryName(path)!); await File.WriteAllTextAsync(path, "not-an-elf"); var buildId = await ElfBuildIdReader.TryReadBuildIdAsync(path, CancellationToken.None); Assert.Null(buildId); } private sealed class TempDirectory : IDisposable { public TempDirectory() { RootPath = Path.Combine(Path.GetTempPath(), "elf-buildid-tests", Guid.NewGuid().ToString("N")); Directory.CreateDirectory(RootPath); } public string RootPath { get; } public void Dispose() { try { if (Directory.Exists(RootPath)) { Directory.Delete(RootPath, recursive: true); } } catch { } } } }