github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/common/bitutil/compress_fuzz.go (about)

     1  // +build gofuzz
     2  
     3  package bitutil
     4  
     5  import "bytes"
     6  
     7  // Fuzz implements a go-fuzz fuzzer method to test various encoding method
     8  // invocations.
     9  func Fuzz(data []byte) int {
    10  	if len(data) == 0 {
    11  		return -1
    12  	}
    13  	if data[0]%2 == 0 {
    14  		return fuzzEncode(data[1:])
    15  	}
    16  	return fuzzDecode(data[1:])
    17  }
    18  
    19  // fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and
    20  // decoding algorithm.
    21  func fuzzEncode(data []byte) int {
    22  	proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
    23  	if !bytes.Equal(data, proc) {
    24  		panic("content mismatch")
    25  	}
    26  	return 0
    27  }
    28  
    29  // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
    30  // reencoding algorithm.
    31  func fuzzDecode(data []byte) int {
    32  	blob, err := bitsetDecodeBytes(data, 1024)
    33  	if err != nil {
    34  		return 0
    35  	}
    36  	if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
    37  		panic("content mismatch")
    38  	}
    39  	return 0
    40  }