Skip to content

Commit

Permalink
Backport upstream changes
Browse files Browse the repository at this point in the history
Summary:
Backport changes from upstream.

Specifically from apache/thrift@64c2a4b

Reviewed By: podtserkovskiy

Differential Revision: D65171077

fbshipit-source-id: b81946b28531ea294d3196337e166098a8057b85
  • Loading branch information
echistyakov authored and facebook-github-bot committed Nov 1, 2024
1 parent 50bcf32 commit 5963809
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 46 deletions.
3 changes: 1 addition & 2 deletions thrift/lib/go/thrift/compact_json_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ var _ types.Format = (*compactJSONFormat)(nil)
// NewCompactJSONFormat creates a new compact JSON Format.
func NewCompactJSONFormat(buffer io.ReadWriteCloser) types.Format {
v := &compactJSONFormat{simpleJSONFormat: newSimpleJSONFormat(buffer)}
v.parseContextStack = append(v.parseContextStack, int(_CONTEXT_IN_TOPLEVEL))
v.dumpContext = append(v.dumpContext, int(_CONTEXT_IN_TOPLEVEL))
v.resetContextStack()
return v
}

Expand Down
4 changes: 4 additions & 0 deletions thrift/lib/go/thrift/compact_json_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,7 @@ func TestWriteJSONProtocolMap(t *testing.T) {
}
trans.Close()
}

func TestCompactJSONProtocolUnmatchedBeginEnd(t *testing.T) {
UnmatchedBeginEndProtocolTest(t, NewCompactJSONFormat)
}
85 changes: 85 additions & 0 deletions thrift/lib/go/thrift/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,88 @@ func ReadWriteStruct(t testing.TB, p types.Format, buffer io.ReadWriter) {
WriteStruct(t, p, buffer)
ReadStruct(t, p, buffer)
}

func UnmatchedBeginEndProtocolTest(t *testing.T, formatFactory func(io.ReadWriteCloser) types.Format) {
// NOTE: not all protocol implementations do strict state check to
// return an error on unmatched Begin/End calls.
// This test is only meant to make sure that those unmatched Begin/End
// calls won't cause panic. There's no real "test" here.
trans := NewMemoryBuffer()
t.Run("Read", func(t *testing.T) {
t.Run("Message", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.ReadMessageEnd()
p.ReadMessageEnd()
})
t.Run("Struct", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.ReadStructEnd()
p.ReadStructEnd()
})
t.Run("Field", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.ReadFieldEnd()
p.ReadFieldEnd()
})
t.Run("Map", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.ReadMapEnd()
p.ReadMapEnd()
})
t.Run("List", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.ReadListEnd()
p.ReadListEnd()
})
t.Run("Set", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.ReadSetEnd()
p.ReadSetEnd()
})
})
t.Run("Write", func(t *testing.T) {
t.Run("Message", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.WriteMessageEnd()
p.WriteMessageEnd()
})
t.Run("Struct", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.WriteStructEnd()
p.WriteStructEnd()
})
t.Run("Field", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.WriteFieldEnd()
p.WriteFieldEnd()
})
t.Run("Map", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.WriteMapEnd()
p.WriteMapEnd()
})
t.Run("List", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.WriteListEnd()
p.WriteListEnd()
})
t.Run("Set", func(t *testing.T) {
trans.Reset()
p := formatFactory(trans)
p.WriteSetEnd()
p.WriteSetEnd()
})
})
trans.Close()
}
Loading

0 comments on commit 5963809

Please sign in to comment.