11 "github.com/anacrolix/missinggo/v2"
12 "github.com/anacrolix/missinggo/v2/bitmap"
13 "github.com/bradfitz/iter"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
17 "github.com/anacrolix/torrent/bencode"
18 "github.com/anacrolix/torrent/internal/testutil"
19 "github.com/anacrolix/torrent/metainfo"
20 pp "github.com/anacrolix/torrent/peer_protocol"
21 "github.com/anacrolix/torrent/storage"
24 func r(i, b, l pp.Integer) Request {
25 return Request{i, ChunkSpec{b, l}}
28 // Check the given request is correct for various torrent offsets.
29 func TestTorrentRequest(t *testing.T) {
30 const s = 472183431 // Length of torrent.
31 for _, _case := range []struct {
32 off int64 // An offset into the torrent.
33 req Request // The expected request. The zero value means !ok.
38 // One before the end of a piece.
39 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
40 // Offset beyond torrent length.
41 {472 * 1 << 20, Request{}},
42 // One before the end of the torrent. Complicates the chunk length.
43 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
45 // One before end of chunk.
46 {16383, r(0, 0, 16384)},
48 {16384, r(0, 16384, 16384)},
50 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
51 if (_case.req == Request{}) == ok {
52 t.Fatalf("expected %v, got %v", _case.req, req)
55 t.Fatalf("expected %v, got %v", _case.req, req)
60 func TestAppendToCopySlice(t *testing.T) {
61 orig := []int{1, 2, 3}
62 dupe := append([]int{}, orig...)
69 func TestTorrentString(t *testing.T) {
71 s := tor.InfoHash().HexString()
72 if s != "0000000000000000000000000000000000000000" {
77 // This benchmark is from the observation that a lot of overlapping Readers on
78 // a large torrent with small pieces had a lot of overhead in recalculating
79 // piece priorities everytime a reader (possibly in another Torrent) changed.
80 func BenchmarkUpdatePiecePriorities(b *testing.B) {
83 pieceLength = 256 << 10
85 cl := &Client{config: TestingConfig(b)}
87 t := cl.newTorrent(metainfo.Hash{}, nil)
88 require.NoError(b, t.setInfo(&metainfo.Info{
89 Pieces: make([]byte, metainfo.HashSize*numPieces),
90 PieceLength: pieceLength,
91 Length: pieceLength * numPieces,
93 assert.EqualValues(b, 13410, t.numPieces())
96 r.SetReadahead(32 << 20)
97 r.Seek(3500000, io.SeekStart)
99 assert.Len(b, t.readers, 7)
100 for i := 0; i < t.numPieces(); i += 3 {
101 t._completedPieces.Add(bitmap.BitIndex(i))
103 t.DownloadPieces(0, t.numPieces())
104 for range iter.N(b.N) {
105 t.updateAllPiecePriorities()
109 // Check that a torrent containing zero-length file(s) will start, and that
110 // they're created in the filesystem. The client storage is assumed to be
111 // file-based on the native filesystem based.
112 func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *ClientConfig) {
113 cl, err := NewClient(cfg)
114 require.NoError(t, err)
116 ib, err := bencode.Marshal(metainfo.Info{
121 require.NoError(t, err)
122 fp := filepath.Join(cfg.DataDir, "empty")
124 assert.False(t, missinggo.FilePathExists(fp))
125 tt, err := cl.AddTorrent(&metainfo.MetaInfo{
128 require.NoError(t, err)
131 require.True(t, cl.WaitAll())
132 assert.True(t, missinggo.FilePathExists(fp))
135 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
136 cfg := TestingConfig(t)
137 ci := storage.NewFile(cfg.DataDir)
139 cfg.DefaultStorage = ci
140 testEmptyFilesAndZeroPieceLength(t, cfg)
143 func TestPieceHashFailed(t *testing.T) {
144 mi := testutil.GreetingMetaInfo()
146 cl.config = TestingConfig(t)
148 tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
150 require.NoError(t, tt.setInfoBytesLocked(mi.InfoBytes))
152 tt.pieces[1]._dirtyChunks.AddRange(0, 3)
153 require.True(t, tt.pieceAllDirty(1))
154 tt.pieceHashed(1, false, nil)
155 // Dirty chunks should be cleared so we can try again.
156 require.False(t, tt.pieceAllDirty(1))
160 // Check the behaviour of Torrent.Metainfo when metadata is not completed.
161 func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
162 cfg := TestingConfig(t)
164 cl, err := NewClient(cfg)
165 require.NoError(t, err)
168 mi := testutil.GreetingMetaInfo()
169 ih := mi.HashInfoBytes()
171 tt, _ := cl.AddTorrentInfoHash(ih)
172 assert.Nil(t, tt.Metainfo().InfoBytes)
173 assert.False(t, tt.haveAllMetadataPieces())
175 nc, err := net.Dial("tcp", fmt.Sprintf(":%d", cl.LocalPort()))
176 require.NoError(t, err)
179 var pex PeerExtensionBits
180 pex.SetBit(pp.ExtensionBitExtended, true)
181 hr, err := pp.Handshake(nc, &ih, [20]byte{}, pex)
182 require.NoError(t, err)
183 assert.True(t, hr.PeerExtensionBits.GetBit(pp.ExtensionBitExtended))
184 assert.EqualValues(t, cl.PeerID(), hr.PeerID)
185 assert.EqualValues(t, ih, hr.Hash)
187 assert.EqualValues(t, 0, tt.metadataSize())
193 _, err = nc.Write(pp.Message{
195 ExtendedID: pp.HandshakeExtendedID,
196 ExtendedPayload: func() []byte {
197 d := map[string]interface{}{
198 "metadata_size": len(mi.InfoBytes),
200 b, err := bencode.Marshal(d)
206 }.MustMarshalBinary())
207 require.NoError(t, err)
209 tt.metadataChanged.Wait()
211 assert.Equal(t, make([]byte, len(mi.InfoBytes)), tt.metadataBytes)
212 assert.False(t, tt.haveAllMetadataPieces())
213 assert.Nil(t, tt.Metainfo().InfoBytes)