From 4419260cd03e3630c33679b6e96e98b47380a2f7 Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 22 Nov 2023 20:04:53 -0600 Subject: [PATCH] rename TestPacket -> TestPacketEncodeDecode also, verify encoded bytes to a sample --- internal/protocol/packet_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/protocol/packet_test.go b/internal/protocol/packet_test.go index dc690eb..5a1ee41 100644 --- a/internal/protocol/packet_test.go +++ b/internal/protocol/packet_test.go @@ -13,7 +13,17 @@ type TestPacketData struct { C int32 } -func TestPacket(t *testing.T) { +// this is the data we expect to get from encoding the above struct with: +// +// Encode(TestPacketData{ +// A: 1, +// B: 2, +// UTF16Str: "hello world", +// C: 3, +// }) +var testData = [...]byte{0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x77, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00} + +func TestPacketEncodeDecode(t *testing.T) { pkt := NewPacket(&bytes.Buffer{}) if err := pkt.Encode(TestPacketData{ A: 1, @@ -24,12 +34,16 @@ func TestPacket(t *testing.T) { t.Error(err) } + if !bytes.Equal(pkt.readWriter.(*bytes.Buffer).Bytes(), testData[:]) { + t.Error("packet data does not match!") + } + var test TestPacketData if err := pkt.Decode(&test); err != nil { t.Error(err) } if test.A != 1 || test.B != 2 || test.C != 3 || test.UTF16Str != "hello world" { - t.Error("packet data does not match!") + t.Error("decoded packet data does not match!") } }