package main import ( "context" "os" "path/filepath" "strings" "testing" "time" "i3/internal/desktop" ) // runCall records one call the stood-in opener saw, so a test can check the exact argv it was // handed (design 08 sign-in-page ยง29, R2-9: one argument, no shell). type runCall struct { name string args []string } // stubOpener builds an i3 whose session is found (unless find answers an error) and whose // commands are answered from answers, keyed by the command's name ("xdg-mime", "systemd-run"); // every call is recorded in calls. func stubOpener(t *testing.T, find func() (*desktop.Session, error), answers map[string]desktop.Result) (*i3, *[]runCall) { t.Helper() calls := &[]runCall{} run := func(ctx context.Context, env []string, stdin []byte, name string, args ...string) desktop.Result { *calls = append(*calls, runCall{name, args}) res, ok := answers[name] if !ok { t.Fatalf("unexpected command %s %v", name, args) } if res.TimedOut { <-ctx.Done() return desktop.Result{TimedOut: true} } return res } return &i3{d: desktop.Desk{Find: find, Run: run}, openBound: 50 * time.Millisecond}, calls } func foundSession() (*desktop.Session, error) { return &desktop.Session{}, nil } func writeDesktopEntry(t *testing.T, entry, execLine string) { t.Helper() dir := t.TempDir() t.Setenv("XDG_DATA_HOME", dir) apps := filepath.Join(dir, "applications") if err := os.MkdirAll(apps, 0o755); err != nil { t.Fatal(err) } text := "[Desktop Entry]\nType=Application\nName=Browser\nExec=" + execLine + "\n" if err := os.WriteFile(filepath.Join(apps, entry), []byte(text), 0o644); err != nil { t.Fatal(err) } } // 935/I-1: every scheme but http and https, and a file path, is refused. func TestOpenRefusesEveryOtherScheme(t *testing.T) { x, calls := stubOpener(t, foundSession, map[string]desktop.Result{}) for _, address := range []string{ "/etc/passwd", // no scheme: a file path "file:///etc/passwd", // a scheme other than http or https "ftp://example.net/f", // a scheme other than http or https "mailto:a@example.net", // a scheme other than http or https, no // "http://", // no host "https:///path", // no host } { if _, err := run(t, x, "node-display-session.open", map[string]any{"address": address}); err == nil { t.Errorf("%s: not refused", address) } } if len(*calls) != 0 { t.Fatalf("a refused address never reaches the opener: %v", *calls) } } // 935/I-1: an address over 2048 bytes once encoded is refused. func TestOpenRefusesAnAddressTooLongOnceEncoded(t *testing.T) { x, _ := stubOpener(t, foundSession, map[string]desktop.Result{}) address := "https://example.net/" + strings.Repeat("a", 2048) if _, err := run(t, x, "node-display-session.open", map[string]any{"address": address}); err == nil { t.Fatal("not refused") } } // 935/I-1: with no graphical session it refuses, as its other tools answer. func TestOpenRefusesWithNoGraphicalSession(t *testing.T) { x, _ := stubOpener(t, func() (*desktop.Session, error) { return nil, &desktop.NoSession{Reason: "none"} }, map[string]desktop.Result{}) _, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}) if !desktop.IsNoSession(err) { t.Fatalf("%v", err) } } // 935/I-1: no default program for the scheme is refused. func TestOpenRefusesWithNoDefaultProgram(t *testing.T) { x, calls := stubOpener(t, foundSession, map[string]desktop.Result{ "xdg-mime": {Code: 0, Stdout: ""}, }) if _, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}); err == nil { t.Fatal("not refused") } for _, c := range *calls { if c.name == "systemd-run" { t.Fatal("the opener never runs with no default program") } } } // 935/I-2 (R2-9): every character outside the kept set is percent-encoded, as the bytes of its // UTF-8 form, and the address reaches the opener as one argument, never a shell command line. func TestOpenEncodesEveryOtherCharacterAsOneArgument(t *testing.T) { writeDesktopEntry(t, "browser.desktop", "firefox %u") x, calls := stubOpener(t, foundSession, map[string]desktop.Result{ "xdg-mime": {Code: 0, Stdout: "browser.desktop\n"}, "systemd-run": {Code: 0}, }) address := `https://example.net/a'b;c d$(e),f"g` got, err := run(t, x, "node-display-session.open", map[string]any{"address": address}) if err != nil { t.Fatal(err) } want := `https://example.net/a%27b%3Bc%20d%24%28e%29%2Cf%22g` answer := got.(map[string]any) if answer["address"] != want { t.Fatalf("got %q, want %q", answer["address"], want) } var last runCall for _, c := range *calls { if c.name == "systemd-run" { last = c } } if len(last.args) == 0 || last.args[len(last.args)-1] != want { t.Fatalf("the address is not the opener's last, whole argument: %v", last.args) } for _, a := range last.args { if strings.Contains(a, "'") || strings.Contains(a, ";") || strings.Contains(a, " ") { t.Fatalf("an argument still carries a shell character: %q", a) } } } // 935/I-3: an opener that fails is refused with its status and names no program. func TestOpenRefusesWhenTheOpenerFails(t *testing.T) { writeDesktopEntry(t, "browser.desktop", "firefox %u") x, _ := stubOpener(t, foundSession, map[string]desktop.Result{ "xdg-mime": {Code: 0, Stdout: "browser.desktop\n"}, "systemd-run": {Code: 1, Stderr: "no such unit"}, }) _, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}) if err == nil || !strings.Contains(err.Error(), "no such unit") { t.Fatalf("%v", err) } } // 935/I-3: an opener that ends well answers opened with the program. func TestOpenAnswersOpenedWithTheProgram(t *testing.T) { writeDesktopEntry(t, "browser.desktop", "firefox %u") x, _ := stubOpener(t, foundSession, map[string]desktop.Result{ "xdg-mime": {Code: 0, Stdout: "browser.desktop\n"}, "systemd-run": {Code: 0}, }) got, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}) if err != nil { t.Fatal(err) } answer := got.(map[string]any) if answer["started"] != "opened" || answer["program"] != "browser.desktop" || answer["command"] != "firefox" { t.Fatalf("%+v", answer) } } // 935/I-3: one still running at its bound answers opening, with the program (H-M2: the answer // names the program only once the opener started). func TestOpenAnswersOpeningWhenStillRunningAtItsBound(t *testing.T) { writeDesktopEntry(t, "browser.desktop", "firefox %u") x, _ := stubOpener(t, foundSession, map[string]desktop.Result{ "xdg-mime": {Code: 0, Stdout: "browser.desktop\n"}, "systemd-run": {TimedOut: true}, }) got, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}) if err != nil { t.Fatal(err) } answer := got.(map[string]any) if answer["started"] != "opening" || answer["program"] != "browser.desktop" { t.Fatalf("%+v", answer) } } // DQ-7: each call that is acted on writes one act to the log of acts done by hand, with its caller // and why; a refusal before the opener ran writes none. func TestOpenWritesOneActDoneByHand(t *testing.T) { writeDesktopEntry(t, "browser.desktop", "firefox %u") x, _ := stubOpener(t, foundSession, map[string]desktop.Result{ "xdg-mime": {Code: 0, Stdout: "browser.desktop\n"}, "systemd-run": {Code: 0}, }) var recorded []string x.handAct = func(what, why string) error { recorded = append(recorded, what+"|"+why) return nil } if _, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}); err != nil { t.Fatal(err) } if len(recorded) != 1 || !strings.Contains(recorded[0], "https://example.net") { t.Fatalf("%v", recorded) } recorded = nil if _, err := run(t, x, "node-display-session.open", map[string]any{"address": "ftp://example.net"}); err == nil { t.Fatal("expected a refusal") } if len(recorded) != 0 { t.Fatalf("a refusal before the opener ran writes no act: %v", recorded) } }