369 lines
15 KiB
Go
369 lines
15 KiB
Go
package protocol7
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/teeworlds-go/go-teeworlds-protocol/chunk7"
|
|
"github.com/teeworlds-go/go-teeworlds-protocol/internal/testutils/require"
|
|
"github.com/teeworlds-go/go-teeworlds-protocol/messages7"
|
|
)
|
|
|
|
// 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 := Session{}
|
|
|
|
packet := Packet{}
|
|
err := packet.Unpack(dump)
|
|
require.NoError(t, err)
|
|
|
|
conn.Ack = packet.Header.Ack
|
|
repack := packet.Pack(&conn)
|
|
require.Equal(t, dump, repack)
|
|
}
|
|
|
|
// 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()
|
|
require.NotNil(t, got)
|
|
}
|
|
|
|
// When packing the chunk header will be set automatically
|
|
// Based on the current context
|
|
conn := &Session{Sequence: 1}
|
|
_ = packet.Pack(conn)
|
|
|
|
{
|
|
got := packet.Messages[0].Header()
|
|
want := &chunk7.ChunkHeader{
|
|
Flags: chunk7.ChunkFlags{
|
|
Vital: true,
|
|
},
|
|
Size: 8,
|
|
Seq: 2,
|
|
}
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
// 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,
|
|
}
|
|
require.Equal(t, want, got)
|
|
}
|
|
}
|
|
|
|
// 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},
|
|
}
|
|
want := []byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d}
|
|
got := header.Pack()
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
// pack flags
|
|
|
|
func TestPackFlagsUnset(t *testing.T) {
|
|
flags := PacketFlags{
|
|
Connless: false,
|
|
Compression: false,
|
|
Resend: false,
|
|
Control: false,
|
|
}
|
|
|
|
want := []byte{0b0000}
|
|
got := flags.Pack()
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
func TestPackFlagsCompressionSet(t *testing.T) {
|
|
flags := PacketFlags{
|
|
Connless: false,
|
|
Compression: true,
|
|
Resend: false,
|
|
Control: false,
|
|
}
|
|
|
|
want := []byte{0b0100}
|
|
got := flags.Pack()
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
func TestPackFlagsAllSet(t *testing.T) {
|
|
flags := PacketFlags{
|
|
Connless: true,
|
|
Compression: true,
|
|
Resend: true,
|
|
Control: true,
|
|
}
|
|
|
|
want := []byte{0b1111}
|
|
got := flags.Pack()
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
// unpack
|
|
|
|
func TestUnpackFlagsAllSet(t *testing.T) {
|
|
want := PacketFlags{
|
|
Connless: true,
|
|
Compression: true,
|
|
Resend: true,
|
|
Control: true,
|
|
}
|
|
got := PacketFlags{}
|
|
err := got.Unpack([]byte{0b00111100})
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
func TestUnpackFlagsControlSet(t *testing.T) {
|
|
want := PacketFlags{
|
|
Connless: false,
|
|
Compression: false,
|
|
Resend: false,
|
|
Control: true,
|
|
}
|
|
got := PacketFlags{}
|
|
|
|
err := got.Unpack([]byte{0b00000100})
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
func TestUnpackFlagsUnset(t *testing.T) {
|
|
want := PacketFlags{
|
|
Connless: false,
|
|
Compression: false,
|
|
Resend: false,
|
|
Control: false,
|
|
}
|
|
|
|
got := PacketFlags{}
|
|
err := got.Unpack([]byte{0b00000000})
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
// packet header unpack
|
|
|
|
func TestUnpackCloseWithReason(t *testing.T) {
|
|
want := PacketHeader{
|
|
Flags: PacketFlags{
|
|
Connless: false,
|
|
Compression: false,
|
|
Resend: false,
|
|
Control: true,
|
|
},
|
|
Ack: 10,
|
|
NumChunks: 0,
|
|
Token: [4]byte{0xcf, 0x2e, 0xde, 0x1d},
|
|
}
|
|
|
|
got := PacketHeader{}
|
|
err := got.Unpack(slices.Concat(
|
|
[]byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d, 0x04},
|
|
[]byte("shutdown"),
|
|
[]byte{0x00},
|
|
))
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
func TestUnpackHeaderFlagsControlSet(t *testing.T) {
|
|
want := PacketHeader{
|
|
Flags: PacketFlags{
|
|
Connless: false,
|
|
Compression: false,
|
|
Resend: false,
|
|
Control: true,
|
|
},
|
|
Ack: 0,
|
|
NumChunks: 0,
|
|
Token: [4]byte{0xff, 0xff, 0xff, 0xff},
|
|
}
|
|
|
|
got := PacketHeader{}
|
|
err := got.Unpack([]byte{0b00000100, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff})
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got)
|
|
}
|
|
|
|
func TestUnpackHeaderFlagsAllSet(t *testing.T) {
|
|
want := PacketHeader{
|
|
Flags: PacketFlags{
|
|
Connless: true,
|
|
Compression: true,
|
|
Resend: true,
|
|
Control: true,
|
|
},
|
|
Ack: 0,
|
|
NumChunks: 0,
|
|
Token: [4]byte{0xff, 0xff, 0xff, 0xff},
|
|
}
|
|
|
|
got := PacketHeader{}
|
|
err := got.Unpack([]byte{0b00111100, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff})
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got)
|
|
}
|