import contextlib import gzip import io import json import os import runpy import shutil import struct import tempfile import unittest from pathlib import Path from unittest import mock BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" FEX = runpy.run_path(str(BIN / "droiddeck-fex")) G = FEX["main"].__globals__ def elf(machine, data=1, size=64): head = bytearray(size) head[0:4] = b"\x7fELF" head[4] = 2 head[5] = data struct.pack_into("H", head, 18, machine) return bytes(head) class FexTestCase(unittest.TestCase): def setUp(self): self.tmp = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.tmp, True) self.home = self.tmp / "home" self.steam = self.home / ".local/share/Steam" (self.steam / "steamapps/common").mkdir(parents=True) state = self.home / ".local/share/droiddeck-fex" patches = { "HOME": str(self.home), "STEAM_ROOT": str(self.steam), "LIBRARIES": (str(self.tmp / "sd"),), "STATE": str(state), "CONFIG_DIR": str(state / "config"), "DATA_DIR": str(state / "data"), "CACHE_DIR": str(self.home / ".cache/droiddeck-fex"), "STATUS_FILE": str(state / "status.json"), "WANTED": str(self.home / ".config/droiddeck/fex-wanted"), "PRELOADS": str(self.tmp / "preloads"), "HOST_PRELOAD": str(self.tmp / "ld.so.preload"), } patcher = mock.patch.dict(G, patches) patcher.start() self.addCleanup(patcher.stop) def write(self, path, data=b"", mode=0o644): path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(data if isinstance(data, bytes) else data.encode()) path.chmod(mode) return path def fex_tool(self, library=None, server=True, thunks=True): tool = Path(library or self.steam) / "steamapps/common/FEX-Emu" self.write(tool / "usr/bin/FEX", elf(183), 0o755) if server: self.write(tool / "usr/bin/FEXServer", elf(183), 0o755) if thunks: (tool / "usr/lib/aarch64-linux-gnu/fex-emu/HostThunks").mkdir(parents=True) (tool / "usr/share/fex-emu/GuestThunks").mkdir(parents=True) self.write(tool / "usr/share/fex-emu/ThunksDB.json", "{}") self.write(Path(library or self.steam) / "steamapps/appmanifest_3127680.acf", '"AppState" { "installdir" "FEX-Emu" }') return tool def runtime(self, version="3.0.20260805.254768", lines=None): runtime = self.steam / "steamapps/common/SteamLinuxRuntime_sniper" platform = runtime / ("sniper_platform_" + version) files = platform / "files" self.write(files / "lib/x86_64-linux-gnu/libc.so.6", elf(62)) self.write(files / "ab/cdef-1.bin", b"blob") self.write(files / "share/ca/real.crt", b"cert") mtree = lines or [ "#mtree", ". type=dir", "./bin type=dir", "./lib64 type=dir", "./lib64/ld-linux-x86-64.so.2 type=link link=/usr/lib/x86_64-linux-gnu/ld-2.31.so", "./lib/x86_64-linux-gnu/libc.so.6 type=file mode=644 size=64", "./bin/\\133 type=file mode=755 size=4 contents=./ab/cdef-1.bin", "./share/ca/Na\\075me.crt type=link link=real.crt", "./etc/ssl/cert.pem type=link link=/etc/ssl/certs/ca.pem", ] with gzip.open(platform / "usr-mtree.txt.gz", "wt") as out: out.write("\n".join(mtree) + "\n") self.write(self.steam / "steamapps/appmanifest_1628350.acf", '"AppState" { "installdir" "SteamLinuxRuntime_sniper" }') return platform def graphics(self, sniper, libc=b"GLIBC_2.2.5 GLIBC_2.31 GLIBC_2.34 GLIBC_2.41", libstdcxx=b"GLIBCXX_3.4.28 GLIBCXX_3.4.33"): files = Path(sniper) / "files" x86 = files / "lib/x86_64-linux-gnu" self.write(x86 / "libc-2.31.so", b"GLIBC_2.2.5 GLIBC_2.31") os.unlink(x86 / "libc.so.6") os.symlink("libc-2.31.so", x86 / "libc.so.6") self.write(x86 / "ld-2.31.so", b"old ld") os.symlink("ld-2.31.so", x86 / "ld-linux-x86-64.so.2") self.write(x86 / "libstdc++.so.6", b"GLIBCXX_3.4.28") self.write(x86 / "libSegFault.so", b"old") self.write(x86 / "libSDL2-2.0.so.0", b"sdl") self.write(x86 / "libfoo.so.1.2", b"foo") os.symlink("libfoo.so.1.2", x86 / "libfoo.so.1") self.write(x86 / "gconv/OLD.so", b"old gconv") self.write(files / "lib/i386-linux-gnu/libc.so.6", b"GLIBC_2.31") self.write(files / "lib/i386-linux-gnu/ld-2.31.so", b"old ld32") os.symlink("i386-linux-gnu/ld-2.31.so", files / "lib/ld-linux.so.2") os.unlink(files / "lib64/ld-linux-x86-64.so.2") os.symlink("../lib/x86_64-linux-gnu/ld-2.31.so", files / "lib64/ld-linux-x86-64.so.2") platform = self.steam / "steamapps/common/SteamLinuxRuntime_4/steamrt4_platform_4.0.20260928.262390" provided = platform / "files/lib/x86_64-linux-gnu" self.write(provided / "libc.so.6", libc) self.write(provided / "libstdc++.so.6", libstdcxx) self.write(provided / "ld-linux-x86-64.so.2", b"new ld") self.write(provided / "gconv/NEW.so", b"new gconv") self.write(platform / "files/lib/i386-linux-gnu/libc.so.6", b"GLIBC_2.41") self.write(platform / "files/lib/i386-linux-gnu/ld-linux.so.2", b"new ld32") with gzip.open(platform / "usr-mtree.txt.gz", "wt") as out: out.write("#mtree\n. type=dir\n") self.write(self.steam / "steamapps/appmanifest_4183110.acf", '"AppState" { "installdir" "SteamLinuxRuntime_4" }') return platform def quiet(self, function, *args): with contextlib.redirect_stderr(io.StringIO()) as err: return function(*args), err.getvalue() class ArchitectureTest(FexTestCase): def test_elf_machines(self): self.assertEqual(FEX["elf_arch"](self.write(self.tmp / "a", elf(62))), "x86_64") self.assertEqual(FEX["elf_arch"](self.write(self.tmp / "b", elf(3))), "i386") self.assertEqual(FEX["elf_arch"](self.write(self.tmp / "c", elf(183))), "arm64") self.assertEqual(FEX["elf_arch"](self.write(self.tmp / "d", elf(62, data=2))), "x86_64") self.assertEqual(FEX["elf_arch"](self.write(self.tmp / "e", elf(8))), "other") self.assertIsNone(FEX["elf_arch"](self.write(self.tmp / "f", "#!/bin/sh\n"))) self.assertIsNone(FEX["elf_arch"](self.tmp / "missing")) def test_a_script_follows_the_programs_beside_it(self): game = self.tmp / "game" script = self.write(game / "start.sh", "#!/bin/bash\n./bin/game\n") self.assertFalse(FEX["needs_fex"](str(script))) self.write(game / "bin/game.x86_64", elf(62), 0o755) self.assertTrue(FEX["needs_fex"](str(script))) self.assertTrue(FEX["needs_fex"](str(game))) self.write(game / "bin/game.arm64", elf(183), 0o755) self.assertFalse(FEX["needs_fex"](str(script))) self.assertTrue(FEX["needs_fex"](str(game / "bin/game.x86_64"))) self.assertFalse(FEX["needs_fex"](str(game / "bin/game.arm64"))) def test_the_scan_stops_at_its_depth(self): game = self.tmp / "deep" self.write(game / "a/b/c/d/e/game", elf(62), 0o755) self.assertFalse(FEX["needs_fex"](str(game))) self.write(game / "a/b/game", elf(62), 0o755) self.assertTrue(FEX["needs_fex"](str(game))) def test_appimage_offset_is_the_end_of_the_section_headers(self): head = bytearray(elf(62)) struct.pack_into("&2\n" "echo 'write_xattr: could not write xattr security.selinux for file ./AppDir/.DirIcon because you are not superuser!' >&2\n" "echo 'Further error messages of this type are suppressed!' >&2\n" "exit 2\n") with mock.patch.dict(G, {"URUNTIME": tool, "fex_install": lambda: None, "steam_running": lambda: False}): status, said = self.quiet(FEX["extract"], "image.AppImage") self.assertEqual(status, 0) self.assertIn("could not keep the image's extended attributes", said) self.assertTrue(Path("squashfs-root/AppRun").is_file()) def test_an_attribute_only_failure_without_apprun_is_not_taken_as_unpacked(self): tool = self.uruntime( "mkdir -p AppDir\n" "echo 'write_xattr: could not write xattr security.selinux for file ./AppDir/.DirIcon because you are not superuser!' >&2\n" "exit 2\n") with mock.patch.dict(G, {"URUNTIME": tool, "fex_install": lambda: None, "steam_running": lambda: False, "prepare": lambda: None}): status, said = self.quiet(FEX["extract"], "image.AppImage") self.assertIn("uruntime could not extract it (status 2)", said) self.assertNotIn("could not keep the image's extended attributes", said) self.assertFalse(os.path.lexists("squashfs-root/AppRun")) def test_other_uruntime_errors_are_not_taken_as_unpacked(self): self.assertFalse(FEX["only_xattrs_failed"]("write_xattr: could not write xattr\nwrite_file: failed to create file x\n")) self.assertFalse(FEX["only_xattrs_failed"]("")) self.assertTrue(FEX["only_xattrs_failed"]("proot error: execveat() failed\nwrite_xattr: could not write xattr\n")) def test_an_arm64_image_does_not_ask_for_fex(self): self.write(self.image, elf(183) + b"payload", 0o755) tool = self.uruntime("mkdir -p squashfs-root\n") with mock.patch.dict(G, {"URUNTIME": tool}): status, said = self.quiet(FEX["extract"], "image.AppImage") self.assertEqual((status, said), (0, "")) self.assertFalse((self.home / ".config/droiddeck/fex-wanted").exists()) def test_nothing_to_unpack_with_says_so(self): tool = self.uruntime("mkdir -p squashfs-root\nexit 1\n") with mock.patch.dict(G, {"URUNTIME": tool, "fex_install": lambda: None, "steam_running": lambda: False}), \ mock.patch("shutil.which", return_value=None): status, said = self.quiet(FEX["extract"], "image.AppImage") self.assertEqual(status, 1) self.assertIn("uruntime could not extract it (status 1)", said) self.assertIn("no AppImage runtime for ARM64, no FEX yet and no unsquashfs", said) self.assertFalse(os.path.lexists("squashfs-root")) def test_unsquashfs_fallback_disables_unwritable_xattrs(self): tool = self.uruntime("exit 1\n") commands = [] def call(argv, **kwargs): commands.append(argv) if argv[0] == tool: return 1 (self.work / "squashfs-root").mkdir() (self.work / "squashfs-root/AppRun").touch() return 0 with mock.patch.dict(G, { "URUNTIME": tool, "prepare": lambda: None, "fex_install": lambda: None, "steam_running": lambda: False, "want_fex": lambda: None, }), mock.patch("shutil.which", return_value="/usr/bin/unsquashfs"), \ mock.patch("subprocess.call", side_effect=call): status, said = self.quiet(FEX["extract"], "image.AppImage") self.assertEqual(status, 0) self.assertIn("uruntime could not extract it (status 1)", said) self.assertEqual(commands[-1][0], "/usr/bin/unsquashfs") self.assertIn("-no-xattrs", commands[-1]) self.assertTrue(Path("squashfs-root/AppRun").is_file()) class LaunchTest(FexTestCase): def test_the_guest_environment(self): arm = self.write(self.tmp / "arm.so", elf(183)) x86 = self.write(self.tmp / "x86.so", elf(62)) env = {"LD_PRELOAD": "%s:%s %s" % (arm, x86, self.tmp / "gone.so"), "LD_LIBRARY_PATH": "/steam/steamrtarm64:/usr/lib/aarch64-linux-gnu:/game/lib:/steam/linuxarm64", "FEX_ROOTFS": "/usr/share/guestos/fex-mesa", "FEX_TSOENABLED": "1"} guest = FEX["guest_environment"]({"portable": True}, env) self.assertEqual((guest["LD_PRELOAD"], guest["LD_LIBRARY_PATH"], guest["FEX_PORTABLE"]), (str(x86), "/game/lib", "1")) self.assertNotIn("FEX_ROOTFS", guest) self.assertEqual(guest["FEX_TSOENABLED"], "1") self.assertTrue(guest["FEX_APP_CONFIG_LOCATION"].endswith("/config/")) self.assertEqual((guest["FEX_SILENTLOG"], guest["FEX_OUTPUTLOG"]), ("0", "stderr")) self.assertEqual(guest["GC_DISABLE_INCREMENTAL"], "1") guest = FEX["guest_environment"]({"portable": False}, {"LD_PRELOAD": str(arm), "FEX_PORTABLE": "1", "FEX_OUTPUTLOG": "/tmp/fex.log", "GC_DISABLE_INCREMENTAL": "0"}) self.assertEqual((guest["FEX_OUTPUTLOG"], guest["GC_DISABLE_INCREMENTAL"]), ("/tmp/fex.log", "0")) self.assertNotIn("LD_PRELOAD", guest) self.assertNotIn("FEX_PORTABLE", guest) def test_the_server_socket_stays_under_the_runtime_directory(self): runtime = self.tmp / "run" runtime.mkdir() guest = FEX["guest_environment"]({"portable": True}, {"XDG_RUNTIME_DIR": str(runtime)}) self.assertEqual(guest["FEX_APP_DATA_LOCATION"], str(runtime / "fex") + "/") self.assertTrue((runtime / "fex").is_dir()) guest = FEX["guest_environment"]({"portable": True}, {"XDG_RUNTIME_DIR": str(self.tmp / "missing")}) self.assertEqual(guest["FEX_APP_DATA_LOCATION"], G["DATA_DIR"] + "/") def test_the_runtime_and_thunks_are_passed_to_fex(self): fex = {"portable": True, "rootfs": "/rt/files", "host_thunks": "/h", "guest_thunks": "/g", "thunks_db": "/db"} guest = FEX["guest_environment"](fex, {}) self.assertEqual((guest["FEX_ROOTFS"], guest["FEX_THUNKHOSTLIBS"], guest["FEX_THUNKGUESTLIBS"]), ("/rt/files", "/h", "/g")) self.assertTrue(guest["FEX_THUNKCONFIG"].endswith("/config/ThunksConfig.json")) guest = FEX["guest_environment"](dict(fex, host_thunks=None), {}) self.assertEqual(guest["FEX_ROOTFS"], "/rt/files") self.assertNotIn("FEX_THUNKCONFIG", guest) FEX["write_config"](fex, "/rt/files") thunks = json.loads((self.home / ".local/share/droiddeck-fex/config/ThunksConfig.json").read_text()) self.assertEqual(thunks, {"ThunksDB": {"GL": 1, "Vulkan": 1}}) def test_native_programs_run_directly(self): program = self.write(self.tmp / "native", elf(183), 0o755) with mock.patch("os.execvpe") as native, mock.patch("os.execve") as emulated: FEX["launch"]([str(program), "--flag"], {"PATH": "/usr/bin"}, "auto") native.assert_called_once_with(str(program), [str(program), "--flag"], {"PATH": "/usr/bin"}) emulated.assert_not_called() def test_x86_programs_run_under_fex(self): tool = self.fex_tool() self.runtime() program = self.write(self.tmp / "game.x86_64", elf(62), 0o755) with mock.patch("os.execvpe") as native, mock.patch("os.execve") as emulated: self.quiet(FEX["launch"], [str(program), "-w"], {"PATH": "/usr/bin"}, "auto") native.assert_not_called() binary, argv, env = emulated.call_args[0] self.assertEqual((binary, argv), (str(tool / "usr/bin/FEX"), [str(tool / "usr/bin/FEX"), str(program), "-w"])) self.assertEqual(env["FEX_PORTABLE"], "1") self.assertNotIn("LD_PRELOAD", env) self.assertEqual(env["FEX_ROOTFS"], str(self.steam / "steamapps/common/SteamLinuxRuntime_sniper/sniper_platform_3.0.20260805.254768/files")) for arch in ("x86_64", "i386"): self.write(self.tmp / "preloads" / arch / "libfaultreport.so", elf(62 if arch == "x86_64" else 3)) x86 = self.write(self.tmp / "x86.so", elf(62)) with mock.patch("os.execve") as emulated: self.quiet(FEX["launch"], [str(program)], {"LD_PRELOAD": str(x86)}, "auto") self.assertEqual(emulated.call_args[0][2]["LD_PRELOAD"], str(x86)) old = self.write(self.tmp / "game.i386", elf(3), 0o755) with mock.patch("os.execve") as emulated: self.quiet(FEX["launch"], [str(old)], {}, "auto") self.assertNotIn("LD_PRELOAD", emulated.call_args[0][2]) with mock.patch("os.execvpe") as native, mock.patch("os.execve") as emulated: self.quiet(FEX["launch"], [str(program)], {}, "off") native.assert_called_once() emulated.assert_not_called() def test_programs_that_bring_a_new_enough_runtime_get_the_vulkan_thunk(self): tool = self.fex_tool() self.runtime() self.write(tool / "usr/share/fex-emu/GuestThunks/libvulkan-guest.so", elf(62) + b"\0libstdc++.so.6\0GLIBCXX_3.4\0GLIBCXX_3.4.29\0libc.so.6\0GLIBC_2.2.5\0GLIBC_2.34\0") pin = self.write(self.tmp / "preloads/x86_64/libvulkan-thunk.so", elf(62) + b"\0libc.so.6\0GLIBC_2.2.5\0") pinned = "/usr/lib/x86_64-linux-gnu/droiddeck/vulkan" def app(name, glibc=b"GLIBC_2.34\0GLIBC_2.44", glibcxx=b"GLIBCXX_3.4.29\0GLIBCXX_3.4.36", bundled=True): run = self.write(self.tmp / name / "AppRun", elf(62), 0o755) if bundled: self.write(self.tmp / name / "shared/lib/libc.so.6", elf(62) + b"\0GLIBC_2.2.5\0" + glibc + b"\0") self.write(self.tmp / name / "shared/lib/libstdc++.so.6", elf(62) + b"\0GLIBCXX_3.4\0" + glibcxx + b"\0GLIBC_2.34\0") return run def started(program, env=None, decide=None): with mock.patch("os.execve") as emulated: _, said = self.quiet(FEX["launch"], [str(program)], dict(env or {}), "on", decide) return emulated.call_args[0][2], said def search(env): return env.get("LD_LIBRARY_PATH", "").split(":")[0] env, said = started(app("new")) self.assertEqual(search(env), pinned) self.assertNotIn("LD_AUDIT", env) self.assertNotIn("LD_BIND_NOW", env) self.assertIn("thunks on, its own Vulkan loader too", said) env, _ = started(app("kept"), {"LD_LIBRARY_PATH": "/game/lib"}) self.assertEqual(env["LD_LIBRARY_PATH"], pinned + ":/game/lib") env, _ = started(self.write(self.tmp / "elsewhere/run.sh", "#!/bin/sh\n", 0o755), decide=str(self.tmp / "new")) self.assertEqual(search(env), pinned) env, said = started(app("old-cxx", glibcxx=b"GLIBCXX_3.4.28")) self.assertNotEqual(search(env), pinned) self.assertIn("thunks on)", said) self.assertNotEqual(search(started(app("old-libc", glibc=b"GLIBC_2.31"))[0]), pinned) self.assertNotEqual(search(started(app("runtime-only", bundled=False))[0]), pinned) i386 = self.write(self.tmp / "new/game.i386", elf(3), 0o755) self.assertNotEqual(search(started(i386)[0]), pinned) self.write(pin, elf(62) + b"\0libc.so.6\0GLIBC_2.45\0") self.assertNotEqual(search(started(app("new"))[0]), pinned) pin.unlink() self.assertNotEqual(search(started(app("no-pin"))[0]), pinned) self.write(pin, elf(62)) (tool / "usr/share/fex-emu/GuestThunks/libvulkan-guest.so").unlink() self.assertNotEqual(search(started(app("no-thunk"))[0]), pinned) def test_steam_verbs(self): with mock.patch.dict(G, {"launch": mock.Mock(return_value=0)}): self.assertEqual(FEX["main"](["steam", "getcompatpath", "/x"]), 0) G["launch"].assert_not_called() FEX["main"](["steam", "waitforexitandrun", "/game/run.sh", "-a"]) command, env, mode = G["launch"].call_args[0] self.assertEqual((command, mode), (["/game/run.sh", "-a"], "auto")) class AppInfoTest(unittest.TestCase): COMPAT = runpy.run_path(str(BIN / "steam-compatibility")) @staticmethod def appinfo(apps, magic=0x07564429): strings = [] def key(name): if magic != 0x07564429: return name.encode() + b"\0" if name not in strings: strings.append(name) return struct.pack("= 0x07564428 else b"") body += struct.pack("