explore.py (7507B)
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 """Clicking on everything, in an order nobody would choose. 15 16 The scenarios walk the terminal the way it is meant to be walked. This walks 17 it the way it is not: from a configured, already-used terminal, pick a control 18 at random -- biased towards screens the walk has not seen -- and press it. It 19 is the part of the harness that finds sequences no one thought to write down. 20 21 Two things keep it from wandering off into the system: 22 23 * a control that would leave the app (the tutorial link opens a browser) is 24 followed, judged, and then the terminal is brought back to the front -- so 25 the walk cannot spend a hundred steps in Chrome's search suggestions; 26 * the terminal is re-provisioned whenever it ends up unconfigured, because an 27 unconfigured terminal has one screen and the walk would stay on it. 28 29 python3 explore.py --steps 150 --seed 7 30 python3 explore.py --steps 400 --seed 7 --faults 31 """ 32 33 from __future__ import annotations 34 35 import argparse 36 import json 37 import os 38 import random 39 import re 40 import sys 41 import time 42 from collections import Counter 43 from pathlib import Path 44 45 sys.path.insert(0, str(Path(__file__).resolve().parent)) 46 47 import merchant 48 from device import Device, Screen, serial_for 49 from episode import Action, Episode 50 from pos import Pos, PosError 51 52 SIM_ROOT = Path(os.environ.get("POS_SIM_ROOT", "/tmp/taler-pos-sim")) 53 WORKER = int(os.environ.get("POS_WORKER", "0")) 54 SERIAL = serial_for(WORKER) 55 56 # Labels the walk must not press, and why. 57 # 58 # Logging out is not forbidden -- it is a legitimate thing to do and the walk 59 # recovers by re-provisioning -- but it costs a provisioning round trip every 60 # time, so it is rare rather than impossible. 61 AVOID = re.compile(r"(?i)^(Log out)$") 62 # "Share" opens the system chooser, which is a whole other app to get out of, 63 # so it is tried occasionally rather than constantly. 64 RARE = re.compile(r"(?i)log out|Force delete|tutorials|^share$") 65 66 # Text a walk should type into whatever field it lands on: a price, something 67 # that is not a price, and something a layout was not designed for. 68 TYPED = [ 69 "1.00", "0", "0.00", "99999999", "-1", "1,5", "1.2.3", "abc", 70 " ", "'; drop table orders; --", "Добро", 71 "القهوة", "\U0001F382", "x" * 300, 72 ] 73 74 75 def choose(screen: Screen, seen: Counter, rng: random.Random): 76 """Pick the next action, favouring what has not been pressed yet.""" 77 candidates = [] 78 for node in screen.tappable(): 79 label = node.label.strip() 80 if label and AVOID.search(label): 81 continue 82 weight = 1.0 / (1 + seen[label or str(node.bounds)]) 83 if label and RARE.search(label): 84 weight *= 0.05 85 candidates.append((weight, node)) 86 if not candidates: 87 return Action("back") 88 total = sum(w for w, _ in candidates) 89 pick = rng.uniform(0, total) 90 upto = 0.0 91 for weight, node in candidates: 92 upto += weight 93 if upto >= pick: 94 label = node.label.strip() 95 if node.editable: 96 return Action("type", rng.choice(TYPED)) 97 if label: 98 return Action("tap_label", label) 99 return Action("tap_xy", list(node.center)) 100 return Action("back") 101 102 103 def main(argv: list[str]) -> int: 104 ap = argparse.ArgumentParser(description=__doc__, 105 formatter_class=argparse.RawDescriptionHelpFormatter) 106 ap.add_argument("--steps", type=int, default=150) 107 ap.add_argument("--seed", type=int, default=None) 108 ap.add_argument("--faults", action="store_true", 109 help="mix in airplane mode, process death and relaunches") 110 ap.add_argument("--out", default=None) 111 args = ap.parse_args(argv) 112 113 seed = args.seed if args.seed is not None else random.randrange(1 << 30) 114 rng = random.Random(seed) 115 stamp = time.strftime("%Y%m%d-%H%M%S") 116 out_dir = Path(args.out or (SIM_ROOT / "out" / f"explore-{stamp}-w{WORKER}")) 117 out_dir.mkdir(parents=True, exist_ok=True) 118 119 dev = Device(SERIAL) 120 ep = Episode(dev, out_dir, name=f"explore-seed{seed}", 121 coverage_dir=SIM_ROOT / "run" / "coverage") 122 app = Pos(dev) 123 app.before_wipe = lambda: ep.dump_coverage("before-wipe") 124 125 inst = merchant.demo() 126 ep.note(f"seed {seed}, {args.steps} steps, demo instance " 127 f"{inst.merchant.instance}") 128 app.fresh_start() 129 app.provision_by_uri(inst.setup_uri()) 130 ep.observe() 131 132 seen: Counter[str] = Counter() 133 for step in range(args.steps): 134 screen, _ = ep.observe() 135 if not screen.activity.startswith("net.taler.merchantpos"): 136 # Back first, then launch. Bringing the terminal to the front 137 # while a system dialog is up -- the share sheet, which "Share 138 # payment URI" opens -- leaves the dialog on top of it, so the 139 # next step sees the same foreign activity and the walk spends the 140 # rest of its budget bouncing off it. One seed lost 150 of its 141 # 220 steps that way. 142 ep.note(f"step {step}: outside the app ({screen.activity}); coming back") 143 dev.back() 144 time.sleep(1.5) 145 if not dev.dump().activity.startswith("net.taler.merchantpos"): 146 dev.launch() 147 time.sleep(3) 148 continue 149 if screen.find(r"Merchant portal URL") and rng.random() < 0.7: 150 # Unconfigured: one screen, nothing to explore. Put it back. 151 ep.note(f"step {step}: unconfigured; provisioning again") 152 try: 153 app.provision_by_uri(inst.setup_uri()) 154 except (PosError, TimeoutError) as e: 155 ep.note(f"could not re-provision: {e}") 156 app.fresh_start() 157 continue 158 if args.faults and rng.random() < 0.04: 159 fault = rng.choice(["offline_blip", "process_death", "relaunch"]) 160 ep.note(f"step {step}: fault {fault}") 161 ep.perform(Action(fault)) 162 time.sleep(4) 163 continue 164 action = choose(screen, seen, rng) 165 if action.op == "tap_label": 166 seen[str(action.arg)] += 1 167 ep.perform(action, screen) 168 if step % 25 == 0: 169 ep.dump_coverage(f"step{step:04d}") 170 print(f" step {step:>4} {len(ep.signatures)} screens, " 171 f"{len(ep.session.serious())} serious finding(s)", flush=True) 172 173 summary = ep.finish({"seed": seed, "steps_requested": args.steps, 174 "faults": args.faults}) 175 print(json.dumps({k: v for k, v in summary.items() if k != "findings"}, 176 indent=2)) 177 for finding in summary["findings"]: 178 print(f" [{finding['kind']}] {finding['detail'][:200]}") 179 print(f"artifacts: {out_dir}") 180 return 0 181 182 183 if __name__ == "__main__": 184 sys.exit(main(sys.argv[1:]))