unexpectedChunksReceived.Add(1)
}
- index := int(req.Index)
- piece := &t.pieces[index]
-
// Do we actually want this chunk?
if !t.wantPiece(req) {
unwantedChunksReceived.Add(1)
return
}
+ index := int(req.Index)
+ piece := &t.pieces[index]
+
c.UsefulChunksReceived++
c.lastUsefulChunkReceived = time.Now()
"github.com/stretchr/testify/require"
"github.com/anacrolix/torrent/metainfo"
- "github.com/anacrolix/torrent/peer_protocol"
+ pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/storage"
)
}
close(mrlErr)
}()
- msg := peer_protocol.Message{
- Type: peer_protocol.Piece,
+ msg := pp.Message{
+ Type: pp.Piece,
Piece: make([]byte, defaultChunkSize),
}
wb, err := msg.MarshalBinary()
require.NoError(b, <-mrlErr)
require.EqualValues(b, b.N, cn.UsefulChunksReceived)
}
+
+func TestConnectionReceiveBadChunkIndex(t *testing.T) {
+ cn := connection{
+ t: &Torrent{},
+ }
+ require.False(t, cn.t.haveInfo())
+ assert.NotPanics(t, func() { cn.receiveChunk(&pp.Message{}) })
+ cn.t.info = &metainfo.Info{}
+ require.True(t, cn.t.haveInfo())
+ assert.NotPanics(t, func() { cn.receiveChunk(&pp.Message{}) })
+}