]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/nodes_test.go
feat: remove print BuildFromFilePath
[btrtrc.git] / metainfo / nodes_test.go
1 package metainfo
2
3 import (
4         "testing"
5
6         "github.com/stretchr/testify/assert"
7         "github.com/stretchr/testify/require"
8
9         "github.com/anacrolix/torrent/bencode"
10 )
11
12 func testFileNodesMatch(t *testing.T, file string, nodes []Node) {
13         mi, err := LoadFromFile(file)
14         require.NoError(t, err)
15         assert.EqualValues(t, nodes, mi.Nodes)
16 }
17
18 func TestNodesListStrings(t *testing.T) {
19         testFileNodesMatch(t, "testdata/trackerless.torrent", []Node{
20                 "udp://tracker.openbittorrent.com:80",
21                 "udp://tracker.openbittorrent.com:80",
22         })
23 }
24
25 func TestNodesListPairsBEP5(t *testing.T) {
26         testFileNodesMatch(t, "testdata/issue_65a.torrent", []Node{
27                 "185.34.3.132:5680",
28                 "185.34.3.103:12340",
29                 "94.209.253.165:47232",
30                 "78.46.103.11:34319",
31                 "195.154.162.70:55011",
32                 "185.34.3.137:3732",
33         })
34         testFileNodesMatch(t, "testdata/issue_65b.torrent", []Node{
35                 "95.211.203.130:6881",
36                 "84.72.116.169:6889",
37                 "204.83.98.77:7000",
38                 "101.187.175.163:19665",
39                 "37.187.118.32:6881",
40                 "83.128.223.71:23865",
41         })
42 }
43
44 func testMarshalMetainfo(t *testing.T, expected string, mi *MetaInfo) {
45         b, err := bencode.Marshal(*mi)
46         assert.NoError(t, err)
47         assert.EqualValues(t, expected, string(b))
48 }
49
50 func TestMarshalMetainfoNodes(t *testing.T) {
51         testMarshalMetainfo(t, "d4:infodee", &MetaInfo{InfoBytes: []byte("de")})
52         testMarshalMetainfo(t, "d4:infod2:hi5:theree5:nodesl12:1.2.3.4:555514:not a hostportee", &MetaInfo{
53                 Nodes:     []Node{"1.2.3.4:5555", "not a hostport"},
54                 InfoBytes: []byte("d2:hi5:theree"),
55         })
56 }
57
58 func TestUnmarshalBadMetainfoNodes(t *testing.T) {
59         var mi MetaInfo
60         // Should barf on the integer in the nodes list.
61         err := bencode.Unmarshal([]byte("d5:nodesl1:ai42eee"), &mi)
62         require.Error(t, err)
63 }