| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/packagegrant" |
| 11 | ) |
| 12 | |
| 13 | func TestStripPackageGrantsOnlyAnswersForAnApplication(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | for name, app := range map[string]string{ |
| 16 | "no application": "", |
| 17 | "a path that is nothing": filepath.Join(dir, "missing.exe"), |
| 18 | "a directory": dir, |
| 19 | } { |
| 20 | var out, logs bytes.Buffer |
| 21 | if code := stripPackageGrants(&out, &logs, app); code != 2 || out.Len() != 0 { |
| 22 | t.Errorf("%s: code=%d out=%q", name, code, out.String()) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // The shell reads this line as structure, so both lists are present even when |
| 28 | // empty: a null would be a third answer it has to guess about. |
| 29 | func TestStripPackageGrantsReportsOneLineOfJSON(t *testing.T) { |
| 30 | app := filepath.Join(t.TempDir(), "Reasonix.exe") |
| 31 | if err := os.WriteFile(app, []byte("x"), 0o755); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | var out, logs bytes.Buffer |
| 35 | if code := stripPackageGrants(&out, &logs, app); code != 0 { |
| 36 | t.Fatalf("code=%d logs=%s", code, logs.String()) |
| 37 | } |
| 38 | var report packagegrant.Report |
| 39 | if err := json.Unmarshal(out.Bytes(), &report); err != nil { |
| 40 | t.Fatalf("not JSON: %q", out.String()) |
| 41 | } |
| 42 | if len(report.Stripped)+len(report.Refused)+len(report.Unread) != 0 { |
| 43 | t.Fatalf("a clean tree reported %+v from %q", report, out.String()) |
| 44 | } |
| 45 | for _, list := range []string{`"stripped":[]`, `"refused":[]`, `"unread":[]`} { |
| 46 | if !bytes.Contains(out.Bytes(), []byte(list)) { |
| 47 | t.Fatalf("%s was not written as an empty list: %q", list, out.String()) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestStripPackageGrantsRequestReadsBothSpellings(t *testing.T) { |
| 53 | for _, args := range [][]string{ |
| 54 | {"--strip-package-grants", "--app", `C:\R\app\Reasonix.exe`}, |
| 55 | {"-strip-package-grants", "-app=C:\\R\\app\\Reasonix.exe"}, |
| 56 | } { |
| 57 | app, ok := stripPackageGrantsRequest(args) |
| 58 | if !ok || app != `C:\R\app\Reasonix.exe` { |
| 59 | t.Fatalf("%q: app=%q ok=%v", args, app, ok) |
| 60 | } |
| 61 | } |
| 62 | if _, ok := stripPackageGrantsRequest([]string{"--host-rpc", "--app", "x"}); ok { |
| 63 | t.Fatal("a launch without the flag was read as a grant pass") |
| 64 | } |
| 65 | } |
| 66 |