109 lines
7.4 KiB
C#
109 lines
7.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
|
|
[assembly: System.Runtime.Versioning.TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
|
|
|
|
// Exercises the real selector with isolated synthetic release pages. No release is
|
|
// created or downloaded, and the main launcher window is never started.
|
|
// Optional --live also queries the configured public Gitea endpoints.
|
|
internal static class CheckSnapshotUpdates {
|
|
private static int passed;
|
|
|
|
private static void Assert(bool condition, string description) {
|
|
if (!condition) throw new Exception("FAIL: " + description);
|
|
passed++;
|
|
Console.WriteLine("PASS: " + description);
|
|
}
|
|
|
|
private static string Tag(object release) {
|
|
if (release == null) return null;
|
|
return release.GetType().GetProperty("Item", new[] { typeof(string) })
|
|
.GetValue(release, new object[] { "tag_name" }).ToString();
|
|
}
|
|
|
|
private static bool Throws<T>(Action action) where T : Exception {
|
|
try { action(); }
|
|
catch (TargetInvocationException ex) { return ex.InnerException is T; }
|
|
return false;
|
|
}
|
|
|
|
[STAThread]
|
|
private static int Main(string[] args) {
|
|
try {
|
|
Assembly launcher = Assembly.LoadFrom(Path.GetFullPath(args[0]));
|
|
Activator.CreateInstance(launcher.GetType("PCL.Application", true), true);
|
|
Assembly.Load("MeloongCore").GetType("MeloongCore.Main", true).GetMethod("Init")
|
|
.Invoke(null, new object[] { "PCL" });
|
|
launcher.GetType("PCL.ModBase", true).GetMethod("RunInUi", Type.EmptyTypes).Invoke(null, null);
|
|
Type module = launcher.GetType("PCL.ModSecret", true);
|
|
MethodInfo select = module.GetMethod("GetLatestSnapshotRelease", BindingFlags.NonPublic | BindingFlags.Static);
|
|
|
|
int requests = 0;
|
|
Func<int, string> pages = page => {
|
|
requests++;
|
|
switch (page) {
|
|
case 1: return "[{\"tag_name\":\"v9.0.0\",\"prerelease\":false,\"draft\":false},{\"tag_name\":\"v0.2.9\",\"prerelease\":true,\"draft\":false},{\"tag_name\":\"v99.0.0\",\"prerelease\":true,\"draft\":true}]";
|
|
case 2: return "[{\"tag_name\":\"v0.2.10\",\"prerelease\":true,\"draft\":false},{\"tag_name\":\"invalid\",\"prerelease\":true,\"draft\":false},{\"tag_name\":\"v0.1.2\",\"prerelease\":true,\"draft\":false}]";
|
|
default: return "[]";
|
|
}
|
|
};
|
|
object selected = select.Invoke(null, new object[] { pages });
|
|
Assert(Tag(selected) == "v0.2.10", "select highest numeric snapshot version across unsorted pages; ignore stable, draft and invalid releases");
|
|
Assert(requests == 3, "continue short server pages until the first empty page");
|
|
|
|
selected = select.Invoke(null, new object[] { new Func<int, string>(page => "[]") });
|
|
Assert(selected == null, "empty release list has no snapshot update");
|
|
selected = select.Invoke(null, new object[] { new Func<int, string>(page => page == 1 ? "[{\"tag_name\":\"v0.1.1\",\"prerelease\":false,\"draft\":false}]" : "[]") });
|
|
Assert(selected == null, "stable-only repository is not offered as a snapshot update");
|
|
selected = select.Invoke(null, new object[] { new Func<int, string>(page => page == 1 ? "[{\"tag_name\":\"v0.3.0\",\"prerelease\":true,\"draft\":true}]" : "[]") });
|
|
Assert(selected == null, "unpublished draft snapshot is never offered");
|
|
|
|
MethodInfo compare = launcher.GetType("PCL.ModLauncherUpdate", true).GetMethod("ParseLauncherVersion");
|
|
Version installed = (Version)compare.Invoke(null, new object[] { launcher.GetType("PCL.ModBase", true).GetField("VersionBaseName").GetRawConstantValue() });
|
|
Type infoType = module.GetNestedType("LauncherUpdateInfo", BindingFlags.NonPublic);
|
|
object info = Activator.CreateInstance(infoType, true);
|
|
MethodInfo isNewer = module.GetMethod("IsUpdateNewer", BindingFlags.NonPublic | BindingFlags.Static);
|
|
infoType.GetField("VersionNumber").SetValue(info, new Version(0, 0, 0, 0));
|
|
Assert(!(bool)isNewer.Invoke(null, new[] { info }), "older release never triggers an automatic downgrade");
|
|
infoType.GetField("VersionNumber").SetValue(info, installed);
|
|
Assert(!(bool)isNewer.Invoke(null, new[] { info }), "same version is not offered as an update");
|
|
infoType.GetField("VersionNumber").SetValue(info, new Version(installed.Major + 1, 0, 0, 0));
|
|
Assert((bool)isNewer.Invoke(null, new[] { info }), "newer release is offered through the shared update comparison");
|
|
|
|
Assert(Throws<IOException>(() => select.Invoke(null, new object[] { new Func<int, string>(page => { throw new IOException("Synthetic network failure"); }) })), "network failure is reported instead of claiming there are no updates");
|
|
requests = 0;
|
|
Assert(Throws<IOException>(() => select.Invoke(null, new object[] { new Func<int, string>(page => { requests++; return "[{\"tag_name\":\"v0.2.0\",\"prerelease\":true,\"draft\":false}]"; }) })), "bounded pagination never offers a partially inspected release list");
|
|
Assert(requests == 100, "pagination has a finite request limit");
|
|
|
|
Assembly json = Assembly.Load("Newtonsoft.Json");
|
|
MethodInfo parseJson = json.GetType("Newtonsoft.Json.Linq.JObject", true).GetMethod("Parse", new[] { typeof(string) });
|
|
MethodInfo parseInfo = module.GetMethod("ParseLauncherUpdate", BindingFlags.NonPublic | BindingFlags.Static);
|
|
object stableJson = parseJson.Invoke(null, new object[] { "{\"prerelease\":false,\"draft\":false}" });
|
|
object snapshotJson = parseJson.Invoke(null, new object[] { "{\"prerelease\":true,\"draft\":false}" });
|
|
object draftJson = parseJson.Invoke(null, new object[] { "{\"prerelease\":true,\"draft\":true}" });
|
|
Assert(Throws<FormatException>(() => parseInfo.Invoke(null, new[] { stableJson, (object)true })), "snapshot parser rejects a stable release");
|
|
Assert(Throws<FormatException>(() => parseInfo.Invoke(null, new[] { snapshotJson, (object)false })), "stable parser rejects a snapshot release");
|
|
Assert(Throws<FormatException>(() => parseInfo.Invoke(null, new[] { draftJson, (object)true })), "release parser rejects drafts before downloading or caching");
|
|
|
|
if (args.Length > 1 && args[1] == "--live") {
|
|
object stable = Task.Run(() => module.GetMethod("GetLatestLauncherUpdate", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null)).GetAwaiter().GetResult();
|
|
Assert(stable != null, "live stable endpoint resolves a release with verified checksum metadata");
|
|
object snapshot = Task.Run(() => module.GetMethod("GetLatestSnapshotLauncherUpdate", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null)).GetAwaiter().GetResult();
|
|
if (snapshot == null) {
|
|
Console.WriteLine("Live snapshot endpoint: no published pre-release.");
|
|
} else {
|
|
Console.WriteLine("Live snapshot endpoint: " + infoType.GetField("TagName").GetValue(snapshot));
|
|
}
|
|
}
|
|
|
|
Console.WriteLine(passed + " snapshot update checks passed.");
|
|
return 0;
|
|
} catch (Exception ex) {
|
|
Console.Error.WriteLine(ex);
|
|
return 1;
|
|
}
|
|
}
|
|
}
|