aboutsummaryrefslogtreecommitdiff
path: root/cmd/krino/display_test.go
blob: a1b199c9fbc9119a414c5849b186f3b77f07c679 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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)
	}
}