2014-03-17 22:44:22 +08:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-06-28 17:38:31 +08:00
|
|
|
"fmt"
|
2014-03-17 22:44:22 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
2014-08-21 16:07:06 +08:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2014-03-17 22:44:22 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-11-17 03:06:32 +08:00
|
|
|
"strconv"
|
2014-11-21 06:28:14 +08:00
|
|
|
"strings"
|
2014-03-17 22:44:22 +08:00
|
|
|
"testing"
|
2015-01-11 10:21:14 +08:00
|
|
|
"time"
|
2014-03-20 13:58:09 +08:00
|
|
|
|
2015-02-06 13:03:33 +08:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2014-08-21 16:07:06 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent"
|
2015-02-25 11:51:12 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/data/mmap"
|
2014-03-20 13:58:09 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/testutil"
|
2014-08-21 16:07:06 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
2015-02-25 11:51:12 +08:00
|
|
|
|
2014-08-21 16:07:06 +08:00
|
|
|
"github.com/anacrolix/libtorgo/metainfo"
|
2014-03-20 13:58:09 +08:00
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2014-03-17 22:44:22 +08:00
|
|
|
)
|
|
|
|
|
2014-08-21 16:07:06 +08:00
|
|
|
func init() {
|
|
|
|
go http.ListenAndServe(":6061", nil)
|
|
|
|
}
|
|
|
|
|
2014-03-17 22:44:22 +08:00
|
|
|
func TestTCPAddrString(t *testing.T) {
|
2014-11-20 10:01:03 +08:00
|
|
|
l, err := net.Listen("tcp4", "localhost:0")
|
2014-03-17 22:44:22 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
c, err := net.Dial("tcp", l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
ras := c.RemoteAddr().String()
|
2014-11-20 10:01:03 +08:00
|
|
|
ta := &net.TCPAddr{
|
|
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
|
|
Port: util.AddrPort(l.Addr()),
|
|
|
|
}
|
|
|
|
s := ta.String()
|
2014-03-17 22:44:22 +08:00
|
|
|
if ras != s {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 14:37:54 +08:00
|
|
|
type testLayout struct {
|
|
|
|
BaseDir string
|
|
|
|
MountDir string
|
|
|
|
Completed string
|
|
|
|
Metainfo *metainfo.MetaInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *testLayout) Destroy() error {
|
|
|
|
return os.RemoveAll(me.BaseDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGreetingLayout() (tl testLayout, err error) {
|
|
|
|
tl.BaseDir, err = ioutil.TempDir("", "torrentfs")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tl.Completed = filepath.Join(tl.BaseDir, "completed")
|
|
|
|
os.Mkdir(tl.Completed, 0777)
|
|
|
|
tl.MountDir = filepath.Join(tl.BaseDir, "mnt")
|
|
|
|
os.Mkdir(tl.MountDir, 0777)
|
|
|
|
name := testutil.CreateDummyTorrentData(tl.Completed)
|
|
|
|
metaInfoBuf := &bytes.Buffer{}
|
|
|
|
testutil.CreateMetaInfo(name, metaInfoBuf)
|
|
|
|
tl.Metainfo, err = metainfo.Load(metaInfoBuf)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-04 02:53:10 +08:00
|
|
|
// Unmount without first killing the FUSE connection while there are FUSE
|
|
|
|
// operations blocked inside the filesystem code.
|
2014-04-17 14:37:54 +08:00
|
|
|
func TestUnmountWedged(t *testing.T) {
|
|
|
|
layout, err := newGreetingLayout()
|
2014-03-17 22:44:22 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2014-04-17 14:37:54 +08:00
|
|
|
err := layout.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
}()
|
2014-08-21 16:07:06 +08:00
|
|
|
client, err := torrent.NewClient(&torrent.Config{
|
2014-04-17 14:37:54 +08:00
|
|
|
DataDir: filepath.Join(layout.BaseDir, "incomplete"),
|
|
|
|
DisableTrackers: true,
|
2014-08-21 16:07:06 +08:00
|
|
|
NoDHT: true,
|
2014-12-03 04:23:01 +08:00
|
|
|
|
|
|
|
NoDefaultBlocklist: true,
|
2014-08-21 16:07:06 +08:00
|
|
|
})
|
2014-11-21 06:28:14 +08:00
|
|
|
defer client.Stop()
|
2014-04-17 14:37:54 +08:00
|
|
|
client.AddTorrent(layout.Metainfo)
|
2014-08-21 16:07:06 +08:00
|
|
|
fs := New(client)
|
2014-04-17 14:37:54 +08:00
|
|
|
fuseConn, err := fuse.Mount(layout.MountDir)
|
|
|
|
if err != nil {
|
2014-11-21 06:28:14 +08:00
|
|
|
if strings.Contains(err.Error(), "fuse") {
|
|
|
|
t.Skip(err)
|
|
|
|
}
|
2014-04-17 14:37:54 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
server := fusefs.Server{
|
|
|
|
FS: fs,
|
|
|
|
Debug: func(msg interface{}) {
|
2014-12-04 02:53:10 +08:00
|
|
|
t.Log(msg)
|
2014-04-17 14:37:54 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
server.Serve(fuseConn)
|
|
|
|
}()
|
2015-01-11 10:21:14 +08:00
|
|
|
<-fuseConn.Ready
|
|
|
|
if err := fuseConn.MountError; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-04 02:53:10 +08:00
|
|
|
// Read the greeting file, though it will never be available. This should
|
|
|
|
// "wedge" FUSE, requiring the fs object to be forcibly destroyed. The
|
|
|
|
// read call will return with a FS error.
|
2014-04-17 14:37:54 +08:00
|
|
|
go func() {
|
2014-12-04 02:53:10 +08:00
|
|
|
_, err := ioutil.ReadFile(filepath.Join(layout.MountDir, layout.Metainfo.Info.Name))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error reading greeting")
|
|
|
|
}
|
2014-04-17 14:37:54 +08:00
|
|
|
}()
|
2014-12-04 02:53:10 +08:00
|
|
|
|
|
|
|
// Wait until the read has blocked inside the filesystem code.
|
|
|
|
fs.mu.Lock()
|
|
|
|
for fs.blockedReads != 1 {
|
|
|
|
fs.event.Wait()
|
|
|
|
}
|
|
|
|
fs.mu.Unlock()
|
|
|
|
|
2014-04-17 14:37:54 +08:00
|
|
|
fs.Destroy()
|
2014-12-04 02:53:10 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
err = fuse.Unmount(layout.MountDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("error unmounting: %s", err)
|
2015-01-11 10:21:14 +08:00
|
|
|
time.Sleep(time.Millisecond)
|
2014-12-04 02:53:10 +08:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2014-04-17 14:37:54 +08:00
|
|
|
}
|
2014-12-04 02:53:10 +08:00
|
|
|
|
2014-04-17 14:37:54 +08:00
|
|
|
err = fuseConn.Close()
|
|
|
|
if err != nil {
|
2014-12-04 02:53:10 +08:00
|
|
|
t.Fatalf("error closing fuse conn: %s", err)
|
2014-04-17 14:37:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadOnDemand(t *testing.T) {
|
|
|
|
layout, err := newGreetingLayout()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-04 02:53:10 +08:00
|
|
|
defer layout.Destroy()
|
2014-08-21 16:07:06 +08:00
|
|
|
seeder, err := torrent.NewClient(&torrent.Config{
|
|
|
|
DataDir: layout.Completed,
|
2014-06-28 17:38:31 +08:00
|
|
|
DisableTrackers: true,
|
2014-08-21 16:07:06 +08:00
|
|
|
NoDHT: true,
|
2014-11-21 14:07:04 +08:00
|
|
|
ListenAddr: ":0",
|
2014-12-03 04:23:01 +08:00
|
|
|
|
|
|
|
NoDefaultBlocklist: true,
|
2015-02-25 11:48:39 +08:00
|
|
|
// Ensure that the metainfo is obtained over the wire, since we added
|
|
|
|
// the torrent to the seeder by magnet.
|
|
|
|
DisableMetainfoCache: true,
|
2014-08-21 16:07:06 +08:00
|
|
|
})
|
2014-11-21 14:07:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating seeder client: %s", err)
|
|
|
|
}
|
2014-12-01 17:28:39 +08:00
|
|
|
seeder.SetIPBlockList(nil)
|
2014-11-21 14:07:04 +08:00
|
|
|
defer seeder.Stop()
|
2014-08-21 16:07:06 +08:00
|
|
|
http.HandleFunc("/seeder", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
seeder.WriteStatus(w)
|
|
|
|
})
|
2014-09-25 16:05:52 +08:00
|
|
|
_, err = seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%x", layout.Metainfo.Info.Hash))
|
2014-06-28 17:38:31 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-21 16:07:06 +08:00
|
|
|
leecher, err := torrent.NewClient(&torrent.Config{
|
2014-12-26 14:15:17 +08:00
|
|
|
DisableTrackers: true,
|
|
|
|
NoDHT: true,
|
|
|
|
ListenAddr: ":0",
|
2015-02-18 18:33:42 +08:00
|
|
|
DisableTCP: true,
|
2014-11-17 03:54:43 +08:00
|
|
|
|
2014-12-03 04:23:01 +08:00
|
|
|
NoDefaultBlocklist: true,
|
|
|
|
|
2015-02-25 11:48:39 +08:00
|
|
|
TorrentDataOpener: func(info *metainfo.Info) (torrent.TorrentData, error) {
|
|
|
|
return mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
|
|
|
|
},
|
|
|
|
|
2014-11-17 03:54:43 +08:00
|
|
|
// This can be used to check if clients can connect to other clients
|
|
|
|
// with the same ID.
|
|
|
|
|
|
|
|
// PeerID: seeder.PeerID(),
|
2014-08-21 16:07:06 +08:00
|
|
|
})
|
2014-12-01 17:28:39 +08:00
|
|
|
leecher.SetIPBlockList(nil)
|
2014-08-21 16:07:06 +08:00
|
|
|
http.HandleFunc("/leecher", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
leecher.WriteStatus(w)
|
|
|
|
})
|
2014-03-18 19:39:33 +08:00
|
|
|
defer leecher.Stop()
|
2014-04-17 14:37:54 +08:00
|
|
|
leecher.AddTorrent(layout.Metainfo)
|
2014-08-21 16:07:06 +08:00
|
|
|
var ih torrent.InfoHash
|
|
|
|
util.CopyExact(ih[:], layout.Metainfo.Info.Hash)
|
|
|
|
leecher.AddPeers(ih, []torrent.Peer{func() torrent.Peer {
|
2014-11-17 03:06:32 +08:00
|
|
|
_, port, err := net.SplitHostPort(seeder.ListenAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
portInt64, err := strconv.ParseInt(port, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-03-17 22:44:22 +08:00
|
|
|
return torrent.Peer{
|
2015-02-26 13:11:33 +08:00
|
|
|
IP: func() net.IP {
|
|
|
|
ret, _ := net.ResolveIPAddr("ip", "localhost")
|
|
|
|
return ret.IP
|
|
|
|
}(),
|
2014-11-17 03:06:32 +08:00
|
|
|
Port: int(portInt64),
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
}()})
|
2014-08-21 16:07:06 +08:00
|
|
|
fs := New(leecher)
|
2014-11-21 02:50:53 +08:00
|
|
|
defer fs.Destroy()
|
|
|
|
root, _ := fs.Root()
|
2015-02-06 13:03:33 +08:00
|
|
|
node, _ := root.(fusefs.NodeStringLookuper).Lookup(context.Background(), "greeting")
|
2014-11-21 02:50:53 +08:00
|
|
|
size := int(node.Attr().Size)
|
|
|
|
resp := &fuse.ReadResponse{
|
|
|
|
Data: make([]byte, size),
|
|
|
|
}
|
2015-02-06 13:03:33 +08:00
|
|
|
node.(fusefs.HandleReader).Read(context.Background(), &fuse.ReadRequest{
|
2014-11-21 02:50:53 +08:00
|
|
|
Size: size,
|
2015-02-06 13:03:33 +08:00
|
|
|
}, resp)
|
2014-11-21 02:50:53 +08:00
|
|
|
content := resp.Data
|
2014-03-20 13:58:09 +08:00
|
|
|
if string(content) != testutil.GreetingFileContents {
|
2014-03-18 19:39:33 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|