]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Add Torrent.setInfo
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "net"
5         "os"
6         "path/filepath"
7         "testing"
8
9         "github.com/anacrolix/missinggo"
10         "github.com/bradfitz/iter"
11         "github.com/stretchr/testify/assert"
12         "github.com/stretchr/testify/require"
13
14         "github.com/anacrolix/torrent/bencode"
15         "github.com/anacrolix/torrent/internal/testutil"
16         "github.com/anacrolix/torrent/metainfo"
17         pp "github.com/anacrolix/torrent/peer_protocol"
18         "github.com/anacrolix/torrent/storage"
19 )
20
21 func r(i, b, l pp.Integer) request {
22         return request{i, chunkSpec{b, l}}
23 }
24
25 // Check the given Request is correct for various torrent offsets.
26 func TestTorrentRequest(t *testing.T) {
27         const s = 472183431 // Length of torrent.
28         for _, _case := range []struct {
29                 off int64   // An offset into the torrent.
30                 req request // The expected Request. The zero value means !ok.
31         }{
32                 // Invalid offset.
33                 {-1, request{}},
34                 {0, r(0, 0, 16384)},
35                 // One before the end of a piece.
36                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
37                 // Offset beyond torrent length.
38                 {472 * 1 << 20, request{}},
39                 // One before the end of the torrent. Complicates the chunk length.
40                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
41                 {1, r(0, 0, 16384)},
42                 // One before end of chunk.
43                 {16383, r(0, 0, 16384)},
44                 // Second chunk.
45                 {16384, r(0, 16384, 16384)},
46         } {
47                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
48                 if (_case.req == request{}) == ok {
49                         t.Fatalf("expected %v, got %v", _case.req, req)
50                 }
51                 if req != _case.req {
52                         t.Fatalf("expected %v, got %v", _case.req, req)
53                 }
54         }
55 }
56
57 func TestAppendToCopySlice(t *testing.T) {
58         orig := []int{1, 2, 3}
59         dupe := append([]int{}, orig...)
60         dupe[0] = 4
61         if orig[0] != 1 {
62                 t.FailNow()
63         }
64 }
65
66 func TestTorrentString(t *testing.T) {
67         tor := &Torrent{}
68         s := tor.InfoHash().HexString()
69         if s != "0000000000000000000000000000000000000000" {
70                 t.FailNow()
71         }
72 }
73
74 // This benchmark is from the observation that a lot of overlapping Readers on
75 // a large torrent with small pieces had a lot of overhead in recalculating
76 // piece priorities everytime a reader (possibly in another Torrent) changed.
77 func BenchmarkUpdatePiecePriorities(b *testing.B) {
78         const (
79                 numPieces   = 13410
80                 pieceLength = 256 << 10
81         )
82         cl := &Client{}
83         t := cl.newTorrent(metainfo.Hash{}, nil)
84         require.NoError(b, t.setInfo(&metainfo.Info{
85                 Pieces:      make([]byte, metainfo.HashSize*numPieces),
86                 PieceLength: pieceLength,
87                 Length:      pieceLength * numPieces,
88         }))
89         assert.EqualValues(b, 13410, t.numPieces())
90         for range iter.N(7) {
91                 r := t.NewReader()
92                 r.SetReadahead(32 << 20)
93                 r.Seek(3500000, 0)
94         }
95         assert.Len(b, t.readers, 7)
96         t.pendPieceRange(0, t.numPieces())
97         for i := 0; i < t.numPieces(); i += 3 {
98                 t.completedPieces.Set(i, true)
99         }
100         for range iter.N(b.N) {
101                 t.updateAllPiecePriorities()
102         }
103 }
104
105 // Check that a torrent containing zero-length file(s) will start, and that
106 // they're created in the filesystem. The client storage is assumed to be
107 // file-based on the native filesystem based.
108 func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *Config) {
109         cl, err := NewClient(cfg)
110         require.NoError(t, err)
111         defer cl.Close()
112         ib, err := bencode.Marshal(metainfo.Info{
113                 Name:        "empty",
114                 Length:      0,
115                 PieceLength: 0,
116         })
117         require.NoError(t, err)
118         fp := filepath.Join(cfg.DataDir, "empty")
119         os.Remove(fp)
120         assert.False(t, missinggo.FilePathExists(fp))
121         tt, err := cl.AddTorrent(&metainfo.MetaInfo{
122                 InfoBytes: ib,
123         })
124         require.NoError(t, err)
125         defer tt.Drop()
126         tt.DownloadAll()
127         require.True(t, cl.WaitAll())
128         assert.True(t, missinggo.FilePathExists(fp))
129 }
130
131 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
132         cfg := TestingConfig()
133         ci := storage.NewFile(cfg.DataDir)
134         defer ci.Close()
135         cfg.DefaultStorage = ci
136         testEmptyFilesAndZeroPieceLength(t, cfg)
137 }
138
139 func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
140         cfg := TestingConfig()
141         ci := storage.NewMMap(cfg.DataDir)
142         defer ci.Close()
143         cfg.DefaultStorage = ci
144         testEmptyFilesAndZeroPieceLength(t, cfg)
145 }
146
147 func TestPieceHashFailed(t *testing.T) {
148         mi := testutil.GreetingMetaInfo()
149         tt := Torrent{
150                 cl:            new(Client),
151                 infoHash:      mi.HashInfoBytes(),
152                 storageOpener: storage.NewClient(badStorage{}),
153                 chunkSize:     2,
154         }
155         require.NoError(t, tt.setInfoBytes(mi.InfoBytes))
156         tt.cl.mu.Lock()
157         tt.pieces[1].dirtyChunks.AddRange(0, 3)
158         require.True(t, tt.pieceAllDirty(1))
159         tt.pieceHashed(1, false)
160         // Dirty chunks should be cleared so we can try again.
161         require.False(t, tt.pieceAllDirty(1))
162         tt.cl.mu.Unlock()
163 }
164
165 // Check the behaviour of Torrent.Metainfo when metadata is not completed.
166 func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
167         cfg := TestingConfig()
168         cfg.Debug = true
169         cl, err := NewClient(cfg)
170         require.NoError(t, err)
171         defer cl.Close()
172
173         mi := testutil.GreetingMetaInfo()
174         ih := mi.HashInfoBytes()
175
176         tt, _ := cl.AddTorrentInfoHash(ih)
177         assert.Nil(t, tt.Metainfo().InfoBytes)
178         assert.False(t, tt.haveAllMetadataPieces())
179
180         nc, err := net.Dial("tcp", cl.ListenAddr().String())
181         require.NoError(t, err)
182         defer nc.Close()
183
184         var pex peerExtensionBytes
185         pex.SetBit(ExtensionBitExtended)
186         hr, ok, err := handshake(nc, &ih, [20]byte{}, pex)
187         require.NoError(t, err)
188         assert.True(t, ok)
189         assert.True(t, hr.peerExtensionBytes.GetBit(ExtensionBitExtended))
190         assert.EqualValues(t, cl.PeerID(), hr.PeerID)
191         assert.Equal(t, ih, hr.Hash)
192
193         assert.EqualValues(t, 0, tt.metadataSize())
194
195         func() {
196                 cl.mu.Lock()
197                 defer cl.mu.Unlock()
198                 go func() {
199                         _, err = nc.Write(pp.Message{
200                                 Type:       pp.Extended,
201                                 ExtendedID: pp.HandshakeExtendedID,
202                                 ExtendedPayload: func() []byte {
203                                         d := map[string]interface{}{
204                                                 "metadata_size": len(mi.InfoBytes),
205                                         }
206                                         b, err := bencode.Marshal(d)
207                                         if err != nil {
208                                                 panic(err)
209                                         }
210                                         return b
211                                 }(),
212                         }.MustMarshalBinary())
213                         require.NoError(t, err)
214                 }()
215                 tt.metadataChanged.Wait()
216         }()
217         assert.Equal(t, make([]byte, len(mi.InfoBytes)), tt.metadataBytes)
218         assert.False(t, tt.haveAllMetadataPieces())
219         assert.Nil(t, tt.Metainfo().InfoBytes)
220 }