| 1 | |
| 2 | |
| 3 | package vm |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/Code-Hex/vz/v3" |
| 15 | "github.com/lima-vm/lima/v2/pkg/fileutils" |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limayaml" |
| 18 | "github.com/opencontainers/go-digest" |
| 19 | ) |
| 20 | |
| 21 | func TestMacOSInstallSourceAndMounts(t *testing.T) { |
| 22 | l := &Lima{} |
| 23 | dir := t.TempDir() |
| 24 | t.Setenv("LIMA_HOME", filepath.Join(dir, "instances")) |
| 25 | ipsw := filepath.Join(dir, "bad.ipsw") |
| 26 | if err := os.WriteFile(ipsw, []byte("not a restore image"), 0o600); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | shares := []Mount{{Location: dir}} |
| 30 | for _, tc := range []struct { |
| 31 | file string |
| 32 | s Settings |
| 33 | want string |
| 34 | }{ |
| 35 | {ipsw, Settings{Mounts: &shares}, "shared host directories"}, |
| 36 | {"https://localhost/mac.ipsw", Settings{}, "absolute path"}, |
| 37 | {ipsw, Settings{}, "Apple Virtualization"}, |
| 38 | } { |
| 39 | if err := l.CreateMacOSFromIPSW(t.Context(), "mac", tc.file, tc.s, io.Discard); err == nil || !strings.Contains(err.Error(), tc.want) { |
| 40 | t.Fatalf("%q: expected %q, got %v", tc.file, tc.want, err) |
| 41 | } |
| 42 | } |
| 43 | t.Setenv("LIMA_HOME", dir) |
| 44 | if err := l.CreateMacOSFromIPSW(t.Context(), "mac", ipsw, Settings{}, io.Discard); err == nil || !strings.Contains(err.Error(), "outside") { |
| 45 | t.Fatalf("selected image in instance storage: %v", err) |
| 46 | } |
| 47 | if _, err := os.Stat(filepath.Join(dir, "mac")); !errors.Is(err, os.ErrNotExist) { |
| 48 | t.Fatalf("unexpected instance after preflight: %v", err) |
| 49 | } |
| 50 | if err := applyMacOSRequirements(&vz.MacOSRestoreImage{}, &Settings{}); err == nil { |
| 51 | t.Fatal("accepted an unsupported native restore image") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestMacOSHostSourceCompatibility(t *testing.T) { |
| 56 | path := os.Getenv("CAGE_TEST_MACOS_SOURCE") |
| 57 | if path == "" { |
| 58 | t.Skip("set CAGE_TEST_MACOS_SOURCE to verified local restore media") |
| 59 | } |
| 60 | image, err := vz.LoadMacOSRestoreImageFromPath(path) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | settings := Settings{DiskGiB: 64} |
| 65 | if err := applyMacOSRequirements(image, &settings); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | t.Logf("Apple accepted the restore image, minimum CPUs=%d memoryGiB=%d", settings.CPUs, settings.MemoryGiB) |
| 69 | } |
| 70 | |
| 71 | func TestMacOSInstallTemplateIsolation(t *testing.T) { |
| 72 | expectedDigest := "sha256:" + strings.Repeat("a", 64) |
| 73 | body := fmt.Sprintf(macOSInstallTemplate, `"/example.ipsw"`, expectedDigest) |
| 74 | cfg, err := limayaml.Load(t.Context(), []byte(body), "macos.yaml") |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if err := limayaml.Validate(cfg, false); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | if len(cfg.Images) != 1 || cfg.Images[0].Digest.String() != expectedDigest { |
| 82 | t.Fatal("selected restore image digest was lost") |
| 83 | } |
| 84 | if cfg.User.Comment == nil || *cfg.User.Comment != "Cage Provisioning" || *cfg.User.Name != "cageprovision" || *cfg.User.Home != "/Users/cageprovision" { |
| 85 | t.Fatal("macOS template must not inherit the host account's full name") |
| 86 | } |
| 87 | var options limatype.DarwinOpts |
| 88 | if err := limayaml.Convert(cfg.OsOpts[limatype.DARWIN], &options, "osOpts.Darwin"); err != nil || options.SuppressFirstLoginSetup == nil || !*options.SuppressFirstLoginSetup { |
| 89 | t.Fatal("new guest did not prepare first-login settings", err) |
| 90 | } |
| 91 | if len(cfg.Mounts) != 0 || len(cfg.Networks) != 0 || *cfg.SSH.LoadDotSSHPubKeys || *cfg.SSH.ForwardAgent || *cfg.SSH.ForwardX11 { |
| 92 | t.Fatal("unexpected host sharing in macOS template") |
| 93 | } |
| 94 | if len(cfg.PortForwards) != 1 || !cfg.PortForwards[0].Ignore || cfg.PortForwards[0].GuestPortRange != [2]int{1, 65535} || cfg.PortForwards[0].Proto != "any" || *cfg.PortForwards[0].GuestIPMustBeZero { |
| 95 | t.Fatal("default guest port forwarding is not disabled") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestSelectedIPSWRejectsReplacementAtSamePath(t *testing.T) { |
| 100 | dir := t.TempDir() |
| 101 | path := filepath.Join(dir, "selected.ipsw") |
| 102 | if err := os.WriteFile(path, []byte("original"), 0o600); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | selected, err := os.Stat(path) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | pin, err := fileutils.RestoreImageDigest(t.Context(), path, selected) |
| 110 | if err != nil || pin != digest.FromString("original") { |
| 111 | t.Fatalf("unexpected media pin: %s, %v", pin, err) |
| 112 | } |
| 113 | replacement := filepath.Join(dir, "renamed") |
| 114 | if err := os.WriteFile(replacement, []byte("replaced"), 0o600); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | if err := os.Chtimes(replacement, selected.ModTime(), selected.ModTime()); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if err := os.Rename(replacement, path); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if _, err := fileutils.RestoreImageDigest(t.Context(), path, selected); err == nil || !strings.Contains(err.Error(), "changed") { |
| 124 | t.Fatalf("accepted another inode with the original size and mtime: %v", err) |
| 125 | } |
| 126 | file := limatype.File{Location: path, Arch: limatype.AARCH64, Digest: pin} |
| 127 | if _, err := fileutils.DownloadFile(t.Context(), filepath.Join(dir, "image"), file, true, "restore image", limatype.AARCH64, nil); err == nil || !strings.Contains(err.Error(), "expected digest") { |
| 128 | t.Fatalf("Lima accepted replaced installation media: %v", err) |
| 129 | } |
| 130 | } |