go-teeworlds-protocol/packer/packer_state_test.go

174 lines
3 KiB
Go
Raw Normal View History

2024-06-17 05:11:59 +00:00
package packer
import (
"testing"
"github.com/teeworlds-go/go-teeworlds-protocol/internal/testutils/require"
2024-06-17 05:11:59 +00:00
)
2024-06-24 09:03:13 +00:00
// rest and remaining size
2024-06-21 05:14:01 +00:00
func TestUnpackRest(t *testing.T) {
u := Unpacker{}
u.Reset([]byte{0x01, 0xff, 0xaa})
{
want := 1
got := u.GetInt()
require.Equal(t, want, got)
2024-06-24 09:03:13 +00:00
want = 2
got = u.RemainingSize()
require.Equal(t, want, got)
2024-06-21 05:14:01 +00:00
}
{
want := []byte{0xff, 0xaa}
got := u.Rest()
require.Equal(t, want, got)
2024-06-21 05:14:01 +00:00
}
2024-06-24 09:03:13 +00:00
{
want := 0
got := u.RemainingSize()
require.Equal(t, want, got)
}
2024-06-21 05:14:01 +00:00
}
// client info
2024-06-18 05:08:56 +00:00
func TestUnpackClientInfo(t *testing.T) {
u := Unpacker{}
u.Reset([]byte{
0x24, 0x00, 0x01, 0x00, 0x67, 0x6f, 0x70, 0x68, 0x65, 0x72, 0x00,
0x00, 0x40, 0x67, 0x72, 0x65, 0x65, 0x6e, 0x73, 0x77, 0x61, 0x72,
0x64, 0x00, 0x64, 0x75, 0x6f, 0x64, 0x6f, 0x6e, 0x6e, 0x79, 0x00,
0x00, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x73,
0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x73, 0x74, 0x61,
0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x80, 0xfc, 0xaf, 0x05, 0xeb, 0x83, 0xd0, 0x0a, 0x80, 0xfe,
0x07, 0x80, 0xfe, 0x07, 0x80, 0xfe, 0x07, 0x80, 0xfe, 0x07, 0x00,
})
{
// message id
want := 36
got := u.GetInt()
require.Equal(t, want, got)
2024-06-18 05:08:56 +00:00
// client id
want = 0
got = u.GetInt()
require.Equal(t, want, got)
2024-06-18 05:08:56 +00:00
u.GetInt() // Local bool
u.GetInt() // Team int
}
{
// name
want := "gopher"
got, _ := u.GetString()
require.Equal(t, want, got)
2024-06-18 05:08:56 +00:00
// clan
want = ""
got, _ = u.GetString()
require.Equal(t, want, got)
2024-06-18 05:08:56 +00:00
}
{
// country
want := -1
got := u.GetInt()
require.Equal(t, want, got)
2024-06-18 05:08:56 +00:00
}
{
// body
want := "greensward"
got, _ := u.GetString()
require.Equal(t, want, got)
2024-06-18 05:08:56 +00:00
}
}
2024-06-17 05:11:59 +00:00
// unpack with state
func TestUnpackSimpleInts(t *testing.T) {
u := Unpacker{}
u.Reset([]byte{0x01, 0x02, 0x03, 0x0f})
want := 1
got := u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
want = 2
got = u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
want = 3
2024-06-17 05:11:59 +00:00
got = u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
want = 15
got = u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
}
func TestUnpackString(t *testing.T) {
u := Unpacker{}
u.Reset([]byte{'f', 'o', 'o', 0x00})
want := "foo"
got, _ := u.GetString()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
}
func TestUnpackTwoStrings(t *testing.T) {
u := Unpacker{}
u.Reset([]byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00})
want := "foo"
got, _ := u.GetString()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
want = "bar"
got, _ = u.GetString()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
}
func TestUnpackMixed(t *testing.T) {
u := Unpacker{}
u.Reset([]byte{0x0F, 0x0F, 'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00, 0x01})
// ints
{
want := 15
got := u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
want = 15
got = u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
}
// strings
{
want := "foo"
got, _ := u.GetString()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
want = "bar"
got, _ = u.GetString()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
}
// ints
{
want := 1
got := u.GetInt()
require.Equal(t, want, got)
2024-06-17 05:11:59 +00:00
}
}