// SPDX-License-Identifier: GPL-3.0-or-later package main import ( "fmt" "strings" "testing" "unicode" "unicode/utf8" ) // firstUnsafe names the first thing in s a terminal could act on - a // control character (a newline too, unless allowNewline), a Unicode // bidirectional control, a line or paragraph separator, or invalid UTF-8 - // or returns "" when there is none. It is built from Go's Unicode tables, // not from display's own ranges, so it can catch a class display misses. func firstUnsafe(s string, allowNewline bool) string { if !utf8.ValidString(s) { return "invalid UTF-8" } for _, r := range s { if r == '\n' && allowNewline { continue } if unicode.IsControl(r) || unicode.Is(unicode.Bidi_Control, r) || unicode.In(r, unicode.Zl, unicode.Zp) { return fmt.Sprintf("%U", r) } } return "" } // TestDisplayEscapesTerminalControls: display shows every character a // terminal would act on as an escape, and leaves ordinary text - Polish // letters and a backslash included - exactly as it is. func TestDisplayEscapesTerminalControls(t *testing.T) { for in, want := range map[string]string{ "plain name.pdf": "plain name.pdf", "zażółć gęślą jaźń.pdf": "zażółć gęślą jaźń.pdf", "esc\x1b[2Kx.pdf": `esc\x1b[2Kx.pdf`, "bell\a.pdf": `bell\x07.pdf`, "cr\rline.pdf": `cr\x0dline.pdf`, "new\nline.pdf": `new\x0aline.pdf`, "tab\t.pdf": `tab\x09.pdf`, "del\x7f.pdf": `del\x7f.pdf`, "c1\u009bcsi.pdf": `c1\u009bcsi.pdf`, "bidi\u202egnp.pdf": `bidi\u202egnp.pdf`, "isolate\u2066x\u2069.pdf": `isolate\u2066x\u2069.pdf`, "bad\xffbyte.pdf": `bad\xffbyte.pdf`, `back\slash.pdf`: `back\slash.pdf`, "alm\u061c.pdf": `alm\u061c.pdf`, "lrm\u200e.pdf": `lrm\u200e.pdf`, "rlm\u200f.pdf": `rlm\u200f.pdf`, "ls\u2028ps\u2029.pdf": `ls\u2028ps\u2029.pdf`, "replacement\ufffd.pdf": "replacement\ufffd.pdf", } { if got := display(in); got != want { t.Errorf("display(%q) = %q, want %q", in, got, want) } } } // FuzzDisplay: whatever a name holds, display's result holds nothing a // terminal could act on. func FuzzDisplay(f *testing.F) { f.Add("esc\x1b[2K\u202e\xff\x00") f.Add("plain") f.Fuzz(func(t *testing.T, s string) { if bad := firstUnsafe(display(s), false); bad != "" { t.Fatalf("display(%q) = %q still holds %s", s, display(s), bad) } }) } // TestReviewEscapesHostileNames: the per-file header and the delete // confirmation show a hostile name escaped, in review and in undo's review. func TestReviewEscapesHostileNames(t *testing.T) { out := new(strings.Builder) if _, _, _, err := reviewChains(strings.NewReader("cdnn"), out, chains("esc\x1b[2Kx.pdf"), "", palette{}); err != nil { t.Fatal(err) } if bad := firstUnsafe(out.String(), true); bad != "" { t.Errorf("review printed %s:\n%q", bad, out) } undoOut := new(strings.Builder) if _, _, err := reviewUndoFiles(strings.NewReader("cn"), undoOut, undoFiles("esc\x1b[2Kx.pdf"), palette{}); err != nil { t.Fatal(err) } if bad := firstUnsafe(undoOut.String(), true); bad != "" { t.Errorf("undo review printed %s:\n%q", bad, undoOut) } } // TestSafeWriterEscapesButKeepsNewlines: everything written to stderr goes // through display line by line, so a quoted file name cannot act on the // terminal while messages keep their lines (review M5). func TestSafeWriterEscapesButKeepsNewlines(t *testing.T) { var b strings.Builder in := "a\x1b[2J\nb\u202e\n" n, err := safeWriter{&b}.Write([]byte(in)) if err != nil || n != len(in) { t.Fatalf("Write = %d, %v", n, err) } if got, want := b.String(), "a\\x1b[2J\nb\\u202e\n"; got != want { t.Errorf("wrote %q, want %q", got, want) } } // TestJSONSafeEscapesTerminalRunes: in an encoded JSON document, DEL, C1 and // bidirectional controls come out as \uXXXX escapes - the same string // values - and everything else is untouched (review terminal F4). func TestJSONSafeEscapesTerminalRunes(t *testing.T) { in := "{\"rel\": \"c1\u009bx\u202ey\u007fz ż\"}" want := `{"rel": "c1\u009bx\u202ey\u007fz ż"}` if got := string(jsonSafe([]byte(in))); got != want { t.Errorf("jsonSafe = %q, want %q", got, want) } }