10 "github.com/anacrolix/missinggo"
11 "github.com/bradfitz/iter"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "github.com/anacrolix/torrent/bencode"
16 "github.com/anacrolix/torrent/internal/testutil"
17 "github.com/anacrolix/torrent/metainfo"
18 pp "github.com/anacrolix/torrent/peer_protocol"
19 "github.com/anacrolix/torrent/storage"
22 func r(i, b, l pp.Integer) request {
23 return request{i, chunkSpec{b, l}}
26 // Check the given Request is correct for various torrent offsets.
27 func TestTorrentRequest(t *testing.T) {
28 const s = 472183431 // Length of torrent.
29 for _, _case := range []struct {
30 off int64 // An offset into the torrent.
31 req request // The expected Request. The zero value means !ok.
36 // One before the end of a piece.
37 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
38 // Offset beyond torrent length.
39 {472 * 1 << 20, request{}},
40 // One before the end of the torrent. Complicates the chunk length.
41 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
43 // One before end of chunk.
44 {16383, r(0, 0, 16384)},
46 {16384, r(0, 16384, 16384)},
48 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
49 if (_case.req == request{}) == ok {
50 t.Fatalf("expected %v, got %v", _case.req, req)
53 t.Fatalf("expected %v, got %v", _case.req, req)
58 func TestAppendToCopySlice(t *testing.T) {
59 orig := []int{1, 2, 3}
60 dupe := append([]int{}, orig...)
67 func TestTorrentString(t *testing.T) {
69 s := tor.InfoHash().HexString()
70 if s != "0000000000000000000000000000000000000000" {
75 // This benchmark is from the observation that a lot of overlapping Readers on
76 // a large torrent with small pieces had a lot of overhead in recalculating
77 // piece priorities everytime a reader (possibly in another Torrent) changed.
78 func BenchmarkUpdatePiecePriorities(b *testing.B) {
81 pieceLength = 256 << 10
83 cl := &Client{config: TestingConfig()}
85 t := cl.newTorrent(metainfo.Hash{}, nil)
86 require.NoError(b, t.setInfo(&metainfo.Info{
87 Pieces: make([]byte, metainfo.HashSize*numPieces),
88 PieceLength: pieceLength,
89 Length: pieceLength * numPieces,
91 assert.EqualValues(b, 13410, t.numPieces())
94 r.SetReadahead(32 << 20)
97 assert.Len(b, t.readers, 7)
98 for i := 0; i < int(t.numPieces()); i += 3 {
99 t._completedPieces.Set(i, true)
101 t.DownloadPieces(0, t.numPieces())
102 for range iter.N(b.N) {
103 t.updateAllPiecePriorities()
107 // Check that a torrent containing zero-length file(s) will start, and that
108 // they're created in the filesystem. The client storage is assumed to be
109 // file-based on the native filesystem based.
110 func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *ClientConfig) {
111 cl, err := NewClient(cfg)
112 require.NoError(t, err)
114 ib, err := bencode.Marshal(metainfo.Info{
119 require.NoError(t, err)
120 fp := filepath.Join(cfg.DataDir, "empty")
122 assert.False(t, missinggo.FilePathExists(fp))
123 tt, err := cl.AddTorrent(&metainfo.MetaInfo{
126 require.NoError(t, err)
129 require.True(t, cl.WaitAll())
130 assert.True(t, missinggo.FilePathExists(fp))
133 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
134 cfg := TestingConfig()
135 ci := storage.NewFile(cfg.DataDir)
137 cfg.DefaultStorage = ci
138 testEmptyFilesAndZeroPieceLength(t, cfg)
141 func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
142 cfg := TestingConfig()
143 ci := storage.NewMMap(cfg.DataDir)
145 cfg.DefaultStorage = ci
146 testEmptyFilesAndZeroPieceLength(t, cfg)
149 func TestPieceHashFailed(t *testing.T) {
150 mi := testutil.GreetingMetaInfo()
152 cl.config = TestingConfig()
154 tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
156 require.NoError(t, tt.setInfoBytes(mi.InfoBytes))
158 tt.pieces[1]._dirtyChunks.AddRange(0, 3)
159 require.True(t, tt.pieceAllDirty(1))
160 tt.pieceHashed(1, false)
161 // Dirty chunks should be cleared so we can try again.
162 require.False(t, tt.pieceAllDirty(1))
166 // Check the behaviour of Torrent.Metainfo when metadata is not completed.
167 func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
168 cfg := TestingConfig()
170 cl, err := NewClient(cfg)
171 require.NoError(t, err)
174 mi := testutil.GreetingMetaInfo()
175 ih := mi.HashInfoBytes()
177 tt, _ := cl.AddTorrentInfoHash(ih)
178 assert.Nil(t, tt.Metainfo().InfoBytes)
179 assert.False(t, tt.haveAllMetadataPieces())
181 nc, err := net.Dial("tcp", fmt.Sprintf(":%d", cl.LocalPort()))
182 require.NoError(t, err)
185 var pex PeerExtensionBits
186 pex.SetBit(pp.ExtensionBitExtended)
187 hr, err := pp.Handshake(nc, &ih, [20]byte{}, pex)
188 require.NoError(t, err)
189 assert.True(t, hr.PeerExtensionBits.GetBit(pp.ExtensionBitExtended))
190 assert.EqualValues(t, cl.PeerID(), hr.PeerID)
191 assert.EqualValues(t, ih, hr.Hash)
193 assert.EqualValues(t, 0, tt.metadataSize())
199 _, err = nc.Write(pp.Message{
201 ExtendedID: pp.HandshakeExtendedID,
202 ExtendedPayload: func() []byte {
203 d := map[string]interface{}{
204 "metadata_size": len(mi.InfoBytes),
206 b, err := bencode.Marshal(d)
212 }.MustMarshalBinary())
213 require.NoError(t, err)
215 tt.metadataChanged.Wait()
217 assert.Equal(t, make([]byte, len(mi.InfoBytes)), tt.metadataBytes)
218 assert.False(t, tt.haveAllMetadataPieces())
219 assert.Nil(t, tt.Metainfo().InfoBytes)