Initial commit
This commit is contained in:
commit
d5d9fbf47c
1 changed files with 942 additions and 0 deletions
44
Tests/BlockMelonLoaderTests.cs
Normal file
44
Tests/BlockMelonLoaderTests.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using BlockMelonLoader;
|
||||
|
||||
internal static class BlockMelonLoaderTests
|
||||
{
|
||||
internal static void Run()
|
||||
{
|
||||
var alerts = 0;
|
||||
var terminations = 0;
|
||||
Action<string> alert = message =>
|
||||
{
|
||||
if (!message.Contains("SampleMod") || !message.Contains("Unity Mod Manager"))
|
||||
throw new Exception("Missing mod name or installation guidance.");
|
||||
alerts++;
|
||||
};
|
||||
Action<string> terminate = message =>
|
||||
{
|
||||
if (alerts == 0) throw new Exception("Termination preceded notification.");
|
||||
terminations++;
|
||||
};
|
||||
|
||||
var unrelated = AppDomain.CurrentDomain.DefineDynamicAssembly(
|
||||
new AssemblyName("MelonLoader.Helper"), AssemblyBuilderAccess.Run);
|
||||
LoaderGuard.EnsureAllowed("SampleMod", new[] { typeof(LoaderGuard).Assembly, unrelated }, alert, terminate);
|
||||
if (alerts != 0 || terminations != 0) throw new Exception("Blocked an unrelated assembly.");
|
||||
|
||||
var loader = AppDomain.CurrentDomain.DefineDynamicAssembly(
|
||||
new AssemblyName("melonloader"), AssemblyBuilderAccess.Run);
|
||||
LoaderGuard.EnsureAllowed("SampleMod", new[] { loader }, alert, terminate);
|
||||
if (alerts != 1 || terminations != 1) throw new Exception("MelonLoader was not blocked.");
|
||||
|
||||
try
|
||||
{
|
||||
LoaderGuard.EnsureAllowed("SampleMod", new[] { loader },
|
||||
message => { throw new InvalidOperationException("Dialog unavailable."); },
|
||||
message => terminations++);
|
||||
throw new Exception("Expected the simulated dialog failure.");
|
||||
}
|
||||
catch (InvalidOperationException) { }
|
||||
if (terminations != 2) throw new Exception("Dialog failure bypassed termination.");
|
||||
}
|
||||
}
|
||||
Reference in a new issue