From b3a5591d1476cdffe099900d98e56da37f2338b5 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 23 Feb 2016 22:04:06 +1100 Subject: [PATCH] metainfo: It looks like the "nodes" field of metainfo was never handled correctly, support []string instead of [][]string, fix tests --- metainfo/builder.go | 15 ++++++++++++--- metainfo/metainfo.go | 2 +- metainfo/nodes_test.go | 20 ++++++++++++++++++++ metainfo/testdata/trackerless.torrent | 2 +- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 metainfo/nodes_test.go diff --git a/metainfo/builder.go b/metainfo/builder.go index 72683bb5..b600f188 100644 --- a/metainfo/builder.go +++ b/metainfo/builder.go @@ -69,7 +69,7 @@ func (b *Builder) AddAnnounceGroup(group []string) { // Add DHT nodes URLs for trackerless mode func (b *Builder) AddDhtNodes(group []string) { - b.node_list = append(b.node_list, group) + b.node_list = append(b.node_list, group...) } // Sets creation date. The default is time.Now() when the .Build method was @@ -202,6 +202,15 @@ func (b *Builder) set_defaults() { } } +func emptyStringsFiltered(ss []string) (ret []string) { + for _, s := range ss { + if s != "" { + ret = append(ret, s) + } + } + return +} + func (b *Builder) check_parameters() error { // should be at least one file if len(b.filesmap) == 0 { @@ -210,7 +219,7 @@ func (b *Builder) check_parameters() error { // let's clean up the announce_list and node_list b.announce_list = cleanUpLists(b.announce_list) - b.node_list = cleanUpLists(b.node_list) + b.node_list = emptyStringsFiltered(b.node_list) if len(b.announce_list) == 0 && len(b.node_list) == 0 { return errors.New("no announce group or DHT nodes specified") @@ -446,7 +455,7 @@ type batch_state struct { pieces []byte private bool announce_list [][]string - node_list [][]string + node_list []string creation_date time.Time comment string created_by string diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index ad674887..dd8fc6d8 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -233,7 +233,7 @@ type MetaInfo struct { Info InfoEx `bencode:"info"` Announce string `bencode:"announce,omitempty"` AnnounceList [][]string `bencode:"announce-list,omitempty"` - Nodes [][]string `bencode:"nodes,omitempty"` + Nodes []string `bencode:"nodes,omitempty"` CreationDate int64 `bencode:"creation date,omitempty"` Comment string `bencode:"comment,omitempty"` CreatedBy string `bencode:"created by,omitempty"` diff --git a/metainfo/nodes_test.go b/metainfo/nodes_test.go new file mode 100644 index 00000000..c9438b7e --- /dev/null +++ b/metainfo/nodes_test.go @@ -0,0 +1,20 @@ +package metainfo + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNodesListStrings(t *testing.T) { + mi, err := LoadFromFile("testdata/trackerless.torrent") + require.NoError(t, err) + assert.EqualValues(t, []string{ + "udp://tracker.openbittorrent.com:80", + "udp://tracker.openbittorrent.com:80", + }, mi.Nodes) +} + +func TestNodesListPairsBEP5(t *testing.T) { +} diff --git a/metainfo/testdata/trackerless.torrent b/metainfo/testdata/trackerless.torrent index b90f253e..65372766 100644 --- a/metainfo/testdata/trackerless.torrent +++ b/metainfo/testdata/trackerless.torrent @@ -1 +1 @@ -d7:comment19:This is just a test10:created by12:Johnny Bravo13:creation datei1430648794e8:encoding5:UTF-84:infod6:lengthi1128e4:name12:testfile.bin12:piece lengthi32768e6:pieces20:Õˆë =‘UŒäiÎ^æ °Eâ?ÇÒe5:nodesll35:udp://tracker.openbittorrent.com:8035:udp://tracker.openbittorrent.com:80eee \ No newline at end of file +d7:comment19:This is just a test10:created by12:Johnny Bravo13:creation datei1430648794e8:encoding5:UTF-84:infod6:lengthi1128e4:name12:testfile.bin12:piece lengthi32768e6:pieces20:Õˆë =‘UŒäiÎ^æ °Eâ?ÇÒe5:nodesl35:udp://tracker.openbittorrent.com:8035:udp://tracker.openbittorrent.com:80ee