FedP2P/fs/torrentfs_test.go

226 lines
5.4 KiB
Go
Raw Normal View History

package torrentfs
import (
2014-06-28 17:38:31 +08:00
"fmt"
"io/ioutil"
2015-04-07 23:43:50 +08:00
"log"
"net"
2014-08-21 16:07:06 +08:00
_ "net/http/pprof"
"os"
"path/filepath"
"testing"
2015-01-11 10:21:14 +08:00
"time"
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
_ "github.com/anacrolix/envpprof"
2015-08-03 22:29:01 +08:00
"github.com/anacrolix/missinggo"
"github.com/stretchr/testify/assert"
2016-02-21 23:38:52 +08:00
"github.com/stretchr/testify/require"
netContext "golang.org/x/net/context"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/internal/testutil"
2015-04-29 22:31:34 +08:00
"github.com/anacrolix/torrent/metainfo"
2016-03-28 21:24:00 +08:00
"github.com/anacrolix/torrent/storage"
)
2015-04-07 23:43:50 +08:00
func init() {
log.SetFlags(log.Flags() | log.Lshortfile)
}
func TestTCPAddrString(t *testing.T) {
2014-11-20 10:01:03 +08: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 10:01:03 +08:00
ta := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
2015-08-03 22:29:01 +08:00
Port: missinggo.AddrPort(l.Addr()),
2014-11-20 10:01:03 +08:00
}
s := ta.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 (tl *testLayout) Destroy() error {
return os.RemoveAll(tl.BaseDir)
2014-04-17 14:37:54 +08:00
}
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)
testutil.CreateDummyTorrentData(tl.Completed)
tl.Metainfo = testutil.GreetingMetaInfo()
2014-04-17 14:37:54 +08:00
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()
2016-04-03 16:40:43 +08:00
require.NoError(t, err)
defer func() {
2014-04-17 14:37:54 +08:00
err := layout.Destroy()
if err != nil {
t.Log(err)
}
}()
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,
DisableTCP: true,
DisableUTP: true,
2014-08-21 16:07:06 +08:00
})
2016-04-03 16:40:43 +08:00
require.NoError(t, err)
2015-03-08 14:28:14 +08:00
defer client.Close()
tt, err := client.AddTorrent(layout.Metainfo)
2016-04-03 16:40:43 +08:00
require.NoError(t, err)
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 {
2017-06-01 09:19:45 +08:00
switch err.Error() {
2017-06-01 13:39:44 +08:00
case "cannot locate OSXFUSE",
"fusermount: exit status 1":
2017-06-01 09:19:45 +08:00
t.Skip(err)
}
2017-06-01 09:19:45 +08:00
t.Fatal(err)
2014-04-17 14:37:54 +08:00
}
go func() {
server := fusefs.New(fuseConn, &fusefs.Config{
2014-04-17 14:37:54 +08:00
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(fs)
2014-04-17 14:37:54 +08:00
}()
2015-01-11 10:21:14 +08:00
<-fuseConn.Ready
if err := fuseConn.MountError; err != nil {
t.Fatalf("mount error: %s", err)
2015-01-11 10:21:14 +08:00
}
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() {
_, err := ioutil.ReadFile(filepath.Join(layout.MountDir, tt.Info().Name))
2014-12-04 02:53:10 +08:00
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()
2016-02-21 23:38:52 +08:00
require.NoError(t, 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,
ListenAddr: "localhost:0",
2015-06-16 14:57:47 +08:00
Seed: true,
2014-08-21 16:07:06 +08:00
})
2016-02-21 23:38:52 +08:00
require.NoError(t, err)
2015-03-08 14:28:14 +08:00
defer seeder.Close()
2016-02-21 23:38:52 +08:00
testutil.ExportStatusWriter(seeder, "s")
_, err = seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%s", layout.Metainfo.HashInfoBytes().HexString()))
2016-02-21 23:38:52 +08:00
require.NoError(t, err)
2014-08-21 16:07:06 +08:00
leecher, err := torrent.NewClient(&torrent.Config{
DisableTrackers: true,
NoDHT: true,
ListenAddr: "localhost:0",
DisableTCP: true,
DefaultStorage: storage.NewMMap(filepath.Join(layout.BaseDir, "download")),
// 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
})
2016-04-04 13:39:26 +08:00
require.NoError(t, err)
2016-02-21 23:38:52 +08:00
testutil.ExportStatusWriter(leecher, "l")
2015-03-08 14:28:14 +08:00
defer leecher.Close()
leecherTorrent, _ := leecher.AddTorrent(layout.Metainfo)
2016-02-21 23:38:52 +08:00
leecherTorrent.AddPeers([]torrent.Peer{
2016-11-22 11:01:09 +08:00
{
2016-02-21 23:38:52 +08:00
IP: missinggo.AddrIP(seeder.ListenAddr()),
Port: missinggo.AddrPort(seeder.ListenAddr()),
},
})
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-06-02 22:14:55 +08:00
node, _ := root.(fusefs.NodeStringLookuper).Lookup(netContext.Background(), "greeting")
2015-03-25 14:25:24 +08:00
var attr fuse.Attr
node.Attr(netContext.Background(), &attr)
2015-03-25 14:25:24 +08:00
size := attr.Size
2014-11-21 02:50:53 +08:00
resp := &fuse.ReadResponse{
Data: make([]byte, size),
}
2015-06-02 22:14:55 +08:00
node.(fusefs.HandleReader).Read(netContext.Background(), &fuse.ReadRequest{
2015-03-25 14:25:24 +08:00
Size: int(size),
}, resp)
2016-02-21 23:38:52 +08:00
assert.EqualValues(t, testutil.GreetingFileContents, resp.Data)
}
func TestIsSubPath(t *testing.T) {
for _, case_ := range []struct {
parent, child string
is bool
}{
{"", "", false},
{"", "/", true},
{"", "a", true},
{"a/b", "a/bc", false},
{"a/b", "a/b", false},
{"a/b", "a/b/c", true},
{"a/b", "a//b", false},
} {
assert.Equal(t, case_.is, isSubPath(case_.parent, case_.child))
}
}