package main import ( "os" "path/filepath" "reflect" "strings" "testing" "git.novox.be/novox/mesh-sdk/go/catalogue" ) // screen-lock's shape (novox/hq ADR 0208, research 026/04): it claims node-lock-screen serving lock, // requires the X display on its own machine, installs the watcher and the distribution's locker, // keeps the colour build where it is found (the operator's look), declares xscreensaver absent, places its locker, and starts the watcher and // the timeouts once, from the session's start. The lock key is the window manager's. func TestItClaimsTheLockScreenSeatServingLockAndRequiresTheXDisplay(t *testing.T) { manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. m, err := catalogue.Decode(manifestJSON) if err != nil { t.Fatalf("module.json: %v", err) } if m.Module != "screen-lock" || m.DefinesSeats != nil { t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) } if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-lock-screen", Scope: "node", Serves: []string{"lock"}}}) { t.Fatalf("claims: %+v", m.Claims) } if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("requires: %v", m.Requires) } } func TestTheColourBuildIsKeptWithAFallbackAndXscreensaverGoes(t *testing.T) { manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. m, err := catalogue.Decode(manifestJSON) if err != nil { t.Fatalf("module.json: %v", err) } present, absent := packages(m) if !reflect.DeepEqual(present, []string{"xss-lock", "i3lock"}) || !reflect.DeepEqual(absent, []string{"xscreensaver"}) { t.Fatalf("packages: %v, absent %v", present, absent) } fromTemplate(t, m, "wrapper", "files/bin/screen-lock") wrapper := resource(t, m, "wrapper") if wrapper["mode"] != "0755" || wrapper["path"] != "${machine:account-home}/.local/bin/screen-lock" { t.Fatalf("the locker: %v", wrapper) } c := templateText(t, m, "wrapper") // The colour build's options only once it has said it is the colour build: the distribution's // i3lock refuses an option it does not know, and the screen would not lock at all. check := strings.Index(c, `i3lock --version 2>&1 | grep -qE '[0-9]\.c\.[0-9]'`) fallback := strings.Index(c, "\nelse\n") for _, colourOnly := range []string{"--ring-color", "--blur", "--clock", "--indicator", "--time-str"} { at := strings.Index(c, colourOnly) if at < 0 || check < 0 || at < check || at > fallback { t.Errorf("%s is passed outside the colour build's branch", colourOnly) } } if !strings.Contains(c, "XSS_SLEEP_LOCK_FD}<&-") { t.Error("the locker must not inherit the sleep lock") } } func TestTheSessionStartSetsTheTimeoutsAndKeepsOneWatcherForTheSession(t *testing.T) { manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. m, err := catalogue.Decode(manifestJSON) if err != nil { t.Fatalf("module.json: %v", err) } if len(shell(t, m)) != 1 || shell(t, m)[0].For != "xinitrc" || shell(t, m)[0].Slot != "normal" { t.Fatalf("one xinitrc contribution in normal: %+v", shell(t, m)) } code := shell(t, m)[0].Code if strings.Count(code, "xss-lock --") != 1 || !strings.Contains(code, "--transfer-sleep-lock") || !strings.Contains(code, "while kill -0 $$") || !strings.HasSuffix(strings.TrimSpace(code), "&") { t.Fatalf("the watcher: %q", code) } if strings.Contains(code, "xscreensaver") || strings.Contains(code, "my-i3lock") { t.Fatalf("names what it replaced: %q", code) } for _, r := range m.Resources { if r["type"] == "service" || r["type"] == "process" { t.Fatalf("xss-lock needs the login session and is never a unit: %v", r) } } } func TestTheLockKeyIsTheWindowManagersNotThisModules(t *testing.T) { manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. m, err := catalogue.Decode(manifestJSON) if err != nil { t.Fatalf("module.json: %v", err) } // The i3 module binds $mod+Delete to logind's lock, which names no locker; a binding here as well // would be i3's duplicate. for _, r := range m.Resources { if p, _ := r["path"].(string); strings.Contains(p, "i3/config.d") { t.Fatalf("a key binding: %v", r) } } } func TestTheToolsAgreeWithTheManifest(t *testing.T) { manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. m, err := catalogue.Decode(manifestJSON) if err != nil { t.Fatalf("module.json: %v", err) } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 ยง2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. m, err := catalogue.Decode(manifestJSON) if err != nil { t.Fatalf("module.json: %v", err) } everyFileIsATemplate(t, m) }