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

XFEWarframe

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

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

namespace XFEWarframe.Client.Tests;

public class GameTestProtocolTests
{
    [Theory]
    [InlineData("{\"slot\":4,\"pressed\":true}")]
    [InlineData("{\"slot\":-1,\"pressed\":false}")]
    [InlineData("{\"slot\":0,\"pressed\":1}")]
    [InlineData("{\"slot\":0,\"pressed\":true,\"address\":1}")]
    [InlineData("{\"slot\":0,\"pressed\":true,\"exclusive\":1}")]
    public void SkillInputRejectsInvalidOrExtraFields(string body) =>
        Assert.ThrowsAny<ArgumentException>(() => GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes(body)));
    [Fact] public void SkillInputAcceptsOnlyFourNormalSlots()
    {
        foreach (var slot in Enumerable.Range(0, 4))
            Assert.Equal(slot, GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes($$"""{"slot":{{slot}},"pressed":true}"""))["slot"]!.GetValue<int>());
    }
    [Fact] public void DiagnosticInputIsolationIsExplicit()
    {
        Assert.True(GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes("""{"slot":0,"pressed":true,"exclusive":true}"""))["exclusive"]!.GetValue<bool>());
    }
    [Fact] public void EngineCallsCanOnlyReferenceEarlierResults()
    {
        var accepted = GameTestProtocol.Parse("POST lua", Encoding.UTF8.GetBytes("""
            {"steps":[{"global":"gRegion"},{"from":0,"field":"GetLocalPlayerAvatar"},{"call":1,"args":[{"ref":0}]}]}
            """));
        Assert.Equal(3, accepted["steps"]!.AsArray().Count);
    }

    [Theory]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"from\":2,\"field\":\"GetLocalPlayerAvatar\"}]}")]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\",\"call\":0}]}")]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[{\"ref\":1}]}]}")]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[{\"ref\":0,\"address\":123}]}]}")]
    [InlineData("{\"steps\":[{\"global\":\"0xBAD\"}]}")]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\",\"inspect\":1}]}")]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[1e100]}]}")]
    [InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[\"a\\u0000b\"]}]}")]
    [InlineData("{\"steps\":[]}")]
    public void RejectsMalformedCommandsBeforeAnyNativeCalls(string body) =>
        Assert.ThrowsAny<ArgumentException>(() => GameTestProtocol.Parse("POST lua", Encoding.UTF8.GetBytes(body)));

    [Fact] public void LoginRequiresAHashAndDoesNotAcceptPlaintextOrExtraOptions()
    {
        var hash = new string('a', 128);
        Assert.Equal(hash, GameTestProtocol.Parse("POST login", Encoding.UTF8.GetBytes($$"""{"email":"test@example.invalid","passwordHash":"{{hash}}"}"""))["passwordHash"]!.GetValue<string>());
        foreach (var json in new[] { "{\"email\":\"test@example.invalid\",\"password\":\"secret\"}",
                     $$"""{"email":"test@example.invalid","passwordHash":"{{hash}}","force":true}""",
                     "{\"email\":\"test@example.invalid\",\"passwordHash\":\"short\"}" })
            Assert.Throws<ArgumentException>(() => GameTestProtocol.Parse("POST login", Encoding.UTF8.GetBytes(json)));
        Assert.Throws<ArgumentException>(() => GameTestProtocol.Parse("POST anything", []));
    }
}