-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These didn't uncover any issues, so I'm not sure if there's value in keeping them or not.
- Loading branch information
1 parent
33eb805
commit 9a60b74
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,25 @@ func TestBe32(t *testing.T) { | |
} | ||
} | ||
|
||
func FuzzBe32(f *testing.F) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
56quarters
Contributor
|
||
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, | ||
|
@@ -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) | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 { | ||
|
@56quarters looking at these fuzz tests with fresh eyes, I'm not sure they're adding much value - what do you think?