using System.Buffers.Binary; using XFESystem.NativeRuntime.Application; namespace XFESystem.Platform.Tests; public unsafe class NativeGpuReportTests { private static ulong Info(uint field) => field switch { 0 => 1, 1 => 272, 2 => 1, 4 => 2, 13 => 32, _ => 0 }; private static int PhotoRecord(uint index, byte* record, uint bytes) { Assert.Equal(0u, index); Assert.Equal(272u, bytes); Span data = new(record, 272); data.Clear(); U32(data, 0, 1); U32(data, 4, 272); U32(data, 12, 1); U32(data, 24, 0x10de); U32(data, 28, 0x2b85); BinaryPrimitives.WriteUInt64LittleEndian(data[80..], 0xd8000000); BinaryPrimitives.WriteUInt64LittleEndian(data[112..], 0xf000000000); BinaryPrimitives.WriteUInt64LittleEndian(data[120..], 34359738368); U32(data, 136, 1); return 1; } [Fact] public void LegacyPhysicalReportDoesNotPresentDefaultZeroAsARegisterRead() { string text = NativeGpuReportFormatter.Format(Info, PhotoRecord, field => field switch { 0 => 1, 1 => 2, _ => 0 }, out bool rtx); Assert.True(rtx); Assert.Contains("PCI 1:0.0; Vendor 0x10DE; Device 0x2B85", text); Assert.Contains("BAR0 0xD8000000; bytes 0; size source 0", text); Assert.Contains("BAR1 0xF000000000; bytes 34359738368; size source 1", text); Assert.Contains("FSP scratch: not read", text); Assert.DoesNotContain("FSP scratch 0x00000000", text); Assert.Contains("CUDA: unavailable", text); Assert.Contains("GPU rasterization: unavailable", text); } [Theory] [InlineData(2u, "EFI system table not in mapped firmware memory")] [InlineData(7u, "GPU resource table missing")] [InlineData(10u, "GPU resource table schema/CRC rejected")] [InlineData(11u, "accepted")] public void ShowsFirmwareHandoffStageAndFirstProbeRejection(uint stage, string expected) { ulong DetailedInfo(uint field) => field switch { 14 => 1, 15 => stage, 16 => stage == 11 ? 1UL : 0, _ => Info(field) }; string text = NativeGpuReportFormatter.Format(DetailedInfo, PhotoRecord, field => field switch { 0 => 1, 1 => 2, 5 => 8, 7 => 1, _ => 0 }, out _); Assert.Contains("UEFI resource handoff: " + expected, text); Assert.Contains("Probe stopped before MMIO: BAR0 length has no validated firmware source", text); Assert.Contains("FSP scratch: not read", text); } [Theory] [InlineData(4u, 0u)] [InlineData(5u, 255u)] [InlineData(6u, uint.MaxValue)] [InlineData(7u, 255u)] public void ActualSamplesIncludingZeroRemainVisibleWithoutAnAccelerationClaim(uint state, uint sample) { string text = NativeGpuReportFormatter.Format(Info, PhotoRecord, field => field switch { 0 => 1, 1 => state, 2 => sample, 6 => 1, 7 => 1, _ => 0 }, out _); Assert.Contains("FSP scratch 0x" + sample.ToString("X8") + " (register read completed)", text); Assert.Contains("CUDA: unavailable", text); Assert.Contains("GPU rasterization: unavailable", text); } [Fact] public void ExcessiveCountRejectsBeforeCopyingAnyRecord() { string text = NativeGpuReportFormatter.Format(field => field == 2 ? 9 : Info(field), (_, _, _) => throw new InvalidOperationException("Must not copy"), field => 1, out bool rtx); Assert.False(rtx); Assert.Contains("Kernel diagnostic ABI: rejected", text); } private static void U32(Span data, int offset, uint value) => BinaryPrimitives.WriteUInt32LittleEndian(data[offset..], value); }