Skip to content

Commit

Permalink
tests: Add CICD Fuzz testing (#763)
Browse files Browse the repository at this point in the history
Run mutating fuzz tests.
Expand zip fuzz tests and limit memory usage somewhat.
  • Loading branch information
klauspost authored Mar 1, 2023
1 parent aeed811 commit 421a587
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 8 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,91 @@ jobs:
GOAMD64: v4
shell: bash {0}
run: go test . -test.run=None;if [ $? -eq 0 ]; then go test ./s2/...; else true; fi

fuzz-s2:
env:
CGO_ENABLED: 0
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v2

- name: S2/FuzzDictBlocks
run: go test -run=none -fuzz=FuzzDictBlocks -fuzztime=100000x -test.fuzzminimizetime=10ms ./s2/.

- name: S2/FuzzEncodingBlocks
run: go test -run=none -fuzz=FuzzEncodingBlocks -fuzztime=500000x -test.fuzzminimizetime=10ms ./s2/.

- name: S2/FuzzLZ4Block
run: go test -run=none -fuzz=FuzzLZ4Block -fuzztime=500000x -test.fuzzminimizetime=10ms ./s2/.

- name: S2/FuzzDictBlocks/noasm
run: go test -tags=noasm -run=none -fuzz=FuzzDictBlocks -fuzztime=100000x -test.fuzzminimizetime=10ms ./s2/.

- name: S2/FuzzEncodingBlocks/noasm
run: go test -tags=noasm -run=none -fuzz=FuzzEncodingBlocks -fuzztime=500000x -test.fuzzminimizetime=10ms ./s2/.

- name: S2/FuzzLZ4Block/noasm
run: go test -tags=noasm -run=none -fuzz=FuzzLZ4Block -fuzztime=500000x -test.fuzzminimizetime=10ms ./s2/.

fuzz-zstd:
env:
CGO_ENABLED: 0
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v2

- name: zstd/FuzzDecodeAll
run: go test -run=none -fuzz=FuzzDecodeAll -fuzztime=500000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzDecAllNoBMI2
run: go test -run=none -fuzz=FuzzDecAllNoBMI2 -fuzztime=500000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzDecoder
run: go test -run=none -fuzz=FuzzDecoder -fuzztime=500000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzNoBMI2Dec
run: go test -run=none -fuzz=FuzzNoBMI2Dec -fuzztime=500000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzEncoding
run: go test -run=none -fuzz=FuzzEncoding -fuzztime=250000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzDecodeAll/noasm
run: go test -tags=noasm -run=none -fuzz=FuzzDecodeAll -fuzztime=500000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzDecoder/noasm
run: go test -tags=noasm -run=none -fuzz=FuzzDecoder -fuzztime=500000x -test.fuzzminimizetime=10ms ./zstd/.

- name: zstd/FuzzEncoding/noasm
run: go test -tags=noasm -run=none -fuzz=FuzzEncoding -fuzztime=250000x -test.fuzzminimizetime=10ms ./zstd/.

fuzz-other:
env:
CGO_ENABLED: 0
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v2

- name: flate/FuzzEncoding
run: go test -run=none -fuzz=FuzzEncoding -fuzztime=100000x -test.fuzzminimizetime=10ms ./flate/.

- name: zip/FuzzReader
run: go test -run=none -fuzz=FuzzReader -fuzztime=500000x -test.fuzzminimizetime=10ms ./zip/.

26 changes: 18 additions & 8 deletions zip/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build go1.18
// +build go1.18

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -12,7 +9,10 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"

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

func FuzzReader(f *testing.F) {
Expand All @@ -30,6 +30,8 @@ func FuzzReader(f *testing.F) {
}
f.Add(b)
}
fuzz.AddFromZip(f, "testdata/FuzzReader-raw.zip", true, testing.Short())
fuzz.AddFromZip(f, "testdata/FuzzReader-enc.zip", false, testing.Short())

f.Fuzz(func(t *testing.T, b []byte) {
r, err := NewReader(bytes.NewReader(b), int64(len(b)))
Expand All @@ -41,17 +43,23 @@ func FuzzReader(f *testing.F) {
header *FileHeader
content []byte
}
files := []file{}
files := make([]file, 0, len(r.File))

for _, f := range r.File {
for i, f := range r.File {
fr, err := f.Open()
if err != nil {
continue
}
content, err := io.ReadAll(fr)
// No more than 1MiB per file
limit := int64(1 << 20)
if i < 100 {
limit = 10
}
content, err := io.ReadAll(io.LimitReader(fr, limit))
if err != nil {
continue
}

files = append(files, file{header: &f.FileHeader, content: content})
if _, err := r.Open(f.Name); err != nil {
continue
Expand All @@ -70,8 +78,10 @@ func FuzzReader(f *testing.F) {
if err != nil {
t.Fatalf("unable to write previously parsed header: %s", err)
}
if _, err := ww.Write(f.content); err != nil {
t.Fatalf("unable to write previously parsed content: %s", err)
if !strings.HasSuffix(f.header.Name, "/") {
if _, err := ww.Write(f.content); err != nil {
t.Fatalf("unable to write previously parsed content: %s", err)
}
}
}

Expand Down
Binary file added zip/testdata/FuzzReader-enc.zip
Binary file not shown.
Binary file added zip/testdata/FuzzReader-raw.zip
Binary file not shown.
Binary file modified zstd/testdata/fuzz/encode-corpus-encoded.zip
Binary file not shown.

0 comments on commit 421a587

Please sign in to comment.