]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Test empty files and zero piece length for both file and mmap storage backends
[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/metainfo"
15         "github.com/anacrolix/torrent/peer_protocol"
16         "github.com/anacrolix/torrent/storage"
17 )
18
19 func r(i, b, l peer_protocol.Integer) request {
20         return request{i, chunkSpec{b, l}}
21 }
22
23 // Check the given Request is correct for various torrent offsets.
24 func TestTorrentRequest(t *testing.T) {
25         const s = 472183431 // Length of torrent.
26         for _, _case := range []struct {
27                 off int64   // An offset into the torrent.
28                 req request // The expected Request. The zero value means !ok.
29         }{
30                 // Invalid offset.
31                 {-1, request{}},
32                 {0, r(0, 0, 16384)},
33                 // One before the end of a piece.
34                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
35                 // Offset beyond torrent length.
36                 {472 * 1 << 20, request{}},
37                 // One before the end of the torrent. Complicates the chunk length.
38                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
39                 {1, r(0, 0, 16384)},
40                 // One before end of chunk.
41                 {16383, r(0, 0, 16384)},
42                 // Second chunk.
43                 {16384, r(0, 16384, 16384)},
44         } {
45                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
46                 if (_case.req == request{}) == ok {
47                         t.Fatalf("expected %v, got %v", _case.req, req)
48                 }
49                 if req != _case.req {
50                         t.Fatalf("expected %v, got %v", _case.req, req)
51                 }
52         }
53 }
54
55 func TestAppendToCopySlice(t *testing.T) {
56         orig := []int{1, 2, 3}
57         dupe := append([]int{}, orig...)
58         dupe[0] = 4
59         if orig[0] != 1 {
60                 t.FailNow()
61         }
62 }
63
64 func TestTorrentString(t *testing.T) {
65         tor := &Torrent{}
66         s := tor.InfoHash().HexString()
67         if s != "0000000000000000000000000000000000000000" {
68                 t.FailNow()
69         }
70 }
71
72 // This benchmark is from the observation that a lot of overlapping Readers on
73 // a large torrent with small pieces had a lot of overhead in recalculating
74 // piece priorities everytime a reader (possibly in another Torrent) changed.
75 func BenchmarkUpdatePiecePriorities(b *testing.B) {
76         cl := &Client{}
77         t := cl.newTorrent(metainfo.Hash{})
78         t.info = &metainfo.Info{
79                 Pieces:      make([]byte, 20*13410),
80                 PieceLength: 256 << 10,
81         }
82         t.makePieces()
83         assert.EqualValues(b, 13410, t.numPieces())
84         for range iter.N(7) {
85                 r := t.NewReader()
86                 r.SetReadahead(32 << 20)
87                 r.Seek(3500000, 0)
88         }
89         assert.Len(b, t.readers, 7)
90         t.pendPieceRange(0, t.numPieces())
91         for i := 0; i < t.numPieces(); i += 3 {
92                 t.completedPieces.Set(i, true)
93         }
94         for range iter.N(b.N) {
95                 t.updatePiecePriorities()
96         }
97 }
98
99 // Check that a torrent containing zero-length file(s) will start, and that
100 // they're created in the filesystem. The client storage is assumed to be
101 // file-based on the native filesystem based.
102 func testEmptyFilesAndZeroPieceLength(t *testing.T, cs storage.ClientImpl) {
103         cfg := TestingConfig
104         cfg.DefaultStorage = cs
105         cl, err := NewClient(&TestingConfig)
106         require.NoError(t, err)
107         defer cl.Close()
108         ib, err := bencode.Marshal(metainfo.Info{
109                 Name:        "empty",
110                 Length:      0,
111                 PieceLength: 0,
112         })
113         require.NoError(t, err)
114         fp := filepath.Join(TestingConfig.DataDir, "empty")
115         os.Remove(fp)
116         assert.False(t, missinggo.FilePathExists(fp))
117         tt, err := cl.AddTorrent(&metainfo.MetaInfo{
118                 InfoBytes: ib,
119         })
120         require.NoError(t, err)
121         defer tt.Drop()
122         tt.DownloadAll()
123         require.True(t, cl.WaitAll())
124         assert.True(t, missinggo.FilePathExists(fp))
125 }
126
127 func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
128         testEmptyFilesAndZeroPieceLength(t, storage.NewFile(TestingConfig.DataDir))
129 }
130
131 func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
132         testEmptyFilesAndZeroPieceLength(t, storage.NewMMap(TestingConfig.DataDir))
133 }