From c8dffdbb111d08f5f58b27cc584915dcf99b3985 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 6 Dec 2016 15:41:08 +1100 Subject: [PATCH] Add test that dirty chunks are cleared on piece hash failure --- torrent_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/torrent_test.go b/torrent_test.go index a0d0857c..9fe3e963 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "github.com/anacrolix/torrent/bencode" + "github.com/anacrolix/torrent/internal/testutil" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/storage" @@ -131,3 +132,19 @@ func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) { func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) { testEmptyFilesAndZeroPieceLength(t, storage.NewMMap(TestingConfig.DataDir)) } + +func TestPieceHashFailed(t *testing.T) { + mi := testutil.GreetingMetaInfo() + tt := Torrent{ + cl: new(Client), + infoHash: mi.HashInfoBytes(), + storageOpener: storage.NewClient(badStorage{}), + chunkSize: 2, + } + require.NoError(t, tt.setInfoBytes(mi.InfoBytes)) + tt.pieces[1].DirtyChunks.AddRange(0, 3) + require.True(t, tt.pieceAllDirty(1)) + tt.pieceHashed(1, false) + // Dirty chunks should be cleared so we can try again. + require.False(t, tt.pieceAllDirty(1)) +}