coverage.py (3469B)
1 # This file is part of GNU Taler 2 # (C) 2026 Taler Systems S.A. 3 # 4 # GNU Taler is free software; you can redistribute it and/or modify it under the 5 # terms of the GNU General Public License as published by the Free Software 6 # Foundation; either version 3, or (at your option) any later version. 7 # 8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # 12 # You should have received a copy of the GNU General Public License along with 13 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 14 """Pull JaCoCo execution data out of a running wallet. 15 16 The simulator drives the app from outside, so AGP's own instrumented-test 17 coverage plumbing never runs. Instead the "sim" build type carries the JaCoCo 18 agent plus the SimHooks receiver, and this module asks the agent for what it 19 has collected so far. Data accumulates in the live process, so dumping is 20 cheap and can be done between episodes; the file only has to be collected 21 before the process dies. 22 """ 23 24 from __future__ import annotations 25 26 import subprocess 27 from pathlib import Path 28 29 from device import ADB, PKG, Device 30 31 RECEIVER = f"{PKG}/net.taler.wallet.sim.SimHooks" 32 ACTION = "net.taler.wallet.sim.DUMP_COVERAGE" 33 34 35 # Every jacoco .exec stream starts with a block header and this magic. 36 MAGIC = b"\x01\xc0\xc0" 37 38 39 def dump(dev: Device, out_path: str | Path, name: str = "coverage.ec") -> Path: 40 """Dump coverage from the app and copy it off the device. 41 42 Raises if the agent is missing, which means the APK under test was not 43 built from the "sim" build type. 44 """ 45 out = Path(out_path) 46 out.parent.mkdir(parents=True, exist_ok=True) 47 res = dev.shell(f"am broadcast -a {ACTION} -n {RECEIVER} --es file {name}") 48 if "Broadcast completed" not in res: 49 raise RuntimeError(f"coverage broadcast not delivered: {res.strip()}") 50 # run-as is the only way into the app's private files; the sim build is 51 # debuggable, so it is allowed. 52 data = subprocess.run( 53 [ADB, "-s", dev.serial, "exec-out", "run-as", PKG, "cat", f"files/{name}"], 54 capture_output=True, timeout=120, 55 ) 56 if not data.stdout: 57 raise RuntimeError( 58 f"no coverage data (stderr: {data.stderr.decode(errors='replace').strip()}); " 59 "is this the fdroidSim APK?" 60 ) 61 # A dump whose process was killed while writing (the before-wipe one, as 62 # often as not) leaves a truncated file, and jacoco's `merge` aborts on the 63 # first one it cannot read -- losing every other file's data with it. So a 64 # dump that does not at least begin like execution data never gets the 65 # final name. 66 if not data.stdout.startswith(MAGIC): 67 raise RuntimeError( 68 f"coverage data for {name} is not jacoco execution data " 69 f"({len(data.stdout)} bytes, starts {data.stdout[:8]!r}); " 70 "the app was probably killed mid-dump" 71 ) 72 part = out.with_suffix(out.suffix + ".part") 73 part.write_bytes(data.stdout) 74 part.replace(out) 75 return out 76 77 78 if __name__ == "__main__": 79 import sys 80 d = Device(sys.argv[2] if len(sys.argv) > 2 else "emulator-5554") 81 target = sys.argv[1] if len(sys.argv) > 1 else "/opt/taler-sim/run/coverage/manual.ec" 82 p = dump(d, target) 83 print(f"{p} ({p.stat().st_size} bytes)")