From cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Sat, 6 May 2017 18:35:59 +0300 Subject: common/bitutil: fix decompression corner cases; fuzz, test & bench --- common/bitutil/compress_fuzz.go | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 common/bitutil/compress_fuzz.go (limited to 'common/bitutil/compress_fuzz.go') diff --git a/common/bitutil/compress_fuzz.go b/common/bitutil/compress_fuzz.go new file mode 100644 index 000000000..2b7fe2977 --- /dev/null +++ b/common/bitutil/compress_fuzz.go @@ -0,0 +1,56 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// +build gofuzz + +package bitutil + +import "bytes" + +// Fuzz implements a go-fuzz fuzzer method to test various compression method +// invocations. +func Fuzz(data []byte) int { + if len(data) == 0 { + return -1 + } + if data[0]%2 == 0 { + return fuzzCompress(data[1:]) + } + return fuzzDecompress(data[1:]) +} + +// fuzzCompress implements a go-fuzz fuzzer method to test the bit compression and +// decompression algorithm. +func fuzzCompress(data []byte) int { + proc, _ := DecompressBytes(CompressBytes(data), len(data)) + if !bytes.Equal(data, proc) { + panic("content mismatch") + } + return 0 +} + +// fuzzDecompress implements a go-fuzz fuzzer method to test the bit decompression +// and recompression algorithm. +func fuzzDecompress(data []byte) int { + blob, err := DecompressBytes(data, 1024) + if err != nil { + return 0 + } + if comp := CompressBytes(blob); !bytes.Equal(comp, data) { + panic("content mismatch") + } + return 0 +} -- cgit v1.2.3 From 82defe5c5663ca0c28563f8a111d327c87726267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Mon, 8 May 2017 10:40:48 +0300 Subject: common/compress: internalize encoders, add length wrappers --- common/bitutil/compress_fuzz.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'common/bitutil/compress_fuzz.go') diff --git a/common/bitutil/compress_fuzz.go b/common/bitutil/compress_fuzz.go index 2b7fe2977..1b87f50ed 100644 --- a/common/bitutil/compress_fuzz.go +++ b/common/bitutil/compress_fuzz.go @@ -20,36 +20,36 @@ package bitutil import "bytes" -// Fuzz implements a go-fuzz fuzzer method to test various compression method +// Fuzz implements a go-fuzz fuzzer method to test various encoding method // invocations. func Fuzz(data []byte) int { if len(data) == 0 { return -1 } if data[0]%2 == 0 { - return fuzzCompress(data[1:]) + return fuzzEncode(data[1:]) } - return fuzzDecompress(data[1:]) + return fuzzDecode(data[1:]) } -// fuzzCompress implements a go-fuzz fuzzer method to test the bit compression and -// decompression algorithm. -func fuzzCompress(data []byte) int { - proc, _ := DecompressBytes(CompressBytes(data), len(data)) +// fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and +// decoding algorithm. +func fuzzEncode(data []byte) int { + proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data)) if !bytes.Equal(data, proc) { panic("content mismatch") } return 0 } -// fuzzDecompress implements a go-fuzz fuzzer method to test the bit decompression -// and recompression algorithm. -func fuzzDecompress(data []byte) int { - blob, err := DecompressBytes(data, 1024) +// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and +// reencoding algorithm. +func fuzzDecode(data []byte) int { + blob, err := bitsetDecodeBytes(data, 1024) if err != nil { return 0 } - if comp := CompressBytes(blob); !bytes.Equal(comp, data) { + if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) { panic("content mismatch") } return 0 -- cgit v1.2.3