2014-03-17 22:44:22 +08:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
2017-08-27 12:00:37 +08:00
|
|
|
"context"
|
2014-06-28 17:38:31 +08:00
|
|
|
"fmt"
|
2014-03-17 22:44:22 +08:00
|
|
|
"io/ioutil"
|
2015-04-07 23:43:50 +08:00
|
|
|
"log"
|
2014-03-17 22:44:22 +08:00
|
|
|
"net"
|
2014-08-21 16:07:06 +08:00
|
|
|
_ "net/http/pprof"
|
2014-03-17 22:44:22 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2015-01-11 10:21:14 +08:00
|
|
|
"time"
|
2014-03-20 13:58:09 +08:00
|
|
|
|
2015-03-26 14:18:08 +08:00
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2015-10-01 22:09:04 +08:00
|
|
|
_ "github.com/anacrolix/envpprof"
|
2021-06-23 15:24:50 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2"
|
2019-08-21 18:58:40 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"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"
|
2014-03-17 22:44:22 +08:00
|
|
|
)
|
|
|
|
|
2015-04-07 23:43:50 +08:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Flags() | log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
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),
|
2015-08-03 22:29:01 +08:00
|
|
|
Port: missinggo.AddrPort(l.Addr()),
|
2014-11-20 10:01:03 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
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)
|
2016-04-30 09:07:29 +08:00
|
|
|
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)
|
2014-03-17 22:44:22 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}()
|
2018-06-16 14:33:40 +08:00
|
|
|
cfg := torrent.NewDefaultClientConfig()
|
|
|
|
cfg.DataDir = filepath.Join(layout.BaseDir, "incomplete")
|
|
|
|
cfg.DisableTrackers = true
|
|
|
|
cfg.NoDHT = true
|
|
|
|
cfg.DisableTCP = true
|
|
|
|
cfg.DisableUTP = true
|
|
|
|
client, err := torrent.NewClient(cfg)
|
2016-04-03 16:40:43 +08:00
|
|
|
require.NoError(t, err)
|
2015-03-08 14:28:14 +08:00
|
|
|
defer client.Close()
|
2016-08-26 18:29:05 +08:00
|
|
|
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-09-22 11:46:00 +08:00
|
|
|
case "cannot locate OSXFUSE":
|
|
|
|
fallthrough
|
|
|
|
case "fusermount: exit status 1":
|
2017-06-01 09:19:45 +08:00
|
|
|
t.Skip(err)
|
2014-11-21 06:28:14 +08:00
|
|
|
}
|
2017-06-01 09:19:45 +08:00
|
|
|
t.Fatal(err)
|
2014-04-17 14:37:54 +08:00
|
|
|
}
|
|
|
|
go func() {
|
2015-06-22 21:02:29 +08:00
|
|
|
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
|
|
|
},
|
2015-06-22 21:02:29 +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 {
|
2015-04-27 12:06:00 +08:00
|
|
|
t.Fatalf("mount error: %s", err)
|
2015-01-11 10:21:14 +08:00
|
|
|
}
|
2017-08-27 12:00:37 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
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() {
|
2017-08-27 12:00:37 +08:00
|
|
|
<-ctx.Done()
|
|
|
|
fs.mu.Lock()
|
|
|
|
fs.event.Broadcast()
|
|
|
|
fs.mu.Unlock()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
2016-08-26 18:29:05 +08:00
|
|
|
_, err := ioutil.ReadFile(filepath.Join(layout.MountDir, tt.Info().Name))
|
2017-09-22 11:46:00 +08:00
|
|
|
require.Error(t, err)
|
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()
|
2017-08-27 12:00:37 +08:00
|
|
|
for fs.blockedReads != 1 && ctx.Err() == nil {
|
2014-12-04 02:53:10 +08:00
|
|
|
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()
|
2017-09-22 11:46:00 +08:00
|
|
|
assert.NoError(t, 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()
|
2018-06-16 14:33:40 +08:00
|
|
|
cfg := torrent.NewDefaultClientConfig()
|
|
|
|
cfg.DataDir = layout.Completed
|
|
|
|
cfg.DisableTrackers = true
|
|
|
|
cfg.NoDHT = true
|
|
|
|
cfg.Seed = true
|
2019-07-30 15:25:10 +08:00
|
|
|
cfg.ListenPort = 0
|
2018-06-16 14:33:40 +08:00
|
|
|
cfg.ListenHost = torrent.LoopbackListenHost
|
|
|
|
seeder, err := torrent.NewClient(cfg)
|
2016-02-21 23:38:52 +08:00
|
|
|
require.NoError(t, err)
|
2015-03-08 14:28:14 +08:00
|
|
|
defer seeder.Close()
|
2020-10-10 07:59:17 +08:00
|
|
|
defer testutil.ExportStatusWriter(seeder, "s", t)()
|
2017-09-15 17:34:42 +08:00
|
|
|
// Just to mix things up, the seeder starts with the data, but the leecher
|
|
|
|
// starts with the metainfo.
|
|
|
|
seederTorrent, 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)
|
2017-09-15 17:34:42 +08:00
|
|
|
go func() {
|
|
|
|
// Wait until we get the metainfo, then check for the data.
|
|
|
|
<-seederTorrent.GotInfo()
|
|
|
|
seederTorrent.VerifyData()
|
|
|
|
}()
|
2018-06-16 14:33:40 +08:00
|
|
|
cfg = torrent.NewDefaultClientConfig()
|
|
|
|
cfg.DisableTrackers = true
|
|
|
|
cfg.NoDHT = true
|
|
|
|
cfg.DisableTCP = true
|
|
|
|
cfg.DefaultStorage = storage.NewMMap(filepath.Join(layout.BaseDir, "download"))
|
|
|
|
cfg.ListenHost = torrent.LoopbackListenHost
|
2019-07-19 14:19:21 +08:00
|
|
|
cfg.ListenPort = 0
|
2018-06-16 14:33:40 +08:00
|
|
|
leecher, err := torrent.NewClient(cfg)
|
2016-04-04 13:39:26 +08:00
|
|
|
require.NoError(t, err)
|
2020-10-10 07:59:17 +08:00
|
|
|
testutil.ExportStatusWriter(leecher, "l", t)()
|
2015-03-08 14:28:14 +08:00
|
|
|
defer leecher.Close()
|
2018-04-12 09:41:07 +08:00
|
|
|
leecherTorrent, err := leecher.AddTorrent(layout.Metainfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
leecherTorrent.AddClientPeer(seeder)
|
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()
|
2021-01-19 06:16:49 +08:00
|
|
|
node, _ := root.(fusefs.NodeStringLookuper).Lookup(context.Background(), "greeting")
|
2015-03-25 14:25:24 +08:00
|
|
|
var attr fuse.Attr
|
2021-01-19 06:16:49 +08:00
|
|
|
node.Attr(context.Background(), &attr)
|
2015-03-25 14:25:24 +08:00
|
|
|
size := attr.Size
|
2020-10-27 09:59:07 +08:00
|
|
|
data := make([]byte, size)
|
2017-11-07 21:12:34 +08:00
|
|
|
h, err := node.(fusefs.NodeOpener).Open(context.TODO(), nil, nil)
|
2017-08-27 12:19:58 +08:00
|
|
|
require.NoError(t, err)
|
2020-10-27 09:59:07 +08:00
|
|
|
|
|
|
|
// torrent.Reader.Read no longer tries to fill the entire read buffer, so this is a ReadFull for
|
|
|
|
// fusefs.
|
|
|
|
var n int
|
|
|
|
for n < len(data) {
|
|
|
|
resp := fuse.ReadResponse{Data: data[n:]}
|
2021-01-19 06:16:49 +08:00
|
|
|
err := h.(fusefs.HandleReader).Read(context.Background(), &fuse.ReadRequest{
|
2020-10-27 09:59:07 +08:00
|
|
|
Size: int(size) - n,
|
|
|
|
Offset: int64(n),
|
|
|
|
}, &resp)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
n += len(resp.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, testutil.GreetingFileContents, data)
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2015-08-23 17:25:33 +08:00
|
|
|
|
|
|
|
func TestIsSubPath(t *testing.T) {
|
|
|
|
for _, case_ := range []struct {
|
|
|
|
parent, child string
|
|
|
|
is bool
|
|
|
|
}{
|
|
|
|
{"", "", false},
|
|
|
|
{"", "/", true},
|
2016-08-01 21:56:56 +08:00
|
|
|
{"", "a", true},
|
2015-08-23 17:25:33 +08:00
|
|
|
{"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))
|
|
|
|
}
|
|
|
|
}
|