| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 14 | ) |
| 15 | |
| 16 | func TestMacOSClipboardUsesBoundGUIAgent(t *testing.T) { |
| 17 | s := testServer(t) |
| 18 | s.macosGuest = &guestos.MacOSProfile{Instance: "mac", User: "guestdev"} |
| 19 | if err := os.MkdirAll(s.instanceDir(), 0700); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | if err := os.WriteFile(filepath.Join(s.instanceDir(), "ssh.config"), []byte("Host lima-mac\n Port 54432\n User root\n"), 0600); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | selected := []string{"/tmp/中文 ' $(touch unexpected).txt"} |
| 26 | for _, operation := range []string{"stream", "import", "export", "request"} { |
| 27 | cmd, source, err := s.clipboardCommand(context.Background(), operation, clipboardFileSelection{Paths: selected, User: "guest"}) |
| 28 | if err != nil || cmd.Err != nil || source != "guest" { |
| 29 | t.Fatalf("%s transport: %v", operation, err) |
| 30 | } |
| 31 | command := cmd.Args[len(cmd.Args)-1] |
| 32 | if command != "exec '/Applications/Cage Guest Clipboard.app/Contents/MacOS/CageGuestClipboard' --client "+operation || |
| 33 | !slices.Contains(cmd.Args, "guestdev") || slices.Contains(cmd.Args, "root") || strings.Contains(command, selected[0]) { |
| 34 | t.Fatalf("%s escaped its fixed guest transport", operation) |
| 35 | } |
| 36 | if operation == "export" { |
| 37 | data, _ := io.ReadAll(cmd.Stdin) |
| 38 | var actual []string |
| 39 | if json.Unmarshal(data, &actual) != nil || !slices.Equal(actual, selected) { |
| 40 | t.Fatal("file selections did not travel as stdin JSON") |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | for _, item := range []struct{ operation, user string }{{"execute", "guest"}, {"export", "root"}, {"export", "cage-browser"}} { |
| 45 | if _, _, err := s.clipboardCommand(context.Background(), item.operation, clipboardFileSelection{User: item.user}); err == nil { |
| 46 | t.Fatalf("accepted %s for %s", item.operation, item.user) |
| 47 | } |
| 48 | } |
| 49 | } |