Skip to content

Commit

Permalink
add a test for packets smaller than MaxHeaderLength
Browse files Browse the repository at this point in the history
Summary:
#8 mentions this.
stack trace
```
goroutine 12 [running]:
testing.tRunner.func1.2({0x887460, 0xc00002c1c8})
	/usr/lib/golang/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
	/usr/lib/golang/src/testing/testing.go:1529 +0x39f
panic({0x887460, 0xc00002c1c8})
	/usr/lib/golang/src/runtime/panic.go:884 +0x213
github.com/facebookincubator/tacquito.(*Packet).UnmarshalBinary(0xc00006a740, {0xc000028310, 0x52a4a5?, 0x3})
	/home/0x34d/project/tacquito/packet.go:126 +0x205
github.com/facebookincubator/tacquito.TestPacket(0xc000007a00?)
	/home/0x34d/project/tacquito/packet_test.go:450 +0x56
testing.tRunner(0xc000007d40, 0x8ec2b0)
	/usr/lib/golang/src/testing/testing.go:1576 +0x10b
created by testing.(*T).Run
	/usr/lib/golang/src/testing/testing.go:1629 +0x3ea
exit status 2
FAIL	github.com/facebookincubator/tacquito	0.007s
```

Reviewed By: shringiarpit26

Differential Revision: D50025936

fbshipit-source-id: 294e7050556eeb31202c1658bfd802b170b27196
  • Loading branch information
Surya Ahuja authored and facebook-github-bot committed Oct 9, 2023
1 parent ce533c6 commit b24e222
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (p *Packet) UnmarshalBinary(v []byte) error {
// Unmarshal failure will lead to the connection being closed
var err error
var h Header
if len(v) < MaxHeaderLength {
return fmt.Errorf("data length [%v] is smaller than expected header length [%v]", len(v), MaxHeaderLength)
}
err = Unmarshal(v[:MaxHeaderLength], &h)
if err != nil {
return fmt.Errorf("failed header unmarshal: [%w]", err)
Expand Down
12 changes: 11 additions & 1 deletion packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,17 @@ func TestPacketMarshalUnmarshalTooLarge(t *testing.T) {
_, err = p.MarshalBinary()
assert.Error(t, err)

// won't unmarhsal
// won't unmarshal
err = p.UnmarshalBinary(buf)
assert.Error(t, err)
}

func TestPacketMarshalUnmarshalTooSmall(t *testing.T) {
p := NewPacket()
var err error
err = p.UnmarshalBinary([]byte("000"))
assert.Error(t, err)

_, err = p.MarshalBinary()
assert.Error(t, err)
}

0 comments on commit b24e222

Please sign in to comment.