| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "github.com/sylvinus/agent-vm/internal/paths" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestWindowsGuestPathsMatchVMSharePaths(t *testing.T) { |
| 10 | previous := paths.Windows |
| 11 | paths.Windows = func() bool { return true } |
| 12 | t.Cleanup(func() { paths.Windows = previous }) |
| 13 | for _, path := range []string{`C:\Users\甲 用户\project`, "C:/Users/甲 用户/project"} { |
| 14 | if got := guestPath(path); got != "/c/Users/甲 用户/project" { |
| 15 | t.Fatalf("guestPath(%q) = %q", path, got) |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestBoundaryProbeUsesGuestFilePaths(t *testing.T) { |
| 21 | previous := paths.Windows |
| 22 | t.Cleanup(func() { paths.Windows = previous }) |
| 23 | for _, tc := range []struct { |
| 24 | name string |
| 25 | windows bool |
| 26 | private, shared, wantPrivate, wantShared string |
| 27 | }{ |
| 28 | {"Windows backslashes and another drive", true, `C:\Users\甲 用户\Cage\private-probe`, `D:\项目 工作区\shared-probe`, "/c/Users/甲 用户/Cage/private-probe", "/d/项目 工作区/shared-probe"}, |
| 29 | {"Windows forward slashes", true, "C:/Users/user/Cage/private-probe", "C:/Users/user/work/shared-probe", "/c/Users/user/Cage/private-probe", "/c/Users/user/work/shared-probe"}, |
| 30 | {"POSIX", false, "/Users/user/Cage/private-probe", "/Users/user/work/shared-probe", "/Users/user/Cage/private-probe", "/Users/user/work/shared-probe"}, |
| 31 | } { |
| 32 | t.Run(tc.name, func(t *testing.T) { |
| 33 | paths.Windows = func() bool { return tc.windows } |
| 34 | wire, err := json.Marshal(boundaryProbeInput(17777, "check-id", tc.private, tc.shared, "challenge")) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | var input struct { |
| 39 | Stage string `json:"stage"` |
| 40 | Port int `json:"control_port"` |
| 41 | Nonce string `json:"nonce"` |
| 42 | Private string `json:"private_file"` |
| 43 | Shared string `json:"shared_file"` |
| 44 | Challenge string `json:"challenge"` |
| 45 | } |
| 46 | if err := json.Unmarshal(wire, &input); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if input.Private != tc.wantPrivate || input.Shared != tc.wantShared { |
| 50 | t.Fatalf("Linux file probe received host paths: private=%q shared=%q", input.Private, input.Shared) |
| 51 | } |
| 52 | if input.Stage != "boundary" || input.Port != 17777 || input.Nonce != "check-id" || input.Challenge != "challenge" { |
| 53 | t.Fatalf("path conversion changed the probe identity: %+v", input) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |