-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuzz_test.go
More file actions
147 lines (122 loc) · 3.23 KB
/
Copy pathfuzz_test.go
File metadata and controls
147 lines (122 loc) · 3.23 KB
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
package main
import (
"github.com/skx/slisp/parser"
"strings"
"testing"
)
// FuzzProject runs the fuzz-testing against our parser and compiler.
//
// We mostly catch errors with the lexer and parser here, the compiler itself
// will generate and return text for the AST we produce.
func FuzzProject(f *testing.F) {
// Known errors we might see
known := []string{
// #\x
"unterminated character literal",
"unterminated escape",
//
"expected '('",
"expected ')'",
// defun
"expected defun",
"only the last parameter may have a &-prefix",
// variables
"unknown variable",
}
//
// Some examples to seed the fuzz corpus
//
testcases := []string{
// comments
`;; comment
(defun main() (print 3) ;; comment
)`,
// simple maths
"(defun main() (print (= 0 0)))",
"(defun main() (print (! 0)))",
"(defun main() (print (<= 0 0)))",
"(defun main() (print (< 0 0)))",
"(defun main() (print (> 0 0)))",
"(defun main() (print (>= 0 0)))",
"(defun main() (print (+ 0 0)))",
"(defun main() (print (- 0 0)))",
"(defun main() (print (* 0 0)))",
"(defun main() (print (/ 0 0)))",
"(defun main() (print (% 0 0)))",
// params
"(defun foo( a b &c) a) (defun main() (foo 1 2 3 4 ))",
"(defun foo( &a) a) (defun main() (foo 1 2 3 4 ))",
"(defun foo(a b &c) (+ a b) (defun main() (foo 1))",
"(defun foo(a b &c) (+ a b) (defun main() (foo 1 2))",
"(defun foo(a b &c) (+ a b) (defun main() (foo 1 2 3))",
// type-checking
"(defun main() (print (cons? 0)))",
"(defun main() (print (char? 0)))",
"(defun main() (print (int? 0)))",
"(defun main() (print (lambda? 0)))",
"(defun main() (print (nil? 0)))",
"(defun main() (print (str? 0)))",
// strings
`(defun main() (print "Hello, world!"))`,
// if
`(defun main() (if nil (print "nil") (print "true")))`,
`(defun main() (if nil (print "nil")))`,
// lambda
`(defun makeAdder (n) (lambda (x)(+ x n)))
(defun main () (let ((ten (makeAdder 10))) (print (ten 90))))`,
// let
`(defun main() (let ((x 1)) (print x)))`,
// set!
`(defun main() (let ((x 1)) (set! x 3) (print x)))`,
// list
`(defun main() (print (list 1 2 3 #\x "steve")))`,
// do
`(defun main() (do (print (list 1 2 3 #\x "steve"))))`,
// cond
`(defun main() (cond ((1 (print "one")))))`,
`(defun main() (let ((n 2))
(cond (
(= n 1) (print "one")
(= n 2) (print "two")
))))`,
// while
`(defun main() (while nil (print "ok")))`,
`(defun main() (let ((i 0))
(while (< i 10)
(println i)
(set! i (+ i 1)))))`,
}
//
// Seed the fuzzer with our samples
//
for _, tc := range testcases {
f.Add([]byte(tc))
}
//
// Run the fuzzer.
//
f.Fuzz(func(t *testing.T, input []byte) {
falsePositive := false
// Create a parser
p := parser.New(string(input))
// Parse into functions
_, err := p.Parse()
if err != nil {
//
// We got an error, was it a false-positive?
//
for _, ignored := range known {
if strings.Contains(err.Error(), ignored) {
falsePositive = true
}
}
//
// If it wasn't a false positive we want to see what
// was produced and mark it as a failure.
//
if !falsePositive {
t.Fatalf("error running input: '%s': %v", input, err)
}
}
})
}