12 "github.com/anacrolix/missinggo/v2"
13 qt "github.com/frankban/quicktest"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
17 "github.com/anacrolix/torrent/bencode"
20 func testFile(t *testing.T, filename string) {
21 mi, err := LoadFromFile(filename)
22 require.NoError(t, err)
23 info, err := mi.UnmarshalInfo()
24 require.NoError(t, err)
26 if len(info.Files) == 1 {
27 t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
29 t.Logf("Multiple files: %s\n", info.Name)
30 for _, f := range info.Files {
31 t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
35 for _, group := range mi.AnnounceList {
36 for _, tracker := range group {
37 t.Logf("Tracker: %s\n", tracker)
41 b, err := bencode.Marshal(&info)
42 require.NoError(t, err)
43 assert.EqualValues(t, string(b), string(mi.InfoBytes))
46 func TestFile(t *testing.T) {
47 testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
48 testFile(t, "testdata/continuum.torrent")
49 testFile(t, "testdata/23516C72685E8DB0C8F15553382A927F185C4F01.torrent")
50 testFile(t, "testdata/trackerless.torrent")
53 // Ensure that the correct number of pieces are generated when hashing files.
54 func TestNumPieces(t *testing.T) {
55 for _, _case := range []struct {
60 {256 * 1024, []FileInfo{{Length: 1024*1024 + -1}}, 4},
61 {256 * 1024, []FileInfo{{Length: 1024 * 1024}}, 4},
62 {256 * 1024, []FileInfo{{Length: 1024*1024 + 1}}, 5},
63 {5, []FileInfo{{Length: 1}, {Length: 12}}, 3},
64 {5, []FileInfo{{Length: 4}, {Length: 12}}, 4},
68 PieceLength: _case.PieceLength,
70 err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
71 return ioutil.NopCloser(missinggo.ZeroReader), nil
73 assert.NoError(t, err)
74 assert.EqualValues(t, _case.NumPieces, info.NumPieces())
78 func touchFile(path string) (err error) {
79 f, err := os.Create(path)
87 func TestBuildFromFilePathOrder(t *testing.T) {
89 require.NoError(t, touchFile(filepath.Join(td, "b")))
90 require.NoError(t, touchFile(filepath.Join(td, "a")))
94 require.NoError(t, info.BuildFromFilePath(td))
95 assert.EqualValues(t, []FileInfo{{
102 func testUnmarshal(t *testing.T, input string, expected *MetaInfo) {
104 err := bencode.Unmarshal([]byte(input), &actual)
109 assert.NoError(t, err)
110 assert.EqualValues(t, *expected, actual)
113 func TestUnmarshal(t *testing.T) {
114 testUnmarshal(t, `de`, &MetaInfo{})
115 testUnmarshal(t, `d4:infoe`, nil)
116 testUnmarshal(t, `d4:infoabce`, nil)
117 testUnmarshal(t, `d4:infodee`, &MetaInfo{InfoBytes: []byte("de")})
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 qt.Assert(t, mi.Magnet(nil, nil).String(), qt.ContentEquals,
125 strings.Join([]string{
126 "magnet:?xt=urn:btih:d4b197dff199aad447a9a352e31528adbbd97922",
127 "tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce",
128 "tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce",
129 "ws=https%3A%2F%2Farchive.org%2Fdownload%2F",
130 "ws=http%3A%2F%2Fia601600.us.archive.org%2F26%2Fitems%2F",
131 "ws=http%3A%2F%2Fia801600.us.archive.org%2F26%2Fitems%2F",
135 func TestMetainfoWithStringURLList(t *testing.T) {
136 mi, err := LoadFromFile("testdata/flat-url-list.torrent")
137 require.NoError(t, err)
138 assert.Len(t, mi.UrlList, 1)
139 qt.Assert(t, mi.Magnet(nil, nil).String(), qt.ContentEquals,
140 strings.Join([]string{
141 "magnet:?xt=urn:btih:9da24e606e4ed9c7b91c1772fb5bf98f82bd9687",
142 "tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce",
143 "tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce",
144 "ws=https%3A%2F%2Farchive.org%2Fdownload%2F",
148 // https://github.com/anacrolix/torrent/issues/247
150 // The decoder buffer wasn't cleared before starting the next dict item after
151 // a syntax error on a field with the ignore_unmarshal_type_error tag.
152 func TestStringCreationDate(t *testing.T) {
154 assert.NoError(t, bencode.Unmarshal([]byte("d13:creation date23:29.03.2018 22:18:14 UTC4:infodee"), &mi))
157 // See https://github.com/anacrolix/torrent/issues/843.
158 func TestUnmarshalEmptyStringNodes(t *testing.T) {
161 err := bencode.Unmarshal([]byte("d5:nodes0:e"), &mi)
162 c.Assert(err, qt.IsNil)