peer_protocol: Add a test for receiving overlong piece data

The chunk buffer pool decides the upper bound on chunk data len.
This commit is contained in:
Matt Joiner 2018-07-14 11:37:56 +10:00
parent d4d8f78906
commit 01380adac3
1 changed files with 18 additions and 0 deletions

View File

@ -73,3 +73,21 @@ func TestDecodeShortPieceEOF(t *testing.T) {
assert.Len(t, m.Piece, 1)
assert.Equal(t, io.EOF, d.Decode(&m))
}
func TestDecodeOverlongPiece(t *testing.T) {
r, w := io.Pipe()
go func() {
w.Write(Message{Type: Piece, Piece: make([]byte, 3)}.MustMarshalBinary())
w.Close()
}()
d := Decoder{
R: bufio.NewReader(r),
MaxLength: 1 << 15,
Pool: &sync.Pool{New: func() interface{} {
b := make([]byte, 2)
return &b
}},
}
var m Message
require.EqualError(t, d.Decode(&m), "piece data longer than expected")
}