Skip to content

Commit

Permalink
additional test for TSimpleJSONProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens-G committed Feb 24, 2019
1 parent 6e5c0f6 commit 264a3f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 1 addition & 4 deletions lib/go/thrift/json_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ const (
// for references to _ParseContext see tsimplejson_protocol.go

// JSON protocol implementation for thrift.
//
// This protocol produces/consumes a simple output format
// suitable for parsing by scripting languages. It should not be
// confused with the full-featured TJSONProtocol.
// Utilizes Simple JSON protocol
//
type TJSONProtocol struct {
*TSimpleJSONProtocol
Expand Down
4 changes: 2 additions & 2 deletions lib/go/thrift/simple_json_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p _ParseContext) String() string {
return "UNKNOWN-PARSE-CONTEXT"
}

// JSON protocol implementation for thrift.
// Simple JSON protocol implementation for thrift.
//
// This protocol produces/consumes a simple output format
// suitable for parsing by scripting languages. It should not be
Expand Down Expand Up @@ -1316,7 +1316,7 @@ func (p *TSimpleJSONProtocol) readNumeric() (Numeric, error) {
func (p *TSimpleJSONProtocol) safePeekContains(b []byte) bool {
for i := 0; i < len(b); i++ {
a, _ := p.reader.Peek(i + 1)
if len(a) == 0 || a[i] != b[i] {
if len(a) < (i+1) || a[i] != b[i] {
return false
}
}
Expand Down
22 changes: 22 additions & 0 deletions lib/go/thrift/simple_json_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,25 @@ func TestWriteSimpleJSONProtocolMap(t *testing.T) {
}
trans.Close()
}

func TestWriteSimpleJSONProtocolSafePeek(t *testing.T) {
trans := NewTMemoryBuffer()
p := NewTSimpleJSONProtocol(trans)
trans.Write([]byte{'a', 'b'})
trans.Flush(context.Background())

test1 := p.safePeekContains([]byte{'a', 'b'})
if !test1 {
t.Fatalf("Should match at test 1")
}

test2 := p.safePeekContains([]byte{'a', 'b', 'c', 'd'})
if test2 {
t.Fatalf("Should not match at test 2")
}

test3 := p.safePeekContains([]byte{'x', 'y'})
if test3 {
t.Fatalf("Should not match at test 3")
}
}

0 comments on commit 264a3f3

Please sign in to comment.