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