Files
VeraCrypt/Tests/test_macos_gui_lifecycle.py
T
Mounir IDRASSI 596beb82a1 macOS: harden volume discovery and FUSE-T teardown
Run display discovery asynchronously and bound batched disk-image inventory
queries. Filter auxiliary mounts by basename, filesystem and owner, and
keep unresolved candidates separate from verified volumes. Allow targeted
dismounts despite incomplete discovery while requiring complete results
for slot allocation and empty-inventory decisions.

Use nonthrowing GUI snapshots with monotonic freshness, completion events
and safe window lifetimes. Suspend inactivity decisions while the snapshot
is stale, without restarting idle timers: activity counters are cumulative
per volume instance. Refresh logout targets and retry only failed ones.
Show a progress dialog for interactive unmounts, but keep automatic ones
synchronous, so a quit or logout request that arrives meanwhile is handled
afterwards rather than refused. Guard core operations against reentry and
route wait-dialog requests only from worker threads, so a main-thread
message cannot wait for itself.

Bind teardown to captured mount and service identities, including process
start time. Report a service exit that cannot be confirmed after auxiliary
mount removal as a distinct error that keeps the original details. Such
volumes are not retried, and a multi-volume unmount reports all of them
together with any other failure or a declined prompt. Warn about it after
automatic unmounts and at quit, and keep the background application until
the warning is acknowledged. Keep rollback responsive, and let services
remove their auxiliary directories without probing mounted paths. Preserve
released /control compatibility and perform best-effort cleanup for older
services.

Reap bounded subprocesses as soon as their output ends. A child that
survives SIGKILL is reaped by a later call, which starts no new child until
then. Keep subjects and subprocess command, status and error output when
formatting exceptions for wrapping and logs. Clarify discovery and rollback
diagnostics. Fix the localization-dependent busy-volume regression
assertion and extend discovery, snapshot, process identity, cleanup, GUI
lifecycle, inactivity and teardown coverage.
2026-09-28 03:17:28 +02:00

