// SPDX-License-Identifier: GPL-3.0-or-later package main import ( "fmt" "strings" "testing" "unicode/utf8" ) // terminalRunes is the test's own, hand-written list of code points a // terminal acts on or that reorder what it shows: C0 controls, DEL, C1 // controls, the Unicode bidirectional marks, embeddings, overrides and // isolates (UAX #9), and the line and paragraph separators. It is written // out, not derived from Go's tables or from display, so it can catch a // class display misses (re-review term F3). var terminalRunes = [][2]rune{ {0x00, 0x1f}, {0x7f, 0x9f}, {0x061c, 0x061c}, {0x200e, 0x200f}, {0x202a, 0x202e}, {0x2066, 0x2069}, {0x2028, 0x2029}, } // firstUnsafe names the first thing in s from terminalRunes (a newline // allowed when allowNewline), or invalid UTF-8, or returns "" when there is // none. func firstUnsafe(s string, allowNewline bool) string { if !utf8.ValidString(s) { return "invalid UTF-8" } for _, r := range s { if r == '\n' && allowNewline { continue } for _, span := range terminalRunes { if r >= span[0] && r <= span[1] { 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, so a quoted file name cannot act on the terminal; each // Write keeps its final newline (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) } // Only the Write's final newline ends a line; one inside it came from // quoted text and could fake a line of krino's own (re-review term F1). if got, want := b.String(), "a\\x1b[2J\\x0ab\\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) } }