DreamWeaveWeb (constants-only destinations and launch URL) and LaunchWire (/weblogin request, [WEB] reply parser that refuses player-authored copies). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
7.1 KiB
C#
130 lines
7.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Xml.Linq;
|
|
using RynthCore.Plugin.RynthDreamWeave;
|
|
using RynthCore.Plugin.ServerPack;
|
|
using RynthCore.RynthAiTests.Harness;
|
|
|
|
namespace RynthServerPack.Tests;
|
|
|
|
// DreamWeave's web buttons, the pure part: the destinations and the "/weblogin" reply's wire text.
|
|
internal static class DreamWeaveWebTests
|
|
{
|
|
public static void Register(Runner r)
|
|
{
|
|
r.Add("dreamweave web: a player's copy of the launch line is never taken", PlayerCopy);
|
|
r.Add("dreamweave web: the launch URL is built from constants and a clean code", UrlFromConstants);
|
|
r.Add("dreamweave web: destination ids are verbs no one else uses", IdsAreFree);
|
|
r.Add("dreamweave web: ACE's refusals parse as refusals, other lines are ignored", Refusals);
|
|
}
|
|
|
|
private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars
|
|
private const string Line = "[WEB] Launch code: " + Code + " (single use, expires in 60 seconds)";
|
|
|
|
private static bool Parse(string? text, int type, out string code, out string refusal)
|
|
=> LaunchWire.TryParse(text, type, out code, out refusal);
|
|
|
|
private static void PlayerCopy()
|
|
{
|
|
Check.Eq(Code.Length, LaunchWire.CodeLength, "fixture code is 43 chars");
|
|
|
|
foreach (int type in new[] { 0x02, 0x03, 0x08, 0x0C, 0x13 })
|
|
Check.True(!Parse(Line, type, out _, out _), "player-authored chat type 0x" + type.ToString("X2") + " refused");
|
|
|
|
Check.True(!Parse("Bob tells you, " + Line, 0x05, out _, out _), "a tell wrapper with a system type refused");
|
|
Check.True(!Parse("hello " + Line, 0x05, out _, out _), "the lead mid-line refused");
|
|
|
|
Check.True(Parse(Line, 0x05, out string code, out string refusal), "valid line, system type");
|
|
Check.Eq(code, Code, "the code");
|
|
Check.Eq(refusal, "", "no refusal on success");
|
|
Check.True(Parse(Line, -1, out code, out _), "unknown type: the anchor alone protects");
|
|
Check.Eq(code, Code, "the code (type -1)");
|
|
Check.True(!Parse(null, 0x05, out _, out _), "null refused");
|
|
Check.True(!Parse("", 0x05, out _, out _), "empty refused");
|
|
}
|
|
|
|
private static void UrlFromConstants()
|
|
{
|
|
string head = "[WEB] Launch code: ";
|
|
Check.True(!Parse(head + Code.Substring(1) + " (single use)", 0x05, out _, out _), "42-char code refused");
|
|
Check.True(!Parse(head + Code + "A (single use)", 0x05, out _, out _), "44-char run (no space after 43) refused");
|
|
Check.True(!Parse(head + Code + "&to=//evil.com", 0x05, out _, out _), "code followed by &to= refused");
|
|
Check.True(!Parse(head + Code, 0x05, out _, out _), "no trailing space refused");
|
|
Check.True(!Parse(head + Code.Substring(0, 42) + "! (single use)", 0x05, out _, out _), "bad code char refused");
|
|
Check.True(!Parse(head + Code.Substring(0, 42) + "% (single use)", 0x05, out _, out _), "percent in code refused");
|
|
|
|
// None of the malformed success lines may leak out as a refusal either.
|
|
Check.True(!Parse(head + Code.Substring(1) + " x", 0x05, out _, out string r1) && r1 == "", "malformed success is not a refusal");
|
|
|
|
WebDestination suit = DreamWeaveWeb.ById("suit")!;
|
|
string url = DreamWeaveWeb.LaunchUrl(Code, suit);
|
|
Check.True(url.StartsWith(DreamWeaveWeb.SiteBase + "/launch?code=", StringComparison.Ordinal), "url starts with the base");
|
|
Check.Eq(url, DreamWeaveWeb.SiteBase + "/launch?code=" + Code + "&to=%2Ftools%2Fsuit", "exact suit url");
|
|
Check.True(url.EndsWith("%2Ftools%2Fsuit", StringComparison.Ordinal), "ends with the escaped path");
|
|
Check.Eq(url.Split("&to=").Length - 1, 1, "exactly one &to=");
|
|
|
|
foreach (WebDestination d in DreamWeaveWeb.Destinations)
|
|
{
|
|
string u = DreamWeaveWeb.LaunchUrl(Code, d);
|
|
Check.True(ServerWebOpen.IsAllowedUrl(u, DreamWeaveWeb.AllowedHosts), d.Id + ": url passes the allowlist");
|
|
Check.True(d.Path.StartsWith("/", StringComparison.Ordinal) && !d.Path.StartsWith("//", StringComparison.Ordinal), d.Id + ": path is rooted, not //");
|
|
Check.True(d.Path.IndexOfAny(new[] { '?', '#', '\\' }) < 0, d.Id + ": no ? # or backslash in path");
|
|
Check.True(d.Label.Length > 0 && d.Tooltip.Length > 0, d.Id + ": label and tooltip");
|
|
}
|
|
Check.True(DreamWeaveWeb.ById("SUIT") == suit, "ById is case-insensitive");
|
|
Check.True(DreamWeaveWeb.ById("nope") == null && DreamWeaveWeb.ById(null) == null && DreamWeaveWeb.ById("") == null, "unknown id -> null");
|
|
Check.Eq(LaunchWire.RequestFor(suit), "/weblogin", "request is /weblogin");
|
|
}
|
|
|
|
private static void IdsAreFree()
|
|
{
|
|
var seen = new HashSet<string>();
|
|
string[] serverIds = ServerPluginIds();
|
|
Check.True(serverIds.Length > 0, "found the server plugin ids");
|
|
foreach (WebDestination d in DreamWeaveWeb.Destinations)
|
|
{
|
|
Check.True(d.Id.Length >= 1 && d.Id.Length <= 16 && d.Id.All(c => c >= 'a' && c <= 'z'), d.Id + ": 1-16 chars [a-z]");
|
|
Check.True(Array.IndexOf(ServerPackCommands.SharedVerbs, d.Id) < 0, d.Id + ": not a shared verb");
|
|
Check.True(Array.IndexOf(serverIds, d.Id) < 0, d.Id + ": not a server plugin id");
|
|
Check.True(seen.Add(d.Id), d.Id + ": unique");
|
|
}
|
|
}
|
|
|
|
private static void Refusals()
|
|
{
|
|
string[] lines =
|
|
{
|
|
"[WEB] Signing in to the website from the game is turned off on this server.",
|
|
"[WEB] The website sign-in service is not available right now.",
|
|
"[WEB] You are requesting website sign-in codes too quickly. Wait a minute and try again.",
|
|
};
|
|
foreach (string l in lines)
|
|
{
|
|
Check.True(Parse(l, 0x05, out string code, out string refusal), "refusal parses: " + l);
|
|
Check.Eq(code, "", "no code in a refusal");
|
|
Check.Eq(refusal, l.Substring(LaunchWire.ReplyLead.Length), "refusal text after the lead");
|
|
Check.True(!Parse(l, 0x02, out _, out _), "a player's copy of a refusal is ignored");
|
|
}
|
|
Check.True(!Parse("You say, \"hi\"", 0x05, out _, out _), "other chat ignored");
|
|
Check.True(!Parse("[WEBX] Launch code: " + Code, 0x05, out _, out _), "lookalike lead ignored");
|
|
}
|
|
|
|
private static string[] ServerPluginIds()
|
|
{
|
|
string? dir = AppContext.BaseDirectory;
|
|
while (dir != null && !Directory.Exists(Path.Combine(dir, "Plugins", "Shared", "ServerPack")))
|
|
dir = Path.GetDirectoryName(dir);
|
|
if (dir == null) throw new DirectoryNotFoundException("Plugins not found above " + AppContext.BaseDirectory);
|
|
var ids = new List<string>();
|
|
foreach (string p in Directory.GetDirectories(Path.Combine(dir, "Plugins"), "RynthCore.Plugin.Rynth*"))
|
|
{
|
|
string csproj = Path.Combine(p, Path.GetFileName(p) + ".csproj");
|
|
if (!File.Exists(csproj)) continue;
|
|
string id = XDocument.Load(csproj).Descendants("RynthPluginServerId").FirstOrDefault()?.Value.Trim() ?? "";
|
|
if (id.Length > 0) ids.Add(id);
|
|
}
|
|
return ids.ToArray();
|
|
}
|
|
}
|