// Ensure that it's an error for a peer to send an invalid have message. func TestPeerInvalidHave(t *testing.T) { cl, err := NewClient(&TestingConfig) require.NoError(t, err) defer cl.Close() ie := metainfo.InfoEx{ Info: metainfo.Info{ PieceLength: 1, Pieces: make([]byte, 20), Files: []metainfo.FileInfo{{Length: 1}}, }, } ie.UpdateBytes() tt, _new, err := cl.AddTorrentSpec(&TorrentSpec{ Info: &ie, InfoHash: ie.Hash(), }) require.NoError(t, err) assert.True(t, _new) defer tt.Drop() cn := &connection{ t: tt, } assert.NoError(t, cn.peerSentHave(0)) assert.Error(t, cn.peerSentHave(1)) }
// We read from a piece which is marked completed, but is missing data. func TestCompletedPieceWrongSize(t *testing.T) { cfg := TestingConfig cfg.DefaultStorage = badStorage{} cl, err := NewClient(&cfg) require.NoError(t, err) defer cl.Close() ie := metainfo.InfoEx{ Info: metainfo.Info{ PieceLength: 15, Pieces: make([]byte, 20), Files: []metainfo.FileInfo{ metainfo.FileInfo{Path: []string{"greeting"}, Length: 13}, }, }, } ie.UpdateBytes() tt, new, err := cl.AddTorrentSpec(&TorrentSpec{ Info: &ie, InfoHash: ie.Hash(), }) require.NoError(t, err) defer tt.Drop() assert.True(t, new) r := tt.NewReader() defer r.Close() b, err := ioutil.ReadAll(r) assert.Len(t, b, 13) assert.NoError(t, err) }