package main import ( "bytes" "encoding/json" "net/http/httptest" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" ) func TestServerADBTargetsStoredArtifactAndAuthorizedDevice(t *testing.T) { root := t.TempDir() helper := filepath.Join(root, "adb-helper.go") exe := filepath.Join(root, "adb-helper") if runtime.GOOS == "windows" { exe += ".exe" } writeFixture(t, helper, `package main import("os";"fmt";"encoding/json") func main(){ f,_:=os.OpenFile(os.Getenv("ADB_TEST_LOG"),os.O_CREATE|os.O_APPEND|os.O_WRONLY,0600);json.NewEncoder(f).Encode(os.Args[1:]);f.Close();if os.Args[1]=="devices"{fmt.Println("List of devices attached\nremote-123 device model:Remote_Tablet\nlocked unauthorized")}else{fmt.Println("Success")} } `) cmd := exec.Command("go", "build", "-o", exe, helper) if out, e := cmd.CombinedOutput(); e != nil { t.Fatalf("helper: %v %s", e, out) } log := filepath.Join(root, "calls.jsonl") t.Setenv("ADB_TEST_LOG", log) cfg := filepath.Join(root, "env.json") data, _ := json.Marshal(BuildEnvironment{Adb: exe}) writeFixture(t, cfg, string(data)) bid := strings.Repeat("a", 32) content := []byte("apk-fixture") a := Artifact{Name: "0-app.apk", Size: int64(len(content)), Hash: digest(content)} s := &Server{root: root, token: "secret", environmentFile: cfg, builds: map[string]*Build{bid: {ID: bid, Status: "success", Artifacts: []Artifact{a}}}} apk := filepath.Join(root, "artifacts", bid, a.Name) writeFixture(t, apk, string(content)) call := func(route, method, token string, body any) *httptest.ResponseRecorder { b, _ := json.Marshal(body) req := httptest.NewRequest(method, "/api/v1/adb/"+route, bytes.NewReader(b)) req.Header.Set("Authorization", "Bearer "+token) w := httptest.NewRecorder() s.ServeHTTP(w, req) return w } if w := call("devices", "GET", "wrong", nil); w.Code != 401 { t.Fatal(w.Code) } if w := call("devices", "GET", "secret", nil); w.Code != 200 || !strings.Contains(w.Body.String(), "Remote Tablet") { t.Fatal(w.Code, w.Body.String()) } request := map[string]string{"buildId": bid, "artifact": a.Name, "serial": "remote-123"} if w := call("install", "POST", "secret", request); w.Code != 200 { t.Fatal(w.Code, w.Body.String()) } raw, _ := os.ReadFile(log) if !strings.Contains(string(raw), `["-s","remote-123","install","-r",`) { t.Fatal(string(raw)) } for _, serial := range []string{"locked", "missing", "-d"} { request["serial"] = serial if w := call("install", "POST", "secret", request); w.Code == 200 { t.Fatal("accepted unauthorized/missing serial", serial) } } request["serial"] = "remote-123" writeFixture(t, apk, "tampered") if w := call("install", "POST", "secret", request); w.Code != 409 { t.Fatal(w.Code, w.Body.String()) } request["artifact"] = "../foreign.apk" if w := call("install", "POST", "secret", request); w.Code != 400 { t.Fatal(w.Code) } raw, _ = os.ReadFile(log) if strings.Count(string(raw), `"install"`) != 1 { t.Fatal("unexpected additional install", string(raw)) } s.builds[bid].Status = "failed" request["artifact"] = a.Name if w := call("install", "POST", "secret", request); w.Code != 400 { t.Fatal(w.Code) } }