| 1 |
// Copyright (c) 2016, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package syntax |
| 5 |
|
| 6 |
import ( |
| 7 |
"io" |
| 8 |
"os/exec" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/go-quicktest/qt" |
| 13 |
) |
| 14 |
|
| 15 |
func FuzzQuote(f *testing.F) { |
| 16 |
if _, err := exec.LookPath("bash"); err != nil { |
| 17 |
f.Skipf("requires bash to verify quoted strings") |
| 18 |
} |
| 19 |
|
| 20 |
// Keep in sync with ExampleQuote. |
| 21 |
f.Add("foo", uint8(LangBash)) |
| 22 |
f.Add("bar $baz", uint8(LangBash)) |
| 23 |
f.Add(`"won't"`, uint8(LangBash)) |
| 24 |
f.Add(`~/home`, uint8(LangBash)) |
| 25 |
f.Add("#1304", uint8(LangBash)) |
| 26 |
f.Add("name=value", uint8(LangBash)) |
| 27 |
f.Add(`glob-*`, uint8(LangBash)) |
| 28 |
f.Add("invalid-\xe2'", uint8(LangBash)) |
| 29 |
f.Add("nonprint-\x0b\x1b", uint8(LangBash)) |
| 30 |
f.Fuzz(func(t *testing.T, s string, langVariant uint8) { |
| 31 |
lang := LangVariant(langVariant) |
| 32 |
quoted, err := Quote(s, lang) |
| 33 |
if err != nil { |
| 34 |
// Cannot be quoted; not interesting. |
| 35 |
t.Skip() |
| 36 |
} |
| 37 |
|
| 38 |
external, ok := externalShells[lang] |
| 39 |
if !ok { |
| 40 |
t.Skip() // invalid/untested lang variant |
| 41 |
} |
| 42 |
external.require(t) |
| 43 |
|
| 44 |
// Verify that our parser ends up with a simple command with one word. |
| 45 |
f, err := NewParser(Variant(lang)).Parse(strings.NewReader(quoted), "") |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("parse error on %q quoted as %s: %v", s, quoted, err) |
| 48 |
} |
| 49 |
qt.Assert(t, qt.Equals(len(f.Stmts), 1), qt.Commentf("in: %q, quoted: %s", s, quoted)) |
| 50 |
call, ok := f.Stmts[0].Cmd.(*CallExpr) |
| 51 |
qt.Assert(t, qt.IsTrue(ok), qt.Commentf("in: %q, quoted: %s", s, quoted)) |
| 52 |
qt.Assert(t, qt.Equals(len(call.Args), 1), qt.Commentf("in: %q, quoted: %s", s, quoted)) |
| 53 |
|
| 54 |
// Also check that the single word only uses literals or quoted strings. |
| 55 |
Walk(call.Args[0], func(node Node) bool { |
| 56 |
switch node.(type) { |
| 57 |
case nil, *Word, *Lit, *SglQuoted, *DblQuoted: |
| 58 |
default: |
| 59 |
t.Fatalf("unexpected node type: %T", node) |
| 60 |
} |
| 61 |
return true |
| 62 |
}) |
| 63 |
|
| 64 |
// The process below shouldn't run arbitrary code, |
| 65 |
// since our parser checks above should catch the use of ';' or '$', |
| 66 |
// in the case that Quote were too naive to quote them. |
| 67 |
out, err := exec.Command(external.cmd, "-c", "printf %s "+quoted).CombinedOutput() |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("%s error on %q quoted as %s: %v: %s", external.cmd, s, quoted, err, out) |
| 70 |
} |
| 71 |
want, got := s, string(out) |
| 72 |
if want != got { |
| 73 |
t.Fatalf("%s output mismatch on %q quoted as %s: got %q (len=%d)", |
| 74 |
external.cmd, want, quoted, got, len(got)) |
| 75 |
} |
| 76 |
}) |
| 77 |
} |
| 78 |
|
| 79 |
func FuzzParsePrint(f *testing.F) { |
| 80 |
add := func(src string, variant LangVariant, stopAt string) { |
| 81 |
// For now, default to just KeepComments. |
| 82 |
f.Add(src, uint8(variant), true, stopAt, false, |
| 83 |
uint8(0), false, false, false, false, false, false, false) |
| 84 |
} |
| 85 |
|
| 86 |
for _, test := range errorCases { |
| 87 |
add(test.in, LangBash, "") |
| 88 |
} |
| 89 |
for _, test := range printTests { |
| 90 |
add(test.in, LangBash, "") |
| 91 |
} |
| 92 |
for _, test := range fileTests { |
| 93 |
for _, in := range test.inputs { |
| 94 |
for lang := range langResolvedVariants.bits() { |
| 95 |
if test.byLangIndex[lang.index()] != nil { |
| 96 |
add(in, lang, "") |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
for _, test := range stopAtTests { |
| 102 |
add(test.in, LangBash, test.stop) |
| 103 |
} |
| 104 |
|
| 105 |
f.Fuzz(func(t *testing.T, |
| 106 |
src string, |
| 107 |
|
| 108 |
// parser options |
| 109 |
langVariant uint8, |
| 110 |
keepComments bool, |
| 111 |
stopAt string, |
| 112 |
|
| 113 |
simplify bool, |
| 114 |
|
| 115 |
// printer options |
| 116 |
indent uint8, // 0-255 |
| 117 |
binaryNextLine bool, |
| 118 |
switchCaseIndent bool, |
| 119 |
spaceRedirects bool, |
| 120 |
keepPadding bool, |
| 121 |
minify bool, |
| 122 |
singleLine bool, |
| 123 |
functionNextLine bool, |
| 124 |
) { |
| 125 |
lang := LangVariant(langVariant) |
| 126 |
if lang.count() != 1 || !lang.in(langResolvedVariants) { |
| 127 |
t.Skip() |
| 128 |
} |
| 129 |
if indent > 16 { |
| 130 |
t.Skip() // more indentation won't really be interesting |
| 131 |
} |
| 132 |
if len(stopAt) > 4 || strings.ContainsAny(stopAt, " \t\n\r") { |
| 133 |
t.Skip() // StopAt panics on these |
| 134 |
} |
| 135 |
|
| 136 |
opts := []ParserOption{Variant(lang), KeepComments(keepComments)} |
| 137 |
if stopAt != "" { |
| 138 |
opts = append(opts, StopAt(stopAt)) |
| 139 |
} |
| 140 |
parser := NewParser(opts...) |
| 141 |
t.Logf("input: %q", src) |
| 142 |
t.Logf("LangVariant: %s", lang) |
| 143 |
t.Logf("KeepComments: %t", keepComments) |
| 144 |
t.Logf("StopAt: %q", stopAt) |
| 145 |
t.Logf("Simplify: %v", simplify) |
| 146 |
t.Logf("Indent: %v", indent) |
| 147 |
t.Logf("BinaryNextLine: %v", binaryNextLine) |
| 148 |
t.Logf("SwitchCaseIndent: %v", switchCaseIndent) |
| 149 |
t.Logf("SpaceRedirects: %v", spaceRedirects) |
| 150 |
t.Logf("KeepPadding: %v", keepPadding) |
| 151 |
t.Logf("Minify: %v", minify) |
| 152 |
t.Logf("SingleLine: %v", singleLine) |
| 153 |
t.Logf("FunctionNextLine: %v", functionNextLine) |
| 154 |
|
| 155 |
prog, err := parser.Parse(strings.NewReader(src), "") |
| 156 |
if err != nil { |
| 157 |
t.Skip() // not valid shell syntax |
| 158 |
} |
| 159 |
|
| 160 |
if simplify { |
| 161 |
Simplify(prog) |
| 162 |
} |
| 163 |
|
| 164 |
printer := NewPrinter() |
| 165 |
Indent(uint(indent))(printer) |
| 166 |
BinaryNextLine(binaryNextLine)(printer) |
| 167 |
SwitchCaseIndent(switchCaseIndent)(printer) |
| 168 |
SpaceRedirects(spaceRedirects)(printer) |
| 169 |
KeepPadding(keepPadding)(printer) |
| 170 |
Minify(minify)(printer) |
| 171 |
SingleLine(singleLine)(printer) |
| 172 |
FunctionNextLine(functionNextLine)(printer) |
| 173 |
|
| 174 |
if err := printer.Print(io.Discard, prog); err != nil { |
| 175 |
t.Skip() // e.g. invalid option |
| 176 |
} |
| 177 |
}) |
| 178 |
} |