]> Sergey Matveev's repositories - btrtrc.git/blob - client_test.go
Piece priorities, torrent read interface and many fixes
[btrtrc.git] / client_test.go
1 package torrent
2
3 import (
4         "os"
5         "testing"
6         "time"
7
8         "bitbucket.org/anacrolix/go.torrent/testutil"
9         "bitbucket.org/anacrolix/go.torrent/util"
10         "github.com/anacrolix/libtorgo/bencode"
11 )
12
13 func TestClientDefault(t *testing.T) {
14         cl, err := NewClient(&Config{
15                 NoDefaultBlocklist: true,
16         })
17         if err != nil {
18                 t.Fatal(err)
19         }
20         cl.Stop()
21 }
22
23 func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
24         t.SkipNow()
25 }
26
27 func TestAddTorrentNoUsableURLs(t *testing.T) {
28         t.SkipNow()
29 }
30
31 func TestAddPeersToUnknownTorrent(t *testing.T) {
32         t.SkipNow()
33 }
34
35 func TestPieceHashSize(t *testing.T) {
36         if pieceHash.Size() != 20 {
37                 t.FailNow()
38         }
39 }
40
41 func TestTorrentInitialState(t *testing.T) {
42         dir, mi := testutil.GreetingTestTorrent()
43         defer os.RemoveAll(dir)
44         tor, err := newTorrent(func() (ih InfoHash) {
45                 util.CopyExact(ih[:], mi.Info.Hash)
46                 return
47         }(), nil, 0)
48         if err != nil {
49                 t.Fatal(err)
50         }
51         err = tor.setMetadata(mi.Info.Info, dir, mi.Info.Bytes, nil)
52         if err != nil {
53                 t.Fatal(err)
54         }
55         if len(tor.Pieces) != 1 {
56                 t.Fatal("wrong number of pieces")
57         }
58         p := tor.Pieces[0]
59         if len(p.PendingChunkSpecs) != 1 {
60                 t.Fatalf("should only be 1 chunk: %v", p.PendingChunkSpecs)
61         }
62         if _, ok := p.PendingChunkSpecs[chunkSpec{
63                 Length: 13,
64         }]; !ok {
65                 t.Fatal("pending chunk spec is incorrect")
66         }
67 }
68
69 func TestUnmarshalPEXMsg(t *testing.T) {
70         var m peerExchangeMessage
71         if err := bencode.Unmarshal([]byte("d5:added12:\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0ce"), &m); err != nil {
72                 t.Fatal(err)
73         }
74         if len(m.Added) != 2 {
75                 t.FailNow()
76         }
77         if m.Added[0].Port != 0x506 {
78                 t.FailNow()
79         }
80 }
81
82 func TestReducedDialTimeout(t *testing.T) {
83         for _, _case := range []struct {
84                 Max             time.Duration
85                 HalfOpenLimit   int
86                 PendingPeers    int
87                 ExpectedReduced time.Duration
88         }{
89                 {nominalDialTimeout, 40, 0, nominalDialTimeout},
90                 {nominalDialTimeout, 40, 1, nominalDialTimeout},
91                 {nominalDialTimeout, 40, 39, nominalDialTimeout},
92                 {nominalDialTimeout, 40, 40, nominalDialTimeout / 2},
93                 {nominalDialTimeout, 40, 80, nominalDialTimeout / 3},
94                 {nominalDialTimeout, 40, 4000, nominalDialTimeout / 101},
95         } {
96                 reduced := reducedDialTimeout(_case.Max, _case.HalfOpenLimit, _case.PendingPeers)
97                 expected := _case.ExpectedReduced
98                 if expected < minDialTimeout {
99                         expected = minDialTimeout
100                 }
101                 if reduced != expected {
102                         t.Fatalf("expected %s, got %s", _case.ExpectedReduced, reduced)
103                 }
104         }
105 }