Document append. SNBT encoding bug fix (TagList)

This commit is contained in:
Tnze
2021-07-09 16:23:26 +08:00
parent 08d2c03a58
commit 6500fbeaab
7 changed files with 139 additions and 116 deletions

View File

@ -6,56 +6,69 @@ import (
"testing"
)
func TestEncoder_WriteSNBT(t *testing.T) {
type testCase struct {
snbt StringifiedMessage
tagType byte
data []byte
}
var testCases = []testCase{
{`10b`, TagByte, []byte{1, 0, 0, 10}},
{`12S`, TagShort, []byte{2, 0, 0, 0, 12}},
{`0`, TagInt, []byte{3, 0, 0, 0, 0, 0, 0}},
{`12L`, TagLong, []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12}},
{`""`, TagString, []byte{8, 0, 0, 0, 0}},
{`'""' `, TagString, []byte{8, 0, 0, 0, 2, '"', '"'}},
{`"ab\"c\""`, TagString, []byte{8, 0, 0, 0, 5, 'a', 'b', '"', 'c', '"'}},
{` "1\\23"`, TagString, []byte{8, 0, 0, 0, 4, '1', '\\', '2', '3'}},
{`{}`, TagCompound, []byte{10, 0, 0, 0}},
{`{a:1b}`, TagCompound, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}},
{`{ a : 1b }`, TagCompound, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}},
{`{b:1,2:c}`, TagCompound, []byte{10, 0, 0, 3, 0, 1, 'b', 0, 0, 0, 1, 8, 0, 1, '2', 0, 1, 'c', 0}},
{`{c:{d:{}}}`, TagCompound, []byte{10, 0, 0, 10, 0, 1, 'c', 10, 0, 1, 'd', 0, 0, 0}},
{`{h:{},"i":{}}`, TagCompound, []byte{10, 0, 0, 10, 0, 1, 'h', 0, 10, 0, 1, 'i', 0, 0}},
{`[]`, TagList, []byte{9, 0, 0, 0, 0, 0, 0, 0}},
{`[1b,2b,3b]`, TagList, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[ 1b , 2b , 3b ]`, TagList, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[a,"b",'c']`, TagList, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}},
{`[{},{a:1b},{}]`, TagList, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[ { } , { a : 1b } , { } ] `, TagList, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[[],[]]`, TagList, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{`[B; ]`, TagByteArray, []byte{7, 0, 0, 0, 0, 0, 0}},
{`[B; 1b ,2B,3B]`, TagByteArray, []byte{7, 0, 0, 0, 0, 0, 3, 1, 2, 3}},
{`[I;]`, TagIntArray, []byte{11, 0, 0, 0, 0, 0, 0}},
{`[I; 1, 2 ,3]`, TagIntArray, []byte{11, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}},
{`[L;]`, TagLongArray, []byte{12, 0, 0, 0, 0, 0, 0}},
{`[ L; 1L,2L,3L]`, TagLongArray, []byte{12, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}},
{`{d:[]}`, TagCompound, []byte{10, 0, 0, 9, 0, 1, 'd', 0, 0, 0, 0, 0, 0}},
{`{e:[]}`, TagCompound, []byte{10, 0, 0, 9, 0, 1, 'e', 0, 0, 0, 0, 0, 0}},
{`{f:[], g:[]}`, TagCompound, []byte{10, 0, 0, 9, 0, 1, 'f', 0, 0, 0, 0, 0, 9, 0, 1, 'g', 0, 0, 0, 0, 0, 0}},
}
func TestStringifiedMessage_TagType(t *testing.T) {
for i := range testCases {
got := testCases[i].snbt.TagType()
if want := testCases[i].tagType; got != want {
t.Errorf("TagType assert for %s error: want % 02X, got % 02X", testCases[i].snbt, want, got)
}
}
}
func TestEncoder_writeSNBT(t *testing.T) {
var buf bytes.Buffer
e := NewEncoder(&buf)
testCases := []struct {
snbt string
nbt []byte
}{
{`10b`, []byte{1, 0, 0, 10}},
{`12S`, []byte{2, 0, 0, 0, 12}},
{`0`, []byte{3, 0, 0, 0, 0, 0, 0}},
{`12L`, []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12}},
{`""`, []byte{8, 0, 0, 0, 0}},
{`'""' `, []byte{8, 0, 0, 0, 2, '"', '"'}},
{`"ab\"c\""`, []byte{8, 0, 0, 0, 5, 'a', 'b', '"', 'c', '"'}},
{` "1\\23"`, []byte{8, 0, 0, 0, 4, '1', '\\', '2', '3'}},
{`{}`, []byte{10, 0, 0, 0}},
{`{a:1b}`, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}},
{`{ a : 1b }`, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}},
{`{b:1,2:c}`, []byte{10, 0, 0, 3, 0, 1, 'b', 0, 0, 0, 1, 8, 0, 1, '2', 0, 1, 'c', 0}},
{`{c:{d:{}}}`, []byte{10, 0, 0, 10, 0, 1, 'c', 10, 0, 1, 'd', 0, 0, 0}},
{`{h:{},"i":{}}`, []byte{10, 0, 0, 10, 0, 1, 'h', 0, 10, 0, 1, 'i', 0, 0}},
{`[]`, []byte{9, 0, 0, 0, 0, 0, 0, 0}},
{`[1b,2b,3b]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[ 1b , 2b , 3b ]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[a,"b",'c']`, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}},
{`[{},{a:1b},{}]`, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[ { } , { a : 1b } , { } ] `, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[[],[]]`, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{`[B; ]`, []byte{7, 0, 0, 0, 0, 0, 0}},
{`[B; 1b ,2B,3B]`, []byte{7, 0, 0, 0, 0, 0, 3, 1, 2, 3}},
{`[I;]`, []byte{11, 0, 0, 0, 0, 0, 0}},
{`[I; 1, 2 ,3]`, []byte{11, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}},
{`[L;]`, []byte{12, 0, 0, 0, 0, 0, 0}},
{`[ L; 1L,2L,3L]`, []byte{12, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}},
{`{d:[]}`, []byte{10, 0, 0, 9, 0, 1, 'd', 0, 0, 0, 0, 0, 0}},
{`{e:[]}`, []byte{10, 0, 0, 9, 0, 1, 'e', 0, 0, 0, 0, 0, 0}},
{`{f:[], g:[]}`, []byte{10, 0, 0, 9, 0, 1, 'f', 0, 0, 0, 0, 0, 9, 0, 1, 'g', 0, 0, 0, 0, 0, 0}},
}
for i := range testCases {
buf.Reset()
if err := e.WriteSNBT(testCases[i].snbt); err != nil {
if err := e.Encode(testCases[i].snbt, ""); err != nil {
t.Errorf("Convert SNBT %q error: %v", testCases[i].snbt, err)
continue
}
want := testCases[i].nbt
want := testCases[i].data
got := buf.Bytes()
if !bytes.Equal(want, got) {
t.Errorf("Convert SNBT %q wrong:\nwant: % 02X\ngot: % 02X", testCases[i].snbt, want, got)
@ -67,7 +80,7 @@ func TestEncoder_WriteSNBT_bigTest(t *testing.T) {
var buf bytes.Buffer
e := NewEncoder(&buf)
err := e.WriteSNBT(bigTestSNBT)
err := e.Encode(StringifiedMessage(bigTestSNBT), "")
if err != nil {
t.Error(err)
}
@ -77,7 +90,7 @@ func BenchmarkEncoder_WriteSNBT_bigTest(b *testing.B) {
var buf bytes.Buffer
e := NewEncoder(&buf)
for i := 0; i < b.N; i++ {
err := e.WriteSNBT(bigTestSNBT)
err := e.Encode(StringifiedMessage(bigTestSNBT), "")
if err != nil {
b.Fatal(err)
}
@ -91,20 +104,23 @@ func Test_WriteSNBT_nestingList(t *testing.T) {
// Our maximum supported nesting depth is 10000.
// The nesting depth of 10001 is 10000
err := e.WriteSNBT(strings.Repeat("[", 10001) + strings.Repeat("]", 10001))
s := strings.Repeat("[", 10001) + strings.Repeat("]", 10001)
err := e.Encode(StringifiedMessage(s), "")
if err != nil {
t.Error(err)
}
// Following code should return error instant of panic.
buf.Reset()
err = e.WriteSNBT(strings.Repeat("[", 10002) + strings.Repeat("]", 10002))
s = strings.Repeat("[", 10002) + strings.Repeat("]", 10002)
err = e.Encode(StringifiedMessage(s), "")
if err == nil {
t.Error("Exceeded the maximum depth of support, but no error was reported")
}
// Panic test
buf.Reset()
err = e.WriteSNBT(strings.Repeat("[", 20000) + strings.Repeat("]", 20000))
s = strings.Repeat("[", 20000) + strings.Repeat("]", 20000)
err = e.Encode(StringifiedMessage(s), "")
if err == nil {
t.Error("Exceeded the maximum depth of support, but no error was reported")
}