github.com/phillinzzz/newBsc@v1.1.6/tests/fuzzers/bitutil/compress_fuzz.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package bitutil 18 19 import ( 20 "bytes" 21 22 "github.com/phillinzzz/newBsc/common/bitutil" 23 ) 24 25 // Fuzz implements a go-fuzz fuzzer method to test various encoding method 26 // invocations. 27 func Fuzz(data []byte) int { 28 if len(data) == 0 { 29 return 0 30 } 31 if data[0]%2 == 0 { 32 return fuzzEncode(data[1:]) 33 } 34 return fuzzDecode(data[1:]) 35 } 36 37 // fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and 38 // decoding algorithm. 39 func fuzzEncode(data []byte) int { 40 proc, _ := bitutil.DecompressBytes(bitutil.CompressBytes(data), len(data)) 41 if !bytes.Equal(data, proc) { 42 panic("content mismatch") 43 } 44 return 1 45 } 46 47 // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and 48 // reencoding algorithm. 49 func fuzzDecode(data []byte) int { 50 blob, err := bitutil.DecompressBytes(data, 1024) 51 if err != nil { 52 return 0 53 } 54 // re-compress it (it's OK if the re-compressed differs from the 55 // original - the first input may not have been compressed at all) 56 comp := bitutil.CompressBytes(blob) 57 if len(comp) > len(blob) { 58 // After compression, it must be smaller or equal 59 panic("bad compression") 60 } 61 // But decompressing it once again should work 62 decomp, err := bitutil.DecompressBytes(data, 1024) 63 if err != nil { 64 panic(err) 65 } 66 if !bytes.Equal(decomp, blob) { 67 panic("content mismatch") 68 } 69 return 1 70 }