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"
|
2014-04-17 14:37:54 +08:00
|
|
|
"log"
|
2014-03-17 22:44:22 +08:00
|
|
|
"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"
|
|
|
|
"testing"
|
2014-03-20 13:58:09 +08:00
|
|
|
"time"
|
|
|
|
|
2014-08-21 16:07:06 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent"
|
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"
|
|
|
|
"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) {
|
|
|
|
ta := &net.TCPAddr{
|
|
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
|
|
Port: 3000,
|
|
|
|
}
|
|
|
|
s := ta.String()
|
|
|
|
l, err := net.Listen("tcp4", "localhost:3000")
|
|
|
|
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()
|
|
|
|
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)
|
2014-08-21 16:07:06 +08:00
|
|
|
log.Printf("%x", tl.Metainfo.Info.Pieces)
|
2014-04-17 14:37:54 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-28 17:38:31 +08:00
|
|
|
log.Printf("%+v", *layout.Metainfo)
|
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 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
server := fusefs.Server{
|
|
|
|
FS: fs,
|
|
|
|
Debug: func(msg interface{}) {
|
|
|
|
log.Print(msg)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
server.Serve(fuseConn)
|
|
|
|
}()
|
|
|
|
<-fuseConn.Ready
|
|
|
|
if err := fuseConn.MountError; err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
go func() {
|
2014-07-13 15:37:12 +08:00
|
|
|
ioutil.ReadFile(filepath.Join(layout.MountDir, layout.Metainfo.Info.Name))
|
2014-04-17 14:37:54 +08:00
|
|
|
}()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
fs.Destroy()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
err = fuse.Unmount(layout.MountDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
err = fuseConn.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadOnDemand(t *testing.T) {
|
|
|
|
layout, err := newGreetingLayout()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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,
|
|
|
|
})
|
|
|
|
http.HandleFunc("/seeder", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
seeder.WriteStatus(w)
|
|
|
|
})
|
2014-03-18 19:39:33 +08:00
|
|
|
defer seeder.Stop()
|
2014-07-13 15:37:12 +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-05-21 15:40:54 +08:00
|
|
|
DataDir: filepath.Join(layout.BaseDir, "download"),
|
2014-08-21 16:07:06 +08:00
|
|
|
DownloadStrategy: torrent.NewResponsiveDownloadStrategy(0),
|
2014-06-28 17:38:31 +08:00
|
|
|
DisableTrackers: true,
|
2014-08-21 16:07:06 +08:00
|
|
|
NoDHT: true,
|
|
|
|
})
|
|
|
|
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 {
|
|
|
|
tcpAddr := seeder.ListenAddr().(*net.TCPAddr)
|
2014-03-17 22:44:22 +08:00
|
|
|
return torrent.Peer{
|
|
|
|
IP: tcpAddr.IP,
|
|
|
|
Port: tcpAddr.Port,
|
|
|
|
}
|
|
|
|
}()})
|
2014-08-21 16:07:06 +08:00
|
|
|
fs := New(leecher)
|
2014-04-17 14:37:54 +08:00
|
|
|
mountDir := layout.MountDir
|
|
|
|
fuseConn, err := fuse.Mount(layout.MountDir)
|
2014-03-17 22:44:22 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-20 01:30:08 +08:00
|
|
|
defer func() {
|
|
|
|
if err := fuse.Unmount(mountDir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-03-18 19:39:33 +08:00
|
|
|
go func() {
|
|
|
|
if err := fusefs.Serve(fuseConn, fs); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := fuseConn.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-fuseConn.Ready
|
|
|
|
if fuseConn.MountError != nil {
|
|
|
|
t.Fatal(fuseConn.MountError)
|
|
|
|
}
|
2014-03-20 13:58:09 +08:00
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Second)
|
2014-06-28 17:38:31 +08:00
|
|
|
if err := fuse.Unmount(mountDir); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
2014-03-20 13:58:09 +08:00
|
|
|
}()
|
2014-03-18 19:39:33 +08:00
|
|
|
content, err := ioutil.ReadFile(filepath.Join(mountDir, "greeting"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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
|
|
|
}
|