]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Close implicit Client default storage on Client.Close
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "os"
5         "path/filepath"
6         "testing"
7
8         "github.com/anacrolix/missinggo"
9         "github.com/bradfitz/iter"
10         "github.com/stretchr/testify/assert"
11         "github.com/stretchr/testify/require"
12
13         "github.com/anacrolix/torrent/bencode"
14         "github.com/anacrolix/torrent/internal/testutil"
15         "github.com/anacrolix/torrent/metainfo"
16         "github.com/anacrolix/torrent/peer_protocol"
17         "github.com/anacrolix/torrent/storage"
18 )
19
20 func r(i, b, l peer_protocol.Integer) request {
21         return request{i, chunkSpec{b, l}}
22 }
23
24 // Check the given Request is correct for various torrent offsets.
25 func TestTorrentRequest(t *testing.T) {
26         const s = 472183431 // Length of torrent.
27         for _, _case := range []struct {
28                 off int64   // An offset into the torrent.
29                 req request // The expected Request. The zero value means !ok.
30         }{
31                 // Invalid offset.
32                 {-1, request{}},
33                 {0, r(0, 0, 16384)},
34                 // One before the end of a piece.
35                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
36                 // Offset beyond torrent length.
37                 {472 * 1 << 20, request{}},
38                 // One before the end of the torrent. Complicates the chunk length.
39                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
40                 {1, r(0, 0, 16384)},
41                 // One before end of chunk.
42                 {16383, r(0, 0, 16384)},
43                 // Second chunk.
44                 {16384, r(0, 16384, 16384)},
45         } {
46                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
47                 if (_case.req == request{}) == ok {
48                         t.Fatalf("expected %v, got %v", _case.req, req)
49                 }
50                 if req != _case.req {
51                         t.Fatalf("expected %v, got %v", _case.req, req)
52                 }
53         }
54 }
55
56 func TestAppendToCopySlice(t *testing.T) {
57         orig := []int{1, 2, 3}
58         dupe := append([]int{}, orig...)
59         dupe[0] = 4
60         if orig[0] != 1 {
61                 t.FailNow()
62         }
63 }
64
65 func TestTorrentString(t *testing.T) {
66         tor := &Torrent{}
67         s := tor.InfoHash().HexString()
68         if s != "0000000000000000000000000000000000000000" {
69                 t.FailNow()
70         }
71 }
72
73 // This benchmark is from the observation that a lot of overlapping Readers on
74 // a large torrent with small pieces had a lot of overhead in recalculating
75 // piece priorities everytime a reader (possibly in another Torrent) changed.
76 func BenchmarkUpdatePiecePriorities(b *testing.B) {
77         cl := &Client{}
78         t := cl.newTorrent(metainfo.Hash{}, nil)
79         t.info = &metainfo.Info{
80                 Pieces:      make([]byte, 20*13410),
81                 PieceLength: 256 << 10,
82         }
83         t.makePieces()
84         assert.EqualValues(b, 13410, t.numPieces())
85         for range iter.N(7) {
86                 r := t.NewReader()
87                 r.SetReadahead(32 << 20)
88                 r.Seek(3500000, 0)
89         }
90         assert.Len(b, t.readers, 7)
91         t.pendPieceRange(0, t.numPieces())
92         for i := 0; i < t.numPieces(); i += 3 {
93                 t.completedPieces.Set(i, true)
94         }
95         for range iter.N(b.N) {
96                 t.updateAllPiecePriorities()
97         }
98 }
99
100 // Check that a torrent containing zero-length file(s) will start, and that
101 // they're created in the filesystem. The client storage is assumed to be
102 // file-based on the native filesystem based.
103 func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *Config) {
104         cl, err := NewClient(cfg)
105         require.NoError(t, err)
106         defer cl.Close()
107         ib, err := bencode.Marshal(metainfo.Info{
108                 Name:        "empty",
109                 Length:      0,
110                 PieceLength: 0,
111         })
112         require.NoError(t, err)
113         fp := filepath.Join(cfg.DataDir, "empty")
114         os.Remove(fp)
115         assert.False(t, missinggo.FilePathExists(fp))
116         tt, err := cl.AddTorrent(&metainfo.MetaInfo{
117                 InfoBytes: ib,
118         })
119         require.NoError(t, err)
120         defer tt.Drop()
121         tt.DownloadAll()
122         require.True(t, cl.WaitAll())
123         assert.True(t, missinggo.FilePathExists(fp))
124 }
125
126 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
127         cfg := TestingConfig()
128         ci := storage.NewFile(cfg.DataDir)
129         defer ci.Close()
130         cfg.DefaultStorage = ci
131         testEmptyFilesAndZeroPieceLength(t, cfg)
132 }
133
134 func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
135         cfg := TestingConfig()
136         ci := storage.NewMMap(cfg.DataDir)
137         defer ci.Close()
138         cfg.DefaultStorage = ci
139         testEmptyFilesAndZeroPieceLength(t, cfg)
140 }
141
142 func TestPieceHashFailed(t *testing.T) {
143         mi := testutil.GreetingMetaInfo()
144         tt := Torrent{
145                 cl:            new(Client),
146                 infoHash:      mi.HashInfoBytes(),
147                 storageOpener: storage.NewClient(badStorage{}),
148                 chunkSize:     2,
149         }
150         require.NoError(t, tt.setInfoBytes(mi.InfoBytes))
151         tt.cl.mu.Lock()
152         tt.pieces[1].DirtyChunks.AddRange(0, 3)
153         require.True(t, tt.pieceAllDirty(1))
154         tt.pieceHashed(1, false)
155         // Dirty chunks should be cleared so we can try again.
156         require.False(t, tt.pieceAllDirty(1))
157         tt.cl.mu.Unlock()
158 }