From 01380adac30584ff79bf46fddd6cd3bd49c1207d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 14 Jul 2018 11:37:56 +1000 Subject: [PATCH] peer_protocol: Add a test for receiving overlong piece data The chunk buffer pool decides the upper bound on chunk data len. --- peer_protocol/decoder_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/peer_protocol/decoder_test.go b/peer_protocol/decoder_test.go index 5dfd09c5..bbd8194d 100644 --- a/peer_protocol/decoder_test.go +++ b/peer_protocol/decoder_test.go @@ -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") +}