2014-04-08 14:45:33 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-09-12 14:53:20 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-04-08 14:45:33 +08:00
|
|
|
"testing"
|
2014-08-28 06:03:55 +08:00
|
|
|
|
2016-09-12 14:53:20 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2016-08-30 12:21:50 +08:00
|
|
|
"github.com/bradfitz/iter"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-12 14:53:20 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-30 12:21:50 +08:00
|
|
|
|
2016-09-12 14:53:20 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2016-08-30 12:21:50 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent/peer_protocol"
|
2016-09-12 15:11:32 +08:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2014-04-08 14:45:33 +08:00
|
|
|
)
|
|
|
|
|
2014-04-16 19:13:44 +08:00
|
|
|
func r(i, b, l peer_protocol.Integer) request {
|
|
|
|
return request{i, chunkSpec{b, l}}
|
2014-04-08 14:45:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the given Request is correct for various torrent offsets.
|
|
|
|
func TestTorrentRequest(t *testing.T) {
|
|
|
|
const s = 472183431 // Length of torrent.
|
|
|
|
for _, _case := range []struct {
|
|
|
|
off int64 // An offset into the torrent.
|
2014-04-16 19:13:44 +08:00
|
|
|
req request // The expected Request. The zero value means !ok.
|
2014-04-08 14:45:33 +08:00
|
|
|
}{
|
|
|
|
// Invalid offset.
|
2014-04-16 19:13:44 +08:00
|
|
|
{-1, request{}},
|
2014-04-08 14:45:33 +08:00
|
|
|
{0, r(0, 0, 16384)},
|
|
|
|
// One before the end of a piece.
|
|
|
|
{1<<18 - 1, r(0, 1<<18-16384, 16384)},
|
|
|
|
// Offset beyond torrent length.
|
2014-04-16 19:13:44 +08:00
|
|
|
{472 * 1 << 20, request{}},
|
2014-04-08 14:45:33 +08:00
|
|
|
// One before the end of the torrent. Complicates the chunk length.
|
|
|
|
{s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
|
|
|
|
{1, r(0, 0, 16384)},
|
|
|
|
// One before end of chunk.
|
|
|
|
{16383, r(0, 0, 16384)},
|
|
|
|
// Second chunk.
|
|
|
|
{16384, r(0, 16384, 16384)},
|
|
|
|
} {
|
|
|
|
req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
|
2014-04-16 19:13:44 +08:00
|
|
|
if (_case.req == request{}) == ok {
|
2014-04-08 14:45:33 +08:00
|
|
|
t.Fatalf("expected %v, got %v", _case.req, req)
|
|
|
|
}
|
|
|
|
if req != _case.req {
|
|
|
|
t.Fatalf("expected %v, got %v", _case.req, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 06:03:55 +08:00
|
|
|
|
2014-09-11 18:31:31 +08:00
|
|
|
func TestAppendToCopySlice(t *testing.T) {
|
|
|
|
orig := []int{1, 2, 3}
|
|
|
|
dupe := append([]int{}, orig...)
|
|
|
|
dupe[0] = 4
|
|
|
|
if orig[0] != 1 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 06:34:45 +08:00
|
|
|
|
|
|
|
func TestTorrentString(t *testing.T) {
|
2016-04-03 16:40:43 +08:00
|
|
|
tor := &Torrent{}
|
|
|
|
s := tor.InfoHash().HexString()
|
2014-12-02 06:34:45 +08:00
|
|
|
if s != "0000000000000000000000000000000000000000" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 12:21:50 +08:00
|
|
|
|
|
|
|
// This benchmark is from the observation that a lot of overlapping Readers on
|
|
|
|
// a large torrent with small pieces had a lot of overhead in recalculating
|
|
|
|
// piece priorities everytime a reader (possibly in another Torrent) changed.
|
|
|
|
func BenchmarkUpdatePiecePriorities(b *testing.B) {
|
|
|
|
cl := &Client{}
|
|
|
|
t := cl.newTorrent(metainfo.Hash{})
|
|
|
|
t.info = &metainfo.Info{
|
|
|
|
Pieces: make([]byte, 20*13410),
|
|
|
|
PieceLength: 256 << 10,
|
|
|
|
}
|
|
|
|
t.makePieces()
|
|
|
|
assert.EqualValues(b, 13410, t.numPieces())
|
|
|
|
for range iter.N(7) {
|
|
|
|
r := t.NewReader()
|
|
|
|
r.SetReadahead(32 << 20)
|
|
|
|
r.Seek(3500000, 0)
|
|
|
|
}
|
|
|
|
assert.Len(b, t.readers, 7)
|
|
|
|
t.pendPieceRange(0, t.numPieces())
|
|
|
|
for i := 0; i < t.numPieces(); i += 3 {
|
|
|
|
t.completedPieces.Set(i, true)
|
|
|
|
}
|
|
|
|
for range iter.N(b.N) {
|
2016-10-31 16:00:08 +08:00
|
|
|
t.updateAllPiecePriorities()
|
2016-08-30 12:21:50 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 14:53:20 +08:00
|
|
|
|
2016-09-12 15:11:32 +08:00
|
|
|
// Check that a torrent containing zero-length file(s) will start, and that
|
|
|
|
// they're created in the filesystem. The client storage is assumed to be
|
|
|
|
// file-based on the native filesystem based.
|
|
|
|
func testEmptyFilesAndZeroPieceLength(t *testing.T, cs storage.ClientImpl) {
|
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.DefaultStorage = cs
|
2016-09-12 14:53:20 +08:00
|
|
|
cl, err := NewClient(&TestingConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cl.Close()
|
|
|
|
ib, err := bencode.Marshal(metainfo.Info{
|
2016-09-12 15:11:32 +08:00
|
|
|
Name: "empty",
|
|
|
|
Length: 0,
|
|
|
|
PieceLength: 0,
|
2016-09-12 14:53:20 +08:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
fp := filepath.Join(TestingConfig.DataDir, "empty")
|
|
|
|
os.Remove(fp)
|
|
|
|
assert.False(t, missinggo.FilePathExists(fp))
|
|
|
|
tt, err := cl.AddTorrent(&metainfo.MetaInfo{
|
|
|
|
InfoBytes: ib,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tt.Drop()
|
|
|
|
tt.DownloadAll()
|
|
|
|
require.True(t, cl.WaitAll())
|
|
|
|
assert.True(t, missinggo.FilePathExists(fp))
|
|
|
|
}
|
2016-09-12 15:11:32 +08:00
|
|
|
|
|
|
|
func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
|
|
|
|
testEmptyFilesAndZeroPieceLength(t, storage.NewFile(TestingConfig.DataDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
|
|
|
|
testEmptyFilesAndZeroPieceLength(t, storage.NewMMap(TestingConfig.DataDir))
|
|
|
|
}
|