github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/encoding/csv/fuzz.go (about) 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 //go:build gofuzz 6 // +build gofuzz 7 8 package csv 9 10 import ( 11 "bytes" 12 "fmt" 13 "reflect" 14 ) 15 16 func Fuzz(data []byte) int { 17 score := 0 18 buf := new(bytes.Buffer) 19 20 for _, tt := range []Reader{ 21 {}, 22 {Comma: ';'}, 23 {Comma: '\t'}, 24 {LazyQuotes: true}, 25 {TrimLeadingSpace: true}, 26 {Comment: '#'}, 27 {Comment: ';'}, 28 } { 29 r := NewReader(bytes.NewReader(data)) 30 r.Comma = tt.Comma 31 r.Comment = tt.Comment 32 r.LazyQuotes = tt.LazyQuotes 33 r.TrimLeadingSpace = tt.TrimLeadingSpace 34 35 records, err := r.ReadAll() 36 if err != nil { 37 continue 38 } 39 score = 1 40 41 buf.Reset() 42 w := NewWriter(buf) 43 w.Comma = tt.Comma 44 err = w.WriteAll(records) 45 if err != nil { 46 fmt.Printf("writer = %#v\n", w) 47 fmt.Printf("records = %v\n", records) 48 panic(err) 49 } 50 51 r = NewReader(buf) 52 r.Comma = tt.Comma 53 r.Comment = tt.Comment 54 r.LazyQuotes = tt.LazyQuotes 55 r.TrimLeadingSpace = tt.TrimLeadingSpace 56 result, err := r.ReadAll() 57 if err != nil { 58 fmt.Printf("reader = %#v\n", r) 59 fmt.Printf("records = %v\n", records) 60 panic(err) 61 } 62 63 if !reflect.DeepEqual(records, result) { 64 fmt.Println("records = \n", records) 65 fmt.Println("result = \n", records) 66 panic("not equal") 67 } 68 } 69 70 return score 71 }