| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 11 | ) |
| 12 | |
| 13 | func TestMacOSPhoneRequiresCurrentRoomAndAccountUnlock(t *testing.T) { |
| 14 | s := testServer(t) |
| 15 | s.macosGuest = &guestos.MacOSProfile{Instance: "mac", User: "cage"} |
| 16 | workspace := t.TempDir() |
| 17 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": workspace}); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | room, cancel := context.WithCancel(context.Background()) |
| 21 | t.Cleanup(cancel) |
| 22 | s.phones.record = &phoneRecord{} |
| 23 | s.phones.roomCtx, s.phones.roomProof = room, "current-private-room" |
| 24 | path := "/api/mobile/cli/projects" |
| 25 | for _, proof := range []string{"", "expired-private-room"} { |
| 26 | if response := windowsPhoneRequest(t, s, http.MethodGet, path, "", proof); response.Code != http.StatusForbidden { |
| 27 | t.Fatalf("untrusted phone admitted: %d", response.Code) |
| 28 | } |
| 29 | } |
| 30 | response := windowsPhoneRequest(t, s, http.MethodGet, path, "", s.phones.roomProof) |
| 31 | if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"shared_sessions":true`) { |
| 32 | t.Fatalf("Mac phone could not read shared projects: %d %s", response.Code, response.Body.String()) |
| 33 | } |
| 34 | if response := windowsPhoneRequest(t, s, http.MethodGet, "/api/settings", "", s.phones.roomProof); response.Code != http.StatusForbidden { |
| 35 | t.Fatal("phone proof allowed desktop administration") |
| 36 | } |
| 37 | s.account.trustedSession = true |
| 38 | if response := windowsPhoneRequest(t, s, http.MethodGet, path, "", s.phones.roomProof); response.Code != http.StatusLocked { |
| 39 | t.Fatal("phone inherited a desktop-only account unlock") |
| 40 | } |
| 41 | s.account.trustedSession = false |
| 42 | cancel() |
| 43 | if response := windowsPhoneRequest(t, s, http.MethodGet, path, "", s.phones.roomProof); response.Code != http.StatusForbidden { |
| 44 | t.Fatal("revoked phone retained access") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestMacOSPhoneMediaUsesBoundGuestProject(t *testing.T) { |
| 49 | s := testServer(t) |
| 50 | s.macosGuest = &guestos.MacOSProfile{Instance: "mac", User: "cage"} |
| 51 | workspace := t.TempDir() |
| 52 | guest := s.cliWorkspace(workspace) |
| 53 | name, err := mediaFileName(guest, guest, guest+"/images/example.png") |
| 54 | if err != nil || name != filepath.Join("images", "example.png") { |
| 55 | t.Fatalf("guest media did not resolve within host project: %q %v", name, err) |
| 56 | } |
| 57 | if _, err := mediaFileName(guest, guest, guest+"/../outside.png"); err == nil { |
| 58 | t.Fatal("guest media escaped its bound project") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestMacOSPhoneBrowserPreservesPublicNetworkDenial(t *testing.T) { |
| 63 | s := &Server{macosGuest: &guestos.MacOSProfile{User: "cage"}} |
| 64 | s.phones.browser.nativeBrowser = fixtureWindowsBrowserClient(t) |
| 65 | _, status, err := s.phoneSurfaceGuest(context.Background(), phoneBrowser, "", "frame", phoneSurfaceInput{ID: "550e8400-e29b-41d4-a716-446655440000"}) |
| 66 | if err != nil || status != http.StatusForbidden { |
| 67 | t.Fatalf("native public-network denial changed: %d %v", status, err) |
| 68 | } |
| 69 | if !macOSWorkbenchRoute("/api/mobile/browser/550e8400-e29b-41d4-a716-446655440000/frame") { |
| 70 | t.Fatal("native phone browser route remains unavailable") |
| 71 | } |
| 72 | } |