]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Merge branch 'peer-requesting'
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "fmt"
5         "io"
6         "net"
7         "os"
8         "path/filepath"
9         "testing"
10
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"
15
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"
21 )
22
23 func r(i, b, l pp.Integer) Request {
24         return Request{i, ChunkSpec{b, l}}
25 }
26
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.
33         }{
34                 // Invalid offset.
35                 {-1, Request{}},
36                 {0, r(0, 0, 16384)},
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)},
43                 {1, r(0, 0, 16384)},
44                 // One before end of chunk.
45                 {16383, r(0, 0, 16384)},
46                 // Second chunk.
47                 {16384, r(0, 16384, 16384)},
48         } {
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)
52                 }
53                 if req != _case.req {
54                         t.Fatalf("expected %v, got %v", _case.req, req)
55                 }
56         }
57 }
58
59 func TestAppendToCopySlice(t *testing.T) {
60         orig := []int{1, 2, 3}
61         dupe := append([]int{}, orig...)
62         dupe[0] = 4
63         if orig[0] != 1 {
64                 t.FailNow()
65         }
66 }
67
68 func TestTorrentString(t *testing.T) {
69         tor := &Torrent{}
70         s := tor.InfoHash().HexString()
71         if s != "0000000000000000000000000000000000000000" {
72                 t.FailNow()
73         }
74 }
75
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) {
80         const (
81                 numPieces   = 13410
82                 pieceLength = 256 << 10
83         )
84         cl := &Client{config: TestingConfig(b)}
85         cl.initLogger()
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,
91         }))
92         assert.EqualValues(b, 13410, t.numPieces())
93         for i := 0; i < 7; i += 1 {
94                 r := t.NewReader()
95                 r.SetReadahead(32 << 20)
96                 r.Seek(3500000, io.SeekStart)
97         }
98         assert.Len(b, t.readers, 7)
99         for i := 0; i < t.numPieces(); i += 3 {
100                 t._completedPieces.Add(bitmap.BitIndex(i))
101         }
102         t.DownloadPieces(0, t.numPieces())
103         for i := 0; i < b.N; i += 1 {
104                 t.updateAllPiecePriorities("")
105         }
106 }
107
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)
114         defer cl.Close()
115         ib, err := bencode.Marshal(metainfo.Info{
116                 Name:        "empty",
117                 Length:      0,
118                 PieceLength: 0,
119         })
120         require.NoError(t, err)
121         fp := filepath.Join(cfg.DataDir, "empty")
122         os.Remove(fp)
123         assert.False(t, missinggo.FilePathExists(fp))
124         tt, err := cl.AddTorrent(&metainfo.MetaInfo{
125                 InfoBytes: ib,
126         })
127         require.NoError(t, err)
128         defer tt.Drop()
129         tt.DownloadAll()
130         require.True(t, cl.WaitAll())
131         assert.True(t, tt.Complete.Bool())
132         assert.True(t, missinggo.FilePathExists(fp))
133 }
134
135 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
136         cfg := TestingConfig(t)
137         ci := storage.NewFile(cfg.DataDir)
138         defer ci.Close()
139         cfg.DefaultStorage = ci
140         testEmptyFilesAndZeroPieceLength(t, cfg)
141 }
142
143 func TestPieceHashFailed(t *testing.T) {
144         mi := testutil.GreetingMetaInfo()
145         cl := new(Client)
146         cl.config = TestingConfig(t)
147         cl.initLogger()
148         tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
149         tt.setChunkSize(2)
150         require.NoError(t, tt.setInfoBytesLocked(mi.InfoBytes))
151         tt.cl.lock()
152         tt.dirtyChunks.AddRange(
153                 uint64(tt.pieceRequestIndexOffset(1)),
154                 uint64(tt.pieceRequestIndexOffset(1)+3))
155         require.True(t, tt.pieceAllDirty(1))
156         tt.pieceHashed(1, false, nil)
157         // Dirty chunks should be cleared so we can try again.
158         require.False(t, tt.pieceAllDirty(1))
159         tt.cl.unlock()
160 }
161
162 // Check the behaviour of Torrent.Metainfo when metadata is not completed.
163 func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
164         cfg := TestingConfig(t)
165         cfg.Debug = true
166         // Disable this just because we manually initiate a connection without it.
167         cfg.MinPeerExtensions.SetBit(pp.ExtensionBitFast, false)
168         cl, err := NewClient(cfg)
169         require.NoError(t, err)
170         defer cl.Close()
171
172         mi := testutil.GreetingMetaInfo()
173         ih := mi.HashInfoBytes()
174
175         tt, _ := cl.AddTorrentInfoHash(ih)
176         assert.Nil(t, tt.Metainfo().InfoBytes)
177         assert.False(t, tt.haveAllMetadataPieces())
178
179         nc, err := net.Dial("tcp", fmt.Sprintf(":%d", cl.LocalPort()))
180         require.NoError(t, err)
181         defer nc.Close()
182
183         var pex PeerExtensionBits
184         pex.SetBit(pp.ExtensionBitExtended, true)
185         hr, err := pp.Handshake(nc, &ih, [20]byte{}, pex)
186         require.NoError(t, err)
187         assert.True(t, hr.PeerExtensionBits.GetBit(pp.ExtensionBitExtended))
188         assert.EqualValues(t, cl.PeerID(), hr.PeerID)
189         assert.EqualValues(t, ih, hr.Hash)
190
191         assert.EqualValues(t, 0, tt.metadataSize())
192
193         func() {
194                 cl.lock()
195                 defer cl.unlock()
196                 go func() {
197                         _, err = nc.Write(pp.Message{
198                                 Type:       pp.Extended,
199                                 ExtendedID: pp.HandshakeExtendedID,
200                                 ExtendedPayload: func() []byte {
201                                         d := map[string]interface{}{
202                                                 "metadata_size": len(mi.InfoBytes),
203                                         }
204                                         b, err := bencode.Marshal(d)
205                                         if err != nil {
206                                                 panic(err)
207                                         }
208                                         return b
209                                 }(),
210                         }.MustMarshalBinary())
211                         require.NoError(t, err)
212                 }()
213                 tt.metadataChanged.Wait()
214         }()
215         assert.Equal(t, make([]byte, len(mi.InfoBytes)), tt.metadataBytes)
216         assert.False(t, tt.haveAllMetadataPieces())
217         assert.Nil(t, tt.Metainfo().InfoBytes)
218 }