]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo_test.go
metainfo URL list can be a string or list
[btrtrc.git] / metainfo / metainfo_test.go
1 package metainfo
2
3 import (
4         "io"
5         "io/ioutil"
6         "os"
7         "path"
8         "path/filepath"
9         "testing"
10
11         "github.com/anacrolix/missinggo"
12         "github.com/stretchr/testify/assert"
13         "github.com/stretchr/testify/require"
14
15         "github.com/anacrolix/torrent/bencode"
16 )
17
18 func testFile(t *testing.T, filename string) {
19         mi, err := LoadFromFile(filename)
20         require.NoError(t, err)
21         info, err := mi.UnmarshalInfo()
22         require.NoError(t, err)
23
24         if len(info.Files) == 1 {
25                 t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
26         } else {
27                 t.Logf("Multiple files: %s\n", info.Name)
28                 for _, f := range info.Files {
29                         t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
30                 }
31         }
32
33         for _, group := range mi.AnnounceList {
34                 for _, tracker := range group {
35                         t.Logf("Tracker: %s\n", tracker)
36                 }
37         }
38
39         b, err := bencode.Marshal(&info)
40         require.NoError(t, err)
41         assert.EqualValues(t, string(b), string(mi.InfoBytes))
42 }
43
44 func TestFile(t *testing.T) {
45         testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
46         testFile(t, "testdata/continuum.torrent")
47         testFile(t, "testdata/23516C72685E8DB0C8F15553382A927F185C4F01.torrent")
48         testFile(t, "testdata/trackerless.torrent")
49 }
50
51 // Ensure that the correct number of pieces are generated when hashing files.
52 func TestNumPieces(t *testing.T) {
53         for _, _case := range []struct {
54                 PieceLength int64
55                 Files       []FileInfo
56                 NumPieces   int
57         }{
58                 {256 * 1024, []FileInfo{{Length: 1024*1024 + -1}}, 4},
59                 {256 * 1024, []FileInfo{{Length: 1024 * 1024}}, 4},
60                 {256 * 1024, []FileInfo{{Length: 1024*1024 + 1}}, 5},
61                 {5, []FileInfo{{Length: 1}, {Length: 12}}, 3},
62                 {5, []FileInfo{{Length: 4}, {Length: 12}}, 4},
63         } {
64                 info := Info{
65                         Files:       _case.Files,
66                         PieceLength: _case.PieceLength,
67                 }
68                 err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
69                         return ioutil.NopCloser(missinggo.ZeroReader), nil
70                 })
71                 assert.NoError(t, err)
72                 assert.EqualValues(t, _case.NumPieces, info.NumPieces())
73         }
74 }
75
76 func touchFile(path string) (err error) {
77         f, err := os.Create(path)
78         if err != nil {
79                 return
80         }
81         err = f.Close()
82         return
83 }
84
85 func TestBuildFromFilePathOrder(t *testing.T) {
86         td, err := ioutil.TempDir("", "anacrolix")
87         require.NoError(t, err)
88         defer os.RemoveAll(td)
89         require.NoError(t, touchFile(filepath.Join(td, "b")))
90         require.NoError(t, touchFile(filepath.Join(td, "a")))
91         info := Info{
92                 PieceLength: 1,
93         }
94         require.NoError(t, info.BuildFromFilePath(td))
95         assert.EqualValues(t, []FileInfo{{
96                 Path: []string{"a"},
97         }, {
98                 Path: []string{"b"},
99         }}, info.Files)
100 }
101
102 func testUnmarshal(t *testing.T, input string, expected *MetaInfo) {
103         var actual MetaInfo
104         err := bencode.Unmarshal([]byte(input), &actual)
105         if expected == nil {
106                 assert.Error(t, err)
107                 return
108         }
109         assert.NoError(t, err)
110         assert.EqualValues(t, *expected, actual)
111 }
112
113 func TestUnmarshal(t *testing.T) {
114         testUnmarshal(t, `de`, &MetaInfo{})
115         testUnmarshal(t, `d4:infoe`, &MetaInfo{})
116         testUnmarshal(t, `d4:infoabce`, nil)
117         testUnmarshal(t, `d4:infodee`, &MetaInfo{InfoBytes: []byte("de")})
118 }
119
120 func TestMetainfoWithListURLList(t *testing.T) {
121         mi, err := LoadFromFile("testdata/SKODAOCTAVIA336x280_archive.torrent")
122         require.NoError(t, err)
123         assert.Len(t, mi.UrlList, 3)
124 }
125
126 func TestMetainfoWithStringURLList(t *testing.T) {
127         mi, err := LoadFromFile("testdata/flat-url-list.torrent")
128         require.NoError(t, err)
129         assert.Len(t, mi.UrlList, 1)
130 }