WEFT_OS/tests/frame/check_app_data.py
Marco Allegretti a508f1ebe4
fix(appd): move only real app data and never replace a directory
weft-pack treated any top-level data directory in a package as app
data from the earlier layout, in every store and for every app: a
package shipping data/ lost that content to the app data directory on
uninstall and could then no longer be uninstalled. Data is now moved
only from ~/.local/share/weft/apps/<id>/data, the only place earlier
versions wrote it, and only for packages declaring an app-data
capability; packages may no longer contain a top-level data entry.

The move left an empty package directory behind, which then blocked
weft-pack install although nothing was listed; an emptied package
directory is now removed, and install accepts an empty leftover. The
rename uses RENAME_NOREPLACE, so a target created after the check is
reported as a conflict instead of being replaced, and a move across
filesystems is refused with a message naming both locations instead of
a bare OS error. Directories moved by weft-pack are made private too,
and a symbolic link at the target is refused rather than followed.

The system UI now shows launch refusals, which it only logged, and the
weft-appd unit may write under ~/.local/share/weft, which
ProtectSystem=strict made read-only. check_app_data.py also types into
Notes after the move and requires the save to reach the moved file, and
checks that no empty package directory remains.
2026-10-09 08:52:09 +00:00

128 lines
5.6 KiB
Python

"""Check that existing Notes data survives the move to the app data store.
Earlier versions kept app data inside the user package store, at
`~/.local/share/weft/apps/<id>/data`. The harness places a notes file there
(with a backslash sequence, a newline and non-ASCII text, which must come
through byte for byte), runs weft-appd on a nested desktop and launches
`org.weft.demo.notes` with the real runtime and app shell. The session must
reach APP_READY, the file must then be at
`$XDG_DATA_HOME/weft/app-data/org.weft.demo.notes/notes.txt` with identical
bytes, the old directory and the emptied package directory must be gone,
and the new one private (0700). Typing at the end of the note and pressing
Ctrl+S must then store the moved text plus the typed text, which shows that
the session reads and writes the moved directory.
A second launch after placing a different file at the old location must be
refused with error 409, leaving both copies untouched.
Requires Xvfb, xwd, ImageMagick's convert and xdotool.
"""
import argparse
import shutil
import subprocess
import sys
import tempfile
import time
from pathlib import Path
sys.dont_write_bytecode = True
from check_counter import AppdClient, free_port, read_endpoint # noqa: E402
from check_notes import wait_for_file # noqa: E402
from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402
APP_ID = "org.weft.demo.notes"
NOTES = "first line\nliteral \\n stays\ncafé ☃\n".encode()
def run(args, desktop, home, xdotool):
legacy = home / ".local/share/weft/apps" / APP_ID / "data"
legacy.mkdir(parents=True)
(legacy / "notes.txt").write_bytes(NOTES)
data_home = home / "data-home"
target = data_home / "weft/app-data" / APP_ID
t = Path(args.target)
desktop.launch_client("appd", [t / "weft-appd"], {
"WEFT_RUNTIME_BIN": str(t / "weft-runtime"),
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
"WEFT_APP_STORE": str(args.store),
"WEFT_DISABLE_CGROUP": "1",
"WEFT_APPD_WS_PORT": str(free_port()),
"HOME": str(home),
"XDG_DATA_HOME": str(data_home),
})
port, token = read_endpoint(desktop.runtime, 30)
appd = AppdClient(port, token)
appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0})
reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 120)
if reply.get("type") != "APP_READY":
raise AssertionError(f"Notes did not start: {reply}")
if not (target / "notes.txt").is_file() or (target / "notes.txt").read_bytes() != NOTES:
raise AssertionError("notes did not arrive unchanged")
if legacy.parent.exists():
raise AssertionError(f"{legacy.parent} still exists")
if target.stat().st_mode & 0o777 != 0o700:
raise AssertionError(f"{target} is not private: {oct(target.stat().st_mode)}")
def act(*arguments):
subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display},
check=True, timeout=30)
time.sleep(2)
act("search", "--class", "weft-compositor", "windowfocus", "--sync")
act("mousemove", "--sync", "100", "300")
act("click", "1")
act("key", "ctrl+End")
act("type", "--delay", "40", "ok")
act("key", "ctrl+s")
wait_for_file(target / "notes.txt", NOTES.decode() + "ok")
appd.send({"type": "TERMINATE_APP", "session_id": reply["session_id"]})
legacy.mkdir(parents=True)
(legacy / "notes.txt").write_bytes(b"other copy")
appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0})
reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 60)
if reply.get("type") != "ERROR" or reply.get("code") != 409:
raise AssertionError(f"conflicting data was not refused: {reply}")
if (legacy / "notes.txt").read_bytes() != b"other copy" or \
(target / "notes.txt").read_bytes() != NOTES + b"ok":
raise AssertionError("a refused launch changed app data")
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor")
parser.add_argument("--target", type=Path, default=ROOT / "target/debug",
help="directory with weft-appd, weft-runtime and weft-app-shell")
parser.add_argument("--store", type=Path, default=ROOT / "examples",
help="app store containing org.weft.demo.notes")
parser.add_argument("--output", type=Path, default=ROOT / "target/app-data-check")
args = parser.parse_args(argv)
xdotool = shutil.which("xdotool")
missing = missing_tools() + ([] if xdotool else ["xdotool"])
missing += [str(p) for p in (args.compositor, args.target / "weft-appd",
args.target / "weft-runtime",
args.target / "weft-app-shell")
if not p.is_file()]
if missing:
print("missing: " + ", ".join(missing), file=sys.stderr)
return 2
home = Path(tempfile.mkdtemp(prefix="weft-home-"))
try:
with Desktop(args.compositor, args.output, outputs=["appd.log"]) as desktop:
run(args, desktop, home, xdotool)
except (AssertionError, SessionError, TimeoutError) as failure:
print(f"app data check failed: {failure}", file=sys.stderr)
print(f"logs in {args.output}", file=sys.stderr)
return 1
finally:
shutil.rmtree(home, ignore_errors=True)
print("app data check passed")
return 0
if __name__ == "__main__":
raise SystemExit(main())