| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/ed25519" |
| 6 | "crypto/rand" |
| 7 | "github.com/google/uuid" |
| 8 | "golang.org/x/crypto/ssh" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func TestInstallationKeepsChoiceAndRefusesToClearVM(t *testing.T) { |
| 16 | p := t.TempDir() |
| 17 | first := Installation{Version: 1, System: MacOS, ID: uuid.New()} |
| 18 | if err := SaveInstallation(p, first); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | if err := SaveInstallation(p, first); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | other := first |
| 25 | other.ID = uuid.New() |
| 26 | if err := SaveInstallation(p, other); err == nil { |
| 27 | t.Fatal("overwrote an active installation") |
| 28 | } |
| 29 | if err := ClearUnstartedInstallation(p, other.ID); err == nil { |
| 30 | t.Fatal("stale reset changed selection") |
| 31 | } |
| 32 | disk := filepath.Join(p, "state", "lima", "mac") |
| 33 | if err := os.MkdirAll(disk, 0700); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if err := ClearUnstartedInstallation(p, first.ID); err == nil { |
| 37 | t.Fatal("forgot a partially installed VM") |
| 38 | } |
| 39 | got, err := LoadInstallation(p) |
| 40 | if err != nil || got == nil || *got != first { |
| 41 | t.Fatalf("changed original selection: %v %v", got, err) |
| 42 | } |
| 43 | if err := os.Remove(disk); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if err := ClearUnstartedInstallation(p, first.ID); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | got, err = LoadInstallation(p) |
| 50 | if err != nil || got != nil { |
| 51 | t.Fatal("failed to clear media-only choice") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestWindowsInstallRequiresMediaDigestAndEditorAgreement(t *testing.T) { |
| 56 | i := Installation{Version: 1, System: Windows11, ID: uuid.New(), Media: filepath.Join(t.TempDir(), "system.iso"), SHA256: strings.Repeat("a", 64)} |
| 57 | if err := i.Validate(); err == nil { |
| 58 | t.Fatal("accepted without the editor agreement") |
| 59 | } |
| 60 | i.EditorTerms = true |
| 61 | if err := i.Validate(); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | i.SHA256 = "bad" |
| 65 | if err := i.Validate(); err == nil { |
| 66 | t.Fatal("accepted an incomplete ISO digest") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestMacSerialIdentityIsUnambiguous(t *testing.T) { |
| 71 | newKey := func() []byte { |
| 72 | pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | key, err := ssh.NewPublicKey(pub) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | return ssh.MarshalAuthorizedKey(key) |
| 81 | } |
| 82 | key := newKey() |
| 83 | prefix := "CAGE-GUEST-HOST-KEY " |
| 84 | line := prefix + strings.TrimSpace(string(key)) + " guest\r\n" |
| 85 | got, err := parseInstallHostKey(strings.NewReader("startup\n" + line + line)) |
| 86 | if err != nil || !bytes.Equal(key, got) { |
| 87 | t.Fatalf("valid serial identity: %v", err) |
| 88 | } |
| 89 | for _, body := range []string{"", "other " + line, line + prefix + "bad", line + prefix + string(newKey())} { |
| 90 | if _, err := parseInstallHostKey(strings.NewReader(body)); err == nil { |
| 91 | t.Fatal("accepted missing or inconsistent identity") |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestInitialMacPasswordCanBeEnteredAtTheLoginWindow(t *testing.T) { |
| 97 | for _, value := range []string{"Mix @#?9", strings.Repeat("!", 256)} { |
| 98 | if !ValidMacOSInstallPassword(value) { |
| 99 | t.Fatal("rejected an enterable password") |
| 100 | } |
| 101 | } |
| 102 | for _, value := range []string{"short", "中文输入示例密码", "abcde\tfgh", strings.Repeat("a", 257)} { |
| 103 | if ValidMacOSInstallPassword(value) { |
| 104 | t.Fatal("accepted an unsupported password") |
| 105 | } |
| 106 | } |
| 107 | } |