Skip to content

Commit

Permalink
Fix protocol and add test (#916)
Browse files Browse the repository at this point in the history
For `Skip()`, if no type is recognized in switch statement, the function returns nil before. This PR fixed it to return an error.

This PR also adds a test for protocol, which follows [apache thrift library](https://github.com/apache/thrift/blob/master/lib/go/thrift/protocol_test.go). 

The original fix already existed in [upstream](https://github.com/apache/thrift/blob/master/lib/go/thrift/protocol.go#L186)
  • Loading branch information
zhiyipanuber authored Apr 23, 2024
1 parent 63552ae commit 531058b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package thrift

import (
"errors"
"fmt"
)

const (
Expand Down Expand Up @@ -88,7 +89,7 @@ func SkipDefaultDepth(prot TProtocol, typeId TType) (err error) {

// Skips over the next data element from the provided input TProtocol object.
func Skip(self TProtocol, fieldType TType, maxDepth int) (err error) {

if maxDepth <= 0 {
return NewTProtocolExceptionWithType( DEPTH_LIMIT, errors.New("Depth limit exceeded"))
}
Expand Down Expand Up @@ -173,6 +174,8 @@ func Skip(self TProtocol, fieldType TType, maxDepth int) (err error) {
}
}
return self.ReadListEnd()
default:
return NewTProtocolExceptionWithType(INVALID_DATA, fmt.Errorf("Unknown data type %d", fieldType))
}
return nil
}
85 changes: 85 additions & 0 deletions thirdparty/github.com/apache/thrift/lib/go/thrift/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,88 @@ func ReadWriteBinary(t testing.TB, p TProtocol, trans TTransport) {
}
}
}

func UnmatchedBeginEndProtocolTest(t *testing.T, protocolFactory TProtocolFactory) {
// 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 := NewTMemoryBuffer()
t.Run("Read", func(t *testing.T) {
t.Run("Message", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.ReadMessageEnd()
p.ReadMessageEnd()
})
t.Run("Struct", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.ReadStructEnd()
p.ReadStructEnd()
})
t.Run("Field", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.ReadFieldEnd()
p.ReadFieldEnd()
})
t.Run("Map", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.ReadMapEnd()
p.ReadMapEnd()
})
t.Run("List", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.ReadListEnd()
p.ReadListEnd()
})
t.Run("Set", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.ReadSetEnd()
p.ReadSetEnd()
})
})
t.Run("Write", func(t *testing.T) {
t.Run("Message", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.WriteMessageEnd()
p.WriteMessageEnd()
})
t.Run("Struct", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.WriteStructEnd()
p.WriteStructEnd()
})
t.Run("Field", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.WriteFieldEnd()
p.WriteFieldEnd()
})
t.Run("Map", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.WriteMapEnd()
p.WriteMapEnd()
})
t.Run("List", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.WriteListEnd()
p.WriteListEnd()
})
t.Run("Set", func(t *testing.T) {
trans.Reset()
p := protocolFactory.GetProtocol(trans)
p.WriteSetEnd()
p.WriteSetEnd()
})
})
trans.Close()
}

0 comments on commit 531058b

Please sign in to comment.