7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
10 "github.com/anacrolix/torrent/bencode"
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)
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",
26 func TestNodesListPairsBEP5(t *testing.T) {
27 testFileNodesMatch(t, "testdata/issue_65a.torrent", []Node{
30 "94.209.253.165:47232",
32 "195.154.162.70:55011",
35 testFileNodesMatch(t, "testdata/issue_65b.torrent", []Node{
36 "95.211.203.130:6881",
39 "101.187.175.163:19665",
41 "83.128.223.71:23865",
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))
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"),
59 func TestUnmarshalBadMetainfoNodes(t *testing.T) {
61 // Should barf on the integer in the nodes list.
62 err := bencode.Unmarshal([]byte("d5:nodesl1:ai42eee"), &mi)
66 func TestMetainfoEmptyInfoBytes(t *testing.T) {
68 require.NoError(t, (&MetaInfo{
69 // Include a non-empty field that comes after "info".
70 UrlList: []string{"hello"},
73 require.NoError(t, bencode.Unmarshal(buf.Bytes(), &mi))