8 "bitbucket.org/anacrolix/go.torrent/testutil"
9 "bitbucket.org/anacrolix/go.torrent/util"
10 "github.com/anacrolix/libtorgo/bencode"
13 func TestClientDefault(t *testing.T) {
14 cl, err := NewClient(&Config{
15 NoDefaultBlocklist: true,
23 func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
27 func TestAddTorrentNoUsableURLs(t *testing.T) {
31 func TestAddPeersToUnknownTorrent(t *testing.T) {
35 func TestPieceHashSize(t *testing.T) {
36 if pieceHash.Size() != 20 {
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)
51 err = tor.setMetadata(mi.Info.Info, dir, mi.Info.Bytes, nil)
55 if len(tor.Pieces) != 1 {
56 t.Fatal("wrong number of pieces")
59 if len(p.PendingChunkSpecs) != 1 {
60 t.Fatalf("should only be 1 chunk: %v", p.PendingChunkSpecs)
62 if _, ok := p.PendingChunkSpecs[chunkSpec{
65 t.Fatal("pending chunk spec is incorrect")
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 {
74 if len(m.Added) != 2 {
77 if m.Added[0].Port != 0x506 {
82 func TestReducedDialTimeout(t *testing.T) {
83 for _, _case := range []struct {
87 ExpectedReduced time.Duration
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},
96 reduced := reducedDialTimeout(_case.Max, _case.HalfOpenLimit, _case.PendingPeers)
97 expected := _case.ExpectedReduced
98 if expected < minDialTimeout {
99 expected = minDialTimeout
101 if reduced != expected {
102 t.Fatalf("expected %s, got %s", _case.ExpectedReduced, reduced)