using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using Xunit;
namespace XFEWarframe.Client.Tests;
public sealed class GameLauncherTests
{
[Fact]
[SupportedOSPlatform("windows")]
public void FindsNativeImagesBeforeSuspendedProcessLoaderInitialization()
{
if (!OperatingSystem.IsWindows() || !Environment.Is64BitProcess) return;
var executable = Path.Combine(Environment.SystemDirectory, "rundll32.exe");
var startup = new Win32.Startup { Size = Marshal.SizeOf<Win32.Startup>() };
// Never resume this child or execute rundll32: only inspect its initial mappings.
if (!Win32.CreateProcess(executable, new StringBuilder($"\"{executable}\""), 0, 0, false,
0x08000004, 0, Environment.SystemDirectory, ref startup, out var child))
throw new Win32Exception();
try
{
var native = RemoteModules.FindImageBase(child.Process, "ntdll.dll");
var application = RemoteModules.FindImageBase(child.Process, "rundll32.exe");
Assert.NotEqual(0, native);
Assert.NotEqual(native, application);
foreach (var address in new[] { native, application })
{
var signature = new byte[2];
Assert.True(ReadProcessMemory(child.Process, address, signature, 2, out var read));
Assert.Equal((nuint)2, read);
Assert.Equal(new byte[] { (byte)'M', (byte)'Z' }, signature);
}
Assert.Throws<IOException>(() => RemoteModules.FindImageBase(child.Process, "xfe-module-that-does-not-exist.dll"));
using var canceled = new CancellationTokenSource();
canceled.Cancel();
Assert.Throws<OperationCanceledException>(() => RemoteModules.FindImageBase(child.Process, "ntdll.dll", canceled.Token));
}
finally
{
Win32.TerminateProcess(child.Process, 0);
Win32.WaitForSingleObject(child.Process, 5000);
Win32.CloseHandle(child.Thread);
Win32.CloseHandle(child.Process);
}
}
[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ReadProcessMemory(nint process, nint address, byte[] buffer, nuint size, out nuint read);
}
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using Xunit;
namespace XFEWarframe.Client.Tests;
public sealed class GameLauncherTests
{
[Fact]
[SupportedOSPlatform("windows")]
public void FindsNativeImagesBeforeSuspendedProcessLoaderInitialization()
{
if (!OperatingSystem.IsWindows() || !Environment.Is64BitProcess) return;
var executable = Path.Combine(Environment.SystemDirectory, "rundll32.exe");
var startup = new Win32.Startup { Size = Marshal.SizeOf<Win32.Startup>() };
// Never resume this child or execute rundll32: only inspect its initial mappings.
if (!Win32.CreateProcess(executable, new StringBuilder($"\"{executable}\""), 0, 0, false,
0x08000004, 0, Environment.SystemDirectory, ref startup, out var child))
throw new Win32Exception();
try
{
var native = RemoteModules.FindImageBase(child.Process, "ntdll.dll");
var application = RemoteModules.FindImageBase(child.Process, "rundll32.exe");
Assert.NotEqual(0, native);
Assert.NotEqual(native, application);
foreach (var address in new[] { native, application })
{
var signature = new byte[2];
Assert.True(ReadProcessMemory(child.Process, address, signature, 2, out var read));
Assert.Equal((nuint)2, read);
Assert.Equal(new byte[] { (byte)'M', (byte)'Z' }, signature);
}
Assert.Throws<IOException>(() => RemoteModules.FindImageBase(child.Process, "xfe-module-that-does-not-exist.dll"));
using var canceled = new CancellationTokenSource();
canceled.Cancel();
Assert.Throws<OperationCanceledException>(() => RemoteModules.FindImageBase(child.Process, "ntdll.dll", canceled.Token));
}
finally
{
Win32.TerminateProcess(child.Process, 0);
Win32.WaitForSingleObject(child.Process, 5000);
Win32.CloseHandle(child.Thread);
Win32.CloseHandle(child.Process);
}
}
[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ReadProcessMemory(nint process, nint address, byte[] buffer, nuint size, out nuint read);
}