forked from mvdan/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz_test.go
178 lines (158 loc) · 4.47 KB
/
fuzz_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2016, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package syntax
import (
"fmt"
"io"
"os/exec"
"strings"
"testing"
"github.com/go-quicktest/qt"
)
func FuzzQuote(f *testing.F) {
if _, err := exec.LookPath("bash"); err != nil {
f.Skipf("requires bash to verify quoted strings")
}
// Keep in sync with ExampleQuote.
f.Add("foo", uint8(LangBash))
f.Add("bar $baz", uint8(LangBash))
f.Add(`"won't"`, uint8(LangBash))
f.Add(`~/home`, uint8(LangBash))
f.Add("#1304", uint8(LangBash))
f.Add("name=value", uint8(LangBash))
f.Add(`glob-*`, uint8(LangBash))
f.Add("invalid-\xe2'", uint8(LangBash))
f.Add("nonprint-\x0b\x1b", uint8(LangBash))
f.Fuzz(func(t *testing.T, s string, langVariant uint8) {
if langVariant > 3 {
t.Skip() // lang variants are 0-3
}
lang := LangVariant(langVariant)
quoted, err := Quote(s, lang)
if err != nil {
// Cannot be quoted; not interesting.
t.Skip()
}
var shellProgram string
switch lang {
case LangBash:
requireBash52(t)
shellProgram = "bash"
case LangPOSIX:
requireDash059(t)
shellProgram = "dash"
case LangMirBSDKorn:
requireMksh59(t)
shellProgram = "mksh"
case LangBats:
t.Skip() // bats has no shell and its syntax is just bash
default:
panic(fmt.Sprintf("unknown lang variant: %d", lang))
}
// Verify that our parser ends up with a simple command with one word.
f, err := NewParser(Variant(lang)).Parse(strings.NewReader(quoted), "")
if err != nil {
t.Fatalf("parse error on %q quoted as %s: %v", s, quoted, err)
}
qt.Assert(t, qt.Equals(len(f.Stmts), 1), qt.Commentf("in: %q, quoted: %s", s, quoted))
call, ok := f.Stmts[0].Cmd.(*CallExpr)
qt.Assert(t, qt.IsTrue(ok), qt.Commentf("in: %q, quoted: %s", s, quoted))
qt.Assert(t, qt.Equals(len(call.Args), 1), qt.Commentf("in: %q, quoted: %s", s, quoted))
// Also check that the single word only uses literals or quoted strings.
Walk(call.Args[0], func(node Node) bool {
switch node.(type) {
case nil, *Word, *Lit, *SglQuoted, *DblQuoted:
default:
t.Fatalf("unexpected node type: %T", node)
}
return true
})
// The process below shouldn't run arbitrary code,
// since our parser checks above should catch the use of ';' or '$',
// in the case that Quote were too naive to quote them.
out, err := exec.Command(shellProgram, "-c", "printf %s "+quoted).CombinedOutput()
if err != nil {
t.Fatalf("%s error on %q quoted as %s: %v: %s", shellProgram, s, quoted, err, out)
}
want, got := s, string(out)
if want != got {
t.Fatalf("%s output mismatch on %q quoted as %s: got %q (len=%d)",
shellProgram, want, quoted, got, len(got))
}
})
}
func FuzzParsePrint(f *testing.F) {
add := func(src string, variant LangVariant) {
// For now, default to just KeepComments.
f.Add(src, uint8(variant), true, false,
uint8(0), false, false, false, false, false, false, false)
}
for _, test := range shellTests {
add(test.in, LangBash)
}
for _, test := range printTests {
add(test.in, LangBash)
}
for _, test := range fileTests {
for _, in := range test.Strs {
if test.Bash != nil {
add(in, LangBash)
}
if test.Posix != nil {
add(in, LangPOSIX)
}
if test.MirBSDKorn != nil {
add(in, LangMirBSDKorn)
}
if test.Bats != nil {
add(in, LangBats)
}
}
}
f.Fuzz(func(t *testing.T,
src string,
// parser options
// TODO: also fuzz StopAt
langVariant uint8, // 0-3
keepComments bool,
simplify bool,
// printer options
indent uint8, // 0-255
binaryNextLine bool,
switchCaseIndent bool,
spaceRedirects bool,
keepPadding bool,
minify bool,
singleLine bool,
functionNextLine bool,
) {
if langVariant > 3 {
t.Skip() // lang variants are 0-3
}
if indent > 16 {
t.Skip() // more indentation won't really be interesting
}
parser := NewParser()
Variant(LangVariant(langVariant))(parser)
KeepComments(keepComments)(parser)
prog, err := parser.Parse(strings.NewReader(src), "")
if err != nil {
t.Skip() // not valid shell syntax
}
if simplify {
Simplify(prog)
}
printer := NewPrinter()
Indent(uint(indent))(printer)
BinaryNextLine(binaryNextLine)(printer)
SwitchCaseIndent(switchCaseIndent)(printer)
SpaceRedirects(spaceRedirects)(printer)
KeepPadding(keepPadding)(printer)
Minify(minify)(printer)
SingleLine(singleLine)(printer)
FunctionNextLine(functionNextLine)(printer)
if err := printer.Print(io.Discard, prog); err != nil {
t.Skip() // e.g. invalid option
}
})
}