99 lines
4.4 KiB
Python
99 lines
4.4 KiB
Python
"""Opt-in, read-only smoke test through a remote ADB smart socket and all three programs."""
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import secrets
|
|
import subprocess
|
|
import time
|
|
import urllib.request
|
|
|
|
from e2e import ROOT, free_port, wait_until
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--adb-server", required=True)
|
|
parser.add_argument("--serial", required=True)
|
|
parser.add_argument("--adb", default="adb")
|
|
parser.add_argument("--bin-dir", type=Path, default=ROOT / "bin")
|
|
parser.add_argument("--rust-dir", type=Path)
|
|
args = parser.parse_args()
|
|
suffix = ".exe" if os.name == "nt" else ""
|
|
rust_dir = args.rust_dir or args.bin_dir
|
|
relay_port, local_port = free_port(), free_port()
|
|
server_url = f"ws://127.0.0.1:{relay_port}"
|
|
endpoint = f"127.0.0.1:{local_port}"
|
|
env = os.environ.copy()
|
|
env["ADS_AGENT_TOKEN"] = secrets.token_hex(32)
|
|
env["ADS_CLIENT_TOKEN"] = secrets.token_hex(32)
|
|
for key in ("ADB_SERVER_SOCKET", "ANDROID_ADB_SERVER_ADDRESS", "ANDROID_ADB_SERVER_PORT"):
|
|
env.pop(key, None)
|
|
folder = ROOT / "work" / "real-device"
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
processes, files = [], []
|
|
|
|
def start(name, command):
|
|
handle = open(folder / f"{name}.log", "wb")
|
|
files.append(handle)
|
|
process = subprocess.Popen([str(value) for value in command], env=env,
|
|
stdout=handle, stderr=subprocess.STDOUT,
|
|
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
|
|
processes.append(process)
|
|
|
|
def adb(*command):
|
|
return subprocess.run([args.adb, *command], env=env, capture_output=True,
|
|
text=True, timeout=10)
|
|
|
|
def leased():
|
|
request = urllib.request.Request(f"http://127.0.0.1:{relay_port}/v1/devices",
|
|
headers={"Authorization": "Bearer " + env["ADS_CLIENT_TOKEN"]})
|
|
with urllib.request.urlopen(request, timeout=2) as response:
|
|
return b'"busy":true' in response.read()
|
|
|
|
try:
|
|
start("server", [(args.bin_dir / f"ads-server{suffix}").resolve(), "--listen", f"127.0.0.1:{relay_port}"])
|
|
start("agent", [(rust_dir / f"ads-agent{suffix}").resolve(), "--server", server_url,
|
|
"--allow-insecure", "--device", "smoke-phone", "--adb-server", args.adb_server,
|
|
"--adb-serial", args.serial])
|
|
start("client", [(rust_dir / f"ads-client{suffix}").resolve(), "--server", server_url,
|
|
"--allow-insecure", "--device", "smoke-phone", "--listen", endpoint,
|
|
"--no-auto-connect"])
|
|
wait_until(leased, "remote phone lease", seconds=20)
|
|
print(adb("connect", endpoint).stdout.strip(), flush=True)
|
|
print("If requested, approve this computer's debugging key on the phone.", flush=True)
|
|
deadline = time.monotonic() + 20
|
|
while True:
|
|
state = adb("-s", endpoint, "get-state")
|
|
if state.returncode == 0 and state.stdout.strip() == "device":
|
|
break
|
|
if time.monotonic() >= deadline:
|
|
raise RuntimeError("Phone did not authorize local ADB: " + state.stderr.strip())
|
|
time.sleep(1)
|
|
listing = adb("devices", "-l")
|
|
assert listing.returncode == 0 and endpoint in listing.stdout
|
|
print(listing.stdout.strip())
|
|
model = adb("-s", endpoint, "shell", "getprop", "ro.product.model")
|
|
assert model.returncode == 0, model.stderr
|
|
marker = adb("-s", endpoint, "shell", "echo", "ads-read-only-smoke-ok")
|
|
assert marker.returncode == 0 and marker.stdout.strip() == "ads-read-only-smoke-ok", marker.stderr
|
|
print("PASS: ordinary local adb devices and remote shell via Go server + both Rust daemons")
|
|
print("Remote model:", model.stdout.strip())
|
|
finally:
|
|
try:
|
|
adb("disconnect", endpoint)
|
|
finally:
|
|
for process in reversed(processes):
|
|
if process.poll() is None:
|
|
process.terminate()
|
|
try:
|
|
process.wait(timeout=5)
|
|
except subprocess.TimeoutExpired:
|
|
process.kill()
|
|
process.wait(timeout=5)
|
|
for handle in files:
|
|
handle.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|