Files
DroidDeck/tools/tests/test_fex_install.py
T
maxjivi05 d3349f848f Route anti-cheat games to x86-64 Proton only on a capable FEX
The FEX build now lists its patches in share/fex-emu/.droiddeck-patches.
Anti-cheat routing needs the fixed guest memory patch in that list and
otherwise keeps the launch on ARM64, as an older FEX cannot map the
anti-cheat helper buffer above the host address limit. Installing a FEX
build without the list removes a list left by an earlier build.
2026-12-03 17:10:14 -05:00

152 lines
7.5 KiB
Python

import hashlib
import os
from pathlib import Path
import subprocess
import tempfile
import textwrap
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-fex-update"
SOURCE = SCRIPT.read_text()
START = SOURCE.index("fex_install() {")
END = SOURCE.index("\n}\n", START) + 3
FEX_INSTALL = SOURCE[START:END]
THUNK_DIRS = (("share/fex-emu/GuestThunks_32", "libGL-guest.so"), ("lib/fex-emu/HostThunks_32", "libGL-host.so"))
HARNESS = textwrap.dedent("""\
set -eu
# The archive comes from PKG; the installed tree loads when INSTALLED_LOADS=1, any staged tree always does.
curl() { local out=; while [ $# -gt 0 ]; do [ "$1" = -o ] && out=$2; shift; done; cp "$PKG" "$out"; }
fex_loads() { case $1 in "$ROOT"/) [ "${INSTALLED_LOADS:-1}" = 1 ] ;; *) true ;; esac; }
""")
class FexInstallTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
base = Path(self.temp.name)
self.root = base / "root"
old = self.root / "usr/local"
(old / "bin").mkdir(parents=True)
(old / "bin/FEX").write_text("old FEX\n")
(old / "share/fex-emu").mkdir(parents=True)
(old / "share/fex-emu/.droiddeck-tag").write_text("fex-2610-r5\n")
new = base / "pkg/usr/local"
(new / "bin").mkdir(parents=True)
(new / "bin/FEX").write_text("new FEX\n")
(new / "lib").mkdir()
(new / "lib/libxxhash.so.0").write_text("lib\n")
self.pkg = base / "fex.tzst"
self.pack()
def pack(self):
base = Path(self.temp.name)
subprocess.run(f"tar -C '{base / 'pkg'}' -c . | zstd -q -f -o '{self.pkg}'", shell=True, check=True)
self.sha = hashlib.sha256(self.pkg.read_bytes()).hexdigest()
def add_old_thunks(self):
old, new = self.root / "usr/local", Path(self.temp.name) / "pkg/usr/local"
for side, name in THUNK_DIRS:
(new / side).mkdir(parents=True)
(new / side / name).write_text("new thunk\n")
(old / side).mkdir(parents=True)
(old / side / name).write_text("old thunk\n")
(old / side / "libwayland-client-thunk.so").write_text("stale\n")
(old / "share/fex-emu/GuestThunks").mkdir()
(old / "share/fex-emu/GuestThunks/libkept-guest.so").write_text("kept\n")
self.pack()
def install(self, installed_loads, sha=None):
body = FEX_INSTALL.replace("local tag=$1 sha=$2 urls=$3 dst=/ ", "local tag=$1 sha=$2 urls=$3 dst=$ROOT/ ")
self.assertNotEqual(body, FEX_INSTALL)
script = HARNESS + body + 'if fex_install fex-2610-r6 "$SHA" u; then echo OK; else echo FAILED; fi\n'
env = dict(os.environ, ROOT=str(self.root), PKG=str(self.pkg), SHA=sha or self.sha,
INSTALLED_LOADS="1" if installed_loads else "0")
out = subprocess.run(["bash", "-c", script], env=env, text=True, capture_output=True, timeout=30)
self.assertEqual(0, out.returncode, out.stderr)
return out.stdout.strip().splitlines()[-1], out.stderr
def tag(self):
return (self.root / "usr/local/share/fex-emu/.droiddeck-tag").read_text().strip()
def test_a_build_that_loads_once_installed_replaces_the_old_one(self):
result, _ = self.install(installed_loads=True)
self.assertEqual("OK", result)
self.assertEqual("new FEX\n", (self.root / "usr/local/bin/FEX").read_text())
self.assertTrue((self.root / "usr/local/lib/libxxhash.so.0").is_file())
self.assertEqual("fex-2610-r6", self.tag())
def test_an_installed_copy_that_does_not_load_puts_the_old_files_back(self):
result, err = self.install(installed_loads=False)
self.assertEqual("FAILED", result)
self.assertIn("putting the previous files back", err)
self.assertEqual("old FEX\n", (self.root / "usr/local/bin/FEX").read_text())
self.assertFalse((self.root / "usr/local/lib/libxxhash.so.0").exists())
self.assertEqual("fex-2610-r5", self.tag())
def test_a_tag_that_cannot_be_written_puts_the_old_files_back(self):
share = self.root / "usr/local/share/fex-emu"
(share / ".droiddeck-tag").unlink()
share.rmdir()
share.write_text("not a folder\n")
result, err = self.install(installed_loads=True)
self.assertEqual("FAILED", result)
self.assertIn("putting the previous files back", err)
self.assertEqual("old FEX\n", (self.root / "usr/local/bin/FEX").read_text())
self.assertFalse((self.root / "usr/local/lib/libxxhash.so.0").exists())
def test_thunks_the_new_build_no_longer_ships_are_removed(self):
self.add_old_thunks()
self.assertEqual("OK", self.install(installed_loads=True)[0])
for side, name in THUNK_DIRS:
with self.subTest(side=side):
self.assertEqual("new thunk\n", (self.root / "usr/local" / side / name).read_text())
self.assertFalse((self.root / "usr/local" / side / "libwayland-client-thunk.so").exists())
def test_a_thunk_folder_the_new_build_does_not_ship_is_left_alone(self):
self.add_old_thunks()
self.assertEqual("OK", self.install(installed_loads=True)[0])
self.assertEqual("kept\n", (self.root / "usr/local/share/fex-emu/GuestThunks/libkept-guest.so").read_text())
def test_a_failed_install_puts_removed_thunks_back(self):
self.add_old_thunks()
self.assertEqual("FAILED", self.install(installed_loads=False)[0])
for side, name in THUNK_DIRS:
with self.subTest(side=side):
self.assertEqual("old thunk\n", (self.root / "usr/local" / side / name).read_text())
self.assertEqual("stale\n", (self.root / "usr/local" / side / "libwayland-client-thunk.so").read_text())
def test_a_build_without_a_patch_list_drops_the_old_one(self):
marker = self.root / "usr/local/share/fex-emu/.droiddeck-patches"
marker.write_text("fixed-guest-memory-above-host-address-limit\n")
self.assertEqual("OK", self.install(installed_loads=True)[0])
self.assertFalse(marker.exists())
def test_a_failed_install_keeps_the_old_patch_list(self):
marker = self.root / "usr/local/share/fex-emu/.droiddeck-patches"
marker.write_text("fixed-guest-memory-above-host-address-limit\n")
self.assertEqual("FAILED", self.install(installed_loads=False)[0])
self.assertEqual("fixed-guest-memory-above-host-address-limit\n", marker.read_text())
def test_a_build_with_a_patch_list_installs_it(self):
(self.root / "usr/local/share/fex-emu/.droiddeck-patches").write_text("openat2-enosys-fallback\n")
shipped = Path(self.temp.name) / "pkg/usr/local/share/fex-emu"
shipped.mkdir(parents=True)
(shipped / ".droiddeck-patches").write_text("fixed-guest-memory-above-host-address-limit\n")
self.pack()
self.assertEqual("OK", self.install(installed_loads=True)[0])
self.assertEqual("fixed-guest-memory-above-host-address-limit\n",
(self.root / "usr/local/share/fex-emu/.droiddeck-patches").read_text())
def test_an_archive_that_does_not_match_installs_nothing(self):
result, err = self.install(installed_loads=True, sha="0" * 64)
self.assertEqual("FAILED", result)
self.assertIn("no source gave the archive", err)
self.assertEqual("old FEX\n", (self.root / "usr/local/bin/FEX").read_text())
self.assertEqual("fex-2610-r5", self.tag())
if __name__ == "__main__":
unittest.main()