| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func nativeManifestKit(t *testing.T, system string) string { |
| 12 | t.Helper() |
| 13 | kit := t.TempDir() |
| 14 | names := []string{"payload/Cage.GuestClipboard.exe", "payload/cage-guest-files.exe", "payload/cage-guest-boot.exe", "payload/Cage.HyperVManager.exe", "payload/windows-browser.mjs", "payload/cli-dependencies.json"} |
| 15 | if system == MacOS { |
| 16 | names = []string{"Cage Guest Window.app/Contents/MacOS/CageGuestWindow", "Cage Guest Clipboard.app/Contents/MacOS/CageGuestClipboard", "project/cage-sshfs", "project/cage-macos-terminal", "native-browser/macos-native-browser.mjs", "native-browser/macos-browser-service.mjs", "register-macos-browser.sh", "macos-cli.py", "cli_channel.py", "claude-account.py"} |
| 17 | } |
| 18 | for _, name := range names { |
| 19 | filename := filepath.Join(kit, filepath.FromSlash(name)) |
| 20 | if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | data := []byte("#!fixture\n{}") |
| 24 | if name == "payload/cli-dependencies.json" { |
| 25 | data = []byte(`{"claude":{"version":"2.1.311"},"python":{"version":"3.13.18"},"git":{"version":"2.56.0"}}`) |
| 26 | } |
| 27 | if err := os.WriteFile(filename, data, 0600); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | return kit |
| 32 | } |
| 33 | |
| 34 | func TestNativeManifestTracksInstalledComponents(t *testing.T) { |
| 35 | for _, system := range []string{MacOS, Windows11} { |
| 36 | t.Run(system, func(t *testing.T) { |
| 37 | kit := nativeManifestKit(t, system) |
| 38 | first, err := RequiredNativeComponents(kit, system) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | again, err := RequiredNativeComponents(kit, system) |
| 43 | if err != nil || first.Revision != again.Revision { |
| 44 | t.Fatalf("manifest is unstable: %v", err) |
| 45 | } |
| 46 | source := "payload/new-phone-browser.mjs" |
| 47 | if system == MacOS { |
| 48 | source = "native-browser/macos-phone-browser.mjs" |
| 49 | } |
| 50 | if err = os.WriteFile(filepath.Join(kit, filepath.FromSlash(source)), []byte("phone-browser"), 0600); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | next, err := RequiredNativeComponents(kit, system) |
| 54 | if err != nil || next.Revision == first.Revision { |
| 55 | t.Fatalf("new component was omitted: %v", err) |
| 56 | } |
| 57 | found, packet := false, false |
| 58 | for _, item := range next.Files { |
| 59 | if item.Source == source { |
| 60 | found = item.Target != "" && item.SHA256 == item.InstalledSHA256 |
| 61 | } |
| 62 | if item.Target == "network/Cage.GuestNetwork.exe" { |
| 63 | packet = item.Source == "payload/Cage.GuestClipboard.exe" |
| 64 | } |
| 65 | if item.Source == "claude-account.py" { |
| 66 | expected := sha256.Sum256([]byte("#!" + MacOSCLIRuntime + "/bin/python3 -I\n{}")) |
| 67 | if item.InstalledSHA256 != hex.EncodeToString(expected[:]) { |
| 68 | t.Fatal("installed Python entry point digest differs from installer") |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | if !found || (system == Windows11 && !packet) { |
| 73 | t.Fatal("manifest did not include installed guest capabilities") |
| 74 | } |
| 75 | if err = os.Symlink(source, filepath.Join(kit, "unexpected-link")); err != nil { |
| 76 | t.Skipf("symlinks unavailable: %v", err) |
| 77 | } |
| 78 | if _, err = RequiredNativeComponents(kit, system); err == nil { |
| 79 | t.Fatal("linked payload was accepted") |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestNativeManifestRejectsEscapedTargets(t *testing.T) { |
| 86 | manifest, err := RequiredNativeComponents(nativeManifestKit(t, MacOS), MacOS) |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | for _, target := range []string{"/usr/local/libexec/cage-cli/../../private", "/etc/passwd", "network/Cage.GuestNetwork.exe"} { |
| 91 | manifest.Files[0].Target = target |
| 92 | if manifest.Validate() == nil { |
| 93 | t.Fatalf("accepted target %q", target) |
| 94 | } |
| 95 | } |
| 96 | } |