package udp import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "mpbl3p/proxy" "testing" ) func TestPacket_Marshal(t *testing.T) { testContent := []byte("A test string is the content of this packet.") testPacket := Packet{ ack: 18, nack: 29, seq: 431, data: proxy.NewSimplePacket(testContent), } t.Run("Length", func(t *testing.T) { marshalled := testPacket.Marshal() // 12 header + 8 timestamp assert.Len(t, marshalled, len(testContent)+12+8) }) } func TestUnmarshalPacket(t *testing.T) { testContent := []byte("A test string is the content of this packet.") testPacket := Packet{ ack: 18, nack: 29, seq: 431, data: proxy.NewSimplePacket(testContent), } testMarshalled := testPacket.Marshal() t.Run("Length", func(t *testing.T) { p, err := UnmarshalPacket(testMarshalled) require.Nil(t, err) assert.Len(t, p.Contents(), len(testContent)) }) t.Run("Contents", func(t *testing.T) { p, err := UnmarshalPacket(testMarshalled) require.Nil(t, err) assert.Equal(t, p.Contents(), testContent) }) t.Run("Header", func(t *testing.T) { p, err := UnmarshalPacket(testMarshalled) require.Nil(t, err) assert.Equal(t, p.ack, uint32(18)) assert.Equal(t, p.nack, uint32(29)) assert.Equal(t, p.seq, uint32(431)) }) }