17 "golang.org/x/net/context"
19 "bitbucket.org/anacrolix/go.torrent"
20 "bitbucket.org/anacrolix/go.torrent/data/mmap"
21 "bitbucket.org/anacrolix/go.torrent/testutil"
22 "bitbucket.org/anacrolix/go.torrent/util"
24 "github.com/anacrolix/libtorgo/metainfo"
27 fusefs "bazil.org/fuse/fs"
31 go http.ListenAndServe(":6061", nil)
34 func TestTCPAddrString(t *testing.T) {
35 l, err := net.Listen("tcp4", "localhost:0")
40 c, err := net.Dial("tcp", l.Addr().String())
45 ras := c.RemoteAddr().String()
47 IP: net.IPv4(127, 0, 0, 1),
48 Port: util.AddrPort(l.Addr()),
56 type testLayout struct {
60 Metainfo *metainfo.MetaInfo
63 func (me *testLayout) Destroy() error {
64 return os.RemoveAll(me.BaseDir)
67 func newGreetingLayout() (tl testLayout, err error) {
68 tl.BaseDir, err = ioutil.TempDir("", "torrentfs")
72 tl.Completed = filepath.Join(tl.BaseDir, "completed")
73 os.Mkdir(tl.Completed, 0777)
74 tl.MountDir = filepath.Join(tl.BaseDir, "mnt")
75 os.Mkdir(tl.MountDir, 0777)
76 name := testutil.CreateDummyTorrentData(tl.Completed)
77 metaInfoBuf := &bytes.Buffer{}
78 testutil.CreateMetaInfo(name, metaInfoBuf)
79 tl.Metainfo, err = metainfo.Load(metaInfoBuf)
83 // Unmount without first killing the FUSE connection while there are FUSE
84 // operations blocked inside the filesystem code.
85 func TestUnmountWedged(t *testing.T) {
86 layout, err := newGreetingLayout()
91 err := layout.Destroy()
96 client, err := torrent.NewClient(&torrent.Config{
97 DataDir: filepath.Join(layout.BaseDir, "incomplete"),
98 DisableTrackers: true,
101 NoDefaultBlocklist: true,
104 client.AddTorrent(layout.Metainfo)
106 fuseConn, err := fuse.Mount(layout.MountDir)
108 if strings.Contains(err.Error(), "fuse") {
114 server := fusefs.Server{
116 Debug: func(msg interface{}) {
120 server.Serve(fuseConn)
123 if err := fuseConn.MountError; err != nil {
126 // Read the greeting file, though it will never be available. This should
127 // "wedge" FUSE, requiring the fs object to be forcibly destroyed. The
128 // read call will return with a FS error.
130 _, err := ioutil.ReadFile(filepath.Join(layout.MountDir, layout.Metainfo.Info.Name))
132 t.Fatal("expected error reading greeting")
136 // Wait until the read has blocked inside the filesystem code.
138 for fs.blockedReads != 1 {
146 err = fuse.Unmount(layout.MountDir)
148 t.Logf("error unmounting: %s", err)
149 time.Sleep(time.Millisecond)
155 err = fuseConn.Close()
157 t.Fatalf("error closing fuse conn: %s", err)
161 func TestDownloadOnDemand(t *testing.T) {
162 layout, err := newGreetingLayout()
166 defer layout.Destroy()
167 seeder, err := torrent.NewClient(&torrent.Config{
168 DataDir: layout.Completed,
169 DisableTrackers: true,
173 NoDefaultBlocklist: true,
174 // Ensure that the metainfo is obtained over the wire, since we added
175 // the torrent to the seeder by magnet.
176 DisableMetainfoCache: true,
179 t.Fatalf("error creating seeder client: %s", err)
181 seeder.SetIPBlockList(nil)
183 http.HandleFunc("/seeder", func(w http.ResponseWriter, req *http.Request) {
184 seeder.WriteStatus(w)
186 _, err = seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%x", layout.Metainfo.Info.Hash))
190 leecher, err := torrent.NewClient(&torrent.Config{
191 DisableTrackers: true,
196 NoDefaultBlocklist: true,
198 TorrentDataOpener: func(info *metainfo.Info) (torrent.TorrentData, error) {
199 return mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
202 // This can be used to check if clients can connect to other clients
205 // PeerID: seeder.PeerID(),
207 leecher.SetIPBlockList(nil)
208 http.HandleFunc("/leecher", func(w http.ResponseWriter, req *http.Request) {
209 leecher.WriteStatus(w)
212 leecher.AddTorrent(layout.Metainfo)
213 var ih torrent.InfoHash
214 util.CopyExact(ih[:], layout.Metainfo.Info.Hash)
215 leecher.AddPeers(ih, []torrent.Peer{func() torrent.Peer {
216 _, port, err := net.SplitHostPort(seeder.ListenAddr().String())
220 portInt64, err := strconv.ParseInt(port, 0, 0)
226 ret, _ := net.ResolveIPAddr("ip", "localhost")
229 Port: int(portInt64),
235 node, _ := root.(fusefs.NodeStringLookuper).Lookup(context.Background(), "greeting")
236 size := int(node.Attr().Size)
237 resp := &fuse.ReadResponse{
238 Data: make([]byte, size),
240 node.(fusefs.HandleReader).Read(context.Background(), &fuse.ReadRequest{
244 if string(content) != testutil.GreetingFileContents {