2014-03-17 14:44:22 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2015-02-26 14:46:02 +00:00
|
|
|
"encoding/binary"
|
2014-12-04 01:57:43 +00:00
|
|
|
"fmt"
|
2015-06-02 14:16:38 +00:00
|
|
|
"io"
|
2015-02-26 14:46:02 +00:00
|
|
|
"io/ioutil"
|
2014-12-04 01:57:43 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2014-03-20 05:58:09 +00:00
|
|
|
"os"
|
2015-06-02 14:16:38 +00:00
|
|
|
"path/filepath"
|
2015-08-12 06:51:12 +00:00
|
|
|
"sync"
|
2014-03-17 14:44:22 +00:00
|
|
|
"testing"
|
2014-11-18 00:04:09 +00:00
|
|
|
"time"
|
2014-08-21 08:24:19 +00:00
|
|
|
|
2015-05-20 12:20:11 +00:00
|
|
|
_ "github.com/anacrolix/envpprof"
|
2015-08-03 14:29:01 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
2015-09-17 02:53:52 +00:00
|
|
|
. "github.com/anacrolix/missinggo"
|
2015-10-03 14:22:46 +00:00
|
|
|
"github.com/anacrolix/missinggo/filecache"
|
2015-03-26 06:18:08 +00:00
|
|
|
"github.com/anacrolix/utp"
|
2015-02-26 14:46:02 +00:00
|
|
|
"github.com/bradfitz/iter"
|
2015-06-16 07:14:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-07-15 05:51:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-02-26 14:46:02 +00:00
|
|
|
|
2015-04-29 14:31:34 +00:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-10-03 14:22:46 +00:00
|
|
|
"github.com/anacrolix/torrent/data/pieceStore"
|
|
|
|
"github.com/anacrolix/torrent/data/pieceStore/dataBackend/fileCache"
|
2015-08-03 15:07:22 +00:00
|
|
|
"github.com/anacrolix/torrent/dht"
|
2015-03-20 05:37:44 +00:00
|
|
|
"github.com/anacrolix/torrent/internal/testutil"
|
2015-08-03 15:07:22 +00:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
2015-06-02 14:16:38 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2014-03-17 14:44:22 +00:00
|
|
|
)
|
|
|
|
|
2015-03-18 07:32:31 +00:00
|
|
|
func init() {
|
2015-10-03 14:22:46 +00:00
|
|
|
log.SetFlags(log.LstdFlags | log.Llongfile)
|
2015-03-18 07:32:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 14:46:02 +00:00
|
|
|
var TestingConfig = Config{
|
2015-03-18 07:32:31 +00:00
|
|
|
ListenAddr: "localhost:0",
|
2015-02-26 14:46:02 +00:00
|
|
|
NoDHT: true,
|
|
|
|
DisableTrackers: true,
|
|
|
|
NoDefaultBlocklist: true,
|
|
|
|
DisableMetainfoCache: true,
|
2015-06-02 14:16:38 +00:00
|
|
|
DataDir: filepath.Join(os.TempDir(), "anacrolix"),
|
2016-01-16 13:12:53 +00:00
|
|
|
DHTConfig: dht.ServerConfig{
|
|
|
|
NoDefaultBootstrap: true,
|
|
|
|
},
|
2015-02-26 14:46:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 08:07:06 +00:00
|
|
|
func TestClientDefault(t *testing.T) {
|
2015-03-18 07:32:31 +00:00
|
|
|
cl, err := NewClient(&TestingConfig)
|
2014-08-21 08:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-08 06:28:14 +00:00
|
|
|
cl.Close()
|
2014-08-21 08:07:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 03:54:59 +00:00
|
|
|
func TestAddDropTorrent(t *testing.T) {
|
2015-03-10 17:22:56 +00:00
|
|
|
cl, err := NewClient(&TestingConfig)
|
2015-02-06 03:54:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-08 06:28:14 +00:00
|
|
|
defer cl.Close()
|
2015-02-06 03:54:59 +00:00
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(dir)
|
2015-03-18 07:32:31 +00:00
|
|
|
tt, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
2015-02-06 03:54:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-18 07:32:31 +00:00
|
|
|
if !new {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2015-02-06 03:54:59 +00:00
|
|
|
tt.Drop()
|
|
|
|
}
|
|
|
|
|
2014-03-17 14:44:22 +00:00
|
|
|
func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddTorrentNoUsableURLs(t *testing.T) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddPeersToUnknownTorrent(t *testing.T) {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2014-03-20 05:58:09 +00:00
|
|
|
|
|
|
|
func TestPieceHashSize(t *testing.T) {
|
2014-04-08 16:36:05 +00:00
|
|
|
if pieceHash.Size() != 20 {
|
2014-03-20 05:58:09 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTorrentInitialState(t *testing.T) {
|
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(dir)
|
2014-08-21 08:24:19 +00:00
|
|
|
tor, err := newTorrent(func() (ih InfoHash) {
|
2015-08-03 14:29:01 +00:00
|
|
|
missinggo.CopyExact(ih[:], mi.Info.Hash)
|
2014-08-21 08:24:19 +00:00
|
|
|
return
|
2015-03-18 07:32:31 +00:00
|
|
|
}())
|
2014-06-28 09:38:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-15 05:31:18 +00:00
|
|
|
tor.chunkSize = 2
|
2015-11-05 13:40:16 +00:00
|
|
|
err = tor.setMetadata(&mi.Info.Info, mi.Info.Bytes)
|
2014-03-20 05:58:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-26 14:46:02 +00:00
|
|
|
if len(tor.Pieces) != 3 {
|
2014-03-20 05:58:09 +00:00
|
|
|
t.Fatal("wrong number of pieces")
|
|
|
|
}
|
2014-12-09 03:59:01 +00:00
|
|
|
tor.pendAllChunkSpecs(0)
|
2016-01-13 06:11:59 +00:00
|
|
|
assert.EqualValues(t, 3, tor.pieceNumPendingChunks(0))
|
2015-07-15 05:31:18 +00:00
|
|
|
assert.EqualValues(t, chunkSpec{4, 1}, chunkIndexSpec(2, tor.pieceLength(0), tor.chunkSize))
|
2014-03-20 05:58:09 +00:00
|
|
|
}
|
2014-06-29 09:07:43 +00:00
|
|
|
|
|
|
|
func TestUnmarshalPEXMsg(t *testing.T) {
|
|
|
|
var m peerExchangeMessage
|
|
|
|
if err := bencode.Unmarshal([]byte("d5:added12:\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0ce"), &m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(m.Added) != 2 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if m.Added[0].Port != 0x506 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 00:04:09 +00:00
|
|
|
|
|
|
|
func TestReducedDialTimeout(t *testing.T) {
|
|
|
|
for _, _case := range []struct {
|
|
|
|
Max time.Duration
|
|
|
|
HalfOpenLimit int
|
|
|
|
PendingPeers int
|
|
|
|
ExpectedReduced time.Duration
|
|
|
|
}{
|
2014-11-19 03:53:00 +00:00
|
|
|
{nominalDialTimeout, 40, 0, nominalDialTimeout},
|
|
|
|
{nominalDialTimeout, 40, 1, nominalDialTimeout},
|
|
|
|
{nominalDialTimeout, 40, 39, nominalDialTimeout},
|
|
|
|
{nominalDialTimeout, 40, 40, nominalDialTimeout / 2},
|
|
|
|
{nominalDialTimeout, 40, 80, nominalDialTimeout / 3},
|
|
|
|
{nominalDialTimeout, 40, 4000, nominalDialTimeout / 101},
|
2014-11-18 00:04:09 +00:00
|
|
|
} {
|
|
|
|
reduced := reducedDialTimeout(_case.Max, _case.HalfOpenLimit, _case.PendingPeers)
|
2014-11-19 03:53:00 +00:00
|
|
|
expected := _case.ExpectedReduced
|
|
|
|
if expected < minDialTimeout {
|
|
|
|
expected = minDialTimeout
|
|
|
|
}
|
|
|
|
if reduced != expected {
|
2014-11-18 00:04:09 +00:00
|
|
|
t.Fatalf("expected %s, got %s", _case.ExpectedReduced, reduced)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 01:57:43 +00:00
|
|
|
|
|
|
|
func TestUTPRawConn(t *testing.T) {
|
2015-08-04 16:51:40 +00:00
|
|
|
l, err := utp.NewSocket("udp", "")
|
2014-12-04 01:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
_, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Connect a UTP peer to see if the RawConn will still work.
|
2015-08-04 16:51:40 +00:00
|
|
|
s, _ := utp.NewSocket("udp", "")
|
2015-08-03 14:48:17 +00:00
|
|
|
defer s.Close()
|
|
|
|
utpPeer, err := s.Dial(fmt.Sprintf("localhost:%d", missinggo.AddrPort(l.Addr())))
|
2014-12-04 01:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error dialing utp listener: %s", err)
|
|
|
|
}
|
|
|
|
defer utpPeer.Close()
|
|
|
|
peer, err := net.ListenPacket("udp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer peer.Close()
|
|
|
|
|
|
|
|
msgsReceived := 0
|
2015-05-24 11:37:14 +00:00
|
|
|
// How many messages to send. I've set this to double the channel buffer
|
|
|
|
// size in the raw packetConn.
|
|
|
|
const N = 200
|
2014-12-04 01:57:43 +00:00
|
|
|
readerStopped := make(chan struct{})
|
|
|
|
// The reader goroutine.
|
|
|
|
go func() {
|
|
|
|
defer close(readerStopped)
|
|
|
|
b := make([]byte, 500)
|
|
|
|
for i := 0; i < N; i++ {
|
2015-10-03 14:02:14 +00:00
|
|
|
n, _, err := l.ReadFrom(b)
|
2014-12-04 01:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading from raw conn: %s", err)
|
|
|
|
}
|
|
|
|
msgsReceived++
|
|
|
|
var d int
|
|
|
|
fmt.Sscan(string(b[:n]), &d)
|
|
|
|
if d != i {
|
|
|
|
log.Printf("got wrong number: expected %d, got %d", i, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-08-03 14:29:01 +00:00
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", missinggo.AddrPort(l.Addr())))
|
2014-12-26 06:19:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-04 01:57:43 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2014-12-26 06:19:01 +00:00
|
|
|
_, err := peer.WriteTo([]byte(fmt.Sprintf("%d", i)), udpAddr)
|
2014-12-04 01:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Microsecond)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-readerStopped:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("reader timed out")
|
|
|
|
}
|
|
|
|
if msgsReceived != N {
|
|
|
|
t.Fatalf("messages received: %d", msgsReceived)
|
|
|
|
}
|
|
|
|
}
|
2015-01-11 10:42:57 +00:00
|
|
|
|
|
|
|
func TestTwoClientsArbitraryPorts(t *testing.T) {
|
|
|
|
for i := 0; i < 2; i++ {
|
2015-03-18 07:32:31 +00:00
|
|
|
cl, err := NewClient(&TestingConfig)
|
2015-01-11 10:42:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-08 06:28:14 +00:00
|
|
|
defer cl.Close()
|
2015-01-11 10:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-26 14:46:02 +00:00
|
|
|
|
|
|
|
func TestAddDropManyTorrents(t *testing.T) {
|
|
|
|
cl, _ := NewClient(&TestingConfig)
|
2015-03-08 06:28:14 +00:00
|
|
|
defer cl.Close()
|
2015-02-26 14:46:02 +00:00
|
|
|
for i := range iter.N(1000) {
|
2015-03-18 07:32:31 +00:00
|
|
|
var spec TorrentSpec
|
|
|
|
binary.PutVarint(spec.InfoHash[:], int64(i))
|
|
|
|
tt, new, err := cl.AddTorrentSpec(&spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !new {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer tt.Drop()
|
2015-02-26 14:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientTransfer(t *testing.T) {
|
|
|
|
greetingTempDir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(greetingTempDir)
|
|
|
|
cfg := TestingConfig
|
2015-06-16 06:57:47 +00:00
|
|
|
cfg.Seed = true
|
2015-02-26 14:46:02 +00:00
|
|
|
cfg.DataDir = greetingTempDir
|
|
|
|
seeder, err := NewClient(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-08 06:28:14 +00:00
|
|
|
defer seeder.Close()
|
2015-03-18 07:32:31 +00:00
|
|
|
seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
2015-02-26 14:46:02 +00:00
|
|
|
leecherDataDir, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(leecherDataDir)
|
2015-03-07 06:11:45 +00:00
|
|
|
// cfg.TorrentDataOpener = func(info *metainfo.Info) (data.Data, error) {
|
|
|
|
// return blob.TorrentData(info, leecherDataDir), nil
|
|
|
|
// }
|
2015-10-03 14:22:46 +00:00
|
|
|
// blobStore := blob.NewStore(leecherDataDir)
|
|
|
|
// cfg.TorrentDataOpener = func(info *metainfo.Info) Data {
|
|
|
|
// return blobStore.OpenTorrent(info)
|
|
|
|
// }
|
|
|
|
cfg.TorrentDataOpener = func() TorrentDataOpener {
|
|
|
|
fc, err := filecache.NewCache(leecherDataDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
store := pieceStore.New(fileCacheDataBackend.New(fc))
|
|
|
|
return func(mi *metainfo.Info) Data {
|
|
|
|
return store.OpenTorrentData(mi)
|
|
|
|
}
|
|
|
|
}()
|
2015-02-26 14:46:02 +00:00
|
|
|
leecher, _ := NewClient(&cfg)
|
2015-03-08 06:28:14 +00:00
|
|
|
defer leecher.Close()
|
2015-07-15 05:31:18 +00:00
|
|
|
leecherGreeting, _, _ := leecher.AddTorrentSpec(func() (ret *TorrentSpec) {
|
|
|
|
ret = TorrentSpecFromMetaInfo(mi)
|
|
|
|
ret.ChunkSize = 2
|
|
|
|
return
|
|
|
|
}())
|
2015-09-06 02:33:22 +00:00
|
|
|
// TODO: The piece state publishing is kinda jammed in here until I have a
|
|
|
|
// more thorough test.
|
|
|
|
go func() {
|
2016-01-16 13:14:15 +00:00
|
|
|
s := leecherGreeting.torrent.pieceStateChanges.Subscribe()
|
2015-09-06 02:33:22 +00:00
|
|
|
defer s.Close()
|
|
|
|
for i := range s.Values {
|
|
|
|
log.Print(i)
|
|
|
|
}
|
|
|
|
log.Print("finished")
|
|
|
|
}()
|
2015-02-26 14:46:02 +00:00
|
|
|
leecherGreeting.AddPeers([]Peer{
|
|
|
|
Peer{
|
2015-08-03 14:29:01 +00:00
|
|
|
IP: missinggo.AddrIP(seeder.ListenAddr()),
|
|
|
|
Port: missinggo.AddrPort(seeder.ListenAddr()),
|
2015-02-26 14:46:02 +00:00
|
|
|
},
|
|
|
|
})
|
2015-04-14 13:59:41 +00:00
|
|
|
r := leecherGreeting.NewReader()
|
|
|
|
defer r.Close()
|
|
|
|
_greeting, err := ioutil.ReadAll(r)
|
2015-02-26 14:46:02 +00:00
|
|
|
if err != nil {
|
2015-04-14 13:59:41 +00:00
|
|
|
t.Fatalf("%q %s", string(_greeting), err)
|
2015-02-26 14:46:02 +00:00
|
|
|
}
|
|
|
|
greeting := string(_greeting)
|
|
|
|
if greeting != testutil.GreetingFileContents {
|
|
|
|
t.Fatal(":(")
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 02:07:11 +00:00
|
|
|
|
2015-08-12 06:51:12 +00:00
|
|
|
// Check that after completing leeching, a leecher transitions to a seeding
|
|
|
|
// correctly. Connected in a chain like so: Seeder <-> Leecher <-> LeecherLeecher.
|
|
|
|
func TestSeedAfterDownloading(t *testing.T) {
|
|
|
|
greetingTempDir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(greetingTempDir)
|
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.Seed = true
|
|
|
|
cfg.DataDir = greetingTempDir
|
|
|
|
seeder, err := NewClient(&cfg)
|
|
|
|
defer seeder.Close()
|
|
|
|
seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
|
|
|
cfg.DataDir, err = ioutil.TempDir("", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(cfg.DataDir)
|
|
|
|
leecher, _ := NewClient(&cfg)
|
|
|
|
defer leecher.Close()
|
|
|
|
cfg.Seed = false
|
|
|
|
cfg.TorrentDataOpener = nil
|
|
|
|
cfg.DataDir, err = ioutil.TempDir("", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(cfg.DataDir)
|
|
|
|
leecherLeecher, _ := NewClient(&cfg)
|
|
|
|
defer leecherLeecher.Close()
|
|
|
|
leecherGreeting, _, _ := leecher.AddTorrentSpec(func() (ret *TorrentSpec) {
|
|
|
|
ret = TorrentSpecFromMetaInfo(mi)
|
|
|
|
ret.ChunkSize = 2
|
|
|
|
return
|
|
|
|
}())
|
|
|
|
llg, _, _ := leecherLeecher.AddTorrentSpec(func() (ret *TorrentSpec) {
|
|
|
|
ret = TorrentSpecFromMetaInfo(mi)
|
|
|
|
ret.ChunkSize = 3
|
|
|
|
return
|
|
|
|
}())
|
|
|
|
// Simultaneously DownloadAll in Leecher, and read the contents
|
|
|
|
// consecutively in LeecherLeecher. This non-deterministically triggered a
|
|
|
|
// case where the leecher wouldn't unchoke the LeecherLeecher.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
r := llg.NewReader()
|
|
|
|
defer r.Close()
|
|
|
|
b, err := ioutil.ReadAll(r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, testutil.GreetingFileContents, b)
|
|
|
|
}()
|
|
|
|
leecherGreeting.AddPeers([]Peer{
|
|
|
|
Peer{
|
|
|
|
IP: missinggo.AddrIP(seeder.ListenAddr()),
|
|
|
|
Port: missinggo.AddrPort(seeder.ListenAddr()),
|
|
|
|
},
|
|
|
|
Peer{
|
|
|
|
IP: missinggo.AddrIP(leecherLeecher.ListenAddr()),
|
|
|
|
Port: missinggo.AddrPort(leecherLeecher.ListenAddr()),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
leecherGreeting.DownloadAll()
|
|
|
|
leecher.WaitAll()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:07:11 +00:00
|
|
|
func TestReadaheadPieces(t *testing.T) {
|
|
|
|
for _, case_ := range []struct {
|
|
|
|
readaheadBytes, pieceLength int64
|
|
|
|
readaheadPieces int
|
|
|
|
}{
|
|
|
|
{5 * 1024 * 1024, 256 * 1024, 19},
|
2015-06-16 07:14:15 +00:00
|
|
|
{5 * 1024 * 1024, 5 * 1024 * 1024, 1},
|
|
|
|
{5*1024*1024 - 1, 5 * 1024 * 1024, 1},
|
|
|
|
{5 * 1024 * 1024, 5*1024*1024 - 1, 2},
|
|
|
|
{0, 5 * 1024 * 1024, 0},
|
2015-03-04 02:07:11 +00:00
|
|
|
{5 * 1024 * 1024, 1048576, 4},
|
|
|
|
} {
|
2015-06-16 07:14:15 +00:00
|
|
|
pieces := readaheadPieces(case_.readaheadBytes, case_.pieceLength)
|
|
|
|
assert.Equal(t, case_.readaheadPieces, pieces, "%v", case_)
|
2015-03-04 02:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 15:50:55 +00:00
|
|
|
|
2015-08-03 06:23:05 +00:00
|
|
|
func TestMergingTrackersByAddingSpecs(t *testing.T) {
|
2015-03-27 15:50:55 +00:00
|
|
|
cl, _ := NewClient(&TestingConfig)
|
|
|
|
defer cl.Close()
|
|
|
|
spec := TorrentSpec{}
|
|
|
|
T, new, _ := cl.AddTorrentSpec(&spec)
|
|
|
|
if !new {
|
2015-08-03 06:23:05 +00:00
|
|
|
t.FailNow()
|
2015-03-27 15:50:55 +00:00
|
|
|
}
|
|
|
|
spec.Trackers = [][]string{{"http://a"}, {"udp://b"}}
|
|
|
|
_, new, _ = cl.AddTorrentSpec(&spec)
|
|
|
|
if new {
|
2015-08-03 06:23:05 +00:00
|
|
|
t.FailNow()
|
2015-03-27 15:50:55 +00:00
|
|
|
}
|
2016-01-16 13:14:15 +00:00
|
|
|
assert.EqualValues(t, T.torrent.Trackers[0][0].URL(), "http://a")
|
|
|
|
assert.EqualValues(t, T.torrent.Trackers[1][0].URL(), "udp://b")
|
2015-03-27 15:50:55 +00:00
|
|
|
}
|
2015-06-02 14:16:38 +00:00
|
|
|
|
2015-10-01 14:09:04 +00:00
|
|
|
type badData struct{}
|
|
|
|
|
|
|
|
func (me badData) Close() {}
|
2015-06-02 14:16:38 +00:00
|
|
|
|
|
|
|
func (me badData) WriteAt(b []byte, off int64) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me badData) WriteSectionTo(w io.Writer, off, n int64) (int64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me badData) PieceComplete(piece int) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me badData) PieceCompleted(piece int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me badData) ReadAt(b []byte, off int64) (n int, err error) {
|
|
|
|
if off >= 5 {
|
|
|
|
err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n = copy(b, []byte("hello")[off:])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We read from a piece which is marked completed, but is missing data.
|
|
|
|
func TestCompletedPieceWrongSize(t *testing.T) {
|
|
|
|
cfg := TestingConfig
|
2015-10-01 14:09:04 +00:00
|
|
|
cfg.TorrentDataOpener = func(*metainfo.Info) Data {
|
2015-06-02 14:16:38 +00:00
|
|
|
return badData{}
|
|
|
|
}
|
|
|
|
cl, _ := NewClient(&cfg)
|
|
|
|
defer cl.Close()
|
|
|
|
tt, new, err := cl.AddTorrentSpec(&TorrentSpec{
|
|
|
|
Info: &metainfo.InfoEx{
|
|
|
|
Info: metainfo.Info{
|
|
|
|
PieceLength: 15,
|
|
|
|
Pieces: make([]byte, 20),
|
|
|
|
Files: []metainfo.FileInfo{
|
|
|
|
metainfo.FileInfo{Path: []string{"greeting"}, Length: 13},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !new {
|
|
|
|
t.Fatal("expected new")
|
|
|
|
}
|
|
|
|
r := tt.NewReader()
|
|
|
|
defer r.Close()
|
|
|
|
b := make([]byte, 20)
|
|
|
|
n, err := io.ReadFull(r, b)
|
|
|
|
if n != 5 || err != io.ErrUnexpectedEOF {
|
|
|
|
t.Fatal(n, err)
|
|
|
|
}
|
|
|
|
defer tt.Drop()
|
|
|
|
}
|
2015-06-22 09:43:22 +00:00
|
|
|
|
|
|
|
func BenchmarkAddLargeTorrent(b *testing.B) {
|
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.DisableTCP = true
|
|
|
|
cfg.DisableUTP = true
|
|
|
|
cfg.ListenAddr = "redonk"
|
|
|
|
cl, _ := NewClient(&cfg)
|
|
|
|
defer cl.Close()
|
|
|
|
for range iter.N(b.N) {
|
|
|
|
t, err := cl.AddTorrentFromFile("testdata/bootstrap.dat.torrent")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Drop()
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 05:51:42 +00:00
|
|
|
|
|
|
|
func TestResponsive(t *testing.T) {
|
|
|
|
seederDataDir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(seederDataDir)
|
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.Seed = true
|
|
|
|
cfg.DataDir = seederDataDir
|
|
|
|
seeder, err := NewClient(&cfg)
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer seeder.Close()
|
|
|
|
seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
|
|
|
leecherDataDir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.RemoveAll(leecherDataDir)
|
|
|
|
cfg = TestingConfig
|
|
|
|
cfg.DataDir = leecherDataDir
|
|
|
|
leecher, err := NewClient(&cfg)
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer leecher.Close()
|
|
|
|
leecherTorrent, _, _ := leecher.AddTorrentSpec(func() (ret *TorrentSpec) {
|
|
|
|
ret = TorrentSpecFromMetaInfo(mi)
|
|
|
|
ret.ChunkSize = 2
|
|
|
|
return
|
|
|
|
}())
|
|
|
|
leecherTorrent.AddPeers([]Peer{
|
|
|
|
Peer{
|
2015-08-03 14:29:01 +00:00
|
|
|
IP: missinggo.AddrIP(seeder.ListenAddr()),
|
|
|
|
Port: missinggo.AddrPort(seeder.ListenAddr()),
|
2015-07-15 05:51:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
reader := leecherTorrent.NewReader()
|
|
|
|
reader.SetReadahead(0)
|
|
|
|
reader.SetResponsive()
|
|
|
|
b := make([]byte, 2)
|
2016-01-18 07:35:14 +00:00
|
|
|
_, err = reader.Seek(3, os.SEEK_SET)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = io.ReadFull(reader, b)
|
2015-07-15 05:51:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, "lo", string(b))
|
2016-01-18 07:35:14 +00:00
|
|
|
_, err = reader.Seek(11, os.SEEK_SET)
|
|
|
|
require.NoError(t, err)
|
|
|
|
n, err := io.ReadFull(reader, b)
|
2015-07-15 05:51:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, 2, n)
|
|
|
|
assert.EqualValues(t, "d\n", string(b))
|
|
|
|
}
|
2015-08-03 15:07:22 +00:00
|
|
|
|
2015-11-05 13:40:47 +00:00
|
|
|
func TestTorrentDroppedDuringResponsiveRead(t *testing.T) {
|
|
|
|
seederDataDir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(seederDataDir)
|
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.Seed = true
|
|
|
|
cfg.DataDir = seederDataDir
|
|
|
|
seeder, err := NewClient(&cfg)
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer seeder.Close()
|
|
|
|
seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
|
|
|
leecherDataDir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.RemoveAll(leecherDataDir)
|
|
|
|
cfg = TestingConfig
|
|
|
|
cfg.DataDir = leecherDataDir
|
|
|
|
leecher, err := NewClient(&cfg)
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer leecher.Close()
|
|
|
|
leecherTorrent, _, _ := leecher.AddTorrentSpec(func() (ret *TorrentSpec) {
|
|
|
|
ret = TorrentSpecFromMetaInfo(mi)
|
|
|
|
ret.ChunkSize = 2
|
|
|
|
return
|
|
|
|
}())
|
|
|
|
leecherTorrent.AddPeers([]Peer{
|
|
|
|
Peer{
|
|
|
|
IP: missinggo.AddrIP(seeder.ListenAddr()),
|
|
|
|
Port: missinggo.AddrPort(seeder.ListenAddr()),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
reader := leecherTorrent.NewReader()
|
|
|
|
reader.SetReadahead(0)
|
|
|
|
reader.SetResponsive()
|
|
|
|
b := make([]byte, 2)
|
2016-01-18 07:35:14 +00:00
|
|
|
_, err = reader.Seek(3, os.SEEK_SET)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = io.ReadFull(reader, b)
|
2015-11-05 13:40:47 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.EqualValues(t, "lo", string(b))
|
|
|
|
go leecherTorrent.Drop()
|
2016-01-18 07:35:14 +00:00
|
|
|
_, err = reader.Seek(11, os.SEEK_SET)
|
|
|
|
require.NoError(t, err)
|
|
|
|
n, err := reader.Read(b)
|
2015-11-05 13:40:47 +00:00
|
|
|
assert.EqualError(t, err, "torrent closed")
|
|
|
|
assert.EqualValues(t, 0, n)
|
|
|
|
}
|
|
|
|
|
2015-08-03 15:07:22 +00:00
|
|
|
func TestDHTInheritBlocklist(t *testing.T) {
|
|
|
|
ipl := iplist.New(nil)
|
|
|
|
require.NotNil(t, ipl)
|
2016-01-16 13:12:53 +00:00
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.IPBlocklist = ipl
|
|
|
|
cfg.NoDHT = false
|
|
|
|
cl, err := NewClient(&cfg)
|
2015-08-03 15:07:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer cl.Close()
|
|
|
|
require.Equal(t, ipl, cl.DHT().IPBlocklist())
|
|
|
|
}
|
2015-08-23 02:50:32 +00:00
|
|
|
|
|
|
|
// Check that stuff is merged in subsequent AddTorrentSpec for the same
|
|
|
|
// infohash.
|
|
|
|
func TestAddTorrentSpecMerging(t *testing.T) {
|
|
|
|
cl, err := NewClient(&TestingConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cl.Close()
|
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
var ts TorrentSpec
|
|
|
|
missinggo.CopyExact(&ts.InfoHash, mi.Info.Hash)
|
|
|
|
tt, new, err := cl.AddTorrentSpec(&ts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, new)
|
|
|
|
require.Nil(t, tt.Info())
|
|
|
|
_, new, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, new)
|
|
|
|
require.NotNil(t, tt.Info())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that torrent Info is obtained from the metainfo file cache.
|
|
|
|
func TestAddTorrentMetainfoInCache(t *testing.T) {
|
|
|
|
cfg := TestingConfig
|
|
|
|
cfg.DisableMetainfoCache = false
|
|
|
|
cfg.ConfigDir, _ = ioutil.TempDir(os.TempDir(), "")
|
|
|
|
defer os.RemoveAll(cfg.ConfigDir)
|
|
|
|
cl, err := NewClient(&cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cl.Close()
|
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
tt, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, new)
|
|
|
|
require.NotNil(t, tt.Info())
|
|
|
|
_, err = os.Stat(filepath.Join(cfg.ConfigDir, "torrents", fmt.Sprintf("%x.torrent", mi.Info.Hash)))
|
|
|
|
require.NoError(t, err)
|
|
|
|
// Contains only the infohash.
|
|
|
|
var ts TorrentSpec
|
|
|
|
missinggo.CopyExact(&ts.InfoHash, mi.Info.Hash)
|
|
|
|
_, ok := cl.Torrent(ts.InfoHash)
|
|
|
|
require.True(t, ok)
|
|
|
|
tt.Drop()
|
|
|
|
_, ok = cl.Torrent(ts.InfoHash)
|
|
|
|
require.False(t, ok)
|
|
|
|
tt, new, err = cl.AddTorrentSpec(&ts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, new)
|
|
|
|
// Obtained from the metainfo cache.
|
|
|
|
require.NotNil(t, tt.Info())
|
|
|
|
}
|
2015-09-17 02:53:52 +00:00
|
|
|
|
|
|
|
func TestTorrentDroppedBeforeGotInfo(t *testing.T) {
|
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
cl, _ := NewClient(&TestingConfig)
|
|
|
|
defer cl.Close()
|
|
|
|
var ts TorrentSpec
|
|
|
|
CopyExact(&ts.InfoHash, mi.Info.Hash)
|
|
|
|
tt, _, _ := cl.AddTorrentSpec(&ts)
|
|
|
|
tt.Drop()
|
|
|
|
assert.EqualValues(t, 0, len(cl.Torrents()))
|
|
|
|
select {
|
|
|
|
case <-tt.GotInfo():
|
|
|
|
t.FailNow()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|