2014-06-28 09:40:39 +00:00
|
|
|
package metainfo
|
2012-06-27 20:26:56 +00:00
|
|
|
|
2014-12-02 05:27:17 +00:00
|
|
|
import (
|
2015-10-29 14:21:09 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-07-07 09:40:26 +00:00
|
|
|
"os"
|
2014-12-02 05:27:17 +00:00
|
|
|
"path"
|
2016-07-07 09:40:26 +00:00
|
|
|
"path/filepath"
|
2014-12-02 05:27:17 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-10-29 14:21:09 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-04 05:39:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-10-29 14:21:09 +00:00
|
|
|
|
2015-04-27 04:55:01 +00:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2014-12-02 05:27:17 +00:00
|
|
|
)
|
2012-06-27 20:26:56 +00:00
|
|
|
|
2016-02-23 10:50:07 +00:00
|
|
|
func testFile(t *testing.T, filename string) {
|
2012-07-01 19:05:10 +00:00
|
|
|
mi, err := LoadFromFile(filename)
|
2016-05-05 12:40:38 +00:00
|
|
|
require.NoError(t, err)
|
2016-09-20 02:25:40 +00:00
|
|
|
info, err := mi.UnmarshalInfo()
|
|
|
|
require.NoError(t, err)
|
2012-06-27 20:26:56 +00:00
|
|
|
|
2016-08-26 10:29:05 +00:00
|
|
|
if len(info.Files) == 1 {
|
|
|
|
t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
|
2012-07-10 20:30:05 +00:00
|
|
|
} else {
|
2016-08-26 10:29:05 +00:00
|
|
|
t.Logf("Multiple files: %s\n", info.Name)
|
|
|
|
for _, f := range info.Files {
|
2012-06-27 20:26:56 +00:00
|
|
|
t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-01 19:05:10 +00:00
|
|
|
for _, group := range mi.AnnounceList {
|
2012-06-27 20:26:56 +00:00
|
|
|
for _, tracker := range group {
|
|
|
|
t.Logf("Tracker: %s\n", tracker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 10:29:05 +00:00
|
|
|
b, err := bencode.Marshal(&info)
|
2016-04-04 05:39:26 +00:00
|
|
|
require.NoError(t, err)
|
2016-08-26 10:29:05 +00:00
|
|
|
assert.EqualValues(t, string(b), string(mi.InfoBytes))
|
2012-06-27 20:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile(t *testing.T) {
|
2016-02-23 10:50:07 +00: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-27 20:26:56 +00:00
|
|
|
}
|
2015-10-29 14:21:09 +00: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 14:20:02 +00:00
|
|
|
return ioutil.NopCloser(missinggo.ZeroReader), nil
|
2015-10-29 14:21:09 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, _case.NumPieces, info.NumPieces())
|
|
|
|
}
|
|
|
|
}
|
2016-07-07 09:40:26 +00:00
|
|
|
|
|
|
|
func touchFile(path string) (err error) {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildFromFilePathOrder(t *testing.T) {
|
|
|
|
td, err := ioutil.TempDir("", "anacrolix")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
require.NoError(t, touchFile(filepath.Join(td, "b")))
|
|
|
|
require.NoError(t, touchFile(filepath.Join(td, "a")))
|
|
|
|
info := Info{
|
|
|
|
PieceLength: 1,
|
|
|
|
}
|
|
|
|
require.NoError(t, info.BuildFromFilePath(td))
|
|
|
|
assert.EqualValues(t, []FileInfo{{
|
|
|
|
Path: []string{"a"},
|
|
|
|
}, {
|
|
|
|
Path: []string{"b"},
|
|
|
|
}}, info.Files)
|
|
|
|
}
|
2016-08-26 10:29:05 +00:00
|
|
|
|
|
|
|
func testUnmarshal(t *testing.T, input string, expected *MetaInfo) {
|
|
|
|
var actual MetaInfo
|
|
|
|
err := bencode.Unmarshal([]byte(input), &actual)
|
|
|
|
if expected == nil {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, *expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
testUnmarshal(t, `de`, &MetaInfo{})
|
|
|
|
testUnmarshal(t, `d4:infoe`, &MetaInfo{})
|
|
|
|
testUnmarshal(t, `d4:infoabce`, nil)
|
|
|
|
testUnmarshal(t, `d4:infodee`, &MetaInfo{InfoBytes: []byte("de")})
|
|
|
|
}
|