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) def extract_app(image, dest): os.makedirs(os.path.join(dest, "usr/bin")) Path(dest, "AppRun").write_text("run") Path(dest, "usr/bin/EpicGamesLauncher").write_text("bin") 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") code = G["install"]({"app": str(image), "app-version": "0.95.2+g1", "runtime": str(tarball), "runtime-version": "4.0.1", "gles": str(deb)}) 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()) 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^