github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/formats/dot/fuzz/fuzz.go (about)

     1  // Copyright ©2019 The Gonum 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 fuzz
     8  
     9  import (
    10  	"bytes"
    11  	"os/exec"
    12  
    13  	"github.com/jingcheng-WU/gonum/graph/formats/dot"
    14  )
    15  
    16  // Fuzz implements the fuzzing function required for go-fuzz.
    17  //
    18  // See documentation at https://github.com/dvyukov/go-fuzz.
    19  func Fuzz(data []byte) int {
    20  	// We don't accept empty data; the dot command does.
    21  	if len(data) == 0 || bytes.Equal(data, []byte{0}) {
    22  		return -1
    23  	}
    24  
    25  	// Check that dot accepts the input without complaint.
    26  	cmd := exec.Command("dot")
    27  	cmd.Stdin = bytes.NewReader(data)
    28  	err := cmd.Run()
    29  	if err != nil {
    30  		return 0
    31  	}
    32  
    33  	// Try to parse the data.
    34  	_, err = dot.Parse(bytes.NewReader(data))
    35  	if err != nil {
    36  		panic("could not parse good dot")
    37  	}
    38  	return 1
    39  }