60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
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.SimplePacket(testContent),
|
|
}
|
|
|
|
t.Run("Length", func(t *testing.T) {
|
|
marshalled := testPacket.Marshal()
|
|
|
|
// 12 header + 8 timestamp
|
|
assert.Len(t, marshalled, len(testContent)+12)
|
|
})
|
|
}
|
|
|
|
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.SimplePacket(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))
|
|
})
|
|
}
|