torrent/fs/torrentfs_test.go

203 lines
4.4 KiB
Go
Raw Normal View History

package torrentfs
import (
"bytes"
2014-06-28 09:38:31 +00:00
"fmt"
"io/ioutil"
2014-04-17 06:37:54 +00:00
"log"
"net"
2014-08-21 08:07:06 +00:00
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
2014-08-21 08:07:06 +00:00
"bitbucket.org/anacrolix/go.torrent"
"bitbucket.org/anacrolix/go.torrent/testutil"
2014-08-21 08:07:06 +00:00
"bitbucket.org/anacrolix/go.torrent/util"
"github.com/anacrolix/libtorgo/metainfo"
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
)
2014-08-21 08:07:06 +00:00
func init() {
go http.ListenAndServe(":6061", nil)
}
func TestTCPAddrString(t *testing.T) {
2014-11-20 02:01:03 +00:00
l, err := net.Listen("tcp4", "localhost:0")
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 02:01:03 +00:00
ta := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: util.AddrPort(l.Addr()),
}
s := ta.String()
if ras != s {
t.FailNow()
}
}
2014-04-17 06:37:54 +00: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 08:07:06 +00:00
log.Printf("%x", tl.Metainfo.Info.Pieces)
2014-04-17 06:37:54 +00:00
return
}
func TestUnmountWedged(t *testing.T) {
layout, err := newGreetingLayout()
if err != nil {
t.Fatal(err)
}
defer func() {
2014-04-17 06:37:54 +00:00
err := layout.Destroy()
if err != nil {
t.Log(err)
}
}()
2014-08-21 08:07:06 +00:00
client, err := torrent.NewClient(&torrent.Config{
2014-04-17 06:37:54 +00:00
DataDir: filepath.Join(layout.BaseDir, "incomplete"),
DisableTrackers: true,
2014-08-21 08:07:06 +00:00
NoDHT: true,
})
defer client.Stop()
2014-06-28 09:38:31 +00:00
log.Printf("%+v", *layout.Metainfo)
2014-04-17 06:37:54 +00:00
client.AddTorrent(layout.Metainfo)
2014-08-21 08:07:06 +00:00
fs := New(client)
2014-04-17 06:37:54 +00:00
fuseConn, err := fuse.Mount(layout.MountDir)
if err != nil {
if strings.Contains(err.Error(), "fuse") {
t.Skip(err)
}
2014-04-17 06:37:54 +00:00
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() {
ioutil.ReadFile(filepath.Join(layout.MountDir, layout.Metainfo.Info.Name))
2014-04-17 06:37:54 +00: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 08:07:06 +00:00
seeder, err := torrent.NewClient(&torrent.Config{
DataDir: layout.Completed,
2014-06-28 09:38:31 +00:00
DisableTrackers: true,
2014-08-21 08:07:06 +00:00
NoDHT: true,
})
http.HandleFunc("/seeder", func(w http.ResponseWriter, req *http.Request) {
seeder.WriteStatus(w)
})
defer seeder.Stop()
_, err = seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%x", layout.Metainfo.Info.Hash))
2014-06-28 09:38:31 +00:00
if err != nil {
t.Fatal(err)
}
2014-08-21 08:07:06 +00:00
leecher, err := torrent.NewClient(&torrent.Config{
DataDir: filepath.Join(layout.BaseDir, "download"),
2014-08-21 08:07:06 +00:00
DownloadStrategy: torrent.NewResponsiveDownloadStrategy(0),
2014-06-28 09:38:31 +00:00
DisableTrackers: true,
2014-08-21 08:07:06 +00:00
NoDHT: true,
// This can be used to check if clients can connect to other clients
// with the same ID.
// PeerID: seeder.PeerID(),
2014-08-21 08:07:06 +00:00
})
http.HandleFunc("/leecher", func(w http.ResponseWriter, req *http.Request) {
leecher.WriteStatus(w)
})
defer leecher.Stop()
2014-04-17 06:37:54 +00:00
leecher.AddTorrent(layout.Metainfo)
2014-08-21 08:07:06 +00:00
var ih torrent.InfoHash
util.CopyExact(ih[:], layout.Metainfo.Info.Hash)
leecher.AddPeers(ih, []torrent.Peer{func() torrent.Peer {
_, port, err := net.SplitHostPort(seeder.ListenAddr().String())
if err != nil {
panic(err)
}
portInt64, err := strconv.ParseInt(port, 0, 0)
if err != nil {
panic(err)
}
return torrent.Peer{
IP: net.IPv6loopback,
Port: int(portInt64),
}
}()})
2014-08-21 08:07:06 +00:00
fs := New(leecher)
2014-11-20 18:50:53 +00:00
defer fs.Destroy()
root, _ := fs.Root()
node, _ := root.(fusefs.NodeStringLookuper).Lookup("greeting", nil)
size := int(node.Attr().Size)
resp := &fuse.ReadResponse{
Data: make([]byte, size),
}
node.(fusefs.HandleReader).Read(&fuse.ReadRequest{
Size: size,
}, resp, nil)
content := resp.Data
if string(content) != testutil.GreetingFileContents {
t.FailNow()
}
}