using System.Security.Cryptography; using System.Text.Json; using XFEWarframe.Client; using Xunit; namespace XFEWarframe.Client.Tests; public sealed class ClientTests : IDisposable { private readonly string root = Path.Combine(Path.GetTempPath(), "xfe-client-test-" + Guid.NewGuid().ToString("N")); public ClientTests() => Directory.CreateDirectory(root); public void Dispose() => Directory.Delete(root, true); [Fact] public void WildcardsAndOverlappingPatternsRespectBufferBoundaries() { var pattern = new BytePattern("AB ?? AB"); Assert.Equal([0, 2], pattern.Find([0xAB, 1, 0xAB, 2, 0xAB])); Assert.Empty(pattern.Find([0xAB, 1])); Assert.Empty(pattern.Find([])); } [Theory] [InlineData("https://host/path")][InlineData("ftp://host")][InlineData("http://user:pass@host")] [InlineData("http://host/?q=x")][InlineData("http://host/#fragment")][InlineData("invalid")] public void RejectMalformedServerOrigins(string value) => Assert.Throws(() => ClientProtocol.NormalizeServer(value)); [Theory] [InlineData("http://[::1]:8444/", "http://[::1]:8444")] [InlineData("https://my-server.test", "https://my-server.test")] public void SupportsIpv6AndHttps(string value, string expected) => Assert.Equal(expected, ClientProtocol.NormalizeServer(value)); [Fact] public void RedirectKeepsEscapedQueryAndSuppliesCurrentBuild() { var result = ClientProtocol.Redirect("https://content.warframe.com/api/login.php?x=a%2Bb", "http://127.0.0.1:8444", "2026.08.19.11.06"); Assert.Equal("http://127.0.0.1:8444/api/login.php?x=a%2Bb&buildLabel=2026.08.19.11.06&clientMod=XFEWarframe", result); Assert.Equal("http://local/a/b?x=2", ClientProtocol.Redirect("https://old/a/b?x=2", "http://local", "build")); } [Fact] public void ProtectedRequestDecryptionRetainsBinaryZerosAndRejectsInvalidPadding() { using var aes = Aes.Create(); aes.Key = Enumerable.Repeat((byte)'A', 24).ToArray(); byte[] plain = [0x1F,0x8B,0,1,0,2,3,0]; var encrypted = aes.EncryptCbc(plain, Enumerable.Repeat((byte)'A', 16).ToArray(), PaddingMode.PKCS7); byte[] body = [.. new byte[137], .. encrypted]; Assert.Equal(plain, ClientProtocol.DecodeProtected(body)); Assert.Throws(() => ClientProtocol.DecodeProtected(body[..138])); body[^1] ^= 1; Assert.Throws(() => ClientProtocol.DecodeProtected(body)); } [Fact] public void SteamLibrariesUnescapeWindowsPaths() { var text = "\"libraryfolders\" { \"0\" { \"path\" \"D:\\\\SteamLibrary\" } \"1\" { \"path\" \"E:/Games\" } }"; Assert.Equal([@"D:\SteamLibrary", "E:/Games"], GameDiscovery.ParseSteamLibraries(text)); } [Theory][InlineData("../escape")][InlineData("../../Warframe.x64.exe")][InlineData("C:\\other")][InlineData("file:stream")] public void RejectPathEscape(string path) => Assert.Throws(() => SafePath.Within(root, path)); [Fact] public void FailedInstallRestoresExistingFilesAndRemovesOnlyCreatedFiles() { File.WriteAllText(Path.Combine(root, "wtsapi32.dll"), "original"); File.WriteAllText(Path.Combine(root, "Warframe.x64.exe"), "do-not-touch"); var changes = new Dictionary { ["wtsapi32.dll"] = null, ["XFEWarframe/client.json"] = [1,2,3] }; Assert.Throws(() => FileTransaction.Apply(root, changes, afterWrite: i => { if (i == 2) throw new IOException("simulated write failure"); })); Assert.Equal("original", File.ReadAllText(Path.Combine(root, "wtsapi32.dll"))); Assert.False(File.Exists(Path.Combine(root, "XFEWarframe/client.json"))); Assert.Equal("do-not-touch", File.ReadAllText(Path.Combine(root, "Warframe.x64.exe"))); Assert.False(File.Exists(Path.Combine(root, "XFEWarframe/transaction.json"))); } [Fact] public void CanceledTransactionRollsBackCompletedWrites() { using var cancel = new CancellationTokenSource(); var files = new Dictionary { ["one"] = [1], ["two"] = [2] }; Assert.Throws(() => FileTransaction.Apply(root, files, cancel.Token, _ => cancel.Cancel())); Assert.False(File.Exists(Path.Combine(root, "one"))); Assert.False(File.Exists(Path.Combine(root, "two"))); } [Fact] public void InterruptedInstallCanBeRecoveredAfterRestart() { WriteJournal("after"); FileTransaction.Recover(root); Assert.Equal("before", File.ReadAllText(Path.Combine(root, "target"))); } [Fact] public void RecoveryDoesNotOverwriteFilesChangedBySomeoneElse() { WriteJournal("third party edit"); Assert.Throws(() => FileTransaction.Recover(root)); Assert.Equal("third party edit", File.ReadAllText(Path.Combine(root, "target"))); } private void WriteJournal(string current) { File.WriteAllText(Path.Combine(root, "target"), current); Directory.CreateDirectory(Path.Combine(root, "XFEWarframe/backups")); File.WriteAllText(Path.Combine(root, "XFEWarframe/backups/original"), "before"); var journal = new TransactionJournal { Changes = [new("target", "XFEWarframe/backups/original", Convert.ToHexString(SHA256.HashData("before"u8)), Convert.ToHexString(SHA256.HashData("after"u8)))] }; File.WriteAllText(Path.Combine(root, "XFEWarframe/transaction.json"), JsonSerializer.Serialize(journal, ClientJson.Default.TransactionJournal)); } [Fact] public void InvalidClientProducesAnActionableReportWithoutChangingIt() { var executable = Path.Combine(root, "Warframe.x64.exe"); File.WriteAllText(executable, "not a PE"); var before = Compatibility.Hash(executable); var report = Compatibility.Inspect(root); Assert.False(report.Compatible); Assert.NotEmpty(report.Problems); Assert.Equal(before, Compatibility.Hash(executable)); } }