Skip to content

Commit 858064f

Browse files
elwinarbradfitz
authored andcommitted
encoding/csv: add a Fuzz function
Adds a sample Fuzz test function to package encoding/csv based on https://github.com/dvyukov/go-fuzz-corpus/blob/master/csv/main.go Updates #19109 Updates #31309 Change-Id: Ieb0cb6caa1df72dbb7e29df4bdeed0bfa91187d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/174302 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 8e28cd1 commit 858064f

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/encoding/csv/fuzz.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build gofuzz
6+
7+
package csv
8+
9+
import (
10+
"bytes"
11+
"fmt"
12+
"reflect"
13+
)
14+
15+
func Fuzz(data []byte) int {
16+
score := 0
17+
buf := new(bytes.Buffer)
18+
19+
for _, tt := range []Reader{
20+
Reader{},
21+
Reader{Comma: ';'},
22+
Reader{Comma: '\t'},
23+
Reader{LazyQuotes: true},
24+
Reader{TrimLeadingSpace: true},
25+
Reader{Comment: '#'},
26+
Reader{Comment: ';'},
27+
} {
28+
r := NewReader(bytes.NewReader(data))
29+
r.Comma = tt.Comma
30+
r.Comment = tt.Comment
31+
r.LazyQuotes = tt.LazyQuotes
32+
r.TrimLeadingSpace = tt.TrimLeadingSpace
33+
34+
records, err := r.ReadAll()
35+
if err != nil {
36+
continue
37+
}
38+
score = 1
39+
40+
buf.Reset()
41+
w := NewWriter(buf)
42+
w.Comma = tt.Comma
43+
err = w.WriteAll(records)
44+
if err != nil {
45+
fmt.Printf("writer = %#v\n", w)
46+
fmt.Printf("records = %v\n", records)
47+
panic(err)
48+
}
49+
50+
r = NewReader(buf)
51+
r.Comma = tt.Comma
52+
r.Comment = tt.Comment
53+
r.LazyQuotes = tt.LazyQuotes
54+
r.TrimLeadingSpace = tt.TrimLeadingSpace
55+
result, err := r.ReadAll()
56+
if err != nil {
57+
fmt.Printf("reader = %#v\n", r)
58+
fmt.Printf("records = %v\n", records)
59+
panic(err)
60+
}
61+
62+
if !reflect.DeepEqual(records, result) {
63+
fmt.Println("records = \n", records)
64+
fmt.Println("result = \n", records)
65+
panic("not equal")
66+
}
67+
}
68+
69+
return score
70+
}

0 commit comments

Comments
 (0)