File: fuzz_test.go

package info (click to toggle)
golang-github-klauspost-compress 1.17.11%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 46,592 kB
  • sloc: asm: 23,155; sh: 72; makefile: 12
file content (46 lines) | stat: -rw-r--r-- 1,004 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fse

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/klauspost/compress/internal/fuzz"
)

func FuzzCompress(f *testing.F) {
	fuzz.AddFromZip(f, "testdata/fse_compress.zip", fuzz.TypeGoFuzz, false)
	f.Fuzz(func(t *testing.T, buf0 []byte) {
		var s, s2 Scratch
		b, err := Compress(buf0, &s)
		if err != nil || b == nil {
			return
		}
		err = s.validateNorm()
		if err != nil {
			return
		}
		//Decompress
		got, err := Decompress(b, &s2)
		if err != nil || len(got) == 0 {
			return
		}
		if !bytes.Equal(buf0, got) {
			t.Fatal(fmt.Sprintln("FuzzCompress output mismatch\n", len(got), "org: \n", len(buf0)))
		}
	})
}

func FuzzDecompress(f *testing.F) {
	// Input is mixed, but TypeGoFuzz will fall back to raw input.
	fuzz.AddFromZip(f, "testdata/fse_decompress.zip", fuzz.TypeGoFuzz, false)
	f.Fuzz(func(t *testing.T, buf0 []byte) {
		var s2 Scratch
		s2.DecompressLimit = 128 << 10
		//Decompress
		got, err := Decompress(buf0, &s2)
		if err != nil || len(got) == 0 {
			return
		}
	})
}