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

XFEWarframe

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

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Buffers.Binary;
using System.Text;
using Xunit;

namespace XFEWarframe.Client.Tests;

public sealed class SignatureResolverTests
{
    [Fact]
    public void ResourceReferencesInDifferentColdFragmentsResolveToThePrimaryFunction()
    {
        var file = new TestImage();
        file.Function(0x1000, 0x1040, 0x2300);
        file.Function(0x1100, 0x1140, 0x2320, (0x1000, 0x1040, 0x2300), oddCodes: true);
        file.Function(0x1200, 0x1240, 0x2350, (0x1100, 0x1140, 0x2320));
        file.Lea(0x1108, 0x2000, 12);
        file.Lea(0x1208, 0x2040, 3);
        using var image = new BinaryImage(file.Bytes);
        Assert.Equal(0x1000, image.FunctionStart(0x1108));
        Assert.Equal(0x1000, image.FunctionStart(0x1208));
        Assert.Equal([0x1000], SignatureResolver.AnchoredFunctions(image, "NRS Server ", "/NetTest/Firewall"));
    }

    [Theory]
    [InlineData(0, 1)]
    [InlineData(2, 11)]
    [InlineData(12, 13)]
    [InlineData(14, 15)]
    public void AnchorResolutionDoesNotDependOnCompilerRegisterAllocation(int first, int second)
    {
        var file = new TestImage();
        file.Function(0x1000, 0x1100, 0x2300);
        file.Lea(0x1010, 0x2000, first);
        file.Lea(0x1050, 0x2040, second);
        using var image = new BinaryImage(file.Bytes);
        Assert.Equal([0x1000], SignatureResolver.AnchoredFunctions(image, "NRS Server ", "/NetTest/Firewall"));
    }

    [Fact]
    public void AmbiguousResourcesReturnAllCandidatesInsteadOfPickingAnArbitraryAddress()
    {
        var file = new TestImage();
        file.Function(0x1000, 0x1100, 0x2300);
        file.Function(0x1200, 0x1300, 0x2320);
        foreach (var rva in new[] { 0x1010, 0x1210 }) { file.Lea(rva, 0x2000, 1); file.Lea(rva + 32, 0x2040, 2); }
        using var image = new BinaryImage(file.Bytes);
        Assert.Equal([0x1000, 0x1200], SignatureResolver.AnchoredFunctions(image, "NRS Server ", "/NetTest/Firewall"));
    }

    [Fact]
    public void SeparateFunctionsWithOnlyOneAnchorEachAreNotAValidBinding()
    {
        var file = new TestImage();
        file.Function(0x1000, 0x1100, 0x2300);
        file.Function(0x1200, 0x1300, 0x2320);
        file.Lea(0x1010, 0x2000, 1); file.Lea(0x1210, 0x2040, 2);
        using var image = new BinaryImage(file.Bytes);
        Assert.Empty(SignatureResolver.AnchoredFunctions(image, "NRS Server ", "/NetTest/Firewall"));
        Assert.Empty(SignatureResolver.AnchoredFunctions(image, "NRS Server ", "missing resource"));
    }

    [Fact]
    public void ReferencesWithoutFunctionMetadataAreNotTreatedAsFunctionEntries()
    {
        var file = new TestImage();
        file.Lea(0x1010, 0x2000, 1); file.Lea(0x1040, 0x2040, 2);
        using var image = new BinaryImage(file.Bytes);
        Assert.Equal(0x1010, image.FunctionStart(0x1010));
        Assert.Empty(SignatureResolver.AnchoredFunctions(image, "NRS Server ", "/NetTest/Firewall"));
    }

    [Fact]
    public void CyclicChainedUnwindIsRejectedRatherThanFollowingItForever()
    {
        var file = new TestImage();
        file.Function(0x1000, 0x1100, 0x2300, (0x1000, 0x1100, 0x2300));
        using var image = new BinaryImage(file.Bytes);
        Assert.Throws<InvalidDataException>(() => image.FunctionStart(0x1010));
    }

    [Theory]
    [InlineData("/api/inventory.php")]
    [InlineData("/api/missionInventoryUpdate.php?x=1")]
    public void UnavailableMasteryOverrideIsNotAdvertisedToTheServer(string path)
    {
        var url = ClientProtocol.Redirect("https://old.test" + path, "https://private.test", "build", masteryCapOverride: false);
        Assert.Equal("https://private.test" + path, url);
        Assert.Contains("xpBasedLevelCapDisabled=1", ClientProtocol.Redirect("https://old.test" + path, "https://private.test", "build"));
    }

    // Harmless PE fixture containing data references and unwind metadata. No
    // game code, compiler prologue signatures or native execution is involved.
    private sealed class TestImage
    {
        public byte[] Bytes { get; } = new byte[0x1000];
        private int functions;
        public TestImage()
        {
            U16(0, 0x5A4D); I32(0x3C, 0x80); I32(0x80, 0x4550);
            U16(0x84, 0x8664); U16(0x86, 3); U16(0x94, 0xF0); U16(0x96, 0x22);
            U16(0x98, 0x20B); I32(0x98 + 32, 0x1000); I32(0x98 + 36, 0x200);
            I32(0x98 + 56, 0x4000); I32(0x98 + 60, 0x200); U16(0x98 + 68, 3); I32(0x98 + 108, 16);
            Section(0, ".text", 0x1000, 0x800, 0x200, 0x60000020);
            Section(1, ".rdata", 0x2000, 0x400, 0xA00, 0x40000040);
            Section(2, ".pdata", 0x3000, 0x200, 0xE00, 0x40000040);
            Encoding.ASCII.GetBytes("NRS Server \0").CopyTo(Bytes, Offset(0x2000));
            Encoding.ASCII.GetBytes("/NetTest/Firewall\0").CopyTo(Bytes, Offset(0x2040));
        }
        public void Function(int begin, int end, int unwind, (int Begin, int End, int Unwind)? parent = null, bool oddCodes = false)
        {
            I32(0x98 + 112 + 24, 0x3000); I32(0x98 + 112 + 28, ++functions * 12);
            var record = 0xE00 + (functions - 1) * 12;
            I32(record, begin); I32(record + 4, end); I32(record + 8, unwind);
            Bytes[Offset(unwind)] = parent is null ? (byte)1 : (byte)0x21;
            Bytes[Offset(unwind) + 2] = oddCodes ? (byte)3 : (byte)0;
            if (parent is { } target)
            {
                var next = Offset(unwind) + (oddCodes ? 12 : 4);
                I32(next, target.Begin); I32(next + 4, target.End); I32(next + 8, target.Unwind);
            }
        }
        public void Lea(int rva, int target, int register)
        {
            var offset = Offset(rva);
            Bytes[offset] = register < 8 ? (byte)0x48 : (byte)0x4C;
            Bytes[offset + 1] = 0x8D; Bytes[offset + 2] = (byte)(5 | (register & 7) << 3);
            I32(offset + 3, target - rva - 7);
        }
        private void Section(int index, string name, int rva, int size, int raw, int flags)
        {
            var offset = 0x188 + index * 40;
            Encoding.ASCII.GetBytes(name).CopyTo(Bytes, offset);
            I32(offset + 8, size); I32(offset + 12, rva); I32(offset + 16, size); I32(offset + 20, raw); I32(offset + 36, flags);
        }
        private static int Offset(int rva) => rva < 0x2000 ? rva - 0x1000 + 0x200 : rva < 0x3000 ? rva - 0x2000 + 0xA00 : rva - 0x3000 + 0xE00;
        private void I32(int offset, int value) => BinaryPrimitives.WriteInt32LittleEndian(Bytes.AsSpan(offset), value);
        private void U16(int offset, ushort value) => BinaryPrimitives.WriteUInt16LittleEndian(Bytes.AsSpan(offset), value);
    }
}