M3 networkmanager: the person's session

The watcher finds the person's graphical session from logind and the user manager's
environment, and acts on a session's start once that environment names a display; the
user manager sends no PropertiesChanged for Environment, so it is read again at the
file events on the X server's socket folder and the session's runtime directory, for
at most 60 s.

Builds: design 08 sign-in-page §15, §16, §17
This commit is contained in:
jochen
2026-11-24 21:18:36 +01:00
parent dc59860541
commit fee3f8ab8b
5 changed files with 584 additions and 16 deletions
@@ -0,0 +1,124 @@
package main
import (
"strings"
"testing"
"time"
)
// The person's session as logind says it: x11, local, active, of the watcher's account.
func personsSession() loginSession {
return loginSession{ID: "2", Path: "/org/freedesktop/login1/session/_32", UID: 1000, Type: "x11", State: "active",
RuntimePath: "/run/user/1000"}
}
// aSessionWatcher is a subscribed watcher whose stood-in user manager names a display once named is set; the test acts
// as its loop, calling what a file event and the bound's end call.
func aSessionWatcher(t *testing.T, sys *fakeSystem, named *bool) *Watcher {
t.Helper()
w := aWatcher(t, sys)
files, bound := make(chan struct{}, 1), make(chan time.Time, 1)
w.FileEvents = func(dirs []string, stop <-chan struct{}) <-chan struct{} {
if strings.Join(dirs, " ") != "/tmp/.X11-unix /run/user/1000" {
t.Errorf("the session's start is waited for on %v", dirs)
}
return files
}
w.After = func(d time.Duration) <-chan time.Time {
if d != 60*time.Second {
t.Errorf("the session's start is waited for %s", d)
}
return bound
}
w.Environment = func(bus string) ([]string, error) {
if bus != "unix:path=/run/user/1000/bus" {
t.Errorf("the environment is read over %s", bus)
}
if *named {
return []string{"HOME=/home/person", "DISPLAY=:1", "XAUTHORITY=/home/person/.Xauthority"}, nil
}
return []string{"HOME=/home/person"}, nil
}
w.subscribe()
return w
}
// A session that starts while the rating is PORTAL opens the page once, when the user manager's environment names its
// display; one that starts while it is not opens nothing; one whose display is not named within the bound is not acted
// on, and says so (design 08 sign-in-page §15, §16, §17, §25).
func TestASessionStartingWhilePortalOpensThePageOnce(t *testing.T) {
for _, c := range []struct {
name string
rating Rating
named bool
opens int
did string
}{
{"PORTAL", guest(ratingPortal), true, 1, "opened the sign-in page"},
{"FULL", guest(ratingFull), true, 0, "nothing: the rating is FULL"},
{"no display named", guest(ratingPortal), false, 0, "not acted on: no display named within 1m0s"},
} {
t.Run(c.name, func(t *testing.T) {
lines := journalLines(t)
sys := &fakeSystem{}
sys.set(guest(ratingFull))
named := false
w := aSessionWatcher(t, sys, &named)
desk := w.Desk.(*fakeDesk)
sys.sessions = []loginSession{personsSession(), {ID: "9", Path: "/org/freedesktop/login1/session/_39", UID: 1001,
Type: "x11", State: "active", RuntimePath: "/run/user/1001"}}
w.heardSignal(signal{Session: "/org/freedesktop/login1/session/_39"})
if w.starting != nil {
t.Fatal("another account's session is waited for")
}
sys.set(c.rating)
w.heardSignal(signal{Session: personsSession().Path})
w.sessionFiles()
if len(desk.opened()) != 0 || w.starting == nil {
t.Fatal("acted on a session whose display is not named yet")
}
named = c.named
w.sessionFiles()
if w.starting != nil {
w.sessionBound()
}
if got := desk.opened(); len(got) != c.opens || (c.opens == 1 && got[0] != "2 http://check.example.net/") {
t.Fatalf("opened %q", got)
}
if w.state.LastSession == nil || w.state.LastSession.Did != c.did {
t.Fatalf("the session's start is said as %+v", w.state.LastSession)
}
if c.named == false && !strings.Contains(strings.Join(lines(), "\n"), "start was not acted on") {
t.Fatal("a start not acted on was not written to the journal")
}
})
}
}
// With no graphical session found the watcher opens nothing and still keeps the events: a remote session, one in no
// graphical type and one whose environment names no display are no session (§15, §16, §25).
func TestNoSessionOpensNothingAndStillKeeps(t *testing.T) {
journalLines(t)
sys := &fakeSystem{}
named := false
w := aSessionWatcher(t, sys, &named)
remote, tty := personsSession(), personsSession()
remote.Remote, tty.Type = true, "tty"
sys.sessions = []loginSession{remote, tty, personsSession()}
w.heard(guest(ratingFull), false)
w.heard(guest(ratingPortal), false)
w.present(nil, "guest-example", "http://check.example.net/")
if got := w.Desk.(*fakeDesk).opened(); len(got) != 0 {
t.Fatalf("opened %q with no session", got)
}
if kept := keptSaid(t, w.Folder); len(kept) == 0 || kept[len(kept)-1] != "sign-in-asked guest-example wlan0" {
t.Fatalf("kept %q", kept)
}
named = true
if s, err := w.findSession(); err != nil || s == nil || s.ID != "2" || strings.Join(s.Display, " ") != "DISPLAY=:1 XAUTHORITY=/home/person/.Xauthority" {
t.Fatalf("found %+v %v", s, err)
}
if got := displayIn([]string{"WAYLAND_DISPLAY=wayland-0", "DISPLAY=:0"}, "wayland"); strings.Join(got, " ") != "WAYLAND_DISPLAY=wayland-0" {
t.Fatalf("a wayland session's display is %v", got)
}
}