131 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Ordinary FUSE-T container and background GUI lifecycle; no sudo required.
Requires a logged-in macOS desktop and no other VeraCrypt processes or mounts.
Uses only a disposable container, isolated preferences, and a local app bundle.
No fault injection, device reassignment, or forced unmount is performed.
"""
import argparse
import hashlib
import os
from pathlib import Path
import plistlib
import shutil
import subprocess
import tempfile
import time
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--binary", required=True, type=Path)
parser.add_argument("--keep-artifacts", action="store_true")
args = parser.parse_args()
binary = args.binary.resolve(strict=True)
mounts = lambda: subprocess.check_output(["/sbin/mount"], text=True)
if ".veracrypt_aux_mnt" in mounts():
raise RuntimeError("Dismount existing VeraCrypt volumes before this isolated GUI test")
processes = subprocess.check_output(["/bin/ps", "-axo", "comm="], text=True)
if any(Path(line.strip()).name.lower() == "veracrypt" for line in processes.splitlines()):
raise RuntimeError("Close other VeraCrypt processes before this isolated GUI test")
root = Path(tempfile.mkdtemp(prefix="vc-gui-lifecycle-")).resolve()
print("Artifacts:", root, flush=True)
temporary = root / "tmp"
mountpoint = root / "mount"
config = root / "config" / "VeraCrypt"
temporary.mkdir(mode=0o700)
mountpoint.mkdir()
config.mkdir(parents=True)
prefs = dict(BackgroundTaskEnabled=1, CloseBackgroundTaskOnNoVolumes=1,
DismountOnInactivity=0, DismountOnLogOff=1, DismountOnPowerSaving=0,
MountFavoritesOnLogon=0, MountDevicesOnLogon=0,
OpenExplorerWindowAfterMount=0, SaveHistory=0)
(config / "Configuration.xml").write_text("<VeraCrypt><configuration>" + "".join(
f'<config key="{key}">{value}</config>' for key, value in prefs.items()
) + "</configuration></VeraCrypt>")
env = dict(os.environ, TMPDIR=str(temporary) + "/", XDG_CONFIG_HOME=str(root / "config"))
contents = root / "VeraCryptTest.app" / "Contents"
executable = contents / "MacOS" / "VeraCrypt"
executable.parent.mkdir(parents=True)
shutil.copy2(binary, executable)
(contents / "Info.plist").write_bytes(plistlib.dumps(dict(
CFBundleExecutable="VeraCrypt", CFBundleName="VeraCrypt Lifecycle Test",
CFBundleIdentifier=f"org.idrix.veracrypt.lifecycle-test.{os.getpid()}",
CFBundlePackageType="APPL", NSHighResolutionCapable=True)))
volume = root / "ordinary.hc"
password = "Disposable-normal-lifecycle-only"
gui = None
active = None
succeeded = False
def vc(label, *options):
result = subprocess.run([str(binary), "--text", "--non-interactive", *map(str, options)],
env=env, capture_output=True, text=True, timeout=90)
(root / (label + ".log")).write_text(result.stdout + result.stderr)
if result.returncode:
raise RuntimeError(f"{label} failed; see {root / (label + '.log')}")
def mount(read_only=False):
nonlocal active
vc("mount", "--mount", volume, mountpoint, "--password=" + password,
"--pim=1", "--keyfiles=", "--protect-hidden=no",
*(["--mount-options=ro"] if read_only else []))
identities = list(temporary.glob(".veracrypt_aux_mnt*/shutdown"))
assert len(identities) == 1, "Expected one disposable FUSE-T service"
aux = identities[0].parent
active = (int(identities[0].read_text().split()[0]), aux,
Path((aux / "shutdown-socket").read_text().strip()))
def confirm_closed():
if active:
pid, aux, endpoint = active
try:
os.kill(pid, 0)
except ProcessLookupError:
pass
else:
raise AssertionError("Disposable service still running")
assert not aux.exists() and not endpoint.exists(), "Cleanup paths remain"
assert str(root) not in mounts(), "Disposable mount remains"
handles = subprocess.run(["/usr/sbin/lsof", "-t", str(volume)], capture_output=True, text=True)
assert handles.returncode == 1 and not handles.stdout.strip(), "Backing file still open"
try:
vc("create", "--create", volume, "--size=33554432", "--volume-type=normal",
"--encryption=AES", "--hash=sha512", "--filesystem=FAT", "--password=" + password,
"--pim=1", "--keyfiles=", "--random-source=/dev/urandom")
mount()
payload = bytes(range(256)) * 4096
with (mountpoint / "payload.bin").open("wb") as stream:
stream.write(payload)
stream.flush()
os.fsync(stream.fileno())
vc("unmount-write", "--unmount", volume)
confirm_closed()
mount(read_only=True)
assert hashlib.sha256((mountpoint / "payload.bin").read_bytes()).digest() == hashlib.sha256(payload).digest()
with (root / "gui.log").open("w") as log:
gui = subprocess.Popen([str(executable), "--background-task"], env=env,
stdout=log, stderr=log, start_new_session=True)
time.sleep(5)
assert gui.poll() is None, "Background app exited with a mounted volume"
vc("unmount-readonly", "--unmount", volume)
assert gui.wait(timeout=20) == 0, "Background app did not exit cleanly"
confirm_closed()
succeeded = True
print("PASS: payload integrity, initial background discovery, external-dismount refresh, automatic exit, service/file cleanup", flush=True)
finally:
if gui and gui.poll() is None:
gui.terminate()
gui.wait(timeout=10)
if str(root) in mounts():
vc("test-cleanup", "--unmount", volume)
if succeeded and not args.keep_artifacts:
confirm_closed()
shutil.rmtree(root)
if __name__ == "__main__":
main()