2014-03-17 22:44:22 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-12-04 09:57:43 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2014-03-20 13:58:09 +08:00
|
|
|
"os"
|
2014-03-17 22:44:22 +08:00
|
|
|
"testing"
|
2014-11-18 08:04:09 +08:00
|
|
|
"time"
|
2014-08-21 16:24:19 +08:00
|
|
|
|
2014-12-04 09:57:43 +08:00
|
|
|
"github.com/h2so5/utp"
|
|
|
|
|
2014-08-21 16:24:19 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/testutil"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
|
|
|
"github.com/anacrolix/libtorgo/bencode"
|
2014-03-17 22:44:22 +08:00
|
|
|
)
|
|
|
|
|
2014-08-21 16:07:06 +08:00
|
|
|
func TestClientDefault(t *testing.T) {
|
2014-12-03 04:23:01 +08:00
|
|
|
cl, err := NewClient(&Config{
|
|
|
|
NoDefaultBlocklist: true,
|
|
|
|
})
|
2014-08-21 16:07:06 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cl.Stop()
|
|
|
|
}
|
|
|
|
|
2014-03-17 22:44:22 +08: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 13:58:09 +08:00
|
|
|
|
|
|
|
func TestPieceHashSize(t *testing.T) {
|
2014-04-09 00:36:05 +08:00
|
|
|
if pieceHash.Size() != 20 {
|
2014-03-20 13:58:09 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTorrentInitialState(t *testing.T) {
|
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(dir)
|
2014-08-21 16:24:19 +08:00
|
|
|
tor, err := newTorrent(func() (ih InfoHash) {
|
|
|
|
util.CopyExact(ih[:], mi.Info.Hash)
|
|
|
|
return
|
2014-11-17 03:30:44 +08:00
|
|
|
}(), nil, 0)
|
2014-06-28 17:38:31 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-03 15:07:50 +08:00
|
|
|
err = tor.setMetadata(mi.Info.Info, dir, mi.Info.Bytes, nil)
|
2014-03-20 13:58:09 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(tor.Pieces) != 1 {
|
|
|
|
t.Fatal("wrong number of pieces")
|
|
|
|
}
|
|
|
|
p := tor.Pieces[0]
|
2014-12-09 11:59:01 +08:00
|
|
|
tor.pendAllChunkSpecs(0)
|
2014-03-20 13:58:09 +08:00
|
|
|
if len(p.PendingChunkSpecs) != 1 {
|
2014-07-10 00:59:37 +08:00
|
|
|
t.Fatalf("should only be 1 chunk: %v", p.PendingChunkSpecs)
|
2014-03-20 13:58:09 +08:00
|
|
|
}
|
2014-04-09 00:36:05 +08:00
|
|
|
if _, ok := p.PendingChunkSpecs[chunkSpec{
|
2014-03-20 13:58:09 +08:00
|
|
|
Length: 13,
|
|
|
|
}]; !ok {
|
|
|
|
t.Fatal("pending chunk spec is incorrect")
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 17:07:43 +08: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 08:04:09 +08:00
|
|
|
|
|
|
|
func TestReducedDialTimeout(t *testing.T) {
|
|
|
|
for _, _case := range []struct {
|
|
|
|
Max time.Duration
|
|
|
|
HalfOpenLimit int
|
|
|
|
PendingPeers int
|
|
|
|
ExpectedReduced time.Duration
|
|
|
|
}{
|
2014-11-19 11:53:00 +08: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 08:04:09 +08:00
|
|
|
} {
|
|
|
|
reduced := reducedDialTimeout(_case.Max, _case.HalfOpenLimit, _case.PendingPeers)
|
2014-11-19 11:53:00 +08:00
|
|
|
expected := _case.ExpectedReduced
|
|
|
|
if expected < minDialTimeout {
|
|
|
|
expected = minDialTimeout
|
|
|
|
}
|
|
|
|
if reduced != expected {
|
2014-11-18 08:04:09 +08:00
|
|
|
t.Fatalf("expected %s, got %s", _case.ExpectedReduced, reduced)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 09:57:43 +08:00
|
|
|
|
|
|
|
func TestUTPRawConn(t *testing.T) {
|
|
|
|
l, err := utp.Listen("utp", &utp.Addr{&net.UDPAddr{Port: 0}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
2014-12-26 14:19:01 +08:00
|
|
|
utpAddr, err := utp.ResolveAddr("utp", fmt.Sprintf("localhost:%d", util.AddrPort(l.Addr())))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-04 09:57:43 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
_, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Connect a UTP peer to see if the RawConn will still work.
|
2014-12-26 14:19:01 +08:00
|
|
|
utpPeer, err := utp.DialUTP("utp", nil, utpAddr)
|
2014-12-04 09:57:43 +08: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
|
|
|
|
const N = 5000 // How many messages to send.
|
|
|
|
readerStopped := make(chan struct{})
|
|
|
|
// The reader goroutine.
|
|
|
|
go func() {
|
|
|
|
defer close(readerStopped)
|
|
|
|
b := make([]byte, 500)
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
n, _, err := l.RawConn.ReadFrom(b)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-12-26 14:19:01 +08:00
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", util.AddrPort(l.Addr())))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-04 09:57:43 +08:00
|
|
|
for i := 0; i < N; i++ {
|
2014-12-26 14:19:01 +08:00
|
|
|
_, err := peer.WriteTo([]byte(fmt.Sprintf("%d", i)), udpAddr)
|
2014-12-04 09:57:43 +08: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)
|
|
|
|
}
|
|
|
|
}
|