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