github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/fspath/fuzz.go (about)

     1  //go:build gofuzz
     2  
     3  /*
     4     Fuzz test the Parse function
     5  
     6     Generate corpus
     7  
     8     go test -v -make-corpus
     9  
    10     Install go fuzz
    11  
    12     go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build
    13  
    14     Compile and fuzz
    15  
    16     go-fuzz-build
    17     go-fuzz
    18  
    19     Tidy up
    20  
    21     rm -rf corpus/ crashers/ suppressions/
    22     git co ../../go.mod ../../go.sum
    23  */
    24  
    25  package fspath
    26  
    27  func Fuzz(data []byte) int {
    28  	path := string(data)
    29  	parsed, err := Parse(path)
    30  	if err != nil {
    31  		return 0
    32  	}
    33  	if parsed.Name == "" {
    34  		if parsed.ConfigString != "" {
    35  			panic("bad ConfigString")
    36  		}
    37  		if parsed.Path != path {
    38  			panic("local path not preserved")
    39  		}
    40  	} else {
    41  		if parsed.ConfigString+":"+parsed.Path != path {
    42  			panic("didn't split properly")
    43  		}
    44  	}
    45  	return 0
    46  }