| 1 | package maclogin |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func maintenanceFixture() maintenanceWindow { |
| 11 | return maintenanceWindow{Version: 1, Session: "607bceb6-c53f-4253-bad4-9c22b4ae7325", User: "trial", PID: 42, Window: 7, |
| 12 | Bundle: "com.apple.systempreferences", Rect: [4]float64{300, 150, 1200, 900}, Display: [4]float64{0, 0, 1920, 1200}} |
| 13 | } |
| 14 | |
| 15 | func TestMaintenanceWindowRequiresBoundIdentityAndVisibleRegion(t *testing.T) { |
| 16 | valid := maintenanceFixture() |
| 17 | if !valid.valid("trial") || valid.region() != [4]int{160, 80, 640, 480} { |
| 18 | t.Fatal("verified settings window was rejected") |
| 19 | } |
| 20 | for _, change := range []func(*maintenanceWindow){ |
| 21 | func(w *maintenanceWindow) { w.User = "another-user" }, |
| 22 | func(w *maintenanceWindow) { w.Version = 0 }, |
| 23 | func(w *maintenanceWindow) { w.PID = 0 }, |
| 24 | func(w *maintenanceWindow) { w.Window = 0 }, |
| 25 | func(w *maintenanceWindow) { w.Bundle = "com.apple.finder" }, |
| 26 | func(w *maintenanceWindow) { w.Bundle = "com.example.authorizationhost" }, |
| 27 | func(w *maintenanceWindow) { w.Session = "" }, |
| 28 | func(w *maintenanceWindow) { w.Error = "requires_gui_login" }, |
| 29 | func(w *maintenanceWindow) { w.Error = "maintenance_window_obscured" }, |
| 30 | func(w *maintenanceWindow) { w.Rect[0] = -1 }, |
| 31 | func(w *maintenanceWindow) { w.Rect[2] = 80_000 }, |
| 32 | func(w *maintenanceWindow) { w.Rect[3] = math.NaN() }, |
| 33 | func(w *maintenanceWindow) { w.Display[2] = math.Inf(1) }, |
| 34 | func(w *maintenanceWindow) { w.Display[3] = 1080 }, |
| 35 | } { |
| 36 | invalid := valid |
| 37 | change(&invalid) |
| 38 | if invalid.valid("trial") { |
| 39 | t.Fatalf("accepted unverified maintenance target: %+v", invalid) |
| 40 | } |
| 41 | } |
| 42 | if !valid.contains(160, 80) || !valid.contains(799, 559) || valid.contains(159, 80) || valid.contains(800, 559) || valid.contains(160, 560) { |
| 43 | t.Fatal("maintenance input escaped the shown crop") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestMaintenanceGeometryChangesWithWindowAndSession(t *testing.T) { |
| 48 | first := maintenanceFixture() |
| 49 | focused := first |
| 50 | focused.PasswordFocused = true |
| 51 | if focused.geometry() != first.geometry() { |
| 52 | t.Fatal("a click that focuses a field invalidated ordinary typing") |
| 53 | } |
| 54 | for _, change := range []func(*maintenanceWindow){ |
| 55 | func(w *maintenanceWindow) { w.Window++ }, |
| 56 | func(w *maintenanceWindow) { w.PID++ }, |
| 57 | func(w *maintenanceWindow) { w.Rect[0]++ }, |
| 58 | func(w *maintenanceWindow) { w.Session = "1e128373-fcb9-42fd-88b2-2a6ca58cf3a1" }, |
| 59 | } { |
| 60 | next := first |
| 61 | change(&next) |
| 62 | if next.geometry() == first.geometry() { |
| 63 | t.Fatal("stale window input retained its authorization") |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestMaintenancePasswordSelectionWorksOnceBeforeAccessibility(t *testing.T) { |
| 69 | now := time.Now() |
| 70 | window := maintenanceFixture() |
| 71 | target := passwordTarget{maintenanceWindow: window, SelectionRequired: true} |
| 72 | var selection passwordSelection |
| 73 | if _, allowed := selection.consumeMaintenance(window, target, "trial", now); allowed { |
| 74 | t.Fatal("maintenance password accepted without the user's selection") |
| 75 | } |
| 76 | selection.selectMaintenance(window, target, "trial", 200, 200, now) |
| 77 | chosen, allowed := selection.consumeMaintenance(window, target, "trial", now.Add(time.Second)) |
| 78 | if !allowed || !window.permitsPassword(target, chosen, "trial", now.Add(2*time.Second)) { |
| 79 | t.Fatal("AX-disabled maintenance could not use the selected Apple authorization panel") |
| 80 | } |
| 81 | if _, allowed := selection.consumeMaintenance(window, target, "trial", now.Add(2*time.Second)); allowed { |
| 82 | t.Fatal("the same manual password selection was consumed twice") |
| 83 | } |
| 84 | if window.permitsPassword(target, chosen, "trial", now.Add(31*time.Second)) { |
| 85 | t.Fatal("expired manual selection remained valid during password entry") |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestMaintenancePasswordRejectsChangedPanelAndSecureFocus(t *testing.T) { |
| 90 | now := time.Now() |
| 91 | window := maintenanceFixture() |
| 92 | target := passwordTarget{maintenanceWindow: window, SelectionRequired: true} |
| 93 | var selection passwordSelection |
| 94 | selection.selectMaintenance(window, target, "trial", 200, 200, now) |
| 95 | chosen, allowed := selection.consumeMaintenance(window, target, "trial", now) |
| 96 | if !allowed { |
| 97 | t.Fatal("could not establish the manual selection fixture") |
| 98 | } |
| 99 | for _, change := range []func(*passwordTarget){ |
| 100 | func(w *passwordTarget) { w.SelectionRequired = false }, |
| 101 | func(w *passwordTarget) { w.Window++ }, |
| 102 | func(w *passwordTarget) { w.PID++ }, |
| 103 | func(w *passwordTarget) { w.Rect[0]++ }, |
| 104 | func(w *passwordTarget) { w.Session = "1e128373-fcb9-42fd-88b2-2a6ca58cf3a1" }, |
| 105 | func(w *passwordTarget) { w.User = "another-user" }, |
| 106 | func(w *passwordTarget) { w.Bundle = "com.apple.finder" }, |
| 107 | func(w *passwordTarget) { w.Accessibility = true }, |
| 108 | } { |
| 109 | changed := target |
| 110 | change(&changed) |
| 111 | if window.permitsPassword(changed, chosen, "trial", now.Add(time.Second)) { |
| 112 | t.Fatal("a changed panel, session or known non-password focus accepted password input") |
| 113 | } |
| 114 | } |
| 115 | ordinary := target |
| 116 | ordinary.SelectionRequired = false |
| 117 | selection.selectMaintenance(window, ordinary, "trial", 200, 200, now) |
| 118 | if _, allowed := selection.consumeMaintenance(window, ordinary, "trial", now); allowed { |
| 119 | t.Fatal("an ordinary System Settings window accepted manual password selection") |
| 120 | } |
| 121 | selection.selectMaintenance(window, target, "trial", 159, 80, now) |
| 122 | if _, allowed := selection.consumeMaintenance(window, target, "trial", now); allowed { |
| 123 | t.Fatal("a click outside the published window accepted password entry") |
| 124 | } |
| 125 | target.Accessibility, target.PasswordFocused = true, true |
| 126 | if _, allowed := selection.consumeMaintenance(window, target, "trial", now); !allowed { |
| 127 | t.Fatal("verified secure focus required an unnecessary manual selection") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestMaintenanceProbeDistinguishesBlockedWindow(t *testing.T) { |
| 132 | w := maintenanceWindow{Error: "maintenance_window_obscured", BlockingPID: 42, BlockingWindow: 7, BlockingBundle: "com.apple.notificationcenterui"} |
| 133 | if message := w.probeMessage(); !strings.Contains(message, "通知中心 PID 42 窗口 7") { |
| 134 | t.Fatal(message) |
| 135 | } |
| 136 | w.BlockingBundle = "unexpected\nwindow title" |
| 137 | if strings.Contains(w.probeMessage(), "unexpected") { |
| 138 | t.Fatal("invalid bundle identifier reported") |
| 139 | } |
| 140 | w.Error = "maintenance_window_unavailable" |
| 141 | if strings.Contains(w.probeMessage(), "遮挡") { |
| 142 | t.Fatal("a missing window was attributed to an obstruction") |
| 143 | } |
| 144 | } |