From 451d517f977e7a4921f96524378b165f9cd2a5d6 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 26 Aug 2024 09:03:58 +0200 Subject: [PATCH] network mapblock tests --- parse.go | 6 +++--- parse_block_mapping.go | 5 +++++ parse_static_objects.go | 6 ++++++ parse_test.go | 33 +++++++++++++++++++++++++------- testdata/mapblock3516192727.bin | Bin 0 -> 329 bytes 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 testdata/mapblock3516192727.bin diff --git a/parse.go b/parse.go index 446d4f4..bd9c449 100644 --- a/parse.go +++ b/parse.go @@ -3,7 +3,7 @@ package mapparser import ( "encoding/binary" "errors" - "strconv" + "fmt" "github.com/minetest-go/types" ) @@ -46,11 +46,11 @@ func Parse(data []byte) (*types.MapBlock, error) { params_width := data[offset+1] if content_width != 2 { - return nil, errors.New("content_width = " + strconv.Itoa(int(content_width))) + return nil, fmt.Errorf("content_width unexpected: %d", content_width) } if params_width != 2 { - return nil, errors.New("params_width = " + strconv.Itoa(int(params_width))) + return nil, fmt.Errorf("params_width unexpected: %d", params_width) } // mapdata offset diff --git a/parse_block_mapping.go b/parse_block_mapping.go index 1d4f0b8..c8e1424 100644 --- a/parse_block_mapping.go +++ b/parse_block_mapping.go @@ -7,6 +7,11 @@ import ( ) func parseBlockMapping(data []byte, offset *int, mapblock *types.MapBlock) { + if *offset >= len(data) { + // eof + return + } + if len(data) > (*offset + 2) { // disk-data has per-block mapping, network-data has a global mapping numMappings := int(binary.BigEndian.Uint16(data[*offset:])) diff --git a/parse_static_objects.go b/parse_static_objects.go index 40aa3c6..23faecd 100644 --- a/parse_static_objects.go +++ b/parse_static_objects.go @@ -4,6 +4,12 @@ import "encoding/binary" func parseStaticObjects(data []byte, offset *int) { *offset++ //static objects version + + if *offset >= len(data) { + // eof + return + } + staticObjectsCount := int(binary.BigEndian.Uint16(data[*offset:])) *offset += 2 for i := 0; i < staticObjectsCount; i++ { diff --git a/parse_test.go b/parse_test.go index ae995db..ce57a4d 100644 --- a/parse_test.go +++ b/parse_test.go @@ -3,7 +3,7 @@ package mapparser import ( "encoding/json" "fmt" - "io/ioutil" + "os" "strconv" "testing" @@ -67,7 +67,7 @@ func validateMapblock(t *testing.T, mapblock *types.MapBlock) { func TestParse(t *testing.T) { - data, err := ioutil.ReadFile("testdata/0.0.0") + data, err := os.ReadFile("testdata/0.0.0") if err != nil { t.Error(err) } @@ -168,7 +168,7 @@ func TestParseError2(t *testing.T) { func TestParseZstd(t *testing.T) { - data, err := ioutil.ReadFile("testdata/zstd-block.bin") + data, err := os.ReadFile("testdata/zstd-block.bin") if err != nil { t.Error(err) } @@ -186,7 +186,7 @@ func TestParseZstd(t *testing.T) { func TestParse2(t *testing.T) { - data, err := ioutil.ReadFile("testdata/11.0.2") + data, err := os.ReadFile("testdata/11.0.2") if err != nil { t.Error(err) } @@ -210,7 +210,7 @@ func TestParse2(t *testing.T) { func TestParse3(t *testing.T) { - data, err := ioutil.ReadFile("testdata/0.1.0") + data, err := os.ReadFile("testdata/0.1.0") if err != nil { t.Error(err) } @@ -225,7 +225,7 @@ func TestParse3(t *testing.T) { } func TestParseMetadata(t *testing.T) { - data, err := ioutil.ReadFile("testdata/mb-with-metadata.bin") + data, err := os.ReadFile("testdata/mb-with-metadata.bin") if err != nil { t.Error(err) } @@ -244,7 +244,7 @@ func TestParseMetadata(t *testing.T) { } func TestParseNetworkBlock(t *testing.T) { - data, err := ioutil.ReadFile("testdata/network-blockdata.bin") + data, err := os.ReadFile("testdata/network-blockdata.bin") if err != nil { t.Error(err) } @@ -265,3 +265,22 @@ func TestParseNetworkBlock(t *testing.T) { } fmt.Println(string(str)) } + +func TestParseNetworkBlock2(t *testing.T) { + data, err := os.ReadFile("testdata/mapblock3516192727.bin") + if err != nil { + t.Error(err) + } + + mb, err := Parse(data) + + if err != nil { + t.Error(err) + } + + str, err := json.MarshalIndent(mb, "", " ") + if err != nil { + t.Error(err) + } + fmt.Println(string(str)) +} diff --git a/testdata/mapblock3516192727.bin b/testdata/mapblock3516192727.bin new file mode 100644 index 0000000000000000000000000000000000000000..ae33a72498971117b1018e69230d71690d537244 GIT binary patch literal 329 zcmV-P0k-}e1ONX50(hM5luHhTAP_{&8~_gCW{7w6?4$9Mp+Or0G$K`Rg&1S?BMjp( z4xa^cGFgl}#sjy%@?E&wpBL75<`Z}S3T_|pZ1yL{zuz6Vzxlu4r}p7rmc#Rv|7&xG z-4DV2BlwrZH68BXRQ5l^e=^kcRM|g&KJ2$^&#!ghzi({U)PB0(IRE_l0C{nZ*Yh88 zKY+Z1`7d`Gw!a9yA3$z6|FX9M{7VD>eG}(j{sh#X&)b3jzDe~jaVBcd=*`6Xr?%y8 zhT4;QC;9y2475mOl(N@aE2Y<*>;KU8^Ag>ktdIJ=^rxX8qF)!T6P>dCbvON;dF+pc z>qH0rv-Ef7(Mt04cjnPdGW7S%qn)Jo_spZIpg;Pff4>vR-@mO96zSj41Pb*>5ClOG b1VIo4K@j9Y`2k*5?gV(8V*mgE0RRC4WvQ{X literal 0 HcmV?d00001