From a58425ea5623ad82193cfd7ba128e985ede0af25 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 16 Jun 2024 09:21:49 +0800 Subject: [PATCH] Add more chunk header tests --- chunk/chunk_test.go | 45 +++++++++++++++++++++++++++++++++++++++++- chunk/splitter_test.go | 27 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/chunk/chunk_test.go b/chunk/chunk_test.go index 11fc060..2cc743d 100644 --- a/chunk/chunk_test.go +++ b/chunk/chunk_test.go @@ -5,10 +5,53 @@ import ( "testing" ) +func TestBrokenNonVitalHeader(t *testing.T) { + // this is a real vital header at a wrong offset + // so it creates a actually non vital header with a size that is bigger than usual + // verified results with teeworlds-network/twnet_parser python lib + + header := ChunkHeader{} + // {0x40, 0x3a, 0x01} + header.Unpack([]byte{0x3a, 0x01}) + + want := ChunkHeader { + Flags: ChunkFlags { + Vital: false, + Resend: false, + }, + Size: 3713, + Seq: 0, + } + + if !reflect.DeepEqual(header, want) { + t.Errorf("got %v, wanted %v", header, want) + } +} + +func TestVitalHeaderMapChange(t *testing.T) { + // generated by vanilla teeworlds 0.7 server + // verified with libtw2 wireshark dissector + + header := ChunkHeader{} + header.Unpack([]byte{0x40, 0x3a, 0x01}) + + want := ChunkHeader { + Flags: ChunkFlags { + Vital: true, + Resend: false, + }, + Size: 58, + Seq: 1, + } + + if !reflect.DeepEqual(header, want) { + t.Errorf("got %v, wanted %v", header, want) + } +} + func TestVitalHeader(t *testing.T) { header := ChunkHeader{} header.Unpack([]byte{0x40, 0x10, 0x0a}) - want := ChunkHeader { Flags: ChunkFlags { Vital: true, diff --git a/chunk/splitter_test.go b/chunk/splitter_test.go index 92ff608..7676ec4 100644 --- a/chunk/splitter_test.go +++ b/chunk/splitter_test.go @@ -6,6 +6,33 @@ import ( "testing" ) +func TestSplitterMapChange(t *testing.T) { + // real map change packet from a vanilla teeworlds 0.7 server + // dumped with the go client + + // header := []byte{ + // 0x00, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, + // } + + payload := []byte { + 0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70, 0x73, + 0x00, 0x91, 0xa7, 0xf2, 0xa0, 0x05, 0x8b, 0x11, 0x08, 0xa8, 0x15, 0xa9, 0x19, 0x38, 0xaf, 0xfd, 0xb6, + 0xcb, 0xf1, 0xdb, 0x5a, 0xfc, 0x73, 0x34, 0xae, 0xa3, 0x68, 0xa7, 0xfa, 0x35, 0x54, 0x37, 0xf9, 0x39, + 0x96, 0xd6, 0x05, 0x25, 0xef, 0x7b, 0x22, 0x40, 0x9d, + } + + chunks := UnpackChunks(payload) + + { + got := len(chunks) + want := 1 + + if want != got { + t.Errorf("got %v, wanted %v", got, want) + } + } +} + func TestSplitter(t *testing.T) { // packet payload of real traffic //