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