aboutsummaryrefslogtreecommitdiffstats
path: root/common/bitutil/compress_fuzz.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-05-06 23:35:59 +0800
committerPéter Szilágyi <peterke@gmail.com>2017-05-07 00:06:17 +0800
commitcf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb (patch)
treef6ca01371745603c7921713853fab601de44ca4d /common/bitutil/compress_fuzz.go
parentfd5d51c9ae3256a1f24cf974dcd02433a259677e (diff)
downloadgo-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.tar
go-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.tar.gz
go-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.tar.bz2
go-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.tar.lz
go-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.tar.xz
go-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.tar.zst
go-tangerine-cf19586cfbe5aa379c8fdb046dc5a8c0fa1cebbb.zip
common/bitutil: fix decompression corner cases; fuzz, test & bench
Diffstat (limited to 'common/bitutil/compress_fuzz.go')
-rw-r--r--common/bitutil/compress_fuzz.go56
1 files changed, 56 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+
+// +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
+}