go-teeworlds-protocol/protocol7/packet_test.go

263 lines
4.9 KiB
Go
Raw Normal View History

2024-06-20 03:08:52 +00:00
package protocol7
2024-06-02 02:58:09 +00:00
import (
"reflect"
"slices"
"testing"
"github.com/teeworlds-go/teeworlds/chunk7"
"github.com/teeworlds-go/teeworlds/messages7"
2024-06-02 02:58:09 +00:00
)
// update chunk headers
func TestPackUpdateChunkHeaders(t *testing.T) {
// The chunk header is nil by default
packet := Packet{}
packet.Messages = append(packet.Messages, &messages7.SvChat{Message: "foo"})
{
got := packet.Messages[0].Header()
if got != nil {
t.Errorf("got %v, wanted %v", got, nil)
}
}
// When packing the chunk header will be set automatically
// Based on the current context
conn := &Connection{Sequence: 1}
packet.Pack(conn)
{
got := packet.Messages[0].Header()
want := &chunk7.ChunkHeader{
Flags: chunk7.ChunkFlags{
Vital: true,
},
Size: 8,
Seq: 2,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
// When the chunk header is already set
// Packing will only update the size
var chat *messages7.SvChat
var ok bool
if chat, ok = packet.Messages[0].(*messages7.SvChat); ok {
chat.Message = "hello world"
packet.Messages[0] = chat
} else {
t.Fatal("failed to cast chat message")
}
packet.Pack(conn)
{
got := packet.Messages[0].Header()
want := &chunk7.ChunkHeader{
Flags: chunk7.ChunkFlags{
Vital: true,
},
Size: 16,
Seq: 2,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
}
2024-06-17 05:47:33 +00:00
// pack header
func TestPackHeader(t *testing.T) {
header := PacketHeader{
Flags: PacketFlags{
Connless: false,
Compression: false,
Resend: false,
Control: true,
},
Ack: 10,
NumChunks: 0,
Token: [4]byte{0xcf, 0x2e, 0xde, 0x1d},
}
got := header.Pack()
want := []byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
// pack flags
2024-06-02 02:58:09 +00:00
func TestPackFlagsUnset(t *testing.T) {
flags := PacketFlags{
Connless: false,
Compression: false,
Resend: false,
Control: false,
2024-06-02 02:58:09 +00:00
}
got := flags.Pack()
2024-06-02 02:58:09 +00:00
want := []byte{0b0000}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestPackFlagsCompressionSet(t *testing.T) {
flags := PacketFlags{
Connless: false,
Compression: true,
Resend: false,
Control: false,
2024-06-02 02:58:09 +00:00
}
got := flags.Pack()
2024-06-02 02:58:09 +00:00
want := []byte{0b0100}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestPackFlagsAllSet(t *testing.T) {
flags := PacketFlags{
Connless: true,
Compression: true,
Resend: true,
Control: true,
2024-06-02 02:58:09 +00:00
}
got := flags.Pack()
2024-06-02 02:58:09 +00:00
want := []byte{0b1111}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
// unpack
func TestUnpackFlagsAllSet(t *testing.T) {
got := PacketFlags{}
want := PacketFlags{
Connless: true,
Compression: true,
Resend: true,
Control: true,
2024-06-02 02:58:09 +00:00
}
got.Unpack([]byte{0b00111100})
2024-06-02 02:58:09 +00:00
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestUnpackFlagsControlSet(t *testing.T) {
got := PacketFlags{}
want := PacketFlags{
Connless: false,
Compression: false,
Resend: false,
Control: true,
2024-06-02 02:58:09 +00:00
}
got.Unpack([]byte{0b00000100})
2024-06-02 02:58:09 +00:00
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestUnpackFlagsUnset(t *testing.T) {
got := PacketFlags{}
want := PacketFlags{
Connless: false,
Compression: false,
Resend: false,
Control: false,
2024-06-02 02:58:09 +00:00
}
got.Unpack([]byte{0b00000000})
2024-06-02 02:58:09 +00:00
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
// packet header unpack
func TestUnpackCloseWithReason(t *testing.T) {
got := PacketHeader{}
want := PacketHeader{
Flags: PacketFlags{
Connless: false,
Compression: false,
Resend: false,
Control: true,
2024-06-02 02:58:09 +00:00
},
Ack: 10,
NumChunks: 0,
Token: [4]byte{0xcf, 0x2e, 0xde, 0x1d},
2024-06-02 02:58:09 +00:00
}
got.Unpack(slices.Concat([]byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d, 0x04}, []byte("shutdown"), []byte{0x00}))
2024-06-02 02:58:09 +00:00
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestUnpackHeaderFlagsControlSet(t *testing.T) {
got := PacketHeader{}
want := PacketHeader{
Flags: PacketFlags{
Connless: false,
Compression: false,
Resend: false,
Control: true,
2024-06-02 02:58:09 +00:00
},
Ack: 0,
NumChunks: 0,
Token: [4]byte{0xff, 0xff, 0xff, 0xff},
2024-06-02 02:58:09 +00:00
}
got.Unpack([]byte{0b00000100, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff})
2024-06-02 02:58:09 +00:00
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestUnpackHeaderFlagsAllSet(t *testing.T) {
got := PacketHeader{}
want := PacketHeader{
Flags: PacketFlags{
Connless: true,
Compression: true,
Resend: true,
Control: true,
2024-06-02 02:58:09 +00:00
},
Ack: 0,
NumChunks: 0,
Token: [4]byte{0xff, 0xff, 0xff, 0xff},
2024-06-02 02:58:09 +00:00
}
got.Unpack([]byte{0b00111100, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff})
2024-06-02 02:58:09 +00:00
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}