// Package androidic provides an AndroidInContainer (androidic) client that // runs the official Tailscale Android app inside an Android emulator in a // Docker container, for integration testing with headscale. // // The app exposes no CLI or LocalAPI over adb, so, like tsric, verification // is done externally: headscale's node list, peer connectivity and the // on-screen UI (uiautomator). // // The emulator needs hardware virtualisation: the Docker host must expose // /dev/kvm. package androidic import ( "context" "crypto/md5" //nolint:gosec // Android names CA files by this hash. "crypto/x509" "encoding/binary" "encoding/pem" "encoding/xml" "errors" "fmt" "io" "log" "os" "path" "regexp" "slices" "strconv" "strings" "time" "github.com/cenkalti/backoff/v5" "github.com/juanfont/headscale/integration/dockertestutil" "github.com/juanfont/headscale/integration/integrationutil" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" "tailscale.com/util/rands" ) const ( // Package is the Tailscale Android application ID. Package = "com.tailscale.ipn" androidicHashLength = 6 dockerfileName = "Dockerfile.android-integration" dockerContextPath = "../." apkPath = "/tmp/tailscale.apk" helperAPKPath = "/opt/helper.apk" helperAdmin = "org.headscale.helper/.Admin" helperRestrict = "org.headscale.helper/.SetRestrictions" helperProbe = "org.headscale.helper/.Probe" uiDumpPath = "/sdcard/window_dump.xml" logBasePath = "/tmp/control" adbTimeout = 2 * time.Minute pollInterval = 2 * time.Second uiPollTimeout = 60 * time.Second ) var ( errNoNetwork = errors.New("androidic: no network set") errBootTimeout = errors.New("androidic: timed out waiting for emulator boot") errNoUINode = errors.New("androidic: no UI node matched") errInvalidCA = errors.New("androidic: CA is not PEM") errHelperFailed = errors.New("androidic: helper app failed") errFileNotFound = errors.New("androidic: file not found") errNoNetworkRoute = errors.New("androidic: device has no IPv4 default route") errTextMismatch = errors.New("androidic: text field does not hold the typed text") resultDataRe = regexp.MustCompile(`(?m)data="(.*)"\s*$`) ) // getPrebuiltImage returns the pre-built emulator image name if set. func getPrebuiltImage() string { return os.Getenv("HEADSCALE_INTEGRATION_ANDROID_IMAGE") } // AndroidInContainer runs the Tailscale Android app in an emulator. type AndroidInContainer struct { hostname string pool *dockertest.Pool container *dockertest.Resource network *dockertest.Network helperInstalled bool } // Option represents optional settings for an [AndroidInContainer]. type Option = func(c *AndroidInContainer) // WithNetwork sets the Docker [dockertest.Network]. func WithNetwork(network *dockertest.Network) Option { return func(a *AndroidInContainer) { a.network = network } } // New creates and starts a new [AndroidInContainer] and waits for the // emulator to finish booting. func New( pool *dockertest.Pool, opts ...Option, ) (*AndroidInContainer, error) { hash := rands.HexString(androidicHashLength) hostname := "android-" + hash if runID := dockertestutil.GetIntegrationRunID(); runID != "" { hostname = fmt.Sprintf("android-%s-%s", runID[len(runID)-6:], hash) } a := &AndroidInContainer{ hostname: hostname, pool: pool, } for _, opt := range opts { opt(a) } if a.network == nil { return nil, errNoNetwork } runOptions := &dockertest.RunOptions{ Name: hostname, Networks: []*dockertest.Network{a.network}, } dockertestutil.DockerAddIntegrationLabels(runOptions, "android") err := pool.RemoveContainerByName(hostname) if err != nil { return nil, err } // The emulator needs KVM and more memory than the shared // DockerMemoryLimit allows. hostConfig := func(config *docker.HostConfig) { config.Privileged = true config.Devices = append(config.Devices, docker.Device{ PathOnHost: "/dev/kvm", PathInContainer: "/dev/kvm", CgroupPermissions: "rwm", }) } var container *dockertest.Resource if prebuiltImage := getPrebuiltImage(); prebuiltImage != "" { log.Printf("Using pre-built android image: %s", prebuiltImage) repo, tag, ok := strings.Cut(prebuiltImage, ":") if !ok { return nil, fmt.Errorf("androidic: invalid image format %q, expected repository:tag", prebuiltImage) //nolint:err113 } runOptions.Repository = repo runOptions.Tag = tag container, err = pool.RunWithOptions(runOptions, hostConfig) } else { log.Printf("Building android emulator container %s (first build downloads the SDK)...", hostname) container, err = pool.BuildAndRunWithBuildOptions( &dockertest.BuildOptions{ Dockerfile: dockerfileName, ContextDir: dockerContextPath, Platform: "linux/amd64", }, runOptions, hostConfig, ) } if err != nil { return nil, fmt.Errorf("androidic: could not start container %s: %w", hostname, err) } a.container = container log.Printf("Created android container %s", hostname) err = a.waitForBoot(5 * time.Minute) if err != nil { return a, err } // Not fatal: a test that needs the network reports it more precisely. err = a.waitForNetwork(2 * time.Minute) if err != nil { log.Printf("android %s: %s", hostname, err) } return a, nil } // waitForNetwork waits until the device has an IPv4 default route. The // emulated Wi-Fi sometimes comes up without one, keeping only the IPv6 // default, which does not reach the Docker network; toggling the radios // brings it back. func (a *AndroidInContainer) waitForNetwork(timeout time.Duration) error { attempt := 0 _, err := poll(timeout, func() (struct{}, error) { out, _ := a.Shell("ip", "-4", "route", "show", "table", "all") if strings.Contains(out, "default via") { return struct{}{}, nil } attempt++ if attempt%10 == 0 { _ = a.SetNetwork(false) _ = a.SetNetwork(true) } return struct{}{}, errNoNetworkRoute }) return err } func (a *AndroidInContainer) waitForBoot(timeout time.Duration) error { _, err := poll(timeout, func() (struct{}, error) { out, _, _ := a.Execute([]string{"adb", "shell", "getprop", "sys.boot_completed"}) if strings.TrimSpace(out) != "1" { return struct{}{}, errBootTimeout } return struct{}{}, nil }) if err != nil { return err } // Dismiss the lock screen so UI automation reaches the app. _, _ = a.Shell("input", "keyevent", "82") // A fresh Chrome opens login URLs on its first-run screen, which Back // cannot leave; users have long since passed it. _, err = a.Shell( "echo '_ --disable-fre --no-default-browser-check --no-first-run' > /data/local/tmp/chrome-command-line && " + "am set-debug-app --persistent com.android.chrome", ) return err } func poll[T any](timeout time.Duration, fn func() (T, error)) (T, error) { return backoff.Retry( context.Background(), fn, backoff.WithBackOff(backoff.NewConstantBackOff(pollInterval)), backoff.WithMaxElapsedTime(timeout), ) } // Install installs or upgrades the Tailscale app from apk, an http(s) URL // (downloaded inside the container) or a path readable by the test // process, granting runtime permissions and pre-approving VPN consent. func (a *AndroidInContainer) Install(apk string) error { if strings.HasPrefix(apk, "http://") || strings.HasPrefix(apk, "https://") { _, stderr, err := a.Execute([]string{"curl", "-fsSL", "--retry", "3", "-o", apkPath, apk}) if err != nil { return fmt.Errorf("downloading APK %s: %w: %s", apk, err, stderr) } } else { data, err := os.ReadFile(apk) if err != nil { return fmt.Errorf("reading APK: %w", err) } err = a.WriteFile(apkPath, data) if err != nil { return fmt.Errorf("copying APK: %w", err) } } out, stderr, err := a.Execute([]string{"adb", "install", "-r", "-g", apkPath}) if err != nil || !strings.Contains(out, "Success") { return fmt.Errorf("adb install: %w: %s %s", err, out, stderr) //nolint:err113 } // Stands in for the user tapping "OK" on the system VPN dialog, // which lives outside the app and is not scriptable from it. _, err = a.Shell("appops", "set", Package, "ACTIVATE_VPN", "allow") if err != nil { return fmt.Errorf("granting VPN consent: %w", err) } return nil } // SetManagedConfig makes the bundled helper app device owner and replaces // the app's managed configuration (MDM) with config, as an EMM would. Keys // are the app's restriction keys, e.g. LoginURL. func (a *AndroidInContainer) SetManagedConfig(config map[string]string) error { err := a.installHelper() if err != nil { return err } out, _ := a.Shell("dpm", "set-device-owner", helperAdmin) if !strings.Contains(out, "Success") && !a.isDeviceOwner() { return fmt.Errorf("%w: dpm: %s", errHelperFailed, out) } args := []string{"am", "broadcast", "-n", helperRestrict, "--include-stopped-packages"} for k, v := range config { args = append(args, "--es", k, v) } _, err = a.helperBroadcast(args...) if err != nil { return fmt.Errorf("setting managed config: %w", err) } return nil } func (a *AndroidInContainer) isDeviceOwner() bool { out, _ := a.Shell("dumpsys", "device_policy") return strings.Contains(out, "org.headscale.helper") } // Resolve looks up host from an ordinary app on the device, so the // query goes through the VPN's DNS, and returns the addresses found. func (a *AndroidInContainer) Resolve(host string) ([]string, error) { out, err := a.probe("--es", "resolve", host) return strings.Fields(out), err } // Fetch GETs url from an ordinary app on the device, so the request goes // through the VPN, and returns the HTTP status code. func (a *AndroidInContainer) Fetch(url string) (int, error) { out, err := a.probe("--es", "url", url) if err != nil { return 0, err } return strconv.Atoi(out) } func (a *AndroidInContainer) probe(extras ...string) (string, error) { err := a.installHelper() if err != nil { return "", err } return a.helperBroadcast(append([]string{ "am", "broadcast", "-n", helperProbe, "--include-stopped-packages", }, extras...)...) } // helperBroadcast sends an ordered broadcast to the helper app and returns // the text after "ok" in its result data. Callers pass // --include-stopped-packages: a freshly installed app is stopped, and // Android 14+ does not deliver broadcasts to stopped packages otherwise. func (a *AndroidInContainer) helperBroadcast(args ...string) (string, error) { out, err := a.Shell(args...) if err != nil { return "", err } m := resultDataRe.FindStringSubmatch(out) if m == nil { return "", fmt.Errorf("%w: %s", errHelperFailed, out) } res, ok := strings.CutPrefix(m[1], "ok") if !ok { return "", fmt.Errorf("%w: %s", errHelperFailed, m[1]) } return strings.TrimSpace(res), nil } func (a *AndroidInContainer) installHelper() error { if a.helperInstalled { return nil } out, stderr, err := a.Execute([]string{"adb", "install", "-r", helperAPKPath}) if err != nil || !strings.Contains(out, "Success") { return fmt.Errorf("installing helper: %w: %s %s", err, out, stderr) //nolint:err113 } a.helperInstalled = true return nil } // SetNetwork turns the device's Wi-Fi and mobile data on or off, as // airplane mode or losing signal would. func (a *AndroidInContainer) SetNetwork(on bool) error { state := "disable" if on { state = "enable" } _, err := a.Shell("svc", "wifi", state) if err != nil { return err } _, err = a.Shell("svc", "data", state) return err } // UseExitNode selects the peer with the given display name as exit node // through the app's public USE_EXIT_NODE intent, as automation apps do. func (a *AndroidInContainer) UseExitNode(name string) error { _, err := a.Shell( "am", "broadcast", "-a", Package+".USE_EXIT_NODE", "-n", Package+"/.IPNReceiver", "--include-stopped-packages", "--es", "exitNode", name, ) return err } // FindFile returns the contents of the first file named name in shared // storage or the app's private storage, where Taildrop puts received files. func (a *AndroidInContainer) FindFile(name string) (string, error) { err := a.root() if err != nil { return "", err } out, err := a.Shell(fmt.Sprintf( "f=$(find /sdcard/ /data/media/ /data/data/%s/ -name %q 2>/dev/null | head -n1); [ -n \"$f\" ] && cat \"$f\"", Package, name, )) if err != nil { return "", fmt.Errorf("%w: %s", errFileNotFound, name) } return out, nil } // TapAnyOf taps the first of labels, in order, that is on screen and // enabled, and reports which; "" if none is. One dump serves all labels, // so the choice reflects a single screen. func (a *AndroidInContainer) TapAnyOf(labels ...string) (string, error) { root, err := a.dumpTree() if err != nil { return "", err } label, n, ok := firstOf(root, labels) if !ok { return "", nil } return label, a.tapNode(n) } // FirstVisible returns the first of labels, in order, that is on screen // and enabled; "" if none is. func (a *AndroidInContainer) FirstVisible(labels ...string) (string, error) { root, err := a.dumpTree() if err != nil { return "", err } label, _, _ := firstOf(root, labels) return label, nil } func firstOf(root uiNode, labels []string) (string, uiNode, bool) { for _, label := range labels { n, ok := root.find(func(n uiNode) bool { return (n.Text == label || n.Desc == label) && n.Enabled != "false" }) if ok { return label, n, true } } return "", uiNode{}, false } // Debuggable reports whether the installed app is a debug build, which // carries the integration login hook (LoginHook). func (a *AndroidInContainer) Debuggable() (bool, error) { out, err := a.Shell("dumpsys", "package", Package) return strings.Contains(out, "DEBUGGABLE"), err } // LoginHook logs in via the debug-only integration broadcast, skipping the // UI. It follows the same prefs/start/login sequence as the UI does. func (a *AndroidInContainer) LoginHook(controlURL, authKey string) error { _, err := a.Shell( "am", "broadcast", "-a", Package+".integration.LOGIN", "-n", Package+"/.IPNReceiver", "--include-stopped-packages", "--es", "control_url", controlURL, "--es", "auth_key", authKey, ) return err } // Crashes returns the crash log buffer if the app has crashed since boot. func (a *AndroidInContainer) Crashes() (string, error) { out, _, err := a.Execute([]string{"adb", "logcat", "-d", "-b", "crash"}) if err != nil { return "", err } if !strings.Contains(out, "Process: "+Package) { return "", nil } return out, nil } // InstallUserCA adds a PEM CA certificate to the device's user CA store, // as installing it from Settings would. func (a *AndroidInContainer) InstallUserCA(caPEM []byte) error { const dir = "/data/misc/user/0/cacerts-added" name, err := a.pushCA(caPEM) if err != nil { return err } _, err = a.Shell(fmt.Sprintf( "mkdir -p %[1]s && mv /data/local/tmp/%[2]s %[1]s/%[2]s && "+ "chown -R system:system %[1]s && chmod 644 %[1]s/%[2]s && restorecon -R %[1]s", dir, name, )) return err } // InstallSystemCA adds a PEM CA certificate to the device's system CA // store, which every app version trusts. The store is overlaid with a // tmpfs holding the stock CAs plus caPEM, so the read-only system image is // untouched. Apps started afterwards see it: from Android 14 apps fork // from zygote's mount namespace, so the overlay is repeated there. func (a *AndroidInContainer) InstallSystemCA(caPEM []byte) error { const ( dir = "/system/etc/security/cacerts" staged = "/data/local/tmp/cacerts" ) name, err := a.pushCA(caPEM) if err != nil { return err } overlay := fmt.Sprintf( "mount -t tmpfs none %[1]s && cp %[2]s/* %[1]s/ && chown root:root %[1]s/* && "+ "chmod 644 %[1]s/* && chcon u:object_r:system_file:s0 %[1]s/*", dir, staged, ) _, err = a.Shell(fmt.Sprintf( "mkdir -p %[1]s && cp %[2]s/* %[1]s/ && mv /data/local/tmp/%[3]s %[1]s/ && "+ "%[4]s && for p in $(pidof zygote zygote64); do "+ "nsenter --mount=/proc/$p/ns/mnt -- /system/bin/sh -c '%[4]s' || exit 1; done", staged, dir, name, overlay, )) return err } // AddHost maps name to ip in the device's /etc/hosts, standing in for DNS: // the emulator only reaches Docker's resolver through its NAT, which is // flaky. Like [AndroidInContainer.InstallSystemCA] it overlays the file in // zygote's namespace too, so apps started afterwards see it. func (a *AndroidInContainer) AddHost(name, ip string) error { const ( hosts = "/system/etc/hosts" staged = "/data/local/tmp/hosts" ) err := a.root() if err != nil { return err } overlay := fmt.Sprintf("mount --bind %s %s", staged, hosts) _, err = a.Shell(fmt.Sprintf( "cat %[1]s > %[2]s && echo '%[3]s %[4]s' >> %[2]s && chmod 644 %[2]s && "+ "chcon u:object_r:system_file:s0 %[2]s && %[5]s && for p in $(pidof zygote zygote64); do "+ "nsenter --mount=/proc/$p/ns/mnt -- /system/bin/sh -c '%[5]s' || exit 1; done", hosts, staged, ip, name, overlay, )) return err } // pushCA copies a PEM CA to /data/local/tmp on the device under the name // Android's CA stores expect, leaving adbd running as root. Needs a // google_apis image, where adbd can run as root. func (a *AndroidInContainer) pushCA(caPEM []byte) (string, error) { block, _ := pem.Decode(caPEM) if block == nil { return "", errInvalidCA } cert, err := x509.ParseCertificate(block.Bytes) if err != nil { return "", fmt.Errorf("parsing CA: %w", err) } // Android names store entries by OpenSSL's subject_hash_old: the first // four bytes of the subject's MD5, little endian. sum := md5.Sum(cert.RawSubject) //nolint:gosec name := fmt.Sprintf("%08x.0", binary.LittleEndian.Uint32(sum[:4])) err = a.WriteFile("/tmp/"+name, caPEM) if err != nil { return "", err } err = a.root() if err != nil { return "", err } _, stderr, err := a.Execute([]string{"adb", "push", "/tmp/" + name, "/data/local/tmp/" + name}) if err != nil { return "", fmt.Errorf("pushing CA: %w: %s", err, stderr) } return name, nil } // root restarts adbd as root, retrying while adbd is still restarting. func (a *AndroidInContainer) root() error { _, err := poll(30*time.Second, func() (struct{}, error) { _, stderr, err := a.Execute([]string{"sh", "-c", "adb root && adb wait-for-device"}) if err != nil { return struct{}{}, fmt.Errorf("adb root: %w: %s", err, stderr) } return struct{}{}, nil }) return err } // Launch brings the app's main activity to the front. The app opens login // URLs in a Chrome Custom Tab inside its own task, so Chrome is stopped // first or relaunching would show the tab again. func (a *AndroidInContainer) Launch() error { _, err := a.Shell("am", "force-stop", "com.android.chrome") if err != nil { return err } _, err = a.Shell("monkey", "-p", Package, "-c", "android.intent.category.LAUNCHER", "1") return err } // Version returns the installed app's versionName. func (a *AndroidInContainer) Version() (string, error) { out, err := a.Shell("dumpsys", "package", Package) if err != nil { return "", err } for line := range strings.SplitSeq(out, "\n") { if v, ok := strings.CutPrefix(strings.TrimSpace(line), "versionName="); ok { return v, nil } } return "", nil } // uiNode is a node in a uiautomator hierarchy dump. type uiNode struct { Text string `xml:"text,attr"` Desc string `xml:"content-desc,attr"` Class string `xml:"class,attr"` Pkg string `xml:"package,attr"` Enabled string `xml:"enabled,attr"` Focused string `xml:"focused,attr"` Bounds string `xml:"bounds,attr"` Nodes []uiNode `xml:"node"` } func (n uiNode) walk(fn func(uiNode) bool) bool { if fn(n) { return true } for _, c := range n.Nodes { if c.walk(fn) { return true } } return false } // center returns the centre of bounds "[x1,y1][x2,y2]". func (n uiNode) center() (int, int, error) { var x1, y1, x2, y2 int _, err := fmt.Sscanf(n.Bounds, "[%d,%d][%d,%d]", &x1, &y1, &x2, &y2) if err != nil { return 0, 0, fmt.Errorf("parsing bounds %q: %w", n.Bounds, err) } return (x1 + x2) / 2, (y1 + y2) / 2, nil } // DumpUI returns the raw uiautomator XML of the current screen. func (a *AndroidInContainer) DumpUI() (string, error) { _, err := a.Shell("uiautomator", "dump", uiDumpPath) if err != nil { return "", err } return a.Shell("cat", uiDumpPath) } func (a *AndroidInContainer) findNode(match func(uiNode) bool) (uiNode, bool, error) { root, err := a.dumpTree() if err != nil { return uiNode{}, false, err } n, ok := root.find(match) return n, ok, nil } func (a *AndroidInContainer) dumpTree() (uiNode, error) { raw, err := a.DumpUI() if err != nil { return uiNode{}, err } start := strings.Index(raw, "= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' { return r } return '-' }, s) } // IPInNetwork returns the emulator container's address on network, the // source address headscale sees for the device's connections. func (a *AndroidInContainer) IPInNetwork(network *dockertest.Network) string { return a.container.GetIPInNetwork(network) } // Hostname returns the container hostname. func (a *AndroidInContainer) Hostname() string { return a.hostname } // ContainerID returns the Docker container ID. func (a *AndroidInContainer) ContainerID() string { return a.container.Container.ID } // Shutdown saves logcat, a final screenshot and the container logs, then // removes the container. func (a *AndroidInContainer) Shutdown() (string, string, error) { err := os.MkdirAll(logBasePath, os.ModePerm) if err == nil { a.saveScreen("final") logcat, _, err := a.Execute([]string{"adb", "logcat", "-d", "-b", "all"}) if err == nil { _ = os.WriteFile(path.Join(logBasePath, a.hostname+".logcat.log"), []byte(logcat), 0o644) //nolint:gosec } } stdoutPath, stderrPath, err := a.SaveLog(logBasePath) if err != nil { log.Printf("saving log from %s: %s", a.hostname, err) } return stdoutPath, stderrPath, a.pool.Purge(a.container) } // SaveLog saves the container stdout/stderr to the given path. func (a *AndroidInContainer) SaveLog(path string) (string, string, error) { return dockertestutil.SaveLog(a.pool, a.container, path) } // WriteLogs writes the container stdout/stderr to the given writers. func (a *AndroidInContainer) WriteLogs(stdout, stderr io.Writer) error { return dockertestutil.WriteLog(a.pool, a.container, stdout, stderr) } // Execute runs a command inside the emulator container (not the device). func (a *AndroidInContainer) Execute( command []string, options ...dockertestutil.ExecuteCommandOption, ) (string, string, error) { if len(options) == 0 { options = []dockertestutil.ExecuteCommandOption{dockertestutil.ExecuteCommandTimeout(adbTimeout)} } return dockertestutil.ExecuteCommand(a.container, command, []string{}, options...) } // WriteFile writes a file into the emulator container. func (a *AndroidInContainer) WriteFile(path string, data []byte) error { return integrationutil.WriteFileToContainer(a.pool, a.container, path, data) }