|
| 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