Files
mandy/tests/test_play_linux_wine_check.py

119 lines
6.2 KiB
Python

"""scripts/play-linux.sh says when its Wine watches the title's memory with a signal per page.
python -I tests/test_play_linux_wine_check.py
Wine 10.7 and newer and Proton's Wine track write-watched pages in the kernel (userfaultfd and PAGEMAP_SCAN, Linux 6.7
or newer); older Wine takes a signal at every first write (docs/linux.md, "Which Wine"). The launcher runs here with a stand-in
`wine` and `uname` on PATH, a stand-in ntdll.so beside the stand-in Wine where a case needs one, and placeholder
runtime and VKD3D-Proton files: no Wine, GPU or game files. POSIX only.
"""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
LAUNCHER = Path(__file__).resolve().parents[1] / "scripts" / "play-linux.sh"
SH = shutil.which("sh") if os.name == "posix" else None
NOTE = "watches the title's memory with a signal per page"
@unittest.skipUnless(SH, "POSIX shell required")
class PlayLinuxWineCheckTest(unittest.TestCase):
def run_launcher(self, version, kernel, ntdll=None, extra=None, layout="lib/wine", linked=False):
"""stderr of one launch with `wine --version` printing `version`, `uname -r` printing `kernel` and, unless
`ntdll` is None, an ntdll.so of those bytes under `<root>/<layout>/x86_64-unix`, where a Wine installed in
`<root>/bin` keeps it. With `linked`, PATH reaches that Wine through a symbolic link in another folder, as
Debian's alternatives do."""
with tempfile.TemporaryDirectory(prefix="linux-wine-check-test-") as directory:
root = Path(directory)
(root / "prefix" / "drive_c" / "windows" / "system32").mkdir(parents=True)
bin_dir = root / "wine-root" / "bin"
bin_dir.mkdir(parents=True)
wine = bin_dir / "wine"
wine.write_text(f'#!/bin/sh\nif [ "$1" = --version ]; then echo "{version}"; fi\nexit 0\n',
encoding="utf-8", newline="\n")
uname = bin_dir / "uname"
uname.write_text(f'#!/bin/sh\necho "{kernel}"\n', encoding="utf-8", newline="\n")
for tool in (wine, uname):
tool.chmod(0o755)
path_dir = bin_dir
if linked:
path_dir = root / "alternatives"
path_dir.mkdir()
(path_dir / "wine").symlink_to(wine)
(path_dir / "uname").symlink_to(uname)
if ntdll is not None:
unix = root / "wine-root" / layout / "x86_64-unix"
unix.mkdir(parents=True)
(unix / "ntdll.so").write_bytes(ntdll)
game = root / "placeholder-title"
runtime = root / "placeholder-runtime"
vkd3d = root / "vkd3d"
vkd3d.mkdir()
for name in (game, runtime, vkd3d / "d3d12.dll", vkd3d / "d3d12core.dll"):
name.touch()
env = {
"PATH": str(path_dir) + os.pathsep + os.defpath,
"HOME": str(root),
"LEONIDA_WINEPREFIX": str(root / "prefix"),
"LEONIDA_RUNTIME": str(runtime),
"LEONIDA_VKD3D": str(vkd3d),
}
env.update(extra or {})
result = subprocess.run([SH, str(LAUNCHER), str(game)], env=env,
capture_output=True, text=True, timeout=30)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
return result.stderr
def assert_note(self, stderr, said):
(self.assertIn if said else self.assertNotIn)(NOTE, stderr)
def test_wine_before_10_7_is_named(self):
for version in ("wine-9.0 (Ubuntu 9.0~repack-4build3)", "wine-10.0", "wine-10.0.3", "wine-10.6 (Staging)"):
with self.subTest(version=version):
stderr = self.run_launcher(version, "6.18.44-generic")
self.assert_note(stderr, True)
self.assertIn(version, stderr)
def test_wine_10_7_and_newer_on_a_new_kernel_says_nothing(self):
for version in ("wine-10.7", "wine-10.20 (Staging)", "wine-11.0", "wine-11.0-rc1", "wine-12.1 (Staging)"):
with self.subTest(version=version):
self.assert_note(self.run_launcher(version, "6.18.44-generic"), False)
def test_a_kernel_before_6_7_is_named_even_with_a_new_wine(self):
for kernel in ("6.6.58-1-lts", "5.15.0-130-generic"):
with self.subTest(kernel=kernel):
self.assert_note(self.run_launcher("wine-11.0", kernel), True)
self.assert_note(self.run_launcher("wine-11.0", "6.7.0"), False)
self.assert_note(self.run_launcher("wine-11.0", "7.3.1-cachyos"), False)
def test_the_ntdll_beside_wine_decides_over_its_version(self):
# Proton's Wine reports an older version but carries the userfaultfd and PAGEMAP_SCAN path, whose messages
# name the ioctl (dlls/ntdll/unix/virtual.c, upstream from 10.7 and Valve's alike); a build without that
# code is named whatever its version says.
with_path = b"\x7fELF...ioctl(PAGEMAP_SCAN) failed, err %s.\n..."
without = b"\x7fELF..."
for layout in ("lib/wine", "lib64/wine", "lib/x86_64-linux-gnu/wine"):
for linked in (False, True):
with self.subTest(layout=layout, linked=linked):
self.assert_note(self.run_launcher("wine-10.0", "6.18.44-generic", ntdll=with_path,
layout=layout, linked=linked), False)
self.assert_note(self.run_launcher("wine-11.0", "6.18.44-generic", ntdll=without,
layout=layout, linked=linked), True)
# The code is there, but the kernel cannot run it: still signals.
self.assert_note(self.run_launcher("wine-10.0", "6.6.58-1-lts", ntdll=with_path), True)
def test_an_unknown_wine_says_nothing(self):
self.assert_note(self.run_launcher("wine-test", "6.18.44-generic"), False)
self.assert_note(self.run_launcher("", "6.18.44-generic"), False)
def test_leonida_wine_check_0_leaves_it_out(self):
stderr = self.run_launcher("wine-9.0", "6.18.44-generic", extra={"LEONIDA_WINE_CHECK": "0"})
self.assert_note(stderr, False)
if __name__ == "__main__":
unittest.main()