| 1 | package maclogin |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestLockedConsoleBoundToOneUserAndSession(t *testing.T) { |
| 11 | const session = "607bceb6-c53f-4253-bad4-9c22b4ae7325" |
| 12 | const row = "<dict><key>kCGSSessionOnConsoleKey</key><true/><key>kCGSessionLoginDoneKey</key><true/><key>CGSSessionScreenIsLocked</key><true/><key>kCGSSessionUserIDKey</key><integer>502</integer><key>kCGSSessionUserNameKey</key><string>trial</string><key>CGSSessionUniqueSessionUUID</key><string>" + session + "</string></dict>" |
| 13 | const other = "<dict><key>kCGSSessionOnConsoleKey</key><false/></dict>" |
| 14 | makeProbe := func(header, rows string) string { return header + "\n<plist><array>" + rows + "</array></plist>" } |
| 15 | valid := makeProbe("502 trial 502", row) |
| 16 | for _, tc := range []struct{ label, input, want string }{ |
| 17 | {"system login", "system-login", "system-login"}, |
| 18 | {"locked", valid, "locked-" + session}, |
| 19 | {"logged-out second user", makeProbe("502 trial 502", other+row), "locked-" + session}, |
| 20 | {"unlocked", strings.Replace(valid, "ScreenIsLocked</key><true/>", "ScreenIsLocked</key><false/>", 1), ""}, |
| 21 | {"missing lock value", strings.Replace(valid, "<key>CGSSessionScreenIsLocked</key><true/>", "", 1), ""}, |
| 22 | {"wrong lock type", strings.Replace(valid, "ScreenIsLocked</key><true/>", "ScreenIsLocked</key><string>true</string>", 1), ""}, |
| 23 | {"different console", makeProbe("503 other 502", row), ""}, |
| 24 | {"different profile UID", makeProbe("503 trial 502", row), ""}, |
| 25 | {"different registry UID", strings.Replace(valid, "<integer>502", "<integer>503", 1), ""}, |
| 26 | {"different registry user", strings.Replace(valid, "<string>trial", "<string>other", 1), ""}, |
| 27 | {"not logged in", strings.Replace(valid, "LoginDoneKey</key><true/>", "LoginDoneKey</key><false/>", 1), ""}, |
| 28 | {"ambiguous", makeProbe("502 trial 502", row+row), ""}, |
| 29 | {"missing session identity", strings.Replace(valid, session, "", 1), ""}, |
| 30 | {"missing active flag", strings.Replace(valid, "<key>kCGSSessionOnConsoleKey</key><true/>", "", 1), ""}, |
| 31 | {"bad plist", valid + "<", ""}, |
| 32 | {"no session", makeProbe("502 trial 502", other), ""}, |
| 33 | } { |
| 34 | t.Run(tc.label, func(t *testing.T) { |
| 35 | if got := permittedLoginScreen([]byte(tc.input), "trial"); got != tc.want { |
| 36 | t.Fatalf("allowed screen %q; want %q", got, tc.want) |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestOffscreenViewMatchesDirectoryAliases(t *testing.T) { |
| 43 | instance := t.TempDir() |
| 44 | alias := filepath.Join(t.TempDir(), "lima-mac") |
| 45 | if err := os.Symlink(instance, alias); err != nil { |
| 46 | t.Skipf("symlink unavailable: %v", err) |
| 47 | } |
| 48 | if !sameGuestDirectory(alias, instance) || sameGuestDirectory(t.TempDir(), instance) || sameGuestDirectory(filepath.Join(alias, "absent"), filepath.Join(instance, "absent")) { |
| 49 | t.Fatal("offscreen view must be bound to an existing instance, including its socket path alias") |
| 50 | } |
| 51 | } |