At a join, at the watcher's start and at a session's start while PORTAL, once per ask, the watcher opens the check address with systemd-run --user and xdg-open in the person's session and then shows the notification its outcome calls for, critical when the open failed; it says when the network is open, and handles what it hears during an open after it. Builds: design 08 sign-in-page §15, §18, §19, §22
200 lines
7.7 KiB
Go
200 lines
7.7 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// aDeskWatcher is a watcher with the person's session found and the desktop stood in; it starts subscribed, the rating
|
|
// at its start being start.
|
|
func aDeskWatcher(t *testing.T, start Rating) (*Watcher, *fakeSystem, *fakeDesk) {
|
|
t.Helper()
|
|
sys := &fakeSystem{sessions: []loginSession{personsSession()}}
|
|
sys.set(start)
|
|
w := aWatcher(t, sys)
|
|
w.Environment = func(string) ([]string, error) { return []string{"DISPLAY=:1", "XAUTHORITY=/x"}, nil }
|
|
w.subscribe()
|
|
return w, sys, w.Desk.(*fakeDesk)
|
|
}
|
|
|
|
const openedNote = "This network asks you to sign in: guest-example: its sign-in page is open in browser.desktop."
|
|
|
|
// PORTAL at a join and PORTAL at the watcher's start each open the page and show one notification; a second PORTAL
|
|
// without a leave does nothing; PORTAL to FULL shows that the network is open, PORTAL to any other rating shows nothing
|
|
// (design 08 sign-in-page §15, §19, §25).
|
|
func TestTheRatingOpensThePageAndTellsThePersonOncePerAsk(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
start Rating
|
|
then []Rating
|
|
opens int
|
|
notes []string
|
|
journal string
|
|
}{
|
|
{"PORTAL at a join", guest(ratingFull), []Rating{guest(ratingPortal)}, 1, []string{openedNote},
|
|
"showed the notification"},
|
|
{"PORTAL at the start", guest(ratingPortal), nil, 1, []string{openedNote}, "the sign-in page http://check.example.net/ is opened in browser.desktop"},
|
|
{"a second PORTAL", guest(ratingPortal), []Rating{guest(ratingPortal), guest(ratingPortal)}, 1,
|
|
[]string{openedNote}, ""},
|
|
{"PORTAL to FULL", guest(ratingPortal), []Rating{guest(ratingFull)}, 1,
|
|
[]string{openedNote, "The network is open: guest-example: signed in."}, ""},
|
|
{"PORTAL to LIMITED", guest(ratingPortal), []Rating{guest(ratingLimited)}, 1, []string{openedNote}, ""},
|
|
{"PORTAL, left, PORTAL on another", guest(ratingPortal), []Rating{other(ratingPortal)}, 2,
|
|
[]string{openedNote, "This network asks you to sign in: home-example: its sign-in page is open in browser.desktop."}, ""},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
lines := journalLines(t)
|
|
w, sys, desk := aDeskWatcher(t, c.start)
|
|
for _, r := range c.then {
|
|
sys.set(r)
|
|
w.heardSignal(signal{Rating: true})
|
|
}
|
|
if got := desk.opened(); len(got) != c.opens {
|
|
t.Fatalf("opened %q", got)
|
|
}
|
|
if got := desk.shown(); strings.Join(got, "|") != strings.Join(c.notes, "|") {
|
|
t.Fatalf("showed %q, want %q", got, c.notes)
|
|
}
|
|
if !strings.Contains(strings.Join(lines(), "\n"), c.journal) {
|
|
t.Fatalf("the journal does not say %q: %q", c.journal, lines())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// An opener that fails gives the critical notification naming the reason and the address (§19, §25).
|
|
func TestAFailingOpenerGivesTheCriticalNotification(t *testing.T) {
|
|
journalLines(t)
|
|
sys := &fakeSystem{sessions: []loginSession{personsSession()}}
|
|
sys.set(guest(ratingFull))
|
|
w := aWatcher(t, sys)
|
|
w.Environment = func(string) ([]string, error) { return []string{"DISPLAY=:1"}, nil }
|
|
desk := w.Desk.(*fakeDesk)
|
|
desk.outcome = openOutcome{Address: "http://check.example.net/", Outcome: outcomeFailed, Reason: "xdg-open ended with status 4"}
|
|
w.subscribe()
|
|
sys.set(guest(ratingPortal))
|
|
w.heardSignal(signal{Rating: true})
|
|
want := "critical This network asks you to sign in: guest-example: the page did not open (xdg-open ended with status 4); " +
|
|
"open http://check.example.net/ in a browser."
|
|
if got := desk.shown(); len(got) != 1 || got[0] != want {
|
|
t.Fatalf("showed %q", got)
|
|
}
|
|
if w.state.LastOpen == nil || w.state.LastOpen.Outcome != outcomeFailed || w.state.LastNotification == nil {
|
|
t.Fatalf("the state says %+v", w.state)
|
|
}
|
|
}
|
|
|
|
// A rating change heard while the page opens is handled after the open, in the order heard (§18, §25).
|
|
func TestARatingChangeHeardDuringAnOpenIsHandledAfterIt(t *testing.T) {
|
|
journalLines(t)
|
|
sys := &fakeSystem{sessions: []loginSession{personsSession()}}
|
|
sys.set(guest(ratingFull))
|
|
w := aWatcher(t, sys)
|
|
w.Environment = func(string) ([]string, error) { return []string{"DISPLAY=:1"}, nil }
|
|
var beside []func() func()
|
|
w.Beside = func(work func() func()) { beside = append(beside, work) }
|
|
w.subscribe()
|
|
sys.set(guest(ratingPortal))
|
|
w.heardSignal(signal{Rating: true})
|
|
sys.set(guest(ratingLimited))
|
|
w.heardSignal(signal{Rating: true})
|
|
sys.set(guest(ratingFull))
|
|
w.heardSignal(signal{Rating: true})
|
|
if got := keptSaid(t, w.Folder); len(got) != 2 || len(beside) != 1 {
|
|
t.Fatalf("during the open: kept %q, %d run beside", got, len(beside))
|
|
}
|
|
beside[0]()()
|
|
got := keptSaid(t, w.Folder)
|
|
if len(got) != 3 || got[2] != "sign-in-ended no-longer-asked guest-example wlan0" {
|
|
t.Fatalf("after the open: kept %q", got)
|
|
}
|
|
if w.opening || len(w.later) != 0 {
|
|
t.Fatal("what was heard is still waiting")
|
|
}
|
|
}
|
|
|
|
// The opener in the person's session: xdg-mime asked for the scheme's program, then systemd-run --user with the address
|
|
// as one argument, both with the session's bus and runtime directory; its status, or its bound, decides (§18).
|
|
func TestTheOpenerRunsInThePersonsSession(t *testing.T) {
|
|
s := Session{ID: "2", Type: "x11", RuntimePath: "/run/user/1000"}
|
|
address := "http://check.example.net/a'b;c"
|
|
for _, c := range []struct {
|
|
name string
|
|
program string
|
|
ends error
|
|
running bool
|
|
want openOutcome
|
|
}{
|
|
{"opened", "browser.desktop\n", nil, false, openOutcome{Address: address, Program: "browser.desktop", Outcome: outcomeOpened}},
|
|
{"opening", "browser.desktop\n", nil, true, openOutcome{Address: address, Program: "browser.desktop", Outcome: outcomeOpening}},
|
|
{"failed", "browser.desktop\n", exitStatus(t, 3), false,
|
|
openOutcome{Address: address, Outcome: outcomeFailed, Reason: "xdg-open ended with status 3"}},
|
|
{"no program", "\n", nil, false, openOutcome{Address: address, Outcome: outcomeFailed, Reason: "no default program for http"}},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var ran []string
|
|
envOf := func(env []string) string {
|
|
var out []string
|
|
for _, kv := range env {
|
|
if strings.HasPrefix(kv, "DBUS_SESSION_BUS_ADDRESS=") || strings.HasPrefix(kv, "XDG_RUNTIME_DIR=") {
|
|
out = append(out, kv)
|
|
}
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|
|
bound := make(chan time.Time, 1)
|
|
d := sessionDesk{
|
|
query: func(env []string, name string, args ...string) (string, error) {
|
|
ran = append(ran, envOf(env)+" | "+name+" "+strings.Join(args, " "))
|
|
return c.program, nil
|
|
},
|
|
start: func(env []string, name string, args ...string) (<-chan error, error) {
|
|
ran = append(ran, envOf(env)+" | "+name+" "+strings.Join(args, " "))
|
|
if args[len(args)-1] != address {
|
|
t.Errorf("the address is not one argument: %q", args)
|
|
}
|
|
done := make(chan error, 1)
|
|
if c.running {
|
|
bound <- time.Now()
|
|
} else {
|
|
done <- c.ends
|
|
}
|
|
return done, nil
|
|
},
|
|
after: func(d time.Duration) <-chan time.Time {
|
|
if d != 10*time.Second {
|
|
t.Errorf("the open's bound is %s", d)
|
|
}
|
|
return bound
|
|
},
|
|
}
|
|
got := d.open(s, address)
|
|
if got != c.want {
|
|
t.Fatalf("answered %+v, want %+v", got, c.want)
|
|
}
|
|
env := "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus XDG_RUNTIME_DIR=/run/user/1000 | "
|
|
want := []string{env + "xdg-mime query default x-scheme-handler/http"}
|
|
if c.program != "\n" {
|
|
want = append(want, env+"systemd-run --user --collect --quiet --wait -- xdg-open "+address)
|
|
}
|
|
if strings.Join(ran, "\n") != strings.Join(want, "\n") {
|
|
t.Fatalf("ran\n%s\nwant\n%s", strings.Join(ran, "\n"), strings.Join(want, "\n"))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// exitStatus is the error of a program that ended with status n.
|
|
func exitStatus(t *testing.T, n int) error {
|
|
t.Helper()
|
|
err := exec.Command("sh", "-c", "exit "+string(rune('0'+n))).Run()
|
|
var exit *exec.ExitError
|
|
if !errors.As(err, &exit) {
|
|
t.Fatalf("no exit status: %v", err)
|
|
}
|
|
return err
|
|
}
|