From 97488efed7a6e9d294895989cbf557603b0b433f Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 22 May 2023 15:27:06 +1000 Subject: [PATCH] Add holepunch message fuzzing --- .../ut-holepunch/ut-holepunch_test.go | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 peer_protocol/ut-holepunch/ut-holepunch_test.go diff --git a/peer_protocol/ut-holepunch/ut-holepunch_test.go b/peer_protocol/ut-holepunch/ut-holepunch_test.go new file mode 100644 index 00000000..7221e1f7 --- /dev/null +++ b/peer_protocol/ut-holepunch/ut-holepunch_test.go @@ -0,0 +1,63 @@ +package utHolepunch + +import ( + "bytes" + "net/netip" + "testing" + + qt "github.com/frankban/quicktest" +) + +var exampleMsgs = []Msg{ + { + MsgType: Rendezvous, + AddrPort: netip.MustParseAddrPort("[1234::1]:42069"), + ErrCode: 16777216, + }, + { + MsgType: Connect, + AddrPort: netip.MustParseAddrPort("1.2.3.4:42069"), + ErrCode: 16777216, + }, +} + +func TestUnmarshalMsg(t *testing.T) { + c := qt.New(t) + for _, m := range exampleMsgs { + b, err := m.MarshalBinary() + c.Assert(err, qt.IsNil) + expectedLen := 24 + if m.AddrPort.Addr().Is4() { + expectedLen = 12 + } + c.Check(b, qt.HasLen, expectedLen) + var um Msg + err = um.UnmarshalBinary(b) + c.Assert(err, qt.IsNil) + c.Check(um, qt.Equals, m) + } +} + +func FuzzMsg(f *testing.F) { + for _, m := range exampleMsgs { + emb, err := m.MarshalBinary() + if err != nil { + f.Fatal(err) + } + f.Add(emb) + } + f.Fuzz(func(t *testing.T, b []byte) { + var m Msg + err := m.UnmarshalBinary(b) + if err != nil { + t.SkipNow() + } + mb, err := m.MarshalBinary() + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(b, mb) { + t.FailNow() + } + }) +} -- 2.48.1