Skip to content

Commit

Permalink
Move fuzz tests to standalone test file
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmccluskey committed May 12, 2022
1 parent 4a019a2 commit 5816bc8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 104 deletions.
20 changes: 0 additions & 20 deletions sdks/go/pkg/beam/core/graph/coder/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,3 @@ func TestEncodeDecodeBytes(t *testing.T) {
})
}
}

func FuzzEncodeDecodeBytes(f *testing.F) {
f.Add([]byte{10})
f.Fuzz(func(t *testing.T, b []byte) {
var buf bytes.Buffer
err := EncodeBytes(b, &buf)
if err != nil {
return
}

got, err := DecodeBytes(&buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if d := cmp.Diff(got, b); d != "" {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}
129 changes: 129 additions & 0 deletions sdks/go/pkg/beam/core/graph/coder/coder_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package coder

import (
"bytes"
"math"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func FuzzEncodeDecodeBytes(f *testing.F) {
f.Add([]byte{10})
f.Fuzz(func(t *testing.T, b []byte) {
var buf bytes.Buffer
err := EncodeBytes(b, &buf)
if err != nil {
return
}

got, err := DecodeBytes(&buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if d := cmp.Diff(got, b); d != "" {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}

const floatPrecision = float64(0.001)

func FuzzEncodeDecodeDouble(f *testing.F) {
f.Add(float64(3.141))
f.Fuzz(func(t *testing.T, a float64) {
var buf bytes.Buffer
err := EncodeDouble(a, &buf)
if err != nil {
return
}

actual, err := DecodeDouble(&buf)
if err != nil {
t.Fatalf("DecodeDouble(%v) failed: %v", buf, err)
}
if math.Abs(actual-a) > floatPrecision {
t.Fatalf("got %f, want %f +/- %f", actual, a, floatPrecision)
}
})
}

func FuzzEncodeDecodeUInt64(f *testing.F) {
f.Add(uint64(42))
f.Fuzz(func(t *testing.T, b uint64) {
var buf bytes.Buffer
err := EncodeUint64(b, &buf)
if err != nil {
return
}

got, err := DecodeUint64(&buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if got != b {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}

func FuzzEncodeDecodeInt32(f *testing.F) {
f.Add(int32(42))
f.Fuzz(func(t *testing.T, b int32) {
var buf bytes.Buffer
err := EncodeInt32(b, &buf)
if err != nil {
return
}

got, err := DecodeInt32(&buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if got != b {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}

func FuzzEncodeDecodeStringUTF8LP(f *testing.F) {
for _, s := range testValues {
f.Add(s)
}
f.Fuzz(func(t *testing.T, b string) {
var build strings.Builder
err := EncodeStringUTF8(b, &build)
if err != nil {
return
}

buf := bytes.NewBufferString(build.String())
got, err := DecodeStringUTF8(buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if got != b {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}
21 changes: 0 additions & 21 deletions sdks/go/pkg/beam/core/graph/coder/double_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,3 @@ func TestEncodeDecodeDouble(t *testing.T) {
}
}
}

const floatPrecision = float64(0.001)

func FuzzEncodeDecodeDouble(f *testing.F) {
f.Add(float64(3.141))
f.Fuzz(func(t *testing.T, a float64) {
var buf bytes.Buffer
err := EncodeDouble(a, &buf)
if err != nil {
return
}

actual, err := DecodeDouble(&buf)
if err != nil {
t.Fatalf("DecodeDouble(%v) failed: %v", buf, err)
}
if math.Abs(actual-a) > floatPrecision {
t.Fatalf("got %f, want %f +/- %f", actual, a, floatPrecision)
}
})
}
40 changes: 0 additions & 40 deletions sdks/go/pkg/beam/core/graph/coder/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,3 @@ func TestEncodeDecodeInt32(t *testing.T) {
}
}
}

func FuzzEncodeDecodeUInt64(f *testing.F) {
f.Add(uint64(42))
f.Fuzz(func(t *testing.T, b uint64) {
var buf bytes.Buffer
err := EncodeUint64(b, &buf)
if err != nil {
return
}

got, err := DecodeUint64(&buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if got != b {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}

func FuzzEncodeDecodeInt32(f *testing.F) {
f.Add(int32(42))
f.Fuzz(func(t *testing.T, b int32) {
var buf bytes.Buffer
err := EncodeInt32(b, &buf)
if err != nil {
return
}

got, err := DecodeInt32(&buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if got != b {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}
23 changes: 0 additions & 23 deletions sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,3 @@ func TestEncodeDecodeStringUTF8LP(t *testing.T) {
})
}
}

func FuzzEncodeDecodeStringUTF8LP(f *testing.F) {
for _, s := range testValues {
f.Add(s)
}
f.Fuzz(func(t *testing.T, b string) {
var build strings.Builder
err := EncodeStringUTF8(b, &build)
if err != nil {
return
}

buf := bytes.NewBufferString(build.String())
got, err := DecodeStringUTF8(buf)
if err != nil {
t.Fatalf("failed to decode bytes, got %v", err)
}

if got != b {
t.Errorf("decoded output does not match input: got %v, want %v", got, b)
}
})
}

0 comments on commit 5816bc8

Please sign in to comment.