2014-06-28 17:40:39 +08:00
|
|
|
package metainfo
|
2012-06-28 04:26:56 +08:00
|
|
|
|
2014-12-02 13:27:17 +08:00
|
|
|
import (
|
2015-10-29 22:21:09 +08:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-12-02 13:27:17 +08:00
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2015-10-29 22:21:09 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-04 13:39:26 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-10-29 22:21:09 +08:00
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2014-12-02 13:27:17 +08:00
|
|
|
)
|
2012-06-28 04:26:56 +08:00
|
|
|
|
2016-02-23 18:50:07 +08:00
|
|
|
func testFile(t *testing.T, filename string) {
|
2012-07-02 03:05:10 +08:00
|
|
|
mi, err := LoadFromFile(filename)
|
2012-06-28 04:26:56 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
if len(mi.Info.Files) == 1 {
|
|
|
|
t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
|
2012-07-11 04:30:05 +08:00
|
|
|
} else {
|
2014-07-13 15:36:06 +08:00
|
|
|
t.Logf("Multiple files: %s\n", mi.Info.Name)
|
|
|
|
for _, f := range mi.Info.Files {
|
2012-06-28 04:26:56 +08:00
|
|
|
t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
for _, group := range mi.AnnounceList {
|
2012-06-28 04:26:56 +08:00
|
|
|
for _, tracker := range group {
|
|
|
|
t.Logf("Tracker: %s\n", tracker)
|
|
|
|
}
|
|
|
|
}
|
2014-07-13 15:36:06 +08:00
|
|
|
// for _, url := range mi.WebSeedURLs {
|
|
|
|
// t.Logf("URL: %s\n", url)
|
|
|
|
// }
|
2012-06-28 04:26:56 +08:00
|
|
|
|
2014-12-02 13:27:17 +08:00
|
|
|
b, err := bencode.Marshal(mi.Info)
|
2016-04-04 13:39:26 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, b, mi.Info.Bytes)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile(t *testing.T) {
|
2016-02-23 18:50:07 +08:00
|
|
|
testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
|
|
|
|
testFile(t, "testdata/continuum.torrent")
|
|
|
|
testFile(t, "testdata/23516C72685E8DB0C8F15553382A927F185C4F01.torrent")
|
|
|
|
testFile(t, "testdata/trackerless.torrent")
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
2015-10-29 22:21:09 +08:00
|
|
|
|
|
|
|
// Ensure that the correct number of pieces are generated when hashing files.
|
|
|
|
func TestNumPieces(t *testing.T) {
|
|
|
|
for _, _case := range []struct {
|
|
|
|
PieceLength int64
|
|
|
|
Files []FileInfo
|
|
|
|
NumPieces int
|
|
|
|
}{
|
|
|
|
{256 * 1024, []FileInfo{{Length: 1024*1024 + -1}}, 4},
|
|
|
|
{256 * 1024, []FileInfo{{Length: 1024 * 1024}}, 4},
|
|
|
|
{256 * 1024, []FileInfo{{Length: 1024*1024 + 1}}, 5},
|
|
|
|
{5, []FileInfo{{Length: 1}, {Length: 12}}, 3},
|
|
|
|
{5, []FileInfo{{Length: 4}, {Length: 12}}, 4},
|
|
|
|
} {
|
|
|
|
info := Info{
|
|
|
|
Files: _case.Files,
|
|
|
|
PieceLength: _case.PieceLength,
|
|
|
|
}
|
|
|
|
err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
|
2016-02-04 22:20:02 +08:00
|
|
|
return ioutil.NopCloser(missinggo.ZeroReader), nil
|
2015-10-29 22:21:09 +08:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, _case.NumPieces, info.NumPieces())
|
|
|
|
}
|
|
|
|
}
|