XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using XFEWarframe.Client;
using Xunit;

namespace XFEWarframe.Client.Tests;

public sealed class RuntimeVersionTests
{
    [Theory]
    [InlineData(2, false)]
    [InlineData(3, true)]
    public async Task LauncherRejectsServerWithoutModernGameLoginFix(int apiVersion, bool accepted)
    {
        using var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(10));
        using var listener = new TcpListener(IPAddress.Loopback, 0);
        listener.Start();
        var origin = "http://127.0.0.1:" + ((IPEndPoint)listener.LocalEndpoint).Port;
        var serve = Task.Run(async () =>
        {
            using var connection = await listener.AcceptTcpClientAsync(cancellation.Token);
            await using var stream = connection.GetStream();
            using var reader = new StreamReader(stream, leaveOpen: true);
            while (!string.IsNullOrEmpty(await reader.ReadLineAsync(cancellation.Token))) { }
            var body = JsonSerializer.SerializeToUtf8Bytes(new { service = "XFEWarframe", status = "ok", apiVersion, serverName = "Test", gameUrl = "https://127.0.0.1:8443" });
            var header = Encoding.ASCII.GetBytes($"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {body.Length}\r\nConnection: close\r\n\r\n");
            await stream.WriteAsync(header, cancellation.Token);
            await stream.WriteAsync(body, cancellation.Token);
        }, cancellation.Token);
        if (accepted) Assert.Equal("Test", (await ServerConnection.ReadAsync(origin, cancellation.Token)).ServerName);
        else Assert.Contains("游戏登录修复", (await Assert.ThrowsAsync<InvalidDataException>(() => ServerConnection.ReadAsync(origin, cancellation.Token))).Message);
        await serve;
    }

    [Theory]
    [InlineData("1.0.0", false)]
    [InlineData("1.1.1", false)]
    [InlineData("invalid", false)]
    [InlineData("1.1.2", false)]
    [InlineData("1.2.0", false)]
    [InlineData("1.3.0", false)]
    [InlineData("1.3.1", false)]
    [InlineData("1.4.0", false)]
    [InlineData("1.4.1", false)]
    [InlineData("1.4.2", false)]
    [InlineData("1.4.3", false)]
    [InlineData("1.4.7", false)]
    [InlineData("1.4.8", false)]
    [InlineData("1.4.9", false)]
    [InlineData("1.4.12", false)]
    [InlineData("1.4.13", false)]
    [InlineData("1.4.14", false)]
    [InlineData(ClientInstaller.MinimumBridgeVersion, true)]
    public void InstalledLegacyBridgeIsRejectedBeforeLaunchingGame(string version, bool accepted)
    {
        var root = Path.Combine(Path.GetTempPath(), "xfe-version-test-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(Path.Combine(root, "XFEWarframe"));
        try
        {
            var executable = Path.Combine(root, "Warframe.x64.exe");
            File.WriteAllText(executable, "test-only-placeholder");
            var hash = Compatibility.Hash(executable);
            var state = new InstallationState { Version = version, GameSha256 = hash };
            var statePath = ClientInstaller.Managed(root, ClientInstaller.StateFile);
            File.WriteAllText(statePath, JsonSerializer.Serialize(state, ClientJson.Default.InstallationState));
            File.WriteAllText(ClientInstaller.Managed(root, ClientInstaller.ConfigFile), JsonSerializer.Serialize(new ClientConfiguration(), ClientJson.Default.ClientConfiguration));
            if (accepted) Assert.NotNull(ClientInstaller.VerifyInstalled(root));
            else Assert.Contains(ClientInstaller.MinimumBridgeVersion, Assert.Throws<InvalidDataException>(() => ClientInstaller.VerifyInstalled(root)).Message);
            Assert.Equal(hash, Compatibility.Hash(executable));
            Assert.Equal(version, ClientInstaller.ReadState(root)!.Version);
        }
        finally
        {
            var allowed = Path.GetFullPath(Path.GetTempPath());
            if (Path.GetFullPath(root).StartsWith(allowed, StringComparison.OrdinalIgnoreCase)) Directory.Delete(root, true);
        }
    }
}