Better syntax error handling

This commit is contained in:
Tnze
2021-06-22 10:01:41 +08:00
parent d730153750
commit 723303ce8d
4 changed files with 115 additions and 27 deletions

View File

@ -2,6 +2,7 @@ package nbt
import (
"bytes"
"strings"
"testing"
)
@ -83,3 +84,28 @@ func BenchmarkEncoder_WriteSNBT_bigTest(b *testing.B) {
buf.Reset()
}
}
func Test_WriteSNBT_nestingList(t *testing.T) {
var buf bytes.Buffer
e := NewEncoder(&buf)
// Our maximum supported nesting depth is 10000.
// The nesting depth of 10001 is 10000
err := e.WriteSNBT(strings.Repeat("[", 10001) + strings.Repeat("]", 10001))
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))
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))
if err == nil {
t.Error("Exceeded the maximum depth of support, but no error was reported")
}
}