Skip to content

Commit

Permalink
Add fuzz tests for Decbuf.
Browse files Browse the repository at this point in the history
These didn't uncover any issues, so I'm not sure if there's value in
keeping them or not.
  • Loading branch information
charleskorn committed Nov 24, 2022
1 parent 33eb805 commit 9a60b74
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions pkg/storegateway/indexheader/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ func TestBe32(t *testing.T) {
}
}

func FuzzBe32(f *testing.F) {

This comment has been minimized.

Copy link
@charleskorn

charleskorn Nov 28, 2022

Author Contributor

@56quarters looking at these fuzz tests with fresh eyes, I'm not sure they're adding much value - what do you think?

This comment has been minimized.

Copy link
@56quarters

56quarters Nov 29, 2022

Contributor

I'm not very familiar with how fuzz tests work in go. Offhand, a binary parser seems like a good fit for them but I'll defer to you. We can always add them later if needed.

f.Add(uint32(0))
f.Add(uint32(1))
f.Add(uint32(0xFFFF_FFFF))

f.Fuzz(func(t *testing.T, n uint32) {
enc := prom_encoding.Encbuf{}
enc.PutBE32(n)

dec := NewDecbufRaw(realByteSlice(enc.Get()))
require.Equal(t, 4, dec.Len())

actual := dec.Be32()
require.NoError(t, dec.Err())
require.Equal(t, n, actual)
require.Equal(t, 0, dec.Len())
})
}

func TestBe32int(t *testing.T) {
cases := []int{
0,
Expand All @@ -56,6 +75,29 @@ func TestBe32int(t *testing.T) {
}
}

func FuzzBe32int(f *testing.F) {
f.Add(0)
f.Add(1)
f.Add(0xFFFF_FFFF)

f.Fuzz(func(t *testing.T, n int) {
if n < 0 || n > 0xFFFF_FFFF {
t.Skip()
}

enc := prom_encoding.Encbuf{}
enc.PutBE32int(n)

dec := NewDecbufRaw(realByteSlice(enc.Get()))
require.Equal(t, 4, dec.Len())

actual := dec.Be32int()
require.NoError(t, dec.Err())
require.Equal(t, n, actual)
require.Equal(t, 0, dec.Len())
})
}

func TestSkip(t *testing.T) {
expected := uint32(0x12345678)

Expand Down Expand Up @@ -104,6 +146,27 @@ func TestUvarint(t *testing.T) {
}
}

func FuzzUvarint(f *testing.F) {
f.Add(0)
f.Add(1)
f.Add(0xFFFF_FFFF)

f.Fuzz(func(t *testing.T, n int) {
if n < 0 {
t.Skip()
}

enc := prom_encoding.Encbuf{}
enc.PutUvarint(n)

dec := NewDecbufRaw(realByteSlice(enc.Get()))
actual := dec.Uvarint()
require.NoError(t, dec.Err())
require.Equal(t, n, actual)
require.Equal(t, 0, dec.Len())
})
}

func TestUvarint64(t *testing.T) {
cases := []struct {
value uint64
Expand Down Expand Up @@ -133,6 +196,26 @@ func TestUvarint64(t *testing.T) {
}
}

func FuzzUvarint64(f *testing.F) {
f.Add(uint64(0))
f.Add(uint64(1))
f.Add(uint64(127))
f.Add(uint64(128))
f.Add(uint64(0xFFFF_FFFF))
f.Add(uint64(0xFFFF_FFFF_FFFF_FFFF))

f.Fuzz(func(t *testing.T, n uint64) {
enc := prom_encoding.Encbuf{}
enc.PutUvarint64(n)

dec := NewDecbufRaw(realByteSlice(enc.Get()))
actual := dec.Uvarint64()
require.NoError(t, dec.Err())
require.Equal(t, n, actual)
require.Equal(t, 0, dec.Len())
})
}

func TestUvarintBytes(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -161,6 +244,24 @@ func TestUvarintBytes(t *testing.T) {
}
}

func FuzzUvarintBytes(f *testing.F) {
f.Add([]byte{})
f.Add([]byte{0x12})
f.Add([]byte("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567"))
f.Add([]byte("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"))

f.Fuzz(func(t *testing.T, b []byte) {
enc := prom_encoding.Encbuf{}
enc.PutUvarintBytes(b)

dec := NewDecbufRaw(realByteSlice(enc.Get()))
actual := dec.UvarintBytes()
require.NoError(t, dec.Err())
require.Equal(t, b, actual)
require.Equal(t, 0, dec.Len())
})
}

func TestUvarintString(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -189,6 +290,24 @@ func TestUvarintString(t *testing.T) {
}
}

func FuzzUvarintString(f *testing.F) {
f.Add("")
f.Add("a")
f.Add("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567")
f.Add("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678")

f.Fuzz(func(t *testing.T, s string) {
enc := prom_encoding.Encbuf{}
enc.PutUvarintStr(s)

dec := NewDecbufRaw(realByteSlice(enc.Get()))
actual := dec.UvarintStr()
require.NoError(t, dec.Err())
require.Equal(t, s, actual)
require.Equal(t, 0, dec.Len())
})
}

type realByteSlice []byte

func (b realByteSlice) Len() int {
Expand Down

0 comments on commit 9a60b74

Please sign in to comment.