11 "github.com/anacrolix/missinggo/v2"
12 "github.com/anacrolix/missinggo/v2/bitmap"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
16 "github.com/anacrolix/torrent/bencode"
17 "github.com/anacrolix/torrent/internal/testutil"
18 "github.com/anacrolix/torrent/metainfo"
19 pp "github.com/anacrolix/torrent/peer_protocol"
20 "github.com/anacrolix/torrent/storage"
23 func r(i, b, l pp.Integer) Request {
24 return Request{i, ChunkSpec{b, l}}
27 // Check the given request is correct for various torrent offsets.
28 func TestTorrentRequest(t *testing.T) {
29 const s = 472183431 // Length of torrent.
30 for _, _case := range []struct {
31 off int64 // An offset into the torrent.
32 req Request // The expected request. The zero value means !ok.
37 // One before the end of a piece.
38 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
39 // Offset beyond torrent length.
40 {472 * 1 << 20, Request{}},
41 // One before the end of the torrent. Complicates the chunk length.
42 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
44 // One before end of chunk.
45 {16383, r(0, 0, 16384)},
47 {16384, r(0, 16384, 16384)},
49 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
50 if (_case.req == Request{}) == ok {
51 t.Fatalf("expected %v, got %v", _case.req, req)
54 t.Fatalf("expected %v, got %v", _case.req, req)
59 func TestAppendToCopySlice(t *testing.T) {
60 orig := []int{1, 2, 3}
61 dupe := append([]int{}, orig...)
68 func TestTorrentString(t *testing.T) {
70 s := tor.InfoHash().HexString()
71 if s != "0000000000000000000000000000000000000000" {
76 // This benchmark is from the observation that a lot of overlapping Readers on
77 // a large torrent with small pieces had a lot of overhead in recalculating
78 // piece priorities everytime a reader (possibly in another Torrent) changed.
79 func BenchmarkUpdatePiecePriorities(b *testing.B) {
82 pieceLength = 256 << 10
84 cl := &Client{config: TestingConfig(b)}
86 t := cl.newTorrent(metainfo.Hash{}, nil)
87 require.NoError(b, t.setInfo(&metainfo.Info{
88 Pieces: make([]byte, metainfo.HashSize*numPieces),
89 PieceLength: pieceLength,
90 Length: pieceLength * numPieces,
92 assert.EqualValues(b, 13410, t.numPieces())
93 for i := 0; i < 7; i += 1 {
95 r.SetReadahead(32 << 20)
96 r.Seek(3500000, io.SeekStart)
98 assert.Len(b, t.readers, 7)
99 for i := 0; i < t.numPieces(); i += 3 {
100 t._completedPieces.Add(bitmap.BitIndex(i))
102 t.DownloadPieces(0, t.numPieces())
103 for i := 0; i < b.N; i += 1 {
104 t.updateAllPiecePriorities()
108 // Check that a torrent containing zero-length file(s) will start, and that
109 // they're created in the filesystem. The client storage is assumed to be
110 // file-based on the native filesystem based.
111 func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *ClientConfig) {
112 cl, err := NewClient(cfg)
113 require.NoError(t, err)
115 ib, err := bencode.Marshal(metainfo.Info{
120 require.NoError(t, err)
121 fp := filepath.Join(cfg.DataDir, "empty")
123 assert.False(t, missinggo.FilePathExists(fp))
124 tt, err := cl.AddTorrent(&metainfo.MetaInfo{
127 require.NoError(t, err)
130 require.True(t, cl.WaitAll())
131 assert.True(t, missinggo.FilePathExists(fp))
134 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
135 cfg := TestingConfig(t)
136 ci := storage.NewFile(cfg.DataDir)
138 cfg.DefaultStorage = ci
139 testEmptyFilesAndZeroPieceLength(t, cfg)
142 func TestPieceHashFailed(t *testing.T) {
143 mi := testutil.GreetingMetaInfo()
145 cl.config = TestingConfig(t)
147 tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
149 require.NoError(t, tt.setInfoBytesLocked(mi.InfoBytes))
151 tt.dirtyChunks.AddRange(
152 uint64(tt.pieceRequestIndexOffset(1)),
153 uint64(tt.pieceRequestIndexOffset(1)+3))
154 require.True(t, tt.pieceAllDirty(1))
155 tt.pieceHashed(1, false, nil)
156 // Dirty chunks should be cleared so we can try again.
157 require.False(t, tt.pieceAllDirty(1))
161 // Check the behaviour of Torrent.Metainfo when metadata is not completed.
162 func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
163 cfg := TestingConfig(t)
165 cl, err := NewClient(cfg)
166 require.NoError(t, err)
169 mi := testutil.GreetingMetaInfo()
170 ih := mi.HashInfoBytes()
172 tt, _ := cl.AddTorrentInfoHash(ih)
173 assert.Nil(t, tt.Metainfo().InfoBytes)
174 assert.False(t, tt.haveAllMetadataPieces())
176 nc, err := net.Dial("tcp", fmt.Sprintf(":%d", cl.LocalPort()))
177 require.NoError(t, err)
180 var pex PeerExtensionBits
181 pex.SetBit(pp.ExtensionBitExtended, true)
182 hr, err := pp.Handshake(nc, &ih, [20]byte{}, pex)
183 require.NoError(t, err)
184 assert.True(t, hr.PeerExtensionBits.GetBit(pp.ExtensionBitExtended))
185 assert.EqualValues(t, cl.PeerID(), hr.PeerID)
186 assert.EqualValues(t, ih, hr.Hash)
188 assert.EqualValues(t, 0, tt.metadataSize())
194 _, err = nc.Write(pp.Message{
196 ExtendedID: pp.HandshakeExtendedID,
197 ExtendedPayload: func() []byte {
198 d := map[string]interface{}{
199 "metadata_size": len(mi.InfoBytes),
201 b, err := bencode.Marshal(d)
207 }.MustMarshalBinary())
208 require.NoError(t, err)
210 tt.metadataChanged.Wait()
212 assert.Equal(t, make([]byte, len(mi.InfoBytes)), tt.metadataBytes)
213 assert.False(t, tt.haveAllMetadataPieces())
214 assert.Nil(t, tt.Metainfo().InfoBytes)