| 1 | package desktop |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | func fake(t *testing.T, name, script string) { |
| 15 | t.Helper() |
| 16 | dir := t.TempDir() |
| 17 | if script != "" { |
| 18 | if err := os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"+script+"\n"), 0o755); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | t.Setenv("PATH", dir+":/usr/bin:/bin") |
| 24 | } |
| 25 | |
| 26 | type want struct{ allow, ok bool } |
| 27 | |
| 28 | func TestAskLinux(t *testing.T) { |
| 29 | if runtime.GOOS != "linux" { |
| 30 | t.Skip("linux") |
| 31 | } |
| 32 | for _, tool := range []struct { |
| 33 | name string |
| 34 | deny int |
| 35 | display string |
| 36 | }{ |
| 37 | {"zenity", 1, "cannot open display"}, |
| 38 | {"kdialog", 1, "could not connect to display"}, |
| 39 | {"kdialog", 2, "cannot connect to X server"}, |
| 40 | } { |
| 41 | for _, c := range []struct { |
| 42 | name string |
| 43 | script string |
| 44 | env string |
| 45 | want want |
| 46 | }{ |
| 47 | {"allow", "exit 0", ":0", want{true, true}}, |
| 48 | {"deny", fmt.Sprintf("exit %d", tool.deny), ":0", want{false, true}}, |
| 49 | {"no display tool", "echo '" + tool.display + "' >&2; exit 1", ":0", want{false, false}}, |
| 50 | {"crash", "kill -SEGV $$", ":0", want{false, false}}, |
| 51 | {"no display variable", "exit 0", "", want{false, false}}, |
| 52 | {"tool missing", "", ":0", want{false, false}}, |
| 53 | } { |
| 54 | t.Run(tool.name+"/"+c.name, func(t *testing.T) { |
| 55 | t.Setenv("DISPLAY", c.env) |
| 56 | t.Setenv("WAYLAND_DISPLAY", "") |
| 57 | fake(t, tool.name, c.script) |
| 58 | allow, ok := Ask(context.Background(), "t", "m") |
| 59 | if (want{allow, ok}) != c.want { |
| 60 | t.Errorf("got allow=%v ok=%v, want %v", allow, ok, c.want) |
| 61 | } |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | func TestAskLinuxFallback(t *testing.T) { |
| 69 | if runtime.GOOS != "linux" { |
| 70 | t.Skip("linux") |
| 71 | } |
| 72 | t.Setenv("DISPLAY", ":0") |
| 73 | t.Setenv("WAYLAND_DISPLAY", "") |
| 74 | fake(t, "zenity", "echo 'cannot open display' >&2; exit 1") |
| 75 | dir := filepath.SplitList(os.Getenv("PATH"))[0] |
| 76 | if err := os.WriteFile(filepath.Join(dir, "kdialog"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if allow, ok := Ask(context.Background(), "t", "m"); !allow || !ok { |
| 80 | t.Errorf("got allow=%v ok=%v", allow, ok) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | func TestAskTimeout(t *testing.T) { |
| 86 | var tool string |
| 87 | switch runtime.GOOS { |
| 88 | case "linux": |
| 89 | tool = "zenity" |
| 90 | t.Setenv("DISPLAY", ":0") |
| 91 | t.Setenv("WAYLAND_DISPLAY", "") |
| 92 | case "darwin": |
| 93 | tool = "osascript" |
| 94 | default: |
| 95 | t.Skip("no dialog") |
| 96 | } |
| 97 | fake(t, tool, "exec sleep 30") |
| 98 | ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) |
| 99 | defer cancel() |
| 100 | start := time.Now() |
| 101 | allow, ok := Ask(ctx, "t", "m") |
| 102 | if allow || !ok { |
| 103 | t.Errorf("got allow=%v ok=%v", allow, ok) |
| 104 | } |
| 105 | if time.Since(start) > 10*time.Second { |
| 106 | t.Error("did not stop with the context") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestAskDarwin(t *testing.T) { |
| 111 | if runtime.GOOS != "darwin" { |
| 112 | t.Skip("macOS") |
| 113 | } |
| 114 | for _, c := range []struct { |
| 115 | name string |
| 116 | script string |
| 117 | want want |
| 118 | }{ |
| 119 | {"allow", "echo 'button returned:Allow'", want{true, true}}, |
| 120 | {"deny button", "echo 'button returned:Deny'", want{false, true}}, |
| 121 | {"closed", "echo 'execution error: User canceled. (-128)' >&2; exit 1", want{false, true}}, |
| 122 | {"no session", "echo 'execution error: Application isn'\\''t running. (-600)' >&2; exit 1", want{false, false}}, |
| 123 | {"missing", "", want{false, false}}, |
| 124 | } { |
| 125 | t.Run(c.name, func(t *testing.T) { |
| 126 | fake(t, "osascript", c.script) |
| 127 | if c.script == "" { |
| 128 | |
| 129 | t.Setenv("PATH", t.TempDir()) |
| 130 | } |
| 131 | allow, ok := Ask(context.Background(), "t", "m") |
| 132 | if (want{allow, ok}) != c.want { |
| 133 | t.Errorf("got allow=%v ok=%v, want %v", allow, ok, c.want) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | } |