github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/encoder/filename/fuzz.go (about)

     1  //go:build gofuzz
     2  
     3  package filename
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  )
     9  
    10  // Run like:
    11  // go-fuzz-build -o=fuzz-build.zip -func=Fuzz . && go-fuzz -minimize=5s -bin=fuzz-build.zip -workdir=testdata/corpus -procs=24
    12  
    13  // Fuzz test the provided input.
    14  func Fuzz(data []byte) int {
    15  	// First try to decode as is.
    16  	// We don't care about the result, it just shouldn't crash.
    17  	Decode(string(data))
    18  
    19  	// Now encode
    20  	enc := Encode(string(data))
    21  
    22  	// And decoded must match
    23  	decoded, err := Decode(enc)
    24  	if err != nil {
    25  		panic(fmt.Sprintf("error decoding %q, input %q: %v", enc, string(data), err))
    26  	}
    27  	if !bytes.Equal(data, []byte(decoded)) {
    28  		table := decodeMap[enc[0]]
    29  		table--
    30  		panic(fmt.Sprintf("decode mismatch, encoded: %q, org: %q, got: %q, table %d", enc, string(data), decoded, int(table)))
    31  	}
    32  
    33  	// Everything is good.
    34  	return 1
    35  }