Refactor unpacker and add some more snap tests

This commit is contained in:
ChillerDragon 2024-06-25 12:19:51 +08:00
parent 0492ca26ec
commit 94c6798e8b
33 changed files with 616 additions and 67 deletions

View file

@ -1,5 +1,11 @@
package chunk7 package chunk7
import (
"fmt"
"github.com/teeworlds-go/go-teeworlds-protocol/packer"
)
const ( const (
chunkFlagVital = 1 chunkFlagVital = 1
chunkFlagResend = 2 chunkFlagResend = 2
@ -49,13 +55,49 @@ func (header *ChunkHeader) Pack() []byte {
return data 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 flagBits := (data[0] >> 6) & 0x03
header.Flags.Vital = (flagBits & chunkFlagVital) != 0 header.Flags.Vital = (flagBits & chunkFlagVital) != 0
header.Flags.Resend = (flagBits & chunkFlagResend) != 0 header.Flags.Resend = (flagBits & chunkFlagResend) != 0
header.Size = (int(data[0]&0x3F) << 6) | (int(data[1]) & 0x3F) header.Size = (int(data[0]&0x3F) << 6) | (int(data[1]) & 0x3F)
if header.Flags.Vital { 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]) header.Seq = int((data[1]&0xC0)<<2) | int(data[2])
} }
return nil
} }

View file

@ -12,7 +12,7 @@ func TestBrokenNonVitalHeader(t *testing.T) {
header := ChunkHeader{} header := ChunkHeader{}
// {0x40, 0x3a, 0x01} // {0x40, 0x3a, 0x01}
header.Unpack([]byte{0x3a, 0x01}) header.UnpackRaw([]byte{0x3a, 0x01})
want := ChunkHeader{ want := ChunkHeader{
Flags: ChunkFlags{ Flags: ChunkFlags{
@ -33,7 +33,7 @@ func TestVitalHeaderMapChange(t *testing.T) {
// verified with libtw2 wireshark dissector // verified with libtw2 wireshark dissector
header := ChunkHeader{} header := ChunkHeader{}
header.Unpack([]byte{0x40, 0x3a, 0x01}) header.UnpackRaw([]byte{0x40, 0x3a, 0x01})
want := ChunkHeader{ want := ChunkHeader{
Flags: ChunkFlags{ Flags: ChunkFlags{
@ -51,7 +51,7 @@ func TestVitalHeaderMapChange(t *testing.T) {
func TestVitalHeader(t *testing.T) { func TestVitalHeader(t *testing.T) {
header := ChunkHeader{} header := ChunkHeader{}
header.Unpack([]byte{0x40, 0x10, 0x0a}) header.UnpackRaw([]byte{0x40, 0x10, 0x0a})
want := ChunkHeader{ want := ChunkHeader{
Flags: ChunkFlags{ Flags: ChunkFlags{
Vital: true, Vital: true,

View file

@ -11,7 +11,8 @@ func UnpackChunks(data []byte) []Chunk {
for i < payloadSize { for i < payloadSize {
chunk := Chunk{} 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 i += 2 // header
if chunk.Header.Flags.Vital { if chunk.Header.Flags.Vital {
i++ i++

View file

@ -43,7 +43,7 @@ func (msg *ClSay) Pack() []byte {
func (msg *ClSay) Unpack(u *packer.Unpacker) error { func (msg *ClSay) Unpack(u *packer.Unpacker) error {
msg.Mode = network7.ChatMode(u.GetInt()) msg.Mode = network7.ChatMode(u.GetInt())
msg.TargetId = u.GetInt() msg.TargetId = u.GetInt()
msg.Message = u.GetString() msg.Message, _ = u.GetString()
return nil return nil
} }

View file

@ -77,15 +77,15 @@ func (msg *ClStartInfo) Pack() []byte {
} }
func (msg *ClStartInfo) Unpack(u *packer.Unpacker) error { func (msg *ClStartInfo) Unpack(u *packer.Unpacker) error {
msg.Name = u.GetString() msg.Name, _ = u.GetString()
msg.Clan = u.GetString() msg.Clan, _ = u.GetString()
msg.Country = u.GetInt() msg.Country = u.GetInt()
msg.Body = u.GetString() msg.Body, _ = u.GetString()
msg.Marking = u.GetString() msg.Marking, _ = u.GetString()
msg.Decoration = u.GetString() msg.Decoration, _ = u.GetString()
msg.Hands = u.GetString() msg.Hands, _ = u.GetString()
msg.Feet = u.GetString() msg.Feet, _ = u.GetString()
msg.Eyes = u.GetString() msg.Eyes, _ = u.GetString()
msg.CustomColorBody = u.GetInt() != 0 msg.CustomColorBody = u.GetInt() != 0
msg.CustomColorMarking = u.GetInt() != 0 msg.CustomColorMarking = u.GetInt() != 0
msg.CustomColorDecoration = u.GetInt() != 0 msg.CustomColorDecoration = u.GetInt() != 0

View file

@ -37,7 +37,7 @@ func (msg *CtrlClose) Pack() []byte {
func (msg *CtrlClose) Unpack(u *packer.Unpacker) error { func (msg *CtrlClose) Unpack(u *packer.Unpacker) error {
// TODO: sanitize // TODO: sanitize
msg.Reason = u.GetString() msg.Reason, _ = u.GetString()
return nil return nil
} }

View file

@ -59,8 +59,8 @@ func (msg *Info) Pack() []byte {
} }
func (msg *Info) Unpack(u *packer.Unpacker) error { func (msg *Info) Unpack(u *packer.Unpacker) error {
msg.Version = u.GetString() msg.Version, _ = u.GetString()
msg.Password = u.GetString() msg.Password, _ = u.GetString()
msg.ClientVersion = u.GetInt() msg.ClientVersion = u.GetInt()
return nil return nil
} }

View file

@ -47,7 +47,7 @@ func (msg *MapChange) Pack() []byte {
} }
func (msg *MapChange) Unpack(u *packer.Unpacker) error { func (msg *MapChange) Unpack(u *packer.Unpacker) error {
msg.Name = u.GetString() msg.Name, _ = u.GetString()
msg.Crc = u.GetInt() msg.Crc = u.GetInt()
msg.Size = u.GetInt() msg.Size = u.GetInt()
msg.NumResponseChunksPerRequest = u.GetInt() msg.NumResponseChunksPerRequest = u.GetInt()

View file

@ -37,7 +37,7 @@ func (msg *MaplistEntryAdd) Pack() []byte {
} }
func (msg *MaplistEntryAdd) Unpack(u *packer.Unpacker) error { func (msg *MaplistEntryAdd) Unpack(u *packer.Unpacker) error {
msg.MapName = u.GetString() msg.MapName, _ = u.GetString()
return nil return nil
} }

View file

@ -37,7 +37,7 @@ func (msg *MaplistEntryRem) Pack() []byte {
} }
func (msg *MaplistEntryRem) Unpack(u *packer.Unpacker) error { func (msg *MaplistEntryRem) Unpack(u *packer.Unpacker) error {
msg.MapName = u.GetString() msg.MapName, _ = u.GetString()
return nil return nil
} }

View file

@ -37,7 +37,7 @@ func (msg *RconAuth) Pack() []byte {
} }
func (msg *RconAuth) Unpack(u *packer.Unpacker) error { func (msg *RconAuth) Unpack(u *packer.Unpacker) error {
msg.Password = u.GetString() msg.Password, _ = u.GetString()
return nil return nil
} }

View file

@ -37,7 +37,7 @@ func (msg *RconCmd) Pack() []byte {
} }
func (msg *RconCmd) Unpack(u *packer.Unpacker) error { func (msg *RconCmd) Unpack(u *packer.Unpacker) error {
msg.Command = u.GetString() msg.Command, _ = u.GetString()
return nil return nil
} }

View file

@ -41,9 +41,9 @@ func (msg *RconCmdAdd) Pack() []byte {
} }
func (msg *RconCmdAdd) Unpack(u *packer.Unpacker) error { func (msg *RconCmdAdd) Unpack(u *packer.Unpacker) error {
msg.Name = u.GetString() msg.Name, _ = u.GetString()
msg.Help = u.GetString() msg.Help, _ = u.GetString()
msg.Params = u.GetString() msg.Params, _ = u.GetString()
return nil return nil
} }

View file

@ -37,7 +37,7 @@ func (msg *RconCmdRem) Pack() []byte {
} }
func (msg *RconCmdRem) Unpack(u *packer.Unpacker) error { func (msg *RconCmdRem) Unpack(u *packer.Unpacker) error {
msg.Name = u.GetString() msg.Name, _ = u.GetString()
return nil return nil
} }

View file

@ -37,7 +37,7 @@ func (msg *RconLine) Pack() []byte {
} }
func (msg *RconLine) Unpack(u *packer.Unpacker) error { func (msg *RconLine) Unpack(u *packer.Unpacker) error {
msg.Line = u.GetString() msg.Line, _ = u.GetString()
return nil return nil
} }

View file

@ -57,11 +57,11 @@ func (msg *ServerInfo) Pack() []byte {
} }
func (msg *ServerInfo) Unpack(u *packer.Unpacker) error { func (msg *ServerInfo) Unpack(u *packer.Unpacker) error {
msg.Version = u.GetString() msg.Version, _ = u.GetString()
msg.Name = u.GetString() msg.Name, _ = u.GetString()
msg.Hostname = u.GetString() msg.Hostname, _ = u.GetString()
msg.MapName = u.GetString() msg.MapName, _ = u.GetString()
msg.GameType = u.GetString() msg.GameType, _ = u.GetString()
msg.Flags = u.GetInt() msg.Flags = u.GetInt()
msg.SkillLevel = u.GetInt() msg.SkillLevel = u.GetInt()
msg.PlayerCount = u.GetInt() msg.PlayerCount = u.GetInt()

View file

@ -33,7 +33,7 @@ func (msg *SvBroadcast) Pack() []byte {
} }
func (msg *SvBroadcast) Unpack(u *packer.Unpacker) error { func (msg *SvBroadcast) Unpack(u *packer.Unpacker) error {
msg.Message = u.GetString() msg.Message, _ = u.GetString()
return nil return nil
} }

View file

@ -46,7 +46,7 @@ func (msg *SvChat) Unpack(u *packer.Unpacker) error {
msg.Mode = network7.ChatMode(u.GetInt()) msg.Mode = network7.ChatMode(u.GetInt())
msg.ClientId = u.GetInt() msg.ClientId = u.GetInt()
msg.TargetId = u.GetInt() msg.TargetId = u.GetInt()
msg.Message = u.GetString() msg.Message, _ = u.GetString()
return nil return nil
} }

View file

@ -86,15 +86,15 @@ func (info *SvClientInfo) Unpack(u *packer.Unpacker) error {
info.ClientId = u.GetInt() info.ClientId = u.GetInt()
info.Local = u.GetInt() != 0 info.Local = u.GetInt() != 0
info.Team = u.GetInt() info.Team = u.GetInt()
info.Name = u.GetString() info.Name, _ = u.GetString()
info.Clan = u.GetString() info.Clan, _ = u.GetString()
info.Country = u.GetInt() info.Country = u.GetInt()
info.Body = u.GetString() info.Body, _ = u.GetString()
info.Marking = u.GetString() info.Marking, _ = u.GetString()
info.Decoration = u.GetString() info.Decoration, _ = u.GetString()
info.Hands = u.GetString() info.Hands, _ = u.GetString()
info.Feet = u.GetString() info.Feet, _ = u.GetString()
info.Eyes = u.GetString() info.Eyes, _ = u.GetString()
info.CustomColorBody = u.GetInt() != 0 info.CustomColorBody = u.GetInt() != 0
info.CustomColorMarking = u.GetInt() != 0 info.CustomColorMarking = u.GetInt() != 0
info.CustomColorDecoration = u.GetInt() != 0 info.CustomColorDecoration = u.GetInt() != 0

View file

@ -11,6 +11,7 @@ import (
type SvEmoticon struct { type SvEmoticon struct {
ChunkHeader *chunk7.ChunkHeader ChunkHeader *chunk7.ChunkHeader
ClientId int
Emoticon network7.Emote Emoticon network7.Emote
} }
@ -32,11 +33,13 @@ func (msg *SvEmoticon) Vital() bool {
func (msg *SvEmoticon) Pack() []byte { func (msg *SvEmoticon) Pack() []byte {
return slices.Concat( return slices.Concat(
packer.PackInt(msg.ClientId),
packer.PackInt(int(msg.Emoticon)), packer.PackInt(int(msg.Emoticon)),
) )
} }
func (msg *SvEmoticon) Unpack(u *packer.Unpacker) error { func (msg *SvEmoticon) Unpack(u *packer.Unpacker) error {
msg.ClientId = u.GetInt()
msg.Emoticon = network7.Emote(u.GetInt()) msg.Emoticon = network7.Emote(u.GetInt())
return nil return nil

View file

@ -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)
}
}

View file

@ -33,7 +33,7 @@ func (msg *SvMotd) Pack() []byte {
} }
func (msg *SvMotd) Unpack(u *packer.Unpacker) error { func (msg *SvMotd) Unpack(u *packer.Unpacker) error {
msg.Message = u.GetString() msg.Message, _ = u.GetString()
return nil return nil
} }

View file

@ -37,7 +37,7 @@ func (msg *SvVoteOptionAdd) Pack() []byte {
} }
func (msg *SvVoteOptionAdd) Unpack(u *packer.Unpacker) error { func (msg *SvVoteOptionAdd) Unpack(u *packer.Unpacker) error {
msg.Description = u.GetString() msg.Description, _ = u.GetString()
return nil return nil
} }

View file

@ -52,7 +52,7 @@ func (msg *SvVoteOptionListAdd) Unpack(u *packer.Unpacker) error {
msg.NumOptions = u.GetInt() msg.NumOptions = u.GetInt()
msg.Descriptions = make([]string, msg.NumOptions) msg.Descriptions = make([]string, msg.NumOptions)
for i := 0; i < msg.NumOptions; i++ { for i := 0; i < msg.NumOptions; i++ {
msg.Descriptions[i] = u.GetString() msg.Descriptions[i], _ = u.GetString()
} }
return nil return nil

View file

@ -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())
}

View file

@ -37,7 +37,7 @@ func (msg *SvVoteOptionRemove) Pack() []byte {
} }
func (msg *SvVoteOptionRemove) Unpack(u *packer.Unpacker) error { func (msg *SvVoteOptionRemove) Unpack(u *packer.Unpacker) error {
msg.Description = u.GetString() msg.Description, _ = u.GetString()
return nil return nil
} }

View file

@ -48,8 +48,8 @@ func (msg *SvVoteSet) Unpack(u *packer.Unpacker) error {
msg.ClientId = u.GetInt() msg.ClientId = u.GetInt()
msg.Type = network7.Vote(u.GetInt()) msg.Type = network7.Vote(u.GetInt())
msg.Timeout = u.GetInt() msg.Timeout = u.GetInt()
msg.Description = u.GetString() msg.Description, _ = u.GetString()
msg.Reason = u.GetString() msg.Reason, _ = u.GetString()
return nil return nil
} }

79
object7/laser_test.go Normal file
View file

@ -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)
}
}

View file

@ -1,8 +1,6 @@
package object7 package object7
import ( import (
"log"
"github.com/teeworlds-go/go-teeworlds-protocol/network7" "github.com/teeworlds-go/go-teeworlds-protocol/network7"
"github.com/teeworlds-go/go-teeworlds-protocol/packer" "github.com/teeworlds-go/go-teeworlds-protocol/packer"
) )
@ -61,8 +59,8 @@ func NewObject(typeId int, itemId int) SnapObject {
return &Damage{ItemId: itemId} return &Damage{ItemId: itemId}
} }
// TODO: remove this is just for debugging // TODO: add this panic and remove it again once all tests pass
log.Fatalf("unknown item type %d\n", typeId) // log.Panicf("unknown item type %d\n", typeId)
unknown := &Unknown{ unknown := &Unknown{
ItemId: itemId, ItemId: itemId,

View file

@ -1,6 +1,8 @@
package packer package packer
import ( import (
"errors"
"fmt"
"slices" "slices"
) )
@ -20,10 +22,33 @@ func (u *Unpacker) byte() byte {
} }
// consume one 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] b := u.data[u.idx]
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 { func (u *Unpacker) Data() []byte {
@ -50,13 +75,16 @@ const (
SanitizeSkipWhitespaces = 4 SanitizeSkipWhitespaces = 4
) )
func (u *Unpacker) GetStringSanitized(sanitizeType int) string { func (u *Unpacker) GetStringSanitized(sanitizeType int) (string, error) {
bytes := []byte{} bytes := []byte{}
skipping := sanitizeType&SanitizeSkipWhitespaces != 0 skipping := sanitizeType&SanitizeSkipWhitespaces != 0
for { for {
b := u.getByte() b, err := u.GetByte()
if err != nil {
return "", err
}
if b == 0x00 { if b == 0x00 {
break break
} }
@ -81,10 +109,10 @@ func (u *Unpacker) GetStringSanitized(sanitizeType int) string {
bytes = append(bytes, b) 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) return u.GetStringSanitized(Sanitize)
} }
@ -197,3 +225,10 @@ func UnpackInt(data []byte) int {
res ^= -sign res ^= -sign
return res return res
} }
func UnpackMsgAndSys(data []byte) (msgId int, system bool) {
msg := UnpackInt(data)
sys := msg&1 != 0
msg >>= 1
return msg, sys
}

View file

@ -68,12 +68,12 @@ func TestUnpackClientInfo(t *testing.T) {
{ {
// name // name
want := "gopher" want := "gopher"
got := u.GetString() got, _ := u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
// clan // clan
want = "" want = ""
got = u.GetString() got, _ = u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
} }
@ -87,7 +87,7 @@ func TestUnpackClientInfo(t *testing.T) {
{ {
// body // body
want := "greensward" want := "greensward"
got := u.GetString() got, _ := u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
} }
} }
@ -120,7 +120,7 @@ func TestUnpackString(t *testing.T) {
u.Reset([]byte{'f', 'o', 'o', 0x00}) u.Reset([]byte{'f', 'o', 'o', 0x00})
want := "foo" want := "foo"
got := u.GetString() got, _ := u.GetString()
require.Equal(t, want, got) 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}) u.Reset([]byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00})
want := "foo" want := "foo"
got := u.GetString() got, _ := u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
want = "bar" want = "bar"
got = u.GetString() got, _ = u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
} }
@ -156,11 +156,11 @@ func TestUnpackMixed(t *testing.T) {
// strings // strings
{ {
want := "foo" want := "foo"
got := u.GetString() got, _ := u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
want = "bar" want = "bar"
got = u.GetString() got, _ = u.GetString()
require.Equal(t, want, got) require.Equal(t, want, got)
} }

View file

@ -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, 0x35,
0xab, 0xf0, 0x03, 0x6b, 0x5b, 0xc2, 0xda, 0x75, 0x8e, 0x86, 0x52, 0x5f, 0x7c, 0x0d, 0x6b, 0xd7,
0x6c, 0x62, 0x97, 0x87, 0x83, 0x13, 0x5c, 0x3b, 0x27, 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
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, 0xa5, 0x74, 0x25, 0x2d, 0x60, 0x88, 0xf9, 0xc0, 0x40, 0x96, 0xb4, 0xf8, 0x5e, 0xef, 0xaf,
0xa8, 0x52, 0xba, 0x92, 0x16, 0x8a, 0xc4, 0x7c, 0x14, 0xa1, 0x42, 0x5f, 0x9b, 0xea, 0xf5, 0xfe,
0xda, 0x4b, 0xa7, 0x7a, 0x57, 0xa4, 0x2b, 0x69, 0xa1,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a1, 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
}

View file

@ -7,7 +7,7 @@ import (
"github.com/teeworlds-go/go-teeworlds-protocol/protocol7" "github.com/teeworlds-go/go-teeworlds-protocol/protocol7"
) )
func TestRepackSnapshot(t *testing.T) { func TestSmallVanillaServerSnapSingle(t *testing.T) {
// snapshot captured with tcpdump // snapshot captured with tcpdump
// generated by a vanilla teeworlds 0.7.5 server // generated by a vanilla teeworlds 0.7.5 server
// used the go-teeworlds-protocol sample client to connect to the server // used the go-teeworlds-protocol sample client to connect to the server