From 94c6798e8bf776f6c68d9eb2c9c06fcfb7a6ae54 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Tue, 25 Jun 2024 12:19:51 +0800 Subject: [PATCH] Refactor unpacker and add some more snap tests --- chunk7/chunk.go | 44 ++- chunk7/chunk_test.go | 6 +- chunk7/splitter.go | 3 +- messages7/cl_say.go | 2 +- messages7/cl_start_info.go | 16 +- messages7/ctrl_close.go | 2 +- messages7/info.go | 4 +- messages7/map_change.go | 2 +- messages7/maplist_entry_add.go | 2 +- messages7/maplist_entry_rem.go | 2 +- messages7/rcon_auth.go | 2 +- messages7/rcon_cmd.go | 2 +- messages7/rcon_cmd_add.go | 6 +- messages7/rcon_cmd_rem.go | 2 +- messages7/rcon_line.go | 2 +- messages7/server_info.go | 10 +- messages7/sv_broadcast.go | 2 +- messages7/sv_chat.go | 2 +- messages7/sv_client_info.go | 16 +- messages7/sv_emoticon.go | 3 + messages7/sv_emoticon_test.go | 88 ++++++ messages7/sv_motd.go | 2 +- messages7/sv_vote_option_add.go | 2 +- messages7/sv_vote_option_list_add.go | 2 +- messages7/sv_vote_option_list_add_test.go | 37 +++ messages7/sv_vote_option_remove.go | 2 +- messages7/sv_vote_set.go | 4 +- object7/laser_test.go | 79 ++++++ object7/snap_object.go | 6 +- packer/packer.go | 47 +++- packer/packer_state_test.go | 16 +- snapshot7/full_ddnet_server_test.go | 266 ++++++++++++++++++ ...pshot_test.go => simple_localhost_test.go} | 2 +- 33 files changed, 616 insertions(+), 67 deletions(-) create mode 100644 messages7/sv_emoticon_test.go create mode 100644 messages7/sv_vote_option_list_add_test.go create mode 100644 object7/laser_test.go create mode 100644 snapshot7/full_ddnet_server_test.go rename snapshot7/{snapshot_test.go => simple_localhost_test.go} (96%) diff --git a/chunk7/chunk.go b/chunk7/chunk.go index 3b67372..7fc87dc 100644 --- a/chunk7/chunk.go +++ b/chunk7/chunk.go @@ -1,5 +1,11 @@ package chunk7 +import ( + "fmt" + + "github.com/teeworlds-go/go-teeworlds-protocol/packer" +) + const ( chunkFlagVital = 1 chunkFlagResend = 2 @@ -49,13 +55,49 @@ func (header *ChunkHeader) Pack() []byte { return data } -func (header *ChunkHeader) Unpack(data []byte) { +func (header *ChunkHeader) Unpack(u *packer.Unpacker) error { + data, err := u.GetRaw(2) + if err != nil { + return err + } flagBits := (data[0] >> 6) & 0x03 header.Flags.Vital = (flagBits & chunkFlagVital) != 0 header.Flags.Resend = (flagBits & chunkFlagResend) != 0 header.Size = (int(data[0]&0x3F) << 6) | (int(data[1]) & 0x3F) if header.Flags.Vital { + thirdByte, err := u.GetByte() + if err != nil { + return err + } + header.Seq = int((data[1]&0xC0)<<2) | int(thirdByte) + } + return nil +} + +// TODO: give this a better name +// +// but it would be nice to have two unpack methods for all structs +// +// Unpack(u *packer.Unpacker) +// UnpackTodoFindAGoodName(data []byte) +// +// See https://github.com/teeworlds-go/go-teeworlds-protocol/issues/5 +func (header *ChunkHeader) UnpackRaw(data []byte) error { + if len(data) < 2 { + return fmt.Errorf("size=%d not enough data to read chunk header", len(data)) + } + + flagBits := (data[0] >> 6) & 0x03 + header.Flags.Vital = (flagBits & chunkFlagVital) != 0 + header.Flags.Resend = (flagBits & chunkFlagResend) != 0 + header.Size = (int(data[0]&0x3F) << 6) | (int(data[1]) & 0x3F) + + if header.Flags.Vital { + if len(data) < 3 { + return fmt.Errorf("size=%d not enough data to read vital chunk header", len(data)) + } header.Seq = int((data[1]&0xC0)<<2) | int(data[2]) } + return nil } diff --git a/chunk7/chunk_test.go b/chunk7/chunk_test.go index e15fa30..6904f0f 100644 --- a/chunk7/chunk_test.go +++ b/chunk7/chunk_test.go @@ -12,7 +12,7 @@ func TestBrokenNonVitalHeader(t *testing.T) { header := ChunkHeader{} // {0x40, 0x3a, 0x01} - header.Unpack([]byte{0x3a, 0x01}) + header.UnpackRaw([]byte{0x3a, 0x01}) want := ChunkHeader{ Flags: ChunkFlags{ @@ -33,7 +33,7 @@ func TestVitalHeaderMapChange(t *testing.T) { // verified with libtw2 wireshark dissector header := ChunkHeader{} - header.Unpack([]byte{0x40, 0x3a, 0x01}) + header.UnpackRaw([]byte{0x40, 0x3a, 0x01}) want := ChunkHeader{ Flags: ChunkFlags{ @@ -51,7 +51,7 @@ func TestVitalHeaderMapChange(t *testing.T) { func TestVitalHeader(t *testing.T) { header := ChunkHeader{} - header.Unpack([]byte{0x40, 0x10, 0x0a}) + header.UnpackRaw([]byte{0x40, 0x10, 0x0a}) want := ChunkHeader{ Flags: ChunkFlags{ Vital: true, diff --git a/chunk7/splitter.go b/chunk7/splitter.go index 6e51edb..f21cb53 100644 --- a/chunk7/splitter.go +++ b/chunk7/splitter.go @@ -11,7 +11,8 @@ func UnpackChunks(data []byte) []Chunk { for i < payloadSize { chunk := Chunk{} - chunk.Header.Unpack(data[i:]) + // TODO: can we use ChunkHeader.Unpack(u) here to simplify the code? + chunk.Header.UnpackRaw(data[i:]) i += 2 // header if chunk.Header.Flags.Vital { i++ diff --git a/messages7/cl_say.go b/messages7/cl_say.go index 42f7e2b..f5131df 100644 --- a/messages7/cl_say.go +++ b/messages7/cl_say.go @@ -43,7 +43,7 @@ func (msg *ClSay) Pack() []byte { func (msg *ClSay) Unpack(u *packer.Unpacker) error { msg.Mode = network7.ChatMode(u.GetInt()) msg.TargetId = u.GetInt() - msg.Message = u.GetString() + msg.Message, _ = u.GetString() return nil } diff --git a/messages7/cl_start_info.go b/messages7/cl_start_info.go index 6044b4d..097e5c2 100644 --- a/messages7/cl_start_info.go +++ b/messages7/cl_start_info.go @@ -77,15 +77,15 @@ func (msg *ClStartInfo) Pack() []byte { } func (msg *ClStartInfo) Unpack(u *packer.Unpacker) error { - msg.Name = u.GetString() - msg.Clan = u.GetString() + msg.Name, _ = u.GetString() + msg.Clan, _ = u.GetString() msg.Country = u.GetInt() - msg.Body = u.GetString() - msg.Marking = u.GetString() - msg.Decoration = u.GetString() - msg.Hands = u.GetString() - msg.Feet = u.GetString() - msg.Eyes = u.GetString() + msg.Body, _ = u.GetString() + msg.Marking, _ = u.GetString() + msg.Decoration, _ = u.GetString() + msg.Hands, _ = u.GetString() + msg.Feet, _ = u.GetString() + msg.Eyes, _ = u.GetString() msg.CustomColorBody = u.GetInt() != 0 msg.CustomColorMarking = u.GetInt() != 0 msg.CustomColorDecoration = u.GetInt() != 0 diff --git a/messages7/ctrl_close.go b/messages7/ctrl_close.go index 9f7fe52..4ba8192 100644 --- a/messages7/ctrl_close.go +++ b/messages7/ctrl_close.go @@ -37,7 +37,7 @@ func (msg *CtrlClose) Pack() []byte { func (msg *CtrlClose) Unpack(u *packer.Unpacker) error { // TODO: sanitize - msg.Reason = u.GetString() + msg.Reason, _ = u.GetString() return nil } diff --git a/messages7/info.go b/messages7/info.go index 5abe827..c879e88 100644 --- a/messages7/info.go +++ b/messages7/info.go @@ -59,8 +59,8 @@ func (msg *Info) Pack() []byte { } func (msg *Info) Unpack(u *packer.Unpacker) error { - msg.Version = u.GetString() - msg.Password = u.GetString() + msg.Version, _ = u.GetString() + msg.Password, _ = u.GetString() msg.ClientVersion = u.GetInt() return nil } diff --git a/messages7/map_change.go b/messages7/map_change.go index 21caecb..90ad8ce 100644 --- a/messages7/map_change.go +++ b/messages7/map_change.go @@ -47,7 +47,7 @@ func (msg *MapChange) Pack() []byte { } func (msg *MapChange) Unpack(u *packer.Unpacker) error { - msg.Name = u.GetString() + msg.Name, _ = u.GetString() msg.Crc = u.GetInt() msg.Size = u.GetInt() msg.NumResponseChunksPerRequest = u.GetInt() diff --git a/messages7/maplist_entry_add.go b/messages7/maplist_entry_add.go index 03a57e8..41c1f1a 100644 --- a/messages7/maplist_entry_add.go +++ b/messages7/maplist_entry_add.go @@ -37,7 +37,7 @@ func (msg *MaplistEntryAdd) Pack() []byte { } func (msg *MaplistEntryAdd) Unpack(u *packer.Unpacker) error { - msg.MapName = u.GetString() + msg.MapName, _ = u.GetString() return nil } diff --git a/messages7/maplist_entry_rem.go b/messages7/maplist_entry_rem.go index bd12ff7..cd6cfc2 100644 --- a/messages7/maplist_entry_rem.go +++ b/messages7/maplist_entry_rem.go @@ -37,7 +37,7 @@ func (msg *MaplistEntryRem) Pack() []byte { } func (msg *MaplistEntryRem) Unpack(u *packer.Unpacker) error { - msg.MapName = u.GetString() + msg.MapName, _ = u.GetString() return nil } diff --git a/messages7/rcon_auth.go b/messages7/rcon_auth.go index c95cc93..5f8da2e 100644 --- a/messages7/rcon_auth.go +++ b/messages7/rcon_auth.go @@ -37,7 +37,7 @@ func (msg *RconAuth) Pack() []byte { } func (msg *RconAuth) Unpack(u *packer.Unpacker) error { - msg.Password = u.GetString() + msg.Password, _ = u.GetString() return nil } diff --git a/messages7/rcon_cmd.go b/messages7/rcon_cmd.go index c142e90..15978b4 100644 --- a/messages7/rcon_cmd.go +++ b/messages7/rcon_cmd.go @@ -37,7 +37,7 @@ func (msg *RconCmd) Pack() []byte { } func (msg *RconCmd) Unpack(u *packer.Unpacker) error { - msg.Command = u.GetString() + msg.Command, _ = u.GetString() return nil } diff --git a/messages7/rcon_cmd_add.go b/messages7/rcon_cmd_add.go index 1c45012..358ad1b 100644 --- a/messages7/rcon_cmd_add.go +++ b/messages7/rcon_cmd_add.go @@ -41,9 +41,9 @@ func (msg *RconCmdAdd) Pack() []byte { } func (msg *RconCmdAdd) Unpack(u *packer.Unpacker) error { - msg.Name = u.GetString() - msg.Help = u.GetString() - msg.Params = u.GetString() + msg.Name, _ = u.GetString() + msg.Help, _ = u.GetString() + msg.Params, _ = u.GetString() return nil } diff --git a/messages7/rcon_cmd_rem.go b/messages7/rcon_cmd_rem.go index 99f657c..e24a99e 100644 --- a/messages7/rcon_cmd_rem.go +++ b/messages7/rcon_cmd_rem.go @@ -37,7 +37,7 @@ func (msg *RconCmdRem) Pack() []byte { } func (msg *RconCmdRem) Unpack(u *packer.Unpacker) error { - msg.Name = u.GetString() + msg.Name, _ = u.GetString() return nil } diff --git a/messages7/rcon_line.go b/messages7/rcon_line.go index 60d1954..ff353b3 100644 --- a/messages7/rcon_line.go +++ b/messages7/rcon_line.go @@ -37,7 +37,7 @@ func (msg *RconLine) Pack() []byte { } func (msg *RconLine) Unpack(u *packer.Unpacker) error { - msg.Line = u.GetString() + msg.Line, _ = u.GetString() return nil } diff --git a/messages7/server_info.go b/messages7/server_info.go index 58b747c..80c1c3a 100644 --- a/messages7/server_info.go +++ b/messages7/server_info.go @@ -57,11 +57,11 @@ func (msg *ServerInfo) Pack() []byte { } func (msg *ServerInfo) Unpack(u *packer.Unpacker) error { - msg.Version = u.GetString() - msg.Name = u.GetString() - msg.Hostname = u.GetString() - msg.MapName = u.GetString() - msg.GameType = u.GetString() + msg.Version, _ = u.GetString() + msg.Name, _ = u.GetString() + msg.Hostname, _ = u.GetString() + msg.MapName, _ = u.GetString() + msg.GameType, _ = u.GetString() msg.Flags = u.GetInt() msg.SkillLevel = u.GetInt() msg.PlayerCount = u.GetInt() diff --git a/messages7/sv_broadcast.go b/messages7/sv_broadcast.go index 2912cff..36e208c 100644 --- a/messages7/sv_broadcast.go +++ b/messages7/sv_broadcast.go @@ -33,7 +33,7 @@ func (msg *SvBroadcast) Pack() []byte { } func (msg *SvBroadcast) Unpack(u *packer.Unpacker) error { - msg.Message = u.GetString() + msg.Message, _ = u.GetString() return nil } diff --git a/messages7/sv_chat.go b/messages7/sv_chat.go index 4fc4831..a07423c 100644 --- a/messages7/sv_chat.go +++ b/messages7/sv_chat.go @@ -46,7 +46,7 @@ func (msg *SvChat) Unpack(u *packer.Unpacker) error { msg.Mode = network7.ChatMode(u.GetInt()) msg.ClientId = u.GetInt() msg.TargetId = u.GetInt() - msg.Message = u.GetString() + msg.Message, _ = u.GetString() return nil } diff --git a/messages7/sv_client_info.go b/messages7/sv_client_info.go index dc609ca..20241c9 100644 --- a/messages7/sv_client_info.go +++ b/messages7/sv_client_info.go @@ -86,15 +86,15 @@ func (info *SvClientInfo) Unpack(u *packer.Unpacker) error { info.ClientId = u.GetInt() info.Local = u.GetInt() != 0 info.Team = u.GetInt() - info.Name = u.GetString() - info.Clan = u.GetString() + info.Name, _ = u.GetString() + info.Clan, _ = u.GetString() info.Country = u.GetInt() - info.Body = u.GetString() - info.Marking = u.GetString() - info.Decoration = u.GetString() - info.Hands = u.GetString() - info.Feet = u.GetString() - info.Eyes = u.GetString() + info.Body, _ = u.GetString() + info.Marking, _ = u.GetString() + info.Decoration, _ = u.GetString() + info.Hands, _ = u.GetString() + info.Feet, _ = u.GetString() + info.Eyes, _ = u.GetString() info.CustomColorBody = u.GetInt() != 0 info.CustomColorMarking = u.GetInt() != 0 info.CustomColorDecoration = u.GetInt() != 0 diff --git a/messages7/sv_emoticon.go b/messages7/sv_emoticon.go index 0212f15..3a6fa15 100644 --- a/messages7/sv_emoticon.go +++ b/messages7/sv_emoticon.go @@ -11,6 +11,7 @@ import ( type SvEmoticon struct { ChunkHeader *chunk7.ChunkHeader + ClientId int Emoticon network7.Emote } @@ -32,11 +33,13 @@ func (msg *SvEmoticon) Vital() bool { func (msg *SvEmoticon) Pack() []byte { return slices.Concat( + packer.PackInt(msg.ClientId), packer.PackInt(int(msg.Emoticon)), ) } func (msg *SvEmoticon) Unpack(u *packer.Unpacker) error { + msg.ClientId = u.GetInt() msg.Emoticon = network7.Emote(u.GetInt()) return nil diff --git a/messages7/sv_emoticon_test.go b/messages7/sv_emoticon_test.go new file mode 100644 index 0000000..5a12586 --- /dev/null +++ b/messages7/sv_emoticon_test.go @@ -0,0 +1,88 @@ +package messages7_test + +import ( + "testing" + + "github.com/teeworlds-go/go-teeworlds-protocol/internal/testutils/require" + "github.com/teeworlds-go/go-teeworlds-protocol/messages7" + "github.com/teeworlds-go/go-teeworlds-protocol/network7" + "github.com/teeworlds-go/go-teeworlds-protocol/packer" + "github.com/teeworlds-go/go-teeworlds-protocol/protocol7" +) + +func TestFullPacket(t *testing.T) { + packet := protocol7.Packet{} + packet.Messages = append( + packet.Messages, + &messages7.SvEmoticon{ + ClientId: 0, + Emoticon: network7.EmoteGhost, + }, + ) + + { + // if this test breaks because the session tokens are actually used + // this is not necessarily a bad thing + session := &protocol7.Session{ + ServerToken: [4]byte{0x55, 0x55, 0x55, 0x55}, + ClientToken: [4]byte{0xfa, 0xfa, 0xfa, 0xfa}, + } + + want := []byte{0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0x03, 0x01, 0x14, 0x00, 0x07} + got := packet.Pack(session) + + require.Equal(t, want, got) + } +} + +func TestSvEmoticonStandalone(t *testing.T) { + // simple pack + emoticon := &messages7.SvEmoticon{ + ClientId: 0, + Emoticon: network7.EmoteExclamation, + } + + { + want := []byte{0x00, 0x01} + got := emoticon.Pack() + + require.Equal(t, want, got) + } + + // repack + u := &packer.Unpacker{} + u.Reset(emoticon.Pack()) + emoticon.Unpack(u) + + { + want := network7.EmoteExclamation + got := emoticon.Emoticon + require.Equal(t, want, got) + } +} + +func TestSvEmoticonStandaloneCrazyGirlEdition(t *testing.T) { + // simple pack + emoticon := &messages7.SvEmoticon{ + ClientId: -99999, + Emoticon: 999, + } + + { + want := []byte{222, 154, 12, 167, 15} + got := emoticon.Pack() + + require.Equal(t, want, got) + } + + // repack + u := &packer.Unpacker{} + u.Reset(emoticon.Pack()) + emoticon.Unpack(u) + + { + want := network7.Emote(999) + got := emoticon.Emoticon + require.Equal(t, want, got) + } +} diff --git a/messages7/sv_motd.go b/messages7/sv_motd.go index 44fafdb..b657152 100644 --- a/messages7/sv_motd.go +++ b/messages7/sv_motd.go @@ -33,7 +33,7 @@ func (msg *SvMotd) Pack() []byte { } func (msg *SvMotd) Unpack(u *packer.Unpacker) error { - msg.Message = u.GetString() + msg.Message, _ = u.GetString() return nil } diff --git a/messages7/sv_vote_option_add.go b/messages7/sv_vote_option_add.go index 505ad8f..53796fa 100644 --- a/messages7/sv_vote_option_add.go +++ b/messages7/sv_vote_option_add.go @@ -37,7 +37,7 @@ func (msg *SvVoteOptionAdd) Pack() []byte { } func (msg *SvVoteOptionAdd) Unpack(u *packer.Unpacker) error { - msg.Description = u.GetString() + msg.Description, _ = u.GetString() return nil } diff --git a/messages7/sv_vote_option_list_add.go b/messages7/sv_vote_option_list_add.go index 51340cf..7cf06c1 100644 --- a/messages7/sv_vote_option_list_add.go +++ b/messages7/sv_vote_option_list_add.go @@ -52,7 +52,7 @@ func (msg *SvVoteOptionListAdd) Unpack(u *packer.Unpacker) error { msg.NumOptions = u.GetInt() msg.Descriptions = make([]string, msg.NumOptions) for i := 0; i < msg.NumOptions; i++ { - msg.Descriptions[i] = u.GetString() + msg.Descriptions[i], _ = u.GetString() } return nil diff --git a/messages7/sv_vote_option_list_add_test.go b/messages7/sv_vote_option_list_add_test.go new file mode 100644 index 0000000..c9f4e34 --- /dev/null +++ b/messages7/sv_vote_option_list_add_test.go @@ -0,0 +1,37 @@ +package messages7_test + +import ( + "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" + "github.com/teeworlds-go/go-teeworlds-protocol/network7" + "github.com/teeworlds-go/go-teeworlds-protocol/packer" +) + +func TestVoteListAdd(t *testing.T) { + // unpack + fullChunk := []byte{0x40, 0x06, 0x06, 0x18, 0x01, 0x66, 0x6f, 0x6f, 0x00} + u := &packer.Unpacker{} + u.Reset(fullChunk) + + header := &chunk7.ChunkHeader{} + header.Unpack(u) + + msg, sys, err := u.GetMsgAndSys() + require.NoError(t, err) + require.Equal(t, network7.MsgGameSvVoteOptionListAdd, msg) + require.Equal(t, false, sys) + + listAdd := &messages7.SvVoteOptionListAdd{ChunkHeader: header} + listAdd.Unpack(u) + + require.Equal(t, 1, listAdd.NumOptions) + require.Equal(t, 1, len(listAdd.Descriptions)) + require.Equal(t, "foo", listAdd.Descriptions[0]) + require.Equal(t, 0, u.RemainingSize()) + + // pack + require.Equal(t, []byte{1, 'f', 'o', 'o', 0x00}, listAdd.Pack()) +} diff --git a/messages7/sv_vote_option_remove.go b/messages7/sv_vote_option_remove.go index ba6c88d..acfeb23 100644 --- a/messages7/sv_vote_option_remove.go +++ b/messages7/sv_vote_option_remove.go @@ -37,7 +37,7 @@ func (msg *SvVoteOptionRemove) Pack() []byte { } func (msg *SvVoteOptionRemove) Unpack(u *packer.Unpacker) error { - msg.Description = u.GetString() + msg.Description, _ = u.GetString() return nil } diff --git a/messages7/sv_vote_set.go b/messages7/sv_vote_set.go index 8e05325..d22e39c 100644 --- a/messages7/sv_vote_set.go +++ b/messages7/sv_vote_set.go @@ -48,8 +48,8 @@ func (msg *SvVoteSet) Unpack(u *packer.Unpacker) error { msg.ClientId = u.GetInt() msg.Type = network7.Vote(u.GetInt()) msg.Timeout = u.GetInt() - msg.Description = u.GetString() - msg.Reason = u.GetString() + msg.Description, _ = u.GetString() + msg.Reason, _ = u.GetString() return nil } diff --git a/object7/laser_test.go b/object7/laser_test.go new file mode 100644 index 0000000..61e4e15 --- /dev/null +++ b/object7/laser_test.go @@ -0,0 +1,79 @@ +package object7_test + +import ( + "testing" + + "github.com/teeworlds-go/go-teeworlds-protocol/internal/testutils/require" + "github.com/teeworlds-go/go-teeworlds-protocol/network7" + "github.com/teeworlds-go/go-teeworlds-protocol/object7" + "github.com/teeworlds-go/go-teeworlds-protocol/packer" +) + +func TestLaserStandalone(t *testing.T) { + // simple pack + laser := &object7.Laser{ + ItemId: 1, + X: 200, + Y: 301, + FromX: 20, + FromY: 40, + StartTick: 7812, + } + + { + // this is not verified against anything + want := []byte{3, 1, 136, 3, 173, 4, 20, 40, 132, 122} + got := laser.Pack() + + require.Equal(t, want, got) + } + + // repack + u := &packer.Unpacker{} + u.Reset(laser.Pack()) + typeId := u.GetInt() + require.Equal(t, network7.ObjLaser, typeId) + itemId := u.GetInt() + require.Equal(t, 1, itemId) + laser.Unpack(u) + + require.Equal(t, 200, laser.X) + require.Equal(t, 301, laser.Y) + require.Equal(t, 20, laser.FromX) + require.Equal(t, 40, laser.FromY) + require.Equal(t, 7812, laser.StartTick) +} + +func TestLaserStandaloneAllZeros(t *testing.T) { + // simple pack + laser := &object7.Laser{ + ItemId: 0, + X: 0, + Y: 0, + FromX: 0, + FromY: 0, + StartTick: 0, + } + + { + want := []byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + got := laser.Pack() + + require.Equal(t, want, got) + } + + // repack + u := &packer.Unpacker{} + u.Reset(laser.Pack()) + typeId := u.GetInt() + require.Equal(t, network7.ObjLaser, typeId) + itemId := u.GetInt() + require.Equal(t, 0, itemId) + laser.Unpack(u) + + { + want := 0 + got := laser.X + require.Equal(t, want, got) + } +} diff --git a/object7/snap_object.go b/object7/snap_object.go index 72645db..dbbd03a 100644 --- a/object7/snap_object.go +++ b/object7/snap_object.go @@ -1,8 +1,6 @@ package object7 import ( - "log" - "github.com/teeworlds-go/go-teeworlds-protocol/network7" "github.com/teeworlds-go/go-teeworlds-protocol/packer" ) @@ -61,8 +59,8 @@ func NewObject(typeId int, itemId int) SnapObject { return &Damage{ItemId: itemId} } - // TODO: remove this is just for debugging - log.Fatalf("unknown item type %d\n", typeId) + // TODO: add this panic and remove it again once all tests pass + // log.Panicf("unknown item type %d\n", typeId) unknown := &Unknown{ ItemId: itemId, diff --git a/packer/packer.go b/packer/packer.go index ffd254d..36a2e44 100644 --- a/packer/packer.go +++ b/packer/packer.go @@ -1,6 +1,8 @@ package packer import ( + "errors" + "fmt" "slices" ) @@ -20,10 +22,33 @@ func (u *Unpacker) byte() byte { } // consume one byte -func (u *Unpacker) getByte() byte { +func (u *Unpacker) GetByte() (byte, error) { + if u.RemainingSize() < 1 { + return 0x00, errors.New("GetByte not enough data") + } b := u.data[u.idx] u.idx++ - return b + return b, nil +} + +func (u *Unpacker) GetMsgAndSys() (msgId int, system bool, err error) { + msg := u.GetInt() + sys := msg&1 != 0 + msg >>= 1 + return msg, sys, nil +} + +func (u *Unpacker) GetRaw(size int) ([]byte, error) { + if size < 0 { + return nil, fmt.Errorf("GetRaw called with negative size %d", size) + } + end := u.idx + size + if end > u.Size() { + return nil, fmt.Errorf("GetRaw can not read size %d not enough data", size) + } + b := u.data[u.idx:end] + u.idx += size + return b, nil } func (u *Unpacker) Data() []byte { @@ -50,13 +75,16 @@ const ( SanitizeSkipWhitespaces = 4 ) -func (u *Unpacker) GetStringSanitized(sanitizeType int) string { +func (u *Unpacker) GetStringSanitized(sanitizeType int) (string, error) { bytes := []byte{} skipping := sanitizeType&SanitizeSkipWhitespaces != 0 for { - b := u.getByte() + b, err := u.GetByte() + if err != nil { + return "", err + } if b == 0x00 { break } @@ -81,10 +109,10 @@ func (u *Unpacker) GetStringSanitized(sanitizeType int) string { bytes = append(bytes, b) } - return string(bytes) + return string(bytes), nil } -func (u *Unpacker) GetString() string { +func (u *Unpacker) GetString() (string, error) { return u.GetStringSanitized(Sanitize) } @@ -197,3 +225,10 @@ func UnpackInt(data []byte) int { res ^= -sign return res } + +func UnpackMsgAndSys(data []byte) (msgId int, system bool) { + msg := UnpackInt(data) + sys := msg&1 != 0 + msg >>= 1 + return msg, sys +} diff --git a/packer/packer_state_test.go b/packer/packer_state_test.go index 08a1be3..c1bdb9a 100644 --- a/packer/packer_state_test.go +++ b/packer/packer_state_test.go @@ -68,12 +68,12 @@ func TestUnpackClientInfo(t *testing.T) { { // name want := "gopher" - got := u.GetString() + got, _ := u.GetString() require.Equal(t, want, got) // clan want = "" - got = u.GetString() + got, _ = u.GetString() require.Equal(t, want, got) } @@ -87,7 +87,7 @@ func TestUnpackClientInfo(t *testing.T) { { // body want := "greensward" - got := u.GetString() + got, _ := u.GetString() require.Equal(t, want, got) } } @@ -120,7 +120,7 @@ func TestUnpackString(t *testing.T) { u.Reset([]byte{'f', 'o', 'o', 0x00}) want := "foo" - got := u.GetString() + got, _ := u.GetString() require.Equal(t, want, got) } @@ -129,11 +129,11 @@ func TestUnpackTwoStrings(t *testing.T) { u.Reset([]byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00}) want := "foo" - got := u.GetString() + got, _ := u.GetString() require.Equal(t, want, got) want = "bar" - got = u.GetString() + got, _ = u.GetString() require.Equal(t, want, got) } @@ -156,11 +156,11 @@ func TestUnpackMixed(t *testing.T) { // strings { want := "foo" - got := u.GetString() + got, _ := u.GetString() require.Equal(t, want, got) want = "bar" - got = u.GetString() + got, _ = u.GetString() require.Equal(t, want, got) } diff --git a/snapshot7/full_ddnet_server_test.go b/snapshot7/full_ddnet_server_test.go new file mode 100644 index 0000000..1acc53f --- /dev/null +++ b/snapshot7/full_ddnet_server_test.go @@ -0,0 +1,266 @@ +package snapshot7_test + +import ( + "fmt" + "testing" + + "github.com/teeworlds-go/go-teeworlds-protocol/internal/testutils/require" + "github.com/teeworlds-go/go-teeworlds-protocol/protocol7" +) + +// -------------------------------- +// snap single +// -------------------------------- + +func TestDDNetFullServerSnapSingle(t *testing.T) { + // vanilla client connected to almost full ddnet rus server + // dumped with tcpdump + // libtw2 dissector details + // this is the n-th snapshot that is a snap single the prior snapshots were partials snaps + // + // User Datagram Protocol, Src Port: 8316, Dst Port: 51479 + // Teeworlds 0.7 Protocol packet + // Teeworlds 0.7 Protocol chunk: sys.snap_single + // Header (non-vital) + // Message: sys.snap_single + // Tick: 11271652 + // Delta tick: 22 + // Crc: 1032529567 + // Data (114 bytes) + dump := []byte{ + 0x10, 0x04, 0x01, 0x1f, 0x5a, 0x5e, 0xd8, + 0x28, 0x15, 0xa6, 0x6c, 0xfb, 0x4a, 0xd3, 0xd6, 0xd3, 0xe0, 0x54, 0x85, 0x09, 0x72, 0xc4, + 0x0d, 0x2e, 0xb6, 0xe9, 0x2f, 0xbc, 0xfd, 0xb9, 0x5d, 0xd0, 0x35, 0x5d, 0x5e, 0xcf, 0xb5, 0x35, + 0xeb, 0x92, 0xe8, 0xbe, 0xbb, 0x9f, 0xd1, 0x7f, 0x17, 0x24, 0x2d, 0xb8, 0xfa, 0x7f, 0x5f, 0xf5, + 0x38, 0x7b, 0x7a, 0xc1, 0x47, 0x73, 0xeb, 0x4a, 0xd0, 0x62, 0x02, 0x17, 0x6d, 0xd9, 0xe2, 0x58, + 0x57, 0x68, 0xc9, 0xe6, 0x35, 0xf9, 0x0a, 0xd3, 0xd6, 0x7b, 0xf6, 0x5d, 0x90, 0xb4, 0x10, 0xfa, + 0x9a, 0x7c, 0x85, 0x69, 0xeb, 0xbf, 0xe1, 0x2b, 0x4d, 0x5b, 0xef, 0x1e, 0x4f, 0xea, 0x85, 0xe2, + 0x06, + } + + conn := protocol7.Session{} + + packet := protocol7.Packet{} + err := packet.Unpack(dump) + require.NoError(t, err) + + conn.Ack = packet.Header.Ack + repack := packet.Pack(&conn) + require.Equal(t, dump, repack) +} + +func TestSvEmoticonAndSnapSingle(t *testing.T) { + // Teeworlds 0.7 Protocol packet + // Teeworlds 0.7 Protocol chunk: game.sv_emoticon + // Header (vital: 234) + // Message: game.sv_emoticon + // Client id: 39 + // Emoticon: exclamation + // Teeworlds 0.7 Protocol chunk: sys.snap_single + // Header (non-vital) + // Message: sys.snap_single + // Tick: 11271828 + // Delta tick: 24 + // Crc: 1021386082 + // Data (190 bytes) + dump := []byte{ + 0x10, 0x05, 0x02, 0x1f, 0x5a, 0x5e, + 0xd8, 0x4a, 0x16, 0xd4, 0x0d, 0x2f, 0x0b, 0x2d, 0xfc, 0x4d, 0x19, 0xae, 0xd4, 0xb4, 0xf5, 0x10, + 0x9f, 0xa1, 0x48, 0x8b, 0x2b, 0xbb, 0xd1, 0x15, 0x48, 0x5a, 0xce, 0x52, 0xed, 0xef, 0x97, 0x58, + 0xef, 0xa2, 0x29, 0x6d, 0x82, 0xb7, 0x6b, 0xb6, 0x91, 0x1a, 0x1b, 0x4e, 0x72, 0xbb, 0x1d, 0x7d, + 0xcf, 0x71, 0x04, 0x55, 0x09, 0x5f, 0x21, 0xfe, 0x2e, 0x48, 0x5a, 0xb8, 0xa8, 0xff, 0x87, 0x42, + 0x26, 0x55, 0xa2, 0x9e, 0xeb, 0x22, 0x2d, 0x37, 0xc6, 0xb0, 0xa4, 0x33, 0x9e, 0x96, 0x1b, 0x0b, + 0xfc, 0xab, 0xaa, 0x87, 0x37, 0x5c, 0xa9, 0x69, 0xeb, 0x19, 0x92, 0xe8, 0x3b, 0x56, 0x3e, 0x0b, + 0x5b, 0x80, 0x73, 0xea, 0xb4, 0x50, 0xaa, 0x54, 0x6f, 0x49, 0xa2, 0x0b, 0xac, 0x38, 0x15, 0x3c, + 0x66, 0x71, 0xc4, 0x5d, 0x90, 0xb4, 0x80, 0xd7, 0x03, 0x60, 0x53, 0x14, 0xa7, 0x5e, 0xf8, 0x90, + 0xa7, 0x17, 0x3c, 0xa1, 0x17, 0xbe, 0x2e, 0x54, 0xa0, 0xde, 0x0f, 0xe4, 0x2d, 0xe8, 0xcc, 0x44, + 0xc2, 0x2f, 0x85, 0x36, 0x7f, 0x17, 0x24, 0x2d, 0x84, 0xfe, 0x9f, 0x25, 0x52, 0xaf, 0x57, 0xdc, + 0x00, + } + + conn := protocol7.Session{} + + packet := protocol7.Packet{} + err := packet.Unpack(dump) + require.NoError(t, err) + + conn.Ack = packet.Header.Ack + repack := packet.Pack(&conn) + + fmt.Printf("repack: %x\n", repack) + fmt.Printf("dump: %x\n", dump) + + require.Equal(t, dump, repack) +} + +// -------------------------------- +// multi part snaps +// -------------------------------- + +func TestVoteOptionListAddPlusTwoSnaps(t *testing.T) { + // vanilla client connected to almost full ddnet rus server + // dumped with tcpdump + // + // these are two consecutive packets + // - packet1: vote_list, snap (part 1/2) + // - packet2: snap (part 2/2) + + // Teeworlds 0.7 Protocol packet + // Teeworlds 0.7 Protocol chunk: game.sv_vote_option_list_add + // Teeworlds 0.7 Protocol chunk: sys.snap + // Header (non-vital) + // Message: sys.snap + // Tick: 11271610 + // Delta tick: 11271611 + // Num parts: 2 + // Part: 0 + // Crc: 986678629 + // Data (900 bytes) + dumpVoteListAndPart1 := []byte{ + 0x10, 0x04, 0x02, 0x1f, 0x5a, 0x5e, + 0xd8, 0x5a, 0xd9, 0x55, 0x86, 0xd8, 0xb5, 0xf6, 0x35, 0x1c, 0x0d, 0xe7, 0x8a, 0xc3, 0xda,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d, 0xbf, 0xb9, 0x52, 0xba, + 0x92, 0x16, 0x66, 0x31, 0x1f, 0x33, 0x6a, 0x72, 0x65, 0xd3, 0xd6, 0xfb, 0x2b, 0xd7, 0xc2, 0x5f, + 0x2a, 0xd2, 0x95, 0xb4, 0x50, 0x16, 0xf3, 0x51, 0x46, 0xb8, 0xae, 0x6c, 0xda, 0x7a, 0x7f, 0x97, + 0xda, 0x86, 0x9e, 0x47, 0xe9, 0x4a, 0x5a, 0xb8, 0x24, 0x30, 0x1f, 0x97, 0xd0, 0xc7, 0xd2, 0xf9, + 0x5e, 0xef, 0xcf, 0xba, 0x52, 0xba, 0x92, 0x16, 0xac, 0xc5, 0x7c, 0x58, 0xa3, 0x14, 0x8e, 0xb8, + 0x06, 0xbd, 0x3f, 0xa3, 0x4a, 0xe9, 0x4a, 0x5a, 0x30, 0x12, 0xd8, 0x5f, 0x77, 0x2d, 0xbe, 0xb6, + 0x8f, 0x31, 0x5d, 0x49, 0x0b, 0xdd, 0x62, 0x3e, 0x8a, 0x1b, + } + + // User Datagram Protocol, Src Port: 8316, Dst Port: 51479 + // Teeworlds 0.7 Protocol packet + // Teeworlds 0.7 Protocol chunk: sys.snap + // Header (non-vital) + // Message: sys.snap + // Tick: 11271610 + // Delta tick: 11271611 + // Num parts: 2 + // Part: 1 + // Crc: 986678629 + // Data (556 bytes) + dumpPart2 := []byte{ + + 0x10, 0x04, 0x01, 0x1f, 0x5a, 0x5e, + 0xd8, 0xc4, 0xb4, 0x38, 0x1c, 0x38, 0xc6, 0xb4, 0xf5, 0xee, 0xe5, 0x18, 0xd3, 0xd6, 0x0b, 0x64, + 0xa0, 0x98, 0x69, 0x95, 0xe6, 0x26, 0x8d, 0xe8, 0x46, 0x97, 0x9b, 0x98, 0xea, 0xf5, 0xfe, 0xa6, + 0x57, 0x4a, 0x57, 0xd2, 0xc2, 0x74, 0x31, 0x1f, 0xd3, 0x91, 0x1f, 0xc7, 0x98, 0xb6, 0xde, 0x1f, + 0x46, 0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 0xbe, 0x0b, 0x92, 0x16, 0x5c, 0x3d, 0x00, + 0x36, 0xc5, 0xaf, 0xa4, 0x17, 0x2e, 0xe3, 0x18, 0xd3, 0xd6, 0x53, 0x51, 0xa5, 0xd0, 0xf7, 0x39, + 0x8e, 0xe9, 0xd2, 0x99, 0x2b, 0xd5, 0x53, 0x53, 0xa5, 0xb2, 0xef, 0x5d, 0x40, 0xaa, 0xb3, 0x38, + 0xde, 0x05, 0x49, 0x0b, 0xa1, 0x07, 0xc0, 0xa6, 0x28, 0xee, 0x1a, 0x3c, 0xd9, 0x3d, 0x3d, 0x1d, + 0x68, 0x72, 0x8c, 0x69, 0xeb, 0x7d, 0x86, 0xf8, 0x41, 0xaa, 0x4a, 0x19, 0xe8, 0x57, 0x4a, 0xef, + 0x33, 0xc4, 0x0f, 0x52, 0x75, 0x01, 0xa9, 0xce, 0xe2, 0xed, 0xbb, 0x20, 0x69, 0x41, 0x07, 0x3d, + 0x00, 0x36, 0x45, 0x68, 0xa7, 0x4a, 0xe4, 0x68, 0xc5, 0x0d, + } + + // packet 1 + { + conn := protocol7.Session{} + + packet := protocol7.Packet{} + err := packet.Unpack(dumpVoteListAndPart1) + require.NoError(t, err) + + conn.Ack = packet.Header.Ack + repack := packet.Pack(&conn) + + fmt.Printf(" want: %x\n", dumpVoteListAndPart1) + fmt.Printf("repack: %x\n", repack) + + require.Equal(t, dumpVoteListAndPart1, repack) + } + + // packet 2 + { + conn := protocol7.Session{} + + packet := protocol7.Packet{} + err := packet.Unpack(dumpPart2) + require.NoError(t, err) + + conn.Ack = packet.Header.Ack + repack := packet.Pack(&conn) + require.Equal(t, dumpPart2, repack) + } + + // TODO: combine the snap parts together into one full snap +} diff --git a/snapshot7/snapshot_test.go b/snapshot7/simple_localhost_test.go similarity index 96% rename from snapshot7/snapshot_test.go rename to snapshot7/simple_localhost_test.go index 312cd89..9eb47ac 100644 --- a/snapshot7/snapshot_test.go +++ b/snapshot7/simple_localhost_test.go @@ -7,7 +7,7 @@ import ( "github.com/teeworlds-go/go-teeworlds-protocol/protocol7" ) -func TestRepackSnapshot(t *testing.T) { +func TestSmallVanillaServerSnapSingle(t *testing.T) { // snapshot captured with tcpdump // generated by a vanilla teeworlds 0.7.5 server // used the go-teeworlds-protocol sample client to connect to the server