]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Allow configuring Client torrent data opener, config dir, disabling metainfo cache...
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "sync"
5         "testing"
6         "time"
7
8         "bitbucket.org/anacrolix/go.torrent/peer_protocol"
9 )
10
11 func r(i, b, l peer_protocol.Integer) request {
12         return request{i, chunkSpec{b, l}}
13 }
14
15 // Check the given Request is correct for various torrent offsets.
16 func TestTorrentRequest(t *testing.T) {
17         const s = 472183431 // Length of torrent.
18         for _, _case := range []struct {
19                 off int64   // An offset into the torrent.
20                 req request // The expected Request. The zero value means !ok.
21         }{
22                 // Invalid offset.
23                 {-1, request{}},
24                 {0, r(0, 0, 16384)},
25                 // One before the end of a piece.
26                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
27                 // Offset beyond torrent length.
28                 {472 * 1 << 20, request{}},
29                 // One before the end of the torrent. Complicates the chunk length.
30                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
31                 {1, r(0, 0, 16384)},
32                 // One before end of chunk.
33                 {16383, r(0, 0, 16384)},
34                 // Second chunk.
35                 {16384, r(0, 16384, 16384)},
36         } {
37                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
38                 if (_case.req == request{}) == ok {
39                         t.Fatalf("expected %v, got %v", _case.req, req)
40                 }
41                 if req != _case.req {
42                         t.Fatalf("expected %v, got %v", _case.req, req)
43                 }
44         }
45 }
46
47 func TestTorrentDoubleClose(t *testing.T) {
48         tt, err := newTorrent(InfoHash{}, nil, 0)
49         tt.pruneTimer = time.NewTimer(0)
50         if err != nil {
51                 t.Fatal(err)
52         }
53         wg := sync.WaitGroup{}
54         for i := 0; i < 2; i++ {
55                 wg.Add(1)
56                 go func() {
57                         tt.close()
58                         wg.Done()
59                 }()
60         }
61         wg.Wait()
62 }
63
64 func TestAppendToCopySlice(t *testing.T) {
65         orig := []int{1, 2, 3}
66         dupe := append([]int{}, orig...)
67         dupe[0] = 4
68         if orig[0] != 1 {
69                 t.FailNow()
70         }
71 }
72
73 func TestTorrentString(t *testing.T) {
74         tor := &torrent{}
75         s := tor.InfoHash.HexString()
76         if s != "0000000000000000000000000000000000000000" {
77                 t.FailNow()
78         }
79 }