namespace MoShiro.Measurement.Tests; public sealed class PlatformDetectorTests { [Theory] [InlineData("PRETTY_NAME=\"Debian GNU/Linux 12 (bookworm)\"|NAME=\"Debian GNU/Linux\"|VERSION_ID=\"12\"|ID=debian", "Debian 12")] [InlineData("NAME=\"OpenWrt\"|VERSION_ID=\"24.10.0\"|ID=openwrt", "OpenWrt 24.10")] [InlineData("NAME=\"Alpine Linux\"|ID=alpine|VERSION_ID=3.22.1", "Alpine Linux 3.22")] [InlineData("NAME=\"Ubuntu\"|VERSION_ID=\"24.04\"", "Ubuntu 24.04")] [InlineData("NAME=\"Fedora Linux\"|VERSION_ID=44|HOSTNAME=private-router|KERNEL_VERSION=6.18.1|PRETTY_NAME=\"Fedora Linux 44 (Workstation Edition)\"", "Fedora Linux 44")] [InlineData("ID=arch", "arch")] [InlineData("", "Linux")] public void LinuxName_IsTheDistroAndItsMajorMinorVersion(string osRelease, string expected) { Assert.Equal(expected, PlatformDetector.LinuxName(osRelease.Split('|', StringSplitOptions.RemoveEmptyEntries))); } [Fact] public void Detect_ReportsOnlyCoarseInformation() { var platform = PlatformDetector.Detect(); Assert.Contains(platform.Os, new[] { "linux", "windows", "macos", "other" }); Assert.Contains(platform.Arch, new[] { "x64", "arm64", "arm", "x86" }); // 没有主机名、内核版本:名字很短,也不含小数点以外的版本细节 Assert.True((platform.Name ?? string.Empty).Length < 40); // 主机名可能恰好就是发行版名(例如 fedora),不能用字符串重合判断泄漏。 // 发行版字段的白名单和版本截断由上面的固定输入覆盖。 } }