Files
sanderling/internal/android/apk_test.go
T
pj 9ab59365b2 redact passwords only, and say what each step did in the log (#93)
* feat(hierarchy): name the route a native tree shows

The screen name was web-only: the Chrome driver stamps sanderling-screen on
the root and nothing else does, so every Android and iOS step recorded and
logged an empty screen. The route marker the tree already carries (the
resource id ending in Screen, the same one Transitional counts) names it.

* feat(runner): say what each step did in the step log

One line per step carried only an index and a node count. It now names the
screen, the action, its target and the typed value, the last through the
same redaction the trace and the prompt use. Emitted after the apply so the
line reports what actually happened, skip reason included.

* fix(sidecar): state on android whether a field is a secure entry

maestro's tree mapper copies a fixed attribute list off the device's XML and
password is not on it, so no android element ever reported the fact and the
conservative rule downstream redacted every typed value in the trace, the
prompt and the log. The XML still carries it: re-read it once per settled
snapshot and state the fact on the text fields it matches. A field it cannot
match stays unstated, which still reads as a credential.

* docs: correct the record that android never reports a secure field

Four places said android reports the fact for nothing and that every typed
value there is redacted. The sidecar now states it, so they described the
old behaviour.

* test(sidecar): fail the build if maestro renames the call the fact comes from

* fix(sidecar): state the fact on a field named by its hint alone

collectTextFields matched on class only, so a node the go side calls editable
off its hintText was left unstated and its typed value redacted.

* docs: record that ios and web state secure:false for compose password fields

Both derive the fact from a widget type a compose app never has, so the
value reaches the trace in the clear. Verified on folio on both targets.

* feat(android): read the application id out of an apk

parses the compiled AndroidManifest.xml rather than shelling out to
aapt2, which lives in the versioned build-tools directory that hosts
with only platform-tools never install.

Claude-Session: https://claude.ai/code/session_012PVErdr3ZzyUASeVQDWsUc

* feat(cli): let --android-app-path supply the bundle id

--bundle-id stays required everywhere else, and an explicit one still
wins, so the apk can never quietly override what was asked for.

Claude-Session: https://claude.ai/code/session_012PVErdr3ZzyUASeVQDWsUc

* docs: record that the apk can name the package itself

Claude-Session: https://claude.ai/code/session_012PVErdr3ZzyUASeVQDWsUc

* fix(testrun): pass the jvm the flag that silences the jdk 24 unsafe warning

* feat(folio): ask which android device to run on when none is named

* feat(folio): pin ios recipes to one simulator udid and ask when several match

* docs(ci): say how just ios lands on the simulator the boot step chose

* chore(folio): ignore run output anywhere under examples/folio

* refactor(hierarchy): name no screen for a tree Transitional calls a cross-fade

ScreenName kept its own reading of the route markers and disagreed with
Transitional on a marker repeated by a nested node: it named the screen on
a step the runner was skipping as unsettled. One reading now.

* refactor(sidecar): inline the one attempt passed to callViewHierarchy

A named constant and its own comment for a literal used once.

* test(sidecar): compare the whole tree when checking the annotation changes nothing else

The old assertions checked one id string and one bounds value, and passed
with every other attribute stripped off every node. Now the annotated tree
minus the two facts it stated must equal the input.

* fix(sidecar): match a field to its xml node by class as well as id and bounds

A wrapper drawn to the same bounds as the untagged field inside it shared
the field's key, both were dropped as ambiguous, and every value typed into
an untagged field was redacted. The class tells them apart.

* docs(runs): record what the android hierarchy re-read costs per step

Two 1m runs per binary on folio, same seed, before and after the re-read.
2026-09-05 23:54:12 +05:30

174 lines
5.2 KiB
Go

package android
import (
"archive/zip"
"bytes"
"encoding/binary"
"os"
"path/filepath"
"strings"
"testing"
)
// folioAPK is the folio debug APK stripped to the one entry the reader looks
// at, so the parse runs against a manifest aapt2 really produced.
const folioAPK = "testdata/folio.apk"
func TestPackageName_ReadsCompiledManifest(t *testing.T) {
name, err := PackageName(folioAPK)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if name != "app.folio" {
t.Fatalf("package name: got %q, want app.folio", name)
}
}
func TestPackageName_UTF8StringPool(t *testing.T) {
name, err := PackageName(apkWithManifest(t, utf8PooledManifest("com.example.utf8")))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if name != "com.example.utf8" {
t.Fatalf("package name: got %q, want com.example.utf8", name)
}
}
func TestPackageName_NotAnAPK(t *testing.T) {
path := filepath.Join(t.TempDir(), "app.apk")
if err := os.WriteFile(path, []byte("this is not a zip"), 0o600); err != nil {
t.Fatalf("write file: %v", err)
}
_, err := PackageName(path)
if err == nil || !strings.Contains(err.Error(), path) {
t.Fatalf("expected an error naming %s, got %v", path, err)
}
}
func TestPackageName_NoManifestEntry(t *testing.T) {
path := filepath.Join(t.TempDir(), "app.apk")
file, err := os.Create(path)
if err != nil {
t.Fatalf("create apk: %v", err)
}
archive := zip.NewWriter(file)
if _, err := archive.Create("classes.dex"); err != nil {
t.Fatalf("write entry: %v", err)
}
if err := archive.Close(); err != nil {
t.Fatalf("close archive: %v", err)
}
if err := file.Close(); err != nil {
t.Fatalf("close apk: %v", err)
}
_, err = PackageName(path)
if err == nil || !strings.Contains(err.Error(), "AndroidManifest.xml") {
t.Fatalf("expected a missing-manifest error, got %v", err)
}
}
func TestPackageName_TruncatedManifest(t *testing.T) {
manifest, err := manifestFromAPK(folioAPK)
if err != nil {
t.Fatalf("read fixture manifest: %v", err)
}
for _, keep := range []int{4, 40, 400} {
if _, err := PackageName(apkWithManifest(t, manifest[:keep])); err == nil {
t.Fatalf("expected an error from a manifest cut to %d bytes", keep)
}
}
}
func apkWithManifest(t *testing.T, manifest []byte) string {
t.Helper()
path := filepath.Join(t.TempDir(), "app.apk")
file, err := os.Create(path)
if err != nil {
t.Fatalf("create apk: %v", err)
}
archive := zip.NewWriter(file)
entry, err := archive.Create(manifestEntry)
if err != nil {
t.Fatalf("create manifest entry: %v", err)
}
if _, err := entry.Write(manifest); err != nil {
t.Fatalf("write manifest entry: %v", err)
}
if err := archive.Close(); err != nil {
t.Fatalf("close archive: %v", err)
}
if err := file.Close(); err != nil {
t.Fatalf("close apk: %v", err)
}
return path
}
// utf8PooledManifest encodes the smallest manifest that carries a package
// name, with the UTF-8 string pool that aapt2 does not emit and older builders
// do. Without it the UTF-8 decoding path is never exercised: the compiled
// manifests aapt2 writes all pool their strings as UTF-16.
func utf8PooledManifest(packageName string) []byte {
pool := []string{"manifest", "package", packageName}
var pooled bytes.Buffer
offsets := make([]uint32, len(pool))
for index, value := range pool {
offsets[index] = uint32(pooled.Len())
pooled.WriteByte(byte(len(value)))
pooled.WriteByte(byte(len(value)))
pooled.WriteString(value)
pooled.WriteByte(0)
}
stringsStart := uint32(28 + 4*len(pool))
var poolChunk bytes.Buffer
write16(&poolChunk, chunkStringPool)
write16(&poolChunk, 28)
write32(&poolChunk, stringsStart+uint32(pooled.Len()))
write32(&poolChunk, uint32(len(pool)))
write32(&poolChunk, 0)
write32(&poolChunk, stringPoolUTF8)
write32(&poolChunk, stringsStart)
write32(&poolChunk, 0)
for _, offset := range offsets {
write32(&poolChunk, offset)
}
poolChunk.Write(pooled.Bytes())
var element bytes.Buffer
write16(&element, chunkStartElement)
write16(&element, 16)
write32(&element, 36+20)
write32(&element, 1) // line number
write32(&element, 0xFFFFFFFF) // comment
write32(&element, 0xFFFFFFFF) // namespace
write32(&element, 0) // name: "manifest"
write16(&element, 20) // attributes start, past this header
write16(&element, 20) // attribute stride
write16(&element, 1) // attribute count
write16(&element, 0) // id index
write16(&element, 0) // class index
write16(&element, 0) // style index
write32(&element, 0xFFFFFFFF) // attribute namespace
write32(&element, 1) // attribute name: "package"
write32(&element, 2) // attribute raw value: the package name
write16(&element, 8) // typed value size
write16(&element, 3<<8) // res0, and TYPE_STRING
write32(&element, 2)
var manifest bytes.Buffer
write16(&manifest, 3)
write16(&manifest, 8)
write32(&manifest, uint32(8+poolChunk.Len()+element.Len()))
manifest.Write(poolChunk.Bytes())
manifest.Write(element.Bytes())
return manifest.Bytes()
}
func write16(buffer *bytes.Buffer, value uint16) {
_ = binary.Write(buffer, binary.LittleEndian, value)
}
func write32(buffer *bytes.Buffer, value uint32) {
_ = binary.Write(buffer, binary.LittleEndian, value)
}