From 4af6d0988077b30b830471b49c800a04408895bc Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Mon, 9 May 2022 10:38:35 -0400 Subject: [PATCH 1/5] UTF8 fuzz test --- .../beam/core/graph/coder/stringutf8_test.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go b/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go index 2170a2851340f..40cd7868f01cf 100644 --- a/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go @@ -120,3 +120,26 @@ 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) + } + }) +} From 7a88b9c787396a57f52f1c852985394ac75d23d7 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Mon, 9 May 2022 10:39:31 -0400 Subject: [PATCH 2/5] Ints fuzz tests --- sdks/go/pkg/beam/core/graph/coder/int_test.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/sdks/go/pkg/beam/core/graph/coder/int_test.go b/sdks/go/pkg/beam/core/graph/coder/int_test.go index 25dc681710d0a..682f9161a0da2 100644 --- a/sdks/go/pkg/beam/core/graph/coder/int_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/int_test.go @@ -86,3 +86,43 @@ 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) + } + }) +} From 7f6670cd085dd980a027240b949b6847530c841b Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Mon, 9 May 2022 10:41:41 -0400 Subject: [PATCH 3/5] Bytes fuzz tests --- .../pkg/beam/core/graph/coder/bytes_test.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sdks/go/pkg/beam/core/graph/coder/bytes_test.go b/sdks/go/pkg/beam/core/graph/coder/bytes_test.go index 0d7d1a79a3fbe..7a941d216013f 100644 --- a/sdks/go/pkg/beam/core/graph/coder/bytes_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/bytes_test.go @@ -59,3 +59,23 @@ 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) + } + }) +} From 4a019a2f4aadcd01000e825869f1b8dcd6b4ff47 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Mon, 9 May 2022 10:58:02 -0400 Subject: [PATCH 4/5] Double fuzz test --- .../pkg/beam/core/graph/coder/double_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/sdks/go/pkg/beam/core/graph/coder/double_test.go b/sdks/go/pkg/beam/core/graph/coder/double_test.go index 2fbb7c58183cd..fa22956e9fa98 100644 --- a/sdks/go/pkg/beam/core/graph/coder/double_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/double_test.go @@ -51,3 +51,24 @@ 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) + } + }) +} From 5816bc817f4c7bcad85f699bf19bf19faa55d339 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 May 2022 10:34:25 -0400 Subject: [PATCH 5/5] Move fuzz tests to standalone test file --- .../pkg/beam/core/graph/coder/bytes_test.go | 20 --- .../beam/core/graph/coder/coder_fuzz_test.go | 129 ++++++++++++++++++ .../pkg/beam/core/graph/coder/double_test.go | 21 --- sdks/go/pkg/beam/core/graph/coder/int_test.go | 40 ------ .../beam/core/graph/coder/stringutf8_test.go | 23 ---- 5 files changed, 129 insertions(+), 104 deletions(-) create mode 100644 sdks/go/pkg/beam/core/graph/coder/coder_fuzz_test.go diff --git a/sdks/go/pkg/beam/core/graph/coder/bytes_test.go b/sdks/go/pkg/beam/core/graph/coder/bytes_test.go index 7a941d216013f..0d7d1a79a3fbe 100644 --- a/sdks/go/pkg/beam/core/graph/coder/bytes_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/bytes_test.go @@ -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) - } - }) -} diff --git a/sdks/go/pkg/beam/core/graph/coder/coder_fuzz_test.go b/sdks/go/pkg/beam/core/graph/coder/coder_fuzz_test.go new file mode 100644 index 0000000000000..11c516f73782f --- /dev/null +++ b/sdks/go/pkg/beam/core/graph/coder/coder_fuzz_test.go @@ -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) + } + }) +} diff --git a/sdks/go/pkg/beam/core/graph/coder/double_test.go b/sdks/go/pkg/beam/core/graph/coder/double_test.go index fa22956e9fa98..2fbb7c58183cd 100644 --- a/sdks/go/pkg/beam/core/graph/coder/double_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/double_test.go @@ -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) - } - }) -} diff --git a/sdks/go/pkg/beam/core/graph/coder/int_test.go b/sdks/go/pkg/beam/core/graph/coder/int_test.go index 682f9161a0da2..25dc681710d0a 100644 --- a/sdks/go/pkg/beam/core/graph/coder/int_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/int_test.go @@ -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) - } - }) -} diff --git a/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go b/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go index 40cd7868f01cf..2170a2851340f 100644 --- a/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go +++ b/sdks/go/pkg/beam/core/graph/coder/stringutf8_test.go @@ -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) - } - }) -}