11 "github.com/anacrolix/missinggo"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "github.com/anacrolix/torrent/bencode"
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)
24 if len(info.Files) == 1 {
25 t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
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)
33 for _, group := range mi.AnnounceList {
34 for _, tracker := range group {
35 t.Logf("Tracker: %s\n", tracker)
39 b, err := bencode.Marshal(&info)
40 require.NoError(t, err)
41 assert.EqualValues(t, string(b), string(mi.InfoBytes))
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")
51 // Ensure that the correct number of pieces are generated when hashing files.
52 func TestNumPieces(t *testing.T) {
53 for _, _case := range []struct {
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},
66 PieceLength: _case.PieceLength,
68 err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
69 return ioutil.NopCloser(missinggo.ZeroReader), nil
71 assert.NoError(t, err)
72 assert.EqualValues(t, _case.NumPieces, info.NumPieces())
76 func touchFile(path string) (err error) {
77 f, err := os.Create(path)
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")))
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`, &MetaInfo{})
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)
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)