9 "github.com/anacrolix/missinggo"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
13 "github.com/anacrolix/torrent/bencode"
16 func testFile(t *testing.T, filename string) {
17 mi, err := LoadFromFile(filename)
22 if len(mi.Info.Files) == 1 {
23 t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
25 t.Logf("Multiple files: %s\n", mi.Info.Name)
26 for _, f := range mi.Info.Files {
27 t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
31 for _, group := range mi.AnnounceList {
32 for _, tracker := range group {
33 t.Logf("Tracker: %s\n", tracker)
36 // for _, url := range mi.WebSeedURLs {
37 // t.Logf("URL: %s\n", url)
40 b, err := bencode.Marshal(mi.Info)
41 require.NoError(t, err)
42 assert.EqualValues(t, b, mi.Info.Bytes)
45 func TestFile(t *testing.T) {
46 testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
47 testFile(t, "testdata/continuum.torrent")
48 testFile(t, "testdata/23516C72685E8DB0C8F15553382A927F185C4F01.torrent")
49 testFile(t, "testdata/trackerless.torrent")
52 // Ensure that the correct number of pieces are generated when hashing files.
53 func TestNumPieces(t *testing.T) {
54 for _, _case := range []struct {
59 {256 * 1024, []FileInfo{{Length: 1024*1024 + -1}}, 4},
60 {256 * 1024, []FileInfo{{Length: 1024 * 1024}}, 4},
61 {256 * 1024, []FileInfo{{Length: 1024*1024 + 1}}, 5},
62 {5, []FileInfo{{Length: 1}, {Length: 12}}, 3},
63 {5, []FileInfo{{Length: 4}, {Length: 12}}, 4},
67 PieceLength: _case.PieceLength,
69 err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
70 return ioutil.NopCloser(missinggo.ZeroReader), nil
72 assert.NoError(t, err)
73 assert.EqualValues(t, _case.NumPieces, info.NumPieces())