go-teeworlds-protocol/protocol7/packet_test.go

401 lines
15 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
)
2024-06-21 05:14:01 +00:00
// repack packet with unknown messages should not lose data
func TestRepackUnknownMessages(t *testing.T) {
// TODO: once 9e220138-d393-3cb0-90f1-e587c00ab1d0
// is supported this test makes no sense anymore
// because if the entire ddnet protocol is implemeted this dump does
// not contain an unknown message anymore
// to fix this add a custom unused uuid ddnet msg and or a unused teeworlds msg id
// libtw2 dissector dump of ddnet7 connecting to official ddnet server
// Teeworlds 0.7 Protocol packet
// Teeworlds 0.7 Protocol chunk: game.sv_chat
// Teeworlds 0.7 Protocol chunk: game.sv_chat
// Teeworlds 0.7 Protocol chunk: game.sv_game_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info_remove
// Teeworlds 0.7 Protocol chunk: game.9e220138-d393-3cb0-90f1-e587c00ab1d0
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
// Teeworlds 0.7 Protocol chunk: game.sv_command_info
dump := []byte{
0x00, 0x05, 0x16, 0x8d, 0x5b, 0x17, 0x48, 0x40, 0x2c, 0x24, 0x06, 0x01, 0x40, 0x40, 0x44, 0x44,
0x72, 0x61, 0x63, 0x65, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x4d, 0x6f, 0x64, 0x2e,
0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x30, 0x2e, 0x36, 0x2e, 0x34, 0x2c,
0x20, 0x31, 0x38, 0x2e, 0x33, 0x00, 0x41, 0x09, 0x25, 0x06, 0x01, 0x40, 0x40, 0x70, 0x6c, 0x65,
0x61, 0x73, 0x65, 0x20, 0x76, 0x69, 0x73, 0x69, 0x74, 0x20, 0x44, 0x44, 0x4e, 0x65, 0x74, 0x2e,
0x6f, 0x72, 0x67, 0x20, 0x6f, 0x72, 0x20, 0x73, 0x61, 0x79, 0x20, 0x2f, 0x69, 0x6e, 0x66, 0x6f,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x6d, 0x61, 0x6b, 0x65, 0x20, 0x73, 0x75, 0x72, 0x65, 0x20, 0x74,
0x6f, 0x20, 0x72, 0x65, 0x61, 0x64, 0x20, 0x6f, 0x75, 0x72, 0x20, 0x2f, 0x72, 0x75, 0x6c, 0x65,
0x73, 0x00, 0x40, 0x06, 0x26, 0x26, 0x08, 0x00, 0x00, 0x00, 0x01, 0x40, 0x07, 0x27, 0x8c, 0x01,
0x74, 0x65, 0x61, 0x6d, 0x00, 0x40, 0x11, 0x28, 0x00, 0x9e, 0x22, 0x01, 0x38, 0xd3, 0x93, 0x3c,
0xb0, 0x90, 0xf1, 0xe5, 0x87, 0xc0, 0x0a, 0xb1, 0xd0, 0x42, 0x07, 0x29, 0x8a, 0x01, 0x61, 0x64,
0x64, 0x77, 0x65, 0x61, 0x70, 0x6f, 0x6e, 0x00, 0x69, 0x5b, 0x77, 0x65, 0x61, 0x70, 0x6f, 0x6e,
0x2d, 0x69, 0x64, 0x5d, 0x00, 0x47, 0x69, 0x76, 0x65, 0x73, 0x20, 0x77, 0x65, 0x61, 0x70, 0x6f,
0x6e, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x69, 0x64, 0x20, 0x69, 0x20, 0x74, 0x6f, 0x20, 0x79,
0x6f, 0x75, 0x20, 0x28, 0x61, 0x6c, 0x6c, 0x20, 0x3d, 0x20, 0x2d, 0x31, 0x2c, 0x20, 0x68, 0x61,
0x6d, 0x6d, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x30, 0x2c, 0x20, 0x67, 0x75, 0x6e, 0x20, 0x3d, 0x20,
0x31, 0x2c, 0x20, 0x73, 0x68, 0x6f, 0x74, 0x67, 0x75, 0x6e, 0x20, 0x3d, 0x20, 0x32, 0x2c, 0x20,
0x67, 0x72, 0x65, 0x6e, 0x61, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x33, 0x2c, 0x20, 0x6c, 0x61, 0x73,
0x65, 0x72, 0x20, 0x3d, 0x20, 0x34, 0x2c, 0x20, 0x6e, 0x69, 0x6e, 0x6a, 0x61, 0x20, 0x3d, 0x20,
0x35, 0x29, 0x00, 0x41, 0x10, 0x2a, 0x8a, 0x01, 0x63, 0x00, 0x72, 0x5b, 0x6d, 0x65, 0x73, 0x73,
0x61, 0x67, 0x65, 0x5d, 0x00, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x73, 0x65, 0x20, 0x77, 0x69,
0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x73, 0x74, 0x20, 0x70, 0x65, 0x72, 0x73,
0x6f, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x77, 0x68, 0x69, 0x73, 0x70, 0x65, 0x72, 0x65, 0x64,
0x20, 0x74, 0x6f, 0x20, 0x28, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x20, 0x6d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x29, 0x00, 0x40, 0x3c, 0x2b, 0x8a, 0x01, 0x63, 0x6d, 0x64, 0x6c, 0x69,
0x73, 0x74, 0x00, 0x00, 0x4c, 0x69, 0x73, 0x74, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x63, 0x6f, 0x6d,
0x6d, 0x61, 0x6e, 0x64, 0x73, 0x20, 0x77, 0x68, 0x69, 0x63, 0x68, 0x20, 0x61, 0x72, 0x65, 0x20,
0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x75,
0x73, 0x65, 0x72, 0x73, 0x00, 0x41, 0x17, 0x2c, 0x8a, 0x01, 0x63, 0x6f, 0x6e, 0x76, 0x65, 0x72,
0x73, 0x65, 0x00, 0x72, 0x5b, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5d, 0x00, 0x43, 0x6f,
0x6e, 0x76, 0x65, 0x72, 0x73, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20,
0x6c, 0x61, 0x73, 0x74, 0x20, 0x70, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x20,
0x77, 0x68, 0x69, 0x73, 0x70, 0x65, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x28, 0x70, 0x72,
0x69, 0x76, 0x61, 0x74, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x29, 0x00, 0x40,
0x2e, 0x2d, 0x8a, 0x01, 0x63, 0x72, 0x65, 0x64, 0x69, 0x74, 0x73, 0x00, 0x00, 0x53, 0x68, 0x6f,
0x77, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x72, 0x65, 0x64, 0x69, 0x74, 0x73, 0x20, 0x6f,
0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x44, 0x44, 0x4e, 0x65, 0x74, 0x20, 0x6d, 0x6f, 0x64, 0x00,
0x40, 0x22, 0x2e, 0x8a, 0x01, 0x64, 0x65, 0x65, 0x70, 0x00, 0x00, 0x50, 0x75, 0x74, 0x73, 0x20,
0x79, 0x6f, 0x75, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x64, 0x65, 0x65, 0x70, 0x20, 0x66, 0x72,
0x65, 0x65, 0x7a, 0x65, 0x00, 0x40, 0x3b, 0x2f, 0x8a, 0x01, 0x64, 0x6e, 0x64, 0x00, 0x00, 0x54,
0x6f, 0x67, 0x67, 0x6c, 0x65, 0x20, 0x44, 0x6f, 0x20, 0x4e, 0x6f, 0x74, 0x20, 0x44, 0x69, 0x73,
0x74, 0x75, 0x72, 0x62, 0x20, 0x28, 0x6e, 0x6f, 0x20, 0x63, 0x68, 0x61, 0x74, 0x20, 0x61, 0x6e,
0x64, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x73, 0x29, 0x00, 0x41, 0x08, 0x30, 0x8a, 0x01, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x00, 0x3f, 0x73,
0x5b, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x5d, 0x20, 0x69, 0x5b, 0x64,
0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x73, 0x65, 0x63, 0x6f, 0x6e,
0x64, 0x73, 0x5d, 0x00, 0x53, 0x65, 0x74, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x74, 0x65,
0x65, 0x27, 0x73, 0x20, 0x65, 0x79, 0x65, 0x20, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x00, 0x42, 0x31,
0x31, 0x8a, 0x01, 0x65, 0x79, 0x65, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x00, 0x3f, 0x73, 0x5b, 0x27,
0x6f, 0x6e, 0x27, 0x7c, 0x27, 0x6f, 0x66, 0x66, 0x27, 0x7c, 0x27, 0x74, 0x6f, 0x67, 0x67, 0x6c,
0x65, 0x27, 0x5d, 0x00, 0x54, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x73, 0x20, 0x75, 0x73, 0x65, 0x20,
0x6f, 0x66, 0x20, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x20, 0x65, 0x79, 0x65, 0x2d,
0x65, 0x6d, 0x6f, 0x74, 0x65, 0x73, 0x20, 0x6f, 0x6e, 0x2f, 0x6f, 0x66, 0x66, 0x2c, 0x20, 0x65,
0x79, 0x65, 0x65, 0x6d, 0x6f, 0x74, 0x65, 0x20, 0x73, 0x2c, 0x20, 0x77, 0x68, 0x65, 0x72, 0x65,
0x20, 0x73, 0x20, 0x3d, 0x20, 0x6f, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x2c, 0x20,
0x6f, 0x66, 0x66, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6f, 0x66, 0x66, 0x2c, 0x20, 0x74, 0x6f, 0x67,
0x67, 0x6c, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x20, 0x61,
0x6e, 0x64, 0x20, 0x6e, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x73, 0x68,
0x6f, 0x77, 0x20, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x20, 0x73, 0x74, 0x61, 0x74, 0x75,
0x73, 0x00, 0x40, 0x2b, 0x32, 0x8a, 0x01, 0x67, 0x72, 0x65, 0x6e, 0x61, 0x64, 0x65, 0x00, 0x00,
0x47, 0x69, 0x76, 0x65, 0x73, 0x20, 0x61, 0x20, 0x67, 0x72, 0x65, 0x6e, 0x61, 0x64, 0x65, 0x20,
0x6c, 0x61, 0x75, 0x6e, 0x63, 0x68, 0x65, 0x72, 0x20, 0x74, 0x6f, 0x20, 0x79, 0x6f, 0x75, 0x00,
0x41, 0x07, 0x33, 0x8a, 0x01, 0x68, 0x65, 0x6c, 0x70, 0x00, 0x3f, 0x72, 0x5b, 0x63, 0x6f, 0x6d,
0x6d, 0x61, 0x6e, 0x64, 0x5d, 0x00, 0x53, 0x68, 0x6f, 0x77, 0x73, 0x20, 0x68, 0x65, 0x6c, 0x70,
0x20, 0x74, 0x6f, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x20, 0x72, 0x2c, 0x20, 0x67,
0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x68, 0x65, 0x6c, 0x70, 0x20, 0x69, 0x66, 0x20, 0x6c,
0x65, 0x66, 0x74, 0x20, 0x62, 0x6c, 0x61, 0x6e, 0x6b, 0x00, 0x40, 0x25, 0x34, 0x8a, 0x01, 0x69,
0x6e, 0x66, 0x6f, 0x00, 0x00, 0x53, 0x68, 0x6f, 0x77, 0x73, 0x20, 0x69, 0x6e, 0x66, 0x6f, 0x20,
0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65,
0x72, 0x00, 0x40, 0x39, 0x35, 0x8a, 0x01, 0x69, 0x6e, 0x76, 0x69, 0x74, 0x65, 0x00, 0x72, 0x5b,
0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x5d, 0x00, 0x49, 0x6e, 0x76,
0x69, 0x74, 0x65, 0x20, 0x61, 0x20, 0x70, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x20, 0x74, 0x6f, 0x20,
0x61, 0x20, 0x6c, 0x6f, 0x63, 0x6b, 0x65, 0x64, 0x20, 0x74, 0x65, 0x61, 0x6d, 0x00, 0x40, 0x20,
0x36, 0x8a, 0x01, 0x6a, 0x65, 0x74, 0x70, 0x61, 0x63, 0x6b, 0x00, 0x00, 0x47, 0x69, 0x76, 0x65,
0x73, 0x20, 0x6a, 0x65, 0x74, 0x70, 0x61, 0x63, 0x6b, 0x20, 0x74, 0x6f, 0x20, 0x79, 0x6f, 0x75,
0x00, 0x40, 0x3c, 0x37, 0x8a, 0x01, 0x6a, 0x6f, 0x69, 0x6e, 0x00, 0x72, 0x5b, 0x70, 0x6c, 0x61,
0x79, 0x65, 0x72, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x5d, 0x00, 0x4a, 0x6f, 0x69, 0x6e, 0x20, 0x74,
0x68, 0x65, 0x20, 0x74, 0x65, 0x61, 0x6d, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x00,
0x41, 0x1d, 0x38, 0x8a, 0x01, 0x6b, 0x69, 0x6c, 0x6c, 0x00, 0x00, 0x4b, 0x69, 0x6c, 0x6c, 0x20,
0x79, 0x6f, 0x75, 0x72, 0x73, 0x65, 0x6c, 0x66, 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x6b, 0x69,
0x6c, 0x6c, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x65, 0x63, 0x74, 0x65, 0x64, 0x20, 0x64, 0x75, 0x72,
0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x67, 0x61, 0x6d, 0x65, 0x20,
0x28, 0x75, 0x73, 0x65, 0x20, 0x66, 0x31, 0x2c, 0x20, 0x6b, 0x69, 0x6c, 0x6c, 0x20, 0x66, 0x6f,
0x72, 0x20, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x20, 0x6b, 0x69, 0x6c, 0x6c, 0x29, 0x00,
0x40, 0x1e, 0x39, 0x8a, 0x01, 0x6c, 0x61, 0x73, 0x65, 0x72, 0x00, 0x00, 0x47, 0x69, 0x76, 0x65,
0x73, 0x20, 0x61, 0x20, 0x6c, 0x61, 0x73, 0x65, 0x72, 0x20, 0x74, 0x6f, 0x20, 0x79, 0x6f, 0x75,
0x00,
}
conn := Connection{}
result, err := conn.OnPacket(dump)
if err != nil {
t.Errorf("Unexpected error %v\n", err)
}
repack := result.Packet.Pack(&conn)
if !reflect.DeepEqual(repack, dump) {
t.Errorf("got %v, wanted %v", repack, dump)
}
}
// 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)
}
}