]> Sergey Matveev's repositories - btrtrc.git/blobdiff - metainfo/metainfo_test.go
Fix error unmarshalling bad metainfo nodes field
[btrtrc.git] / metainfo / metainfo_test.go
index de81b8da67c0f8fc11c787084c8b67b513944320..d1a6c356c42fa83d304f754e22db963edfdfdea7 100644 (file)
@@ -6,9 +6,11 @@ import (
        "os"
        "path"
        "path/filepath"
+       "strings"
        "testing"
 
-       "github.com/anacrolix/missinggo"
+       "github.com/anacrolix/missinggo/v2"
+       qt "github.com/frankban/quicktest"
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
 
@@ -18,8 +20,9 @@ import (
 func testFile(t *testing.T, filename string) {
        mi, err := LoadFromFile(filename)
        require.NoError(t, err)
+       info, err := mi.UnmarshalInfo()
+       require.NoError(t, err)
 
-       info := mi.UnmarshalInfo()
        if len(info.Files) == 1 {
                t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
        } else {
@@ -82,9 +85,7 @@ func touchFile(path string) (err error) {
 }
 
 func TestBuildFromFilePathOrder(t *testing.T) {
-       td, err := ioutil.TempDir("", "anacrolix")
-       require.NoError(t, err)
-       defer os.RemoveAll(td)
+       td := t.TempDir()
        require.NoError(t, touchFile(filepath.Join(td, "b")))
        require.NoError(t, touchFile(filepath.Join(td, "a")))
        info := Info{
@@ -111,7 +112,52 @@ func testUnmarshal(t *testing.T, input string, expected *MetaInfo) {
 
 func TestUnmarshal(t *testing.T) {
        testUnmarshal(t, `de`, &MetaInfo{})
-       testUnmarshal(t, `d4:infoe`, &MetaInfo{})
+       testUnmarshal(t, `d4:infoe`, nil)
        testUnmarshal(t, `d4:infoabce`, nil)
        testUnmarshal(t, `d4:infodee`, &MetaInfo{InfoBytes: []byte("de")})
 }
+
+func TestMetainfoWithListURLList(t *testing.T) {
+       mi, err := LoadFromFile("testdata/SKODAOCTAVIA336x280_archive.torrent")
+       require.NoError(t, err)
+       assert.Len(t, mi.UrlList, 3)
+       qt.Assert(t, mi.Magnet(nil, nil).String(), qt.ContentEquals,
+               strings.Join([]string{
+                       "magnet:?xt=urn:btih:d4b197dff199aad447a9a352e31528adbbd97922",
+                       "tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce",
+                       "tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce",
+                       "ws=https%3A%2F%2Farchive.org%2Fdownload%2F",
+                       "ws=http%3A%2F%2Fia601600.us.archive.org%2F26%2Fitems%2F",
+                       "ws=http%3A%2F%2Fia801600.us.archive.org%2F26%2Fitems%2F",
+               }, "&"))
+}
+
+func TestMetainfoWithStringURLList(t *testing.T) {
+       mi, err := LoadFromFile("testdata/flat-url-list.torrent")
+       require.NoError(t, err)
+       assert.Len(t, mi.UrlList, 1)
+       qt.Assert(t, mi.Magnet(nil, nil).String(), qt.ContentEquals,
+               strings.Join([]string{
+                       "magnet:?xt=urn:btih:9da24e606e4ed9c7b91c1772fb5bf98f82bd9687",
+                       "tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce",
+                       "tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce",
+                       "ws=https%3A%2F%2Farchive.org%2Fdownload%2F",
+               }, "&"))
+}
+
+// https://github.com/anacrolix/torrent/issues/247
+//
+// The decoder buffer wasn't cleared before starting the next dict item after
+// a syntax error on a field with the ignore_unmarshal_type_error tag.
+func TestStringCreationDate(t *testing.T) {
+       var mi MetaInfo
+       assert.NoError(t, bencode.Unmarshal([]byte("d13:creation date23:29.03.2018 22:18:14 UTC4:infodee"), &mi))
+}
+
+// See https://github.com/anacrolix/torrent/issues/843.
+func TestUnmarshalEmptyStringNodes(t *testing.T) {
+       var mi MetaInfo
+       c := qt.New(t)
+       err := bencode.Unmarshal([]byte("d5:nodes0:e"), &mi)
+       c.Assert(err, qt.IsNil)
+}