diff options
author | rw <me@rwinslow.com> | 2015-06-24 11:53:44 -0400 |
---|---|---|
committer | rw <me@rwinslow.com> | 2015-06-24 11:53:44 -0400 |
commit | 4d305f5922c52935006987ef27f29516d3fd1583 (patch) | |
tree | 08bca7af73641f6502c16a55c951c1a1e35852a4 /tests | |
parent | 1e6f8f5b8c4d0407d7db750858e7863e07091958 (diff) | |
download | flatbuffers-4d305f5922c52935006987ef27f29516d3fd1583.tar.gz flatbuffers-4d305f5922c52935006987ef27f29516d3fd1583.tar.bz2 flatbuffers-4d305f5922c52935006987ef27f29516d3fd1583.zip |
Panic when nesting strings. Test panic scenarios.
Also add a new `insideObject` boolean to the Builder to track whether an
object is currently being constructed. This fixes a bug with objects
that have zero fields.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/go_test.go | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/tests/go_test.go b/tests/go_test.go index 070045a1..18a6b8e2 100644 --- a/tests/go_test.go +++ b/tests/go_test.go @@ -68,6 +68,13 @@ func TestAll(t *testing.T) { // expected bytes (does not use any schema): CheckByteLayout(t.Fatalf) + // Verify that panics are raised during exceptional conditions: + CheckNotInObjectError(t.Fatalf) + CheckObjectIsNestedError(t.Fatalf) + CheckStringIsNestedError(t.Fatalf) + CheckByteStringIsNestedError(t.Fatalf) + CheckStructIsNotInlineError(t.Fatalf) + // Verify that using the generated Go code builds a buffer without // returning errors: generated, off := CheckGeneratedBuild(t.Fatalf) @@ -540,7 +547,7 @@ func CheckByteLayout(fail func(string, ...interface{})) { // We use escape codes here so that editors without unicode support // aren't bothered: uni_str := "\u65e5\u672c\u8a9e" - b.CreateString(uni_str) + b.CreateString(uni_str) check([]byte{9, 0, 0, 0, 230, 151, 165, 230, 156, 172, 232, 170, 158, 0, // null-terminated, 2-byte pad 0, 0}) @@ -1096,6 +1103,76 @@ func CheckVtableDeduplication(fail func(string, ...interface{})) { testTable(table2, 0, 77, 88, 99) } +// CheckNotInObjectError verifies that `EndObject` fails if not inside an +// object. +func CheckNotInObjectError(fail func(string, ...interface{})) { + b := flatbuffers.NewBuilder(0) + + defer func() { + r := recover() + if r == nil { + fail("expected panic in CheckNotInObjectError") + } + }() + b.EndObject() +} + +// CheckObjectIsNestedError verifies that an object can not be created inside +// another object. +func CheckObjectIsNestedError(fail func(string, ...interface{})) { + b := flatbuffers.NewBuilder(0) + b.StartObject(0) + defer func() { + r := recover() + if r == nil { + fail("expected panic in CheckObjectIsNestedError") + } + }() + b.StartObject(0) +} + +// CheckStringIsNestedError verifies that a string can not be created inside +// another object. +func CheckStringIsNestedError(fail func(string, ...interface{})) { + b := flatbuffers.NewBuilder(0) + b.StartObject(0) + defer func() { + r := recover() + if r == nil { + fail("expected panic in CheckStringIsNestedError") + } + }() + b.CreateString("foo") +} + +// CheckByteStringIsNestedError verifies that a bytestring can not be created +// inside another object. +func CheckByteStringIsNestedError(fail func(string, ...interface{})) { + b := flatbuffers.NewBuilder(0) + b.StartObject(0) + defer func() { + r := recover() + if r == nil { + fail("expected panic in CheckByteStringIsNestedError") + } + }() + b.CreateByteString([]byte("foo")) +} + +// CheckStructIsNotInlineError verifies that writing a struct in a location +// away from where it is used will cause a panic. +func CheckStructIsNotInlineError(fail func(string, ...interface{})) { + b := flatbuffers.NewBuilder(0) + b.StartObject(0) + defer func() { + r := recover() + if r == nil { + fail("expected panic in CheckStructIsNotInlineError") + } + }() + b.PrependStructSlot(0, 1, 0) +} + // CheckDocExample checks that the code given in FlatBuffers documentation // is syntactically correct. func CheckDocExample(buf []byte, off flatbuffers.UOffsetT, fail func(string, ...interface{})) { |