mess around with fuzzing

This commit is contained in:
Joel Wetzell
2026-05-23 07:41:52 -05:00
parent acb6708b80
commit cf2d3a2ae0
+20 -6
View File
@@ -9,12 +9,12 @@ import (
func TestMessageDecoding(t *testing.T) { func TestMessageDecoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
bytes []byte bytes [29]byte
expected FreeDPosition expected FreeDPosition
}{ }{
{ {
description: "simple position message", description: "simple position message",
bytes: []byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff, bytes: [29]byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff,
0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50, 0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50,
}, },
expected: FreeDPosition{ expected: FreeDPosition{
@@ -48,15 +48,15 @@ func TestMessageDecoding(t *testing.T) {
} }
} }
func TestMessageEncoding(t *testing.T) { func TestGoodMessageEncoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
expected []byte expected [29]byte
message FreeDPosition message FreeDPosition
}{ }{
{ {
description: "simple position message", description: "simple position message",
expected: []byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff, expected: [29]byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff,
0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50, 0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50,
}, },
message: FreeDPosition{ message: FreeDPosition{
@@ -86,7 +86,7 @@ func TestMessageEncoding(t *testing.T) {
} }
func BenchmarkMessageDecoding(b *testing.B) { func BenchmarkMessageDecoding(b *testing.B) {
data := []byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff, data := [29]byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff,
0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50, 0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50,
} }
for b.Loop() { for b.Loop() {
@@ -110,3 +110,17 @@ func BenchmarkMessageEncoding(b *testing.B) {
Encode(message) Encode(message)
} }
} }
func FuzzMessageDecoding(f *testing.F) {
f.Add([]byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff,
0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50,
})
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) != 29 {
return
}
var dataArray [29]byte
copy(dataArray[:], data)
Decode(dataArray)
})
}