github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/bitutil/compress_fuzz.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // +build gofuzz
    13  
    14  package bitutil
    15  
    16  import "bytes"
    17  
    18  // Fuzz implements a go-fuzz fuzzer method to test various encoding method
    19  // invocations.
    20  func Fuzz(data []byte) int {
    21  	if len(data) == 0 {
    22  		return -1
    23  	}
    24  	if data[0]%2 == 0 {
    25  		return fuzzEncode(data[1:])
    26  	}
    27  	return fuzzDecode(data[1:])
    28  }
    29  
    30  // fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and
    31  // decoding algorithm.
    32  func fuzzEncode(data []byte) int {
    33  	proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
    34  	if !bytes.Equal(data, proc) {
    35  		panic("content mismatch")
    36  	}
    37  	return 0
    38  }
    39  
    40  // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
    41  // reencoding algorithm.
    42  func fuzzDecode(data []byte) int {
    43  	blob, err := bitsetDecodeBytes(data, 1024)
    44  	if err != nil {
    45  		return 0
    46  	}
    47  	if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
    48  		panic("content mismatch")
    49  	}
    50  	return 0
    51  }