|
| 1 | +package autounattend_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/lima-vm/lima/v2/pkg/windows/autounattend" |
| 8 | +) |
| 9 | + |
| 10 | +func baseConfig() autounattend.Config { |
| 11 | + return autounattend.Config{ |
| 12 | + Hostname: "lima-win", |
| 13 | + Username: "limauser", |
| 14 | + Password: "T3stP@ssw0rd!", |
| 15 | + SSHPublicKeys: []string{"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA user@host"}, |
| 16 | + WindowsEditionIndex: 1, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func TestGenerate_ContainsRequiredPasses(t *testing.T) { |
| 21 | + xml, err := autounattend.Generate(baseConfig()) |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + for _, pass := range []string{"windowsPE", "specialize", "oobeSystem"} { |
| 26 | + if !strings.Contains(string(xml), pass) { |
| 27 | + t.Errorf("missing pass: %s", pass) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestGenerate_ContainsVirtIODriverPaths(t *testing.T) { |
| 33 | + xml, err := autounattend.Generate(baseConfig()) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + for _, driver := range []string{"viostor", "NetKVM", "viofs"} { |
| 38 | + if !strings.Contains(string(xml), driver) { |
| 39 | + t.Errorf("missing VirtIO driver path for: %s", driver) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestGenerate_SSHKeyPresent(t *testing.T) { |
| 45 | + xml, err := autounattend.Generate(baseConfig()) |
| 46 | + if err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + if !strings.Contains(string(xml), "ssh-ed25519") { |
| 50 | + t.Error("SSH public key missing from output") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestGenerate_PasswordNeverPlaintext(t *testing.T) { |
| 55 | + cfg := baseConfig() |
| 56 | + xml, err := autounattend.Generate(cfg) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + if strings.Contains(string(xml), cfg.Password) { |
| 61 | + t.Error("SECURITY: plaintext password present in generated XML") |
| 62 | + } |
| 63 | + if !strings.Contains(string(xml), "<PlainText>false</PlainText>") { |
| 64 | + t.Error("PlainText flag not set to false") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestGenerate_ValidationErrors(t *testing.T) { |
| 69 | + cases := []struct { |
| 70 | + name string |
| 71 | + mutate func(*autounattend.Config) |
| 72 | + }{ |
| 73 | + {"empty hostname", func(c *autounattend.Config) { c.Hostname = "" }}, |
| 74 | + {"hostname too long", func(c *autounattend.Config) { c.Hostname = "this-is-way-too-long" }}, |
| 75 | + {"empty password", func(c *autounattend.Config) { c.Password = "" }}, |
| 76 | + {"no ssh keys", func(c *autounattend.Config) { c.SSHPublicKeys = nil }}, |
| 77 | + {"bad edition index", func(c *autounattend.Config) { c.WindowsEditionIndex = -1 }}, |
| 78 | + } |
| 79 | + for _, tc := range cases { |
| 80 | + t.Run(tc.name, func(t *testing.T) { |
| 81 | + cfg := baseConfig() |
| 82 | + tc.mutate(&cfg) |
| 83 | + if _, err := autounattend.Generate(cfg); err == nil { |
| 84 | + t.Errorf("expected error for %q, got nil", tc.name) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestSanitiseHostname(t *testing.T) { |
| 91 | + cases := []struct{ in, want string }{ |
| 92 | + {"my-vm", "my-vm"}, |
| 93 | + {"my_special_vm!", "my-special-vm"}, |
| 94 | + {"this-is-way-too-long-for-netbios", "this-is-way-too"}, |
| 95 | + {"---", "lima-windows"}, |
| 96 | + {"", "lima-windows"}, |
| 97 | + } |
| 98 | + for _, tc := range cases { |
| 99 | + got := autounattend.SanitiseHostname(tc.in) |
| 100 | + if got != tc.want { |
| 101 | + t.Errorf("SanitiseHostname(%q) = %q, want %q", tc.in, got, tc.want) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments