| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/rand" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/google/uuid" |
| 11 | "golang.org/x/crypto/ssh" |
| 12 | ) |
| 13 | |
| 14 | func TestPreparedMacOSBindingRejectsChangedKeyAndDisk(t *testing.T) { |
| 15 | profile := t.TempDir() |
| 16 | dir := filepath.Join(profile, "state", "lima", "mac") |
| 17 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | identifier := filepath.Join(dir, "vz-identifier") |
| 21 | if err := os.WriteFile(identifier, []byte("one trial machine"), 0600); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | key := func() []byte { |
| 25 | pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | p, err := ssh.NewPublicKey(pub) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | return ssh.MarshalAuthorizedKey(p) |
| 34 | } |
| 35 | pub := key() |
| 36 | if err := BindMacOSProfile(profile, "dev", pub); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if err := BindMacOSProfile(profile, "dev", pub); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if err := BindMacOSProfile(profile, "dev", key()); err == nil { |
| 43 | t.Error("changed host key accepted") |
| 44 | } |
| 45 | if err := BindMacOSProfile(profile, "root", pub); err == nil { |
| 46 | t.Error("root login accepted") |
| 47 | } |
| 48 | if m, err := LoadMacOSProfile(profile); err != nil || m.User != "dev" { |
| 49 | t.Fatal(m, err) |
| 50 | } |
| 51 | if err := os.WriteFile(identifier, []byte("replaced guest"), 0600); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if _, err := LoadMacOSProfile(profile); err == nil { |
| 55 | t.Error("replacement machine accepted") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestMacFirstSetupCanOnlySealItsNewGuest(t *testing.T) { |
| 60 | profile := t.TempDir() |
| 61 | dir := filepath.Join(profile, "state", "lima", "mac") |
| 62 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if err := os.WriteFile(filepath.Join(dir, "vz-identifier"), []byte("new-mac"), 0600); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | key, err := ssh.NewPublicKey(pub) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | hostKey := ssh.MarshalAuthorizedKey(key) |
| 77 | id := uuid.New() |
| 78 | if err = BindNewMacOSProfile(profile, "cageuser", hostKey, id); err == nil { |
| 79 | t.Fatal("accepted missing install record") |
| 80 | } |
| 81 | if err = SaveInstallation(profile, Installation{Version: 1, System: MacOS, ID: id}); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if err = BindNewMacOSProfile(profile, "cageuser", hostKey, id); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | if err = FinishMacOSSetup(profile, uuid.NewString()); err == nil { |
| 88 | t.Fatal("sealed unrelated install") |
| 89 | } |
| 90 | bound, err := LoadMacOSProfile(profile) |
| 91 | if err != nil || bound.SetupID != id.String() { |
| 92 | t.Fatal("lost pending setup") |
| 93 | } |
| 94 | if err = FinishMacOSSetup(profile, id.String()); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | if err = BindNewMacOSProfile(profile, "cageuser", hostKey, id); err == nil { |
| 98 | t.Fatal("a retried installer reopened setup") |
| 99 | } |
| 100 | bound, err = LoadMacOSProfile(profile) |
| 101 | if err != nil || bound.SetupID != "" { |
| 102 | t.Fatal("setup still open") |
| 103 | } |
| 104 | } |