Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fuzzing #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata
2 changes: 1 addition & 1 deletion examples/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestStrings(t *testing.T) {

func TestMax3Quick(t *testing.T) {
expectError(t, func(t quickcheck.TestingT) {
quickcheck.Run(t, quickcheck.Config{}, func(t statefulTest.T) {
quickcheck.Run(t, quickcheck.Config{NumberOfRuns: 1000}, func(t statefulTest.T) {
x := pick.Val(t, generator.Int())
y := pick.Val(t, generator.Int())
z := pick.Val(t, generator.Int())
Expand Down
88 changes: 88 additions & 0 deletions examples/fuzzcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package examples

import (
"github.com/peterzeller/go-stateful-test/fuzzcheck"
"github.com/peterzeller/go-stateful-test/generator"
"github.com/peterzeller/go-stateful-test/pick"
"github.com/peterzeller/go-stateful-test/statefulTest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"sync"
"testing"
)

// FuzzMax3Quick tests the buggy max3 function with fuzzing.
func FuzzMax3Quick(t *testing.F) {
// We skip the test, because it would find the error and I could not figure out how to write a fuzz test that expects an error
t.SkipNow()
fuzzcheck.Run(t, fuzzcheck.Config{}, func(t statefulTest.T) {
x := pick.Val(t, generator.Int())
y := pick.Val(t, generator.Int())
z := pick.Val(t, generator.Int())
res := max3(x, y, z)
t.Logf("min3(%d, %d, %d) = %d", x, y, z, res)
assert.True(t, res >= x, "res >= x")
assert.True(t, res >= y, "res >= y")
assert.True(t, res >= z, "res >= z")
})
}

var mut sync.Mutex

// AppendStringToFile appends the given text to the file specified by filename.
// If the file does not exist, it will be created.
func appendStringToFile(filename, text string) (err error) {
mut.Lock()
defer mut.Unlock()
// Open the file in append mode. If it doesn't exist, create it with permissions 0644.
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func() {
err = file.Close()
}()

// Write the text to the file
_, err = file.WriteString(text)
if err != nil {
return err
}

return nil
}

// FuzzComplexIfs tests code with some complex branching logic.
// You need exactly the right value 5 times to get to the bug.
// Both quickcheck and smallcheck would fail to find this bug, but coverage guided fuzzing finds it.
func FuzzComplexIfs(t *testing.F) {
// We skip the test, because it would find the error and I could not figure out how to write a fuzz test that expects an error
t.SkipNow()
fuzzcheck.Run(t, fuzzcheck.Config{DisableHeuristics: true}, func(t statefulTest.T) {
a := pick.Val(t, generator.IntRange(0, 20))
b := pick.Val(t, generator.IntRange(0, 20))
c := pick.Val(t, generator.IntRange(0, 20))
d := pick.Val(t, generator.IntRange(0, 20))
e := pick.Val(t, generator.IntRange(0, 20))
f := pick.Val(t, generator.IntRange(0, 20))
t.Logf("%d, %d, %d, %d, %d, %d\n", a, b, c, d, e, f)
//require.NoError(t, appendStringToFile("log.txt", fmt.Sprintf("%d, %d, %d, %d, %d, %d\n", a, b, c, d, e, f)))
if a != 11 {
return
}
if b != 12 {
return
}
if c != 13 {
return
}
if d != 14 {
return
}
if e != 15 {
return
}
require.NotEqual(t, 16, f)
})
}
33 changes: 33 additions & 0 deletions fuzzcheck/fuzzcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fuzzcheck

import (
"github.com/peterzeller/go-stateful-test/quickcheck"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"github.com/peterzeller/go-stateful-test/statefulTest"
"testing"
"time"
)

type Config struct {
MaxShrinkDuration time.Duration
PrintAllLogs bool
DisableHeuristics bool
}

func Run(t TestingT, cfg Config, f func(t statefulTest.T)) {
t.Fuzz(func(t *testing.T, generatorString []byte) {
quickcheck.Run(t, quickcheck.Config{
// only one run, because the fuzzing framework controls the runs
NumberOfRuns: 1,
MaxShrinkDuration: cfg.MaxShrinkDuration,
PrintAllLogs: cfg.PrintAllLogs,
FixedRandomSource: randomsource.RandomSourceFromBytes(generatorString),
DisableHeuristics: cfg.DisableHeuristics,
}, f)
})
}

type TestingT interface {
quickcheck.TestingT
Fuzz(f any)
}
6 changes: 4 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/peterzeller/go-fun/iterable"
"github.com/peterzeller/go-fun/zero"
"github.com/peterzeller/go-stateful-test/generator/geniterable"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math/big"
"math/rand"
)

// Generator is an interface for generating values of type T with internal value representation R.
Expand Down Expand Up @@ -122,7 +122,9 @@ type Rand interface {
// HasMore to generate sequences of elements and if there are more elements
HasMore() bool
// R is the underlying random number generator
R() *rand.Rand
R() randomsource.RandomStream
// UseHeuristics is true when the search should use heuristics to find elements. For example try small numbers first.
UseHeuristics() bool
}

func ShrinkValues[T any](gen Generator[T, T], v T) iterable.Iterable[T] {
Expand Down
22 changes: 15 additions & 7 deletions generator/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"github.com/peterzeller/go-fun/iterable"
"github.com/peterzeller/go-stateful-test/generator/geniterable"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math"
"math/big"
)
Expand Down Expand Up @@ -31,8 +32,15 @@ func (g genInt64) Name() string {
}

func (g genInt64) Random(rnd Rand, size int) int64 {
r := rnd.R()
p := r.Float64()
if !rnd.UseHeuristics() {
interval := g.max - g.min
bitSize := findMSBPosition(uint64(interval))
i := randomsource.Int64B(rnd.R(), 1+(bitSize-1)/8)
i = i % interval
i = i + g.min
return i
}
p := randomsource.Float64(rnd.R())
n := 1 + g.max - g.min
switch {
// higher probability for 0
Expand All @@ -45,21 +53,21 @@ func (g genInt64) Random(rnd Rand, size int) int64 {
return (g.max)
case p < 0.8 && g.min <= 0 && 0 < g.max:
// normal distribution around 0
res := int64(math.Abs(r.NormFloat64()) * 3)
res := int64(math.Abs(randomsource.NormFloat64(rnd.R())) * 3)
if res > g.max {
res = g.max
}
return (res)
default:
if n > 0 {
// uniform distribution
return (g.min + r.Int63n(n))
return g.min + randomsource.Int64N(rnd.R(), n)
}
if r.Float64() < 0.1 {
if randomsource.Float64(rnd.R()) < 0.1 {
// negative number
return (-1 + r.Int63n(-1+g.min))
return -1 + randomsource.Int64N(rnd.R(), -1+g.min)
}
return (1 + r.Int63n(g.max))
return 1 + randomsource.Int64N(rnd.R(), g.max)
}
}

Expand Down
5 changes: 3 additions & 2 deletions generator/oneOf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"github.com/peterzeller/go-fun/iterable"
"github.com/peterzeller/go-stateful-test/generator/geniterable"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math/big"

"github.com/peterzeller/go-fun/equality"
Expand All @@ -17,7 +18,7 @@ func OneOf[T, R any](gs ...Generator[T, R]) Generator[T, OneOfRandom[R]] {
return &AnonGenerator[T, OneOfRandom[R]]{
GenName: "OneOf",
GenRandom: func(rnd Rand, size int) OneOfRandom[R] {
n := rnd.R().Intn(len(gs))
n := randomsource.IntN(rnd.R(), len(gs))
g := gs[n]
return OneOfRandom[R]{
generator: n,
Expand Down Expand Up @@ -78,7 +79,7 @@ func OneConstantOf[T comparable](values ...T) Generator[T, T] {
return &AnonGenerator[T, T]{
GenName: "OneConstantOf",
GenRandom: func(rnd Rand, size int) T {
n := rnd.R().Intn(len(values))
n := randomsource.IntN(rnd.R(), len(values))
g := values[n]
return g
},
Expand Down
9 changes: 7 additions & 2 deletions generator/rand_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math/rand"
)

Expand All @@ -15,6 +16,10 @@ type testRand struct {
rnd *rand.Rand
}

func (r *testRand) UseHeuristics() bool {
return true
}

func (r *testRand) Fork(name string) Rand {
return r
}
Expand All @@ -23,6 +28,6 @@ func (r *testRand) HasMore() bool {
}

// R is the underlying random number generator
func (r *testRand) R() *rand.Rand {
return r.rnd
func (r *testRand) R() randomsource.RandomStream {
return randomsource.FromSeed(r.rnd.Int63()).Iterator()
}
3 changes: 2 additions & 1 deletion generator/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/peterzeller/go-fun/iterable"
"github.com/peterzeller/go-stateful-test/generator/geniterable"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math/big"

"github.com/peterzeller/go-fun/hash"
Expand Down Expand Up @@ -63,7 +64,7 @@ func enumerateSets[T any](elems *linked.List[T], h hash.EqHash[T]) geniterable.I

// Random implements Generator
func (s *setGenerator[T, RT]) Random(rnd Rand, size int) hashset.Set[RT] {
n := rnd.R().Intn(size)
n := randomsource.IntN(rnd.R(), size)
set := hashset.New(s.rvHash())
for i := 0; i < n; i++ {
set = set.Add(s.gen.Random(rnd, size))
Expand Down
5 changes: 3 additions & 2 deletions generator/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/peterzeller/go-fun/iterable"
"github.com/peterzeller/go-stateful-test/generator/geniterable"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math/big"

"github.com/peterzeller/go-fun/equality"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (s *sliceGen[T, TR]) Random(rnd Rand, size int) []TR {
if size <= 0 {
return []TR{}
}
l := rnd.R().Intn(size)
l := randomsource.IntN(rnd.R(), size)
res := make([]TR, l)
for i := range res {
res[i] = s.elemGen.Random(rnd, size-1)
Expand Down Expand Up @@ -168,7 +169,7 @@ func (s *sliceDistinctGen[T, TR]) Random(rnd Rand, size int) []TR {
if size <= 0 {
return []TR{}
}
l := rnd.R().Intn(size)
l := randomsource.IntN(rnd.R(), size)
res := make([]TR, 0, l)
resValues := make([]T, 0, l)
for i := 0; i < l; i++ {
Expand Down
2 changes: 1 addition & 1 deletion generator/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestSliceRandom(t *testing.T) {
t.Logf("rv = %+v", rv)
v, ok := g.RValue(rv)
require.True(t, ok)
require.Equal(t, []int{3, 5, 1, 3, 2, 4}, v)
require.Equal(t, []int{4, 2, 2, 5, 4, 4, 1, 3, 1}, v)
}

func TestSliceShrink(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions generator/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/peterzeller/go-fun/slice"
"github.com/peterzeller/go-stateful-test/generator/geniterable"
"github.com/peterzeller/go-stateful-test/generator/shrink"
"github.com/peterzeller/go-stateful-test/quickcheck/randomsource"
"math/big"
"strings"
)
Expand All @@ -29,11 +30,10 @@ func (g genString) Name() string {
}

func (g genString) Random(rnd Rand, size int) string {
r := rnd.R()
length := r.Intn(size + 1)
length := randomsource.IntN(rnd.R(), size+1)
var s strings.Builder
for i := 0; i < length; i++ {
s.WriteRune(g.chars[r.Intn(len(g.chars))])
s.WriteRune(g.chars[randomsource.IntN(rnd.R(), len(g.chars))])
}
return (s.String())
}
Expand Down
Loading
Loading