11 "github.com/anacrolix/missinggo"
12 "github.com/bradfitz/iter"
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()}
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())
95 r.SetReadahead(32 << 20)
96 r.Seek(3500000, io.SeekStart)
98 assert.Len(b, t.readers, 7)
99 for i := 0; i < int(t.numPieces()); i += 3 {
100 t._completedPieces.Set(i, true)
102 t.DownloadPieces(0, t.numPieces())
103 for range iter.N(b.N) {
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()
136 ci := storage.NewFile(cfg.DataDir)
138 cfg.DefaultStorage = ci
139 testEmptyFilesAndZeroPieceLength(t, cfg)
142 func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
143 cfg := TestingConfig()
144 ci := storage.NewMMap(cfg.DataDir)
146 cfg.DefaultStorage = ci
147 testEmptyFilesAndZeroPieceLength(t, cfg)
150 func TestPieceHashFailed(t *testing.T) {
151 mi := testutil.GreetingMetaInfo()
153 cl.config = TestingConfig()
155 tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
157 require.NoError(t, tt.setInfoBytes(mi.InfoBytes))
159 tt.pieces[1]._dirtyChunks.AddRange(0, 3)
160 require.True(t, tt.pieceAllDirty(1))
161 tt.pieceHashed(1, false, nil)
162 // Dirty chunks should be cleared so we can try again.
163 require.False(t, tt.pieceAllDirty(1))
167 // Check the behaviour of Torrent.Metainfo when metadata is not completed.
168 func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
169 cfg := TestingConfig()
171 cl, err := NewClient(cfg)
172 require.NoError(t, err)
175 mi := testutil.GreetingMetaInfo()
176 ih := mi.HashInfoBytes()
178 tt, _ := cl.AddTorrentInfoHash(ih)
179 assert.Nil(t, tt.Metainfo().InfoBytes)
180 assert.False(t, tt.haveAllMetadataPieces())
182 nc, err := net.Dial("tcp", fmt.Sprintf(":%d", cl.LocalPort()))
183 require.NoError(t, err)
186 var pex PeerExtensionBits
187 pex.SetBit(pp.ExtensionBitExtended, true)
188 hr, err := pp.Handshake(nc, &ih, [20]byte{}, pex)
189 require.NoError(t, err)
190 assert.True(t, hr.PeerExtensionBits.GetBit(pp.ExtensionBitExtended))
191 assert.EqualValues(t, cl.PeerID(), hr.PeerID)
192 assert.EqualValues(t, ih, hr.Hash)
194 assert.EqualValues(t, 0, tt.metadataSize())
200 _, err = nc.Write(pp.Message{
202 ExtendedID: pp.HandshakeExtendedID,
203 ExtendedPayload: func() []byte {
204 d := map[string]interface{}{
205 "metadata_size": len(mi.InfoBytes),
207 b, err := bencode.Marshal(d)
213 }.MustMarshalBinary())
214 require.NoError(t, err)
216 tt.metadataChanged.Wait()
218 assert.Equal(t, make([]byte, len(mi.InfoBytes)), tt.metadataBytes)
219 assert.False(t, tt.haveAllMetadataPieces())
220 assert.Nil(t, tt.Metainfo().InfoBytes)