using System.Security.Cryptography; using System.Text.Json; using Xunit; namespace XFEWarframe.Client.Tests; public sealed class InstallerPayloadTests : IDisposable { private readonly string root = Path.Combine(Path.GetTempPath(), "xfe-payload-test-" + Guid.NewGuid().ToString("N")); public InstallerPayloadTests() => Directory.CreateDirectory(root); public void Dispose() => Directory.Delete(root, true); private string Bundle(string directory, string version, int schema = 1) { var bundle = Path.Combine(directory, "Bridge"); Directory.CreateDirectory(bundle); File.WriteAllBytes(Path.Combine(bundle, ClientInstaller.BridgeFile), [1, 2, 3]); File.WriteAllText(Path.Combine(bundle, "payload.json"), JsonSerializer.Serialize( new PayloadManifest { SchemaVersion = schema, Version = version, Sha256 = Convert.ToHexString(SHA256.HashData(new byte[] { 1, 2, 3 })) }, ClientJson.Default.PayloadManifest)); return bundle; } [Fact] public void AutomaticDiscoverySkipsOldInstallerBundleAndFindsCurrentParentBundle() { var application = Path.Combine(root, "Installer"); Bundle(application, "1.4.2"); var current = Bundle(root, ClientInstaller.MinimumBridgeVersion); Assert.Equal(current, ClientInstaller.FindPayload(application)); } [Fact] public void AutomaticDiscoveryReportsOldSourceDirectoryWhenNoCurrentBundleExists() { var old = Bundle(root, "1.4.2"); var error = Assert.Throws(() => ClientInstaller.FindPayload(root)); Assert.Contains(old, error.Message); Assert.Contains("所选兼容组件目录版本过旧", error.Message); Assert.DoesNotContain("安装与维护", error.Message); } [Fact] public void AutomaticDiscoverySkipsUnsupportedManifestAndMissingDll() { var application = Path.Combine(root, "Installer"); var local = Bundle(application, ClientInstaller.MinimumBridgeVersion, 2); var current = Bundle(root, ClientInstaller.MinimumBridgeVersion); Assert.Equal(current, ClientInstaller.FindPayload(application)); Bundle(application, ClientInstaller.MinimumBridgeVersion); File.Delete(Path.Combine(local, ClientInstaller.BridgeFile)); Assert.Equal(current, ClientInstaller.FindPayload(application)); } [Theory] [InlineData("1.4.2", "版本过旧")] [InlineData(ClientInstaller.MinimumBridgeVersion, "SHA-256")] public void InvalidSourceIsRejectedBeforeCreatingGameInstallationFiles(string version, string message) { var payload = Bundle(Path.Combine(root, "Payload"), version); if (version == ClientInstaller.MinimumBridgeVersion) File.WriteAllBytes(Path.Combine(payload, ClientInstaller.BridgeFile), [7, 8, 9]); var game = Path.Combine(root, "Game"); Directory.CreateDirectory(game); var executable = Path.Combine(game, "Warframe.x64.exe"); File.WriteAllText(executable, "unchanged game fixture"); var before = Compatibility.Hash(executable); var error = Assert.Throws(() => new ClientInstaller().Install(game, "http://127.0.0.1:8444", payload)); Assert.Contains(message, error.Message); Assert.Contains(payload, error.Message); Assert.Equal(before, Compatibility.Hash(executable)); Assert.False(Directory.Exists(Path.Combine(game, "XFEWarframe"))); } }