]> Sergey Matveev's repositories - btrtrc.git/blobdiff - metainfo/nodes_test.go
Drop support for go 1.20
[btrtrc.git] / metainfo / nodes_test.go
index 2b8768a32879f3a263cf32e325b4eb91b8c56759..adebbb3670371b2f96de91e61de6a76fe82a5fd2 100644 (file)
@@ -1,6 +1,7 @@
 package metainfo
 
 import (
+       "bytes"
        "testing"
 
        "github.com/stretchr/testify/assert"
@@ -42,15 +43,16 @@ func TestNodesListPairsBEP5(t *testing.T) {
 }
 
 func testMarshalMetainfo(t *testing.T, expected string, mi *MetaInfo) {
-       b, err := bencode.Marshal(mi)
+       b, err := bencode.Marshal(*mi)
        assert.NoError(t, err)
        assert.EqualValues(t, expected, string(b))
 }
 
 func TestMarshalMetainfoNodes(t *testing.T) {
-       testMarshalMetainfo(t, "d4:infod4:name0:12:piece lengthi0e6:piecesleee", &MetaInfo{})
-       testMarshalMetainfo(t, "d4:infod4:name0:12:piece lengthi0e6:pieceslee5:nodesl12:1.2.3.4:555514:not a hostportee", &MetaInfo{
-               Nodes: []Node{"1.2.3.4:5555", "not a hostport"},
+       testMarshalMetainfo(t, "d4:infodee", &MetaInfo{InfoBytes: []byte("de")})
+       testMarshalMetainfo(t, "d4:infod2:hi5:theree5:nodesl12:1.2.3.4:555514:not a hostportee", &MetaInfo{
+               Nodes:     []Node{"1.2.3.4:5555", "not a hostport"},
+               InfoBytes: []byte("d2:hi5:theree"),
        })
 }
 
@@ -60,3 +62,13 @@ func TestUnmarshalBadMetainfoNodes(t *testing.T) {
        err := bencode.Unmarshal([]byte("d5:nodesl1:ai42eee"), &mi)
        require.Error(t, err)
 }
+
+func TestMetainfoEmptyInfoBytes(t *testing.T) {
+       var buf bytes.Buffer
+       require.NoError(t, (&MetaInfo{
+               // Include a non-empty field that comes after "info".
+               UrlList: []string{"hello"},
+       }).Write(&buf))
+       var mi MetaInfo
+       require.NoError(t, bencode.Unmarshal(buf.Bytes(), &mi))
+}