import http.client import io import json import os import runpy import shutil import socket import signal import subprocess import sys import tarfile import tempfile import threading import time import unittest from pathlib import Path from unittest import mock BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" EPIC = runpy.run_path(str(BIN / "droiddeck-epic")) G = EPIC["main"].__globals__ STORE = runpy.run_path(str(BIN / "droiddeck-store-launch")) S = STORE["main"].__globals__ def tar_add(archive, name, data=None, link=None, kind=None, mode=0o644): info = tarfile.TarInfo(name) if kind == "dir": info.type = tarfile.DIRTYPE info.mode = 0o755 archive.addfile(info) elif link is not None: info.type = tarfile.SYMTYPE if kind != "hard" else tarfile.LNKTYPE info.linkname = link archive.addfile(info) else: info.size = len(data) info.mode = mode archive.addfile(info, io.BytesIO(data)) def ar(members): out = bytearray(b"!\n") for name, data in members: out += ("%-16s%-12s%-6s%-6s%-8s%-10d`\n" % (name + "/", 0, 0, 0, "100644", len(data))).encode() out += data if len(data) % 2: out += b"\n" return bytes(out) class EpicTestCase(unittest.TestCase): def setUp(self): self.tmp = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.tmp, True) self.home = self.tmp / "home" state = self.home / ".local/share/droiddeck-epic" data = self.home / ".local/share/com.epicgames.eglv2" runtime = self.tmp / "run" runtime.mkdir() patches = { "HOME": str(self.home), "STATE": str(state), "CURRENT": str(state / "current.json"), "APPS": str(state / "app"), "RUNTIMES": str(state / "runtime"), "LOGS": str(state / "logs"), "SHIMS": str(state / "bin"), "FEX_CONFIG": str(state / "fex"), "FEX_CACHE": str(self.home / ".cache/droiddeck-epic"), "DATA": str(data), "SETTINGS": str(data / "settings/app-settings.json"), "API_FILE": str(data / "api.json"), "AUTH_FILE": str(data / "auth" / "auth.json"), "PROTON_ROOT": str(data / "compat/runtime"), "INSTALLATIONS": str(data / "installations.jsonl"), "V1_MANIFESTS": str(self.home / ".local/share/Epic/EpicGamesLauncher/Data/Manifests"), "AUTO_SIGN_IN": str(state / "auto-sign-in"), "SIGNED_OUT": str(state / "signed-out"), "WEB_COOKIES": str(data / "cookies"), } for name, value in patches.items(): patcher = mock.patch.dict(G, {name: value}) patcher.start() self.addCleanup(patcher.stop) env = mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": str(runtime)}) env.start() self.addCleanup(env.stop) self.state = state self.data = data class PreloadTest(EpicTestCase): def test_launcher_runtime_preloads_the_webkit_input_fix(self): source = self.tmp / "libwebkitinput.so" source.write_bytes(b"scroll") files = self.tmp / "files" with mock.patch.dict(G, {"WEBKIT_INPUT_SOURCE": str(source)}): G["install_preload"](str(files)) self.assertEqual((files / "lib/x86_64-linux-gnu/droiddeck/libwebkitinput.so").read_bytes(), b"scroll") self.assertEqual((files / "etc/ld.so.preload").read_text(), "/usr/lib/x86_64-linux-gnu/droiddeck/libwebkitinput.so\n") with mock.patch.dict(G, {"WEBKIT_INPUT_SOURCE": str(self.tmp / "missing.so")}): G["install_preload"](str(files)) self.assertFalse((files / "lib/x86_64-linux-gnu/droiddeck/libwebkitinput.so").exists()) self.assertEqual((files / "etc/ld.so.preload").read_text(), "") class RuntimeExtractTest(EpicTestCase): def test_runtime_extract_rewrites_links_and_skips_i386(self): tarball = self.tmp / "rt.tar.gz" with tarfile.open(tarball, "w:gz") as archive: tar_add(archive, "metadata", b"[Runtime]\n") tar_add(archive, "files", kind="dir") tar_add(archive, "files/lib/x86_64-linux-gnu/libc.so.6", b"libc", mode=0o755) tar_add(archive, "files/lib/ld-linux-x86-64.so.2", link="/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2") tar_add(archive, "files/etc/alias", link="/etc/real") tar_add(archive, "files/bin/rel", link="../lib/x86_64-linux-gnu/libc.so.6") tar_add(archive, "files/lib/i386-linux-gnu/libc.so.6", b"i386") tar_add(archive, "files/share/doc/readme", b"doc") tar_add(archive, "files/lib/hard", link="files/lib/x86_64-linux-gnu/libc.so.6", kind="hard") tar_add(archive, "../escape", b"x") tar_add(archive, "other/file", b"x") staging, count = G["extract_runtime"](str(tarball), str(self.tmp / "rt")) files = Path(staging) / "files" self.assertEqual(b"libc", (files / "lib/x86_64-linux-gnu/libc.so.6").read_bytes()) self.assertTrue(os.access(files / "lib/x86_64-linux-gnu/libc.so.6", os.X_OK)) self.assertEqual("x86_64-linux-gnu/ld-linux-x86-64.so.2", os.readlink(files / "lib/ld-linux-x86-64.so.2")) self.assertEqual("real", os.readlink(files / "etc/alias")) self.assertEqual("../lib/x86_64-linux-gnu/libc.so.6", os.readlink(files / "bin/rel")) self.assertEqual(".", os.readlink(files / "usr")) self.assertEqual(b"libc", (files / "lib/hard").read_bytes()) self.assertFalse((files / "lib/i386-linux-gnu").exists()) self.assertFalse((files / "share/doc").exists()) self.assertFalse((self.tmp / "escape").exists()) self.assertFalse((Path(staging) / "other").exists()) self.assertTrue((Path(staging) / "metadata").is_file()) self.assertEqual(6, count) def test_runtime_without_libc_is_refused(self): tarball = self.tmp / "rt.tar.gz" with tarfile.open(tarball, "w:gz") as archive: tar_add(archive, "files/bin/sh", b"sh") with self.assertRaises(G["Failure"]): G["extract_runtime"](str(tarball), str(self.tmp / "rt")) self.assertFalse((self.tmp / "rt.staging").exists()) def test_gles_comes_out_of_the_deb(self): data = io.BytesIO() with tarfile.open(fileobj=data, mode="w:xz") as archive: tar_add(archive, "./usr/lib/x86_64-linux-gnu/libGLESv2.so.2.1.0", b"gles") tar_add(archive, "./usr/lib/x86_64-linux-gnu/libGLESv2.so.2", link="libGLESv2.so.2.1.0") tar_add(archive, "./usr/share/doc/libgles2/copyright", b"c") deb = self.tmp / "gles.deb" deb.write_bytes(ar([("debian-binary", b"2.0\n"), ("control.tar.xz", b"x"), ("data.tar.xz", data.getvalue())])) files = self.tmp / "files" G["install_gles"](str(deb), str(files)) lib = files / "lib/x86_64-linux-gnu" self.assertEqual(b"gles", (lib / "libGLESv2.so.2").read_bytes()) self.assertEqual("libGLESv2.so.2.1.0", os.readlink(lib / "libGLESv2.so.2")) self.assertFalse((files / "share").exists()) bad = self.tmp / "bad.deb" bad.write_bytes(b"not an archive") with self.assertRaises(G["Failure"]): G["install_gles"](str(bad), str(files)) def test_install_marks_ready_and_prunes_old_versions(self): tarball = self.tmp / "rt.tar.gz" with tarfile.open(tarball, "w:gz") as archive: tar_add(archive, "files/lib/x86_64-linux-gnu/libc.so.6", b"libc") data = io.BytesIO() with tarfile.open(fileobj=data, mode="w:gz") as archive: tar_add(archive, "./usr/lib/x86_64-linux-gnu/libGLESv2.so.2", b"gles") deb = self.tmp / "gles.deb" deb.write_bytes(ar([("data.tar.gz", data.getvalue())])) old = self.state / "app/0.1" old.mkdir(parents=True) layers = [] def extract_app(image, dest, desktop=()): layers.append(list(desktop)) os.makedirs(os.path.join(dest, "usr/bin")) os.makedirs(os.path.join(dest, "desktop/usr/lib/x86_64-linux-gnu")) Path(dest, "usr/bin/EpicGamesLauncher").write_text("bin") Path(dest, "desktop/usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.1.so.0").write_text("webkit") package = self.tmp / "libwebkit.deb" package.write_bytes(b"x") with mock.patch.dict(G, {"extract_app": extract_app, "fex_tool": lambda request=False: {"bin": "/fex", "server": "/s"}}), \ mock.patch("sys.stdout", new_callable=io.StringIO) as out: image = self.tmp / "image" image.write_bytes(b"x") with self.assertRaises(G["Failure"]): G["install"]({"app": str(image), "app-version": "0.95.2+g1", "runtime": str(tarball), "runtime-version": "4.0.1", "gles": str(deb), "desktop": [str(self.tmp / "missing.deb")]}) code = G["install"]({"app": str(image), "app-version": "0.95.2+g1", "runtime": str(tarball), "runtime-version": "4.0.1", "gles": str(deb), "desktop": [str(package)]}) self.assertEqual([[str(package)]], layers) self.assertEqual(0, code) report = json.loads(out.getvalue()) self.assertTrue(report["installed"]) self.assertEqual({"app": "0.95.2+g1", "runtime": "4.0.1"}, json.loads((self.state / "current.json").read_text())) self.assertFalse(old.exists()) files = self.state / "runtime/4.0.1/files" self.assertEqual("", (files / "etc/ld.so.preload").read_text()) self.assertTrue((self.state / "runtime/4.0.1/.droiddeck-ready").is_file()) self.assertTrue((self.state / "app/0.95.2+g1/.droiddeck-ready").is_file()) def deb(path, entries, compression="gz"): data = io.BytesIO() with tarfile.open(fileobj=data, mode="w:" + compression) as archive: for entry in entries: tar_add(archive, *entry[:1], **entry[1]) path.write_bytes(ar([("debian-binary", b"2.0\n"), ("control.tar.xz", b"c"), ("data.tar." + compression, data.getvalue())])) return path class LauncherDebTest(EpicTestCase): def launcher(self, extra=()): return deb(self.tmp / "EpicGamesLauncher-x86_64.deb", [ ("./usr", {"kind": "dir"}), ("./usr/bin/EpicGamesLauncher", {"data": b"egl", "mode": 0o755}), ("./usr/lib/epic-games-launcher/libEOSSDK-Linux-Shipping.so", {"data": b"eos"}), ("./usr/share/icons/hicolor/256x256@2/apps/EpicGamesLauncher.png", {"data": b"png"}), ("./usr/share/applications/epic-games-launcher.desktop", {"data": b"[Desktop Entry]\n"}), ("./usr/bin/egl", {"link": "/usr/bin/EpicGamesLauncher"}), ("../escape", {"data": b"x"}), *extra, ]) def webkit(self): return deb(self.tmp / "libwebkit2gtk-4.1-0_amd64.deb", [ ("./usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.1.so.0.19.1", {"data": b"webkit"}), ("./usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.1.so.0", {"link": "libwebkit2gtk-4.1.so.0.19.1"}), ("./usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/WebKitNetworkProcess", {"data": b"net", "mode": 0o755}), ("./usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/injected-bundle/libwebkit2gtkinjectedbundle.so", {"data": b"bundle"}), ("./usr/share/glib-2.0/schemas/org.gnome.GWeather4.gschema.xml", {"data": b"schema"}), ("./usr/share/doc/libwebkit2gtk-4.1-0/copyright", {"data": b"c"}), ("./usr/share/locale/fr/LC_MESSAGES/WebKitGTK-4.1.mo", {"data": b"fr"}), ], "xz") def test_the_launcher_and_its_desktop_layer_come_out_of_debs(self): dest = self.state / "app/0.113.0+g1" G["extract_app"](str(self.launcher()), str(dest), [str(self.webkit())]) self.assertEqual(b"egl", (dest / "usr/bin/EpicGamesLauncher").read_bytes()) self.assertTrue(os.access(dest / "usr/bin/EpicGamesLauncher", os.X_OK)) self.assertEqual(b"eos", (dest / "usr/lib/epic-games-launcher/libEOSSDK-Linux-Shipping.so").read_bytes()) self.assertEqual("EpicGamesLauncher", os.readlink(dest / "usr/bin/egl")) self.assertEqual(b"png", (dest / "epic-games-launcher.png").read_bytes()) libs = dest / "desktop/usr/lib/x86_64-linux-gnu" self.assertEqual(b"webkit", (libs / "libwebkit2gtk-4.1.so.0").read_bytes()) self.assertEqual(b"net", (libs / "webkit2gtk-4.1/WebKitNetworkProcess").read_bytes()) self.assertEqual(b"schema", (dest / "desktop/usr/share/glib-2.0/schemas/org.gnome.GWeather4.gschema.xml").read_bytes()) self.assertFalse((dest / "desktop/usr/share/doc").exists()) self.assertFalse((self.state / "app/escape").exists()) self.assertFalse((self.state / "app/0.113.0+g1.staging").exists()) self.assertTrue(G["app_complete"](str(dest))) self.assertEqual(str(dest / "usr/bin/EpicGamesLauncher"), G["app_command"](str(dest))) self.assertEqual(str(libs), G["desktop_libs"](str(dest))) def test_archive_symlinks_cannot_redirect_the_desktop_layer_or_icon(self): outside = self.tmp / "outside" outside.mkdir() dest = self.state / "app/0.113.0+g1" escape = os.path.relpath(outside, Path(str(dest) + ".staging")) launcher = self.launcher([("./desktop", {"link": escape}), ("./epic-games-launcher.png", {"link": escape + "/target"})]) with self.assertRaises(G["Failure"]) as caught: G["extract_app"](str(launcher), str(dest), [str(self.webkit())]) self.assertEqual("app-extract", str(caught.exception)) self.assertFalse((outside / "usr").exists()) self.assertFalse((outside / "target").exists()) icon_dest = self.state / "app/0.113.0+g2" icon_escape = os.path.relpath(outside / "target", Path(str(icon_dest) + ".staging")) G["extract_app"](str(self.launcher([("./epic-games-launcher.png", {"link": icon_escape})])), str(icon_dest), [str(self.webkit())]) self.assertEqual(b"png", (icon_dest / "epic-games-launcher.png").read_bytes()) self.assertFalse((outside / "target").exists()) def test_a_launcher_without_its_desktop_layer_is_refused(self): dest = self.state / "app/0.113.0+g1" with self.assertRaises(G["Failure"]) as caught: G["extract_app"](str(self.launcher()), str(dest)) self.assertEqual("app-extract", str(caught.exception)) self.assertFalse(dest.exists()) self.assertFalse(Path(str(dest) + ".staging").exists()) def test_anything_but_a_deb_is_refused_and_leaves_nothing_behind(self): image = self.tmp / "EpicGamesLauncher-x86_64.AppImage" image.write_bytes(b"\x7fELF\x02\x01\x01\x00AI\x02" + bytes(64)) dest = self.state / "app/0.95.2+g1" with self.assertRaises(G["Failure"]) as caught: G["extract_app"](str(image), str(dest), [str(self.webkit())]) self.assertEqual("app-format", str(caught.exception)) plain = self.tmp / "plain.deb" plain.write_bytes(ar([("data.tar.gz", b"x")])) with self.assertRaises(G["Failure"]) as caught: G["extract_app"](str(self.launcher()), str(dest), [str(plain)]) self.assertEqual("desktop-format", str(caught.exception)) self.assertFalse(dest.exists()) self.assertFalse(Path(str(dest) + ".staging").exists()) def test_an_installed_appimage_launcher_still_runs_through_apprun(self): app = self.state / "app/0.95.2+g1" (app / "usr/bin").mkdir(parents=True) (app / "AppRun").write_text("run") (app / "usr/bin/EpicGamesLauncher").write_text("bin") self.assertTrue(G["app_complete"](str(app))) self.assertEqual(str(app / "AppRun"), G["app_command"](str(app))) self.assertIsNone(G["desktop_libs"](str(app))) (app / "usr/bin/EpicGamesLauncher").unlink() self.assertFalse(G["app_complete"](str(app))) def test_the_webkit_helpers_are_linked_into_the_runtime(self): dest = self.state / "app/0.113.0+g1" G["extract_app"](str(self.launcher()), str(dest), [str(self.webkit())]) files = self.state / "runtime/4.0.1/files" (files / "lib/x86_64-linux-gnu").mkdir(parents=True) link = files / "lib/x86_64-linux-gnu/webkit2gtk-4.1" G["link_webkit"](str(files), G["desktop_libs"](str(dest))) self.assertFalse(os.path.isabs(os.readlink(link))) self.assertEqual(b"net", (link / "WebKitNetworkProcess").read_bytes()) G["link_webkit"](str(files), G["desktop_libs"](str(dest))) self.assertEqual(b"net", (link / "WebKitNetworkProcess").read_bytes()) G["link_webkit"](str(files), None) self.assertFalse(os.path.lexists(link)) link.mkdir() (link / "own").write_text("x") G["link_webkit"](str(files), G["desktop_libs"](str(dest))) self.assertTrue((link / "own").is_file()) self.assertTrue((dest / "desktop/usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/WebKitNetworkProcess").is_file()) def test_only_the_deb_launcher_gets_the_desktop_libraries(self): with mock.patch.dict(G, {"fex_environment": lambda fex, files, base: {}}): plain = G["client_environment"]({"LD_LIBRARY_PATH": "/host"}, None, None, ":0", "bus", "broker") layered = G["client_environment"]({}, None, None, ":0", "bus", "broker", libs="/app/desktop/usr/lib/x86_64-linux-gnu") self.assertNotIn("LD_LIBRARY_PATH", plain) self.assertEqual("/app/desktop/usr/lib/x86_64-linux-gnu", layered["LD_LIBRARY_PATH"]) self.assertEqual("/app/desktop/usr/share:/usr/local/share:/usr/share", layered["XDG_DATA_DIRS"]) self.assertEqual("/app/desktop/usr/share/glib-2.0/schemas", layered["GSETTINGS_SCHEMA_DIR"]) def test_every_desktop_package_reaches_the_install(self): options, rest = G["parse_options"](["--app", "a.deb", "--desktop", "one.deb", "--desktop", "two.deb", "x"], lists=("desktop",)) self.assertEqual({"app": "a.deb", "desktop": ["one.deb", "two.deb"]}, options) self.assertEqual(["x"], rest) self.assertEqual({"desktop": "two.deb"}, G["parse_options"](["--desktop", "one.deb", "--desktop", "two.deb"])[0]) class ArgumentsTest(EpicTestCase): def test_client_arguments_win_and_paths_become_windows_paths(self): ovt = self.tmp / "ovt" ovt.mkdir() client = ["-AUTH_LOGIN=unused", "-AUTH_PASSWORD=abc", "-AUTH_TYPE=exchangecode", "-epicapp=Fig", "-epicovt=%s/token.ovt" % ovt, '-epicusername="Some One"', "-nosplash"] own = ["-EpicPortal", "-epicapp=Fig", "-nosplash", "-windowed"] merged = G["merge_args"](client, own) self.assertIn('-epicovt="Z:%s\\token.ovt"' % str(ovt).replace("/", "\\"), merged) self.assertEqual(1, merged.count("-nosplash")) self.assertNotIn("-EpicPortal", merged) self.assertEqual("-windowed", merged[-1]) self.assertEqual("-AUTH_PASSWORD=abc", merged[1]) def test_command_line_escapes_cmd_specials_outside_quotes(self): line = G["command_line"](["-a=b&c", '-n="A & B"', "two words", "-p=50%", "^x", "-z=a|b>cc^