-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathmisc_test.go
More file actions
120 lines (109 loc) · 3.23 KB
/
Copy pathmisc_test.go
File metadata and controls
120 lines (109 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package torrent
import (
"reflect"
"strings"
"testing"
g "github.com/anacrolix/generics"
"github.com/davecgh/go-spew/spew"
qt "github.com/go-quicktest/qt"
"github.com/anacrolix/torrent/metainfo"
infohash_v2 "github.com/anacrolix/torrent/types/infohash-v2"
)
func TestTorrentOffsetRequest(t *testing.T) {
check := func(tl, ps, off int64, expected Request, ok bool) {
req, _ok := torrentOffsetRequest(tl, ps, defaultChunkSize, off)
qt.Check(t, qt.Equals(ok, _ok))
qt.Check(t, qt.Equals(expected, req))
}
check(13, 5, 0, newRequest(0, 0, 5), true)
check(13, 5, 3, newRequest(0, 0, 5), true)
check(13, 5, 11, newRequest(2, 0, 3), true)
check(13, 5, 13, Request{}, false)
}
func TestSpewConnStats(t *testing.T) {
s := spew.Sdump(ConnStats{})
t.Logf("\n%s", s)
lines := strings.Count(s, "\n")
qt.Check(t, qt.Equals(lines, 2+reflect.ValueOf(ConnStats{}).NumField()))
}
func TestValidateInfoShortV2Root(t *testing.T) {
info := metainfo.Info{
MetaVersion: 2,
PieceLength: 16384,
Name: "x",
FileTree: metainfo.FileTree{
Dir: map[string]metainfo.FileTree{
"a": {File: metainfo.FileTreeFile{Length: 1, PiecesRoot: "short"}},
},
},
}
err := validateInfo(&info)
if err == nil {
t.Fatal("expected validateInfo to reject short pieces root")
}
if !strings.Contains(err.Error(), "pieces root length 5") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidateInfoMissingV2Root(t *testing.T) {
info := metainfo.Info{
MetaVersion: 2,
PieceLength: 16384,
Name: "x",
FileTree: metainfo.FileTree{
Dir: map[string]metainfo.FileTree{
"a": {File: metainfo.FileTreeFile{Length: 100, PiecesRoot: ""}},
},
},
}
err := validateInfo(&info)
if err == nil {
t.Fatal("expected validateInfo to reject missing pieces root on non-empty file")
}
if !strings.Contains(err.Error(), "pieces root length 0") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidateInfoValidV2(t *testing.T) {
valid32 := strings.Repeat("x", 32)
info := metainfo.Info{
MetaVersion: 2,
PieceLength: 16384,
Name: "x",
FileTree: metainfo.FileTree{
Dir: map[string]metainfo.FileTree{
"a": {File: metainfo.FileTreeFile{Length: 1024, PiecesRoot: valid32}},
},
},
}
err := validateInfo(&info)
if err != nil {
t.Fatalf("expected valid v2 info to pass, got: %v", err)
}
}
// End-to-end: AddTorrentSpec with a crafted v2 info dict containing a short pieces root
// must return an error instead of panicking through PiecesRootAsByteArray.
func TestAddTorrentSpecBadV2Root(t *testing.T) {
infoBytes := append(
[]byte("d9:file treed1:ad0:d6:lengthi1e11:pieces root5:shorteee6:lengthi1e12:meta versioni2e4:name1:x12:piece lengthi16384e6:pieces20:"),
make([]byte, metainfo.HashSize)...)
infoBytes = append(infoBytes, 'e')
cl, err := NewClient(TestingConfig(t))
if err != nil {
t.Fatal(err)
}
defer cl.Close()
_, _, err = cl.AddTorrentSpec(&TorrentSpec{
AddTorrentOpts: AddTorrentOpts{
InfoHash: metainfo.HashBytes(infoBytes),
InfoHashV2: g.Some(infohash_v2.HashBytes(infoBytes)),
InfoBytes: infoBytes,
},
})
if err == nil {
t.Fatal("expected AddTorrentSpec to reject malformed v2 pieces root")
}
if !strings.Contains(err.Error(), "bad v2 file tree") {
t.Fatalf("unexpected error: %v", err)
}
}