5 fusefs "bazil.org/fuse/fs"
6 "bitbucket.org/anacrolix/go.torrent"
8 metainfo "github.com/nsf/libtorgo/torrent"
18 func TestTCPAddrString(t *testing.T) {
20 IP: net.IPv4(127, 0, 0, 1),
24 l, err := net.Listen("tcp4", "localhost:3000")
29 c, err := net.Dial("tcp", l.Addr().String())
34 ras := c.RemoteAddr().String()
40 const dummyFileContents = "hello, world\n"
42 func createDummyTorrentData(dirName string) string {
43 f, _ := os.Create(filepath.Join(dirName, "greeting"))
44 f.WriteString("hello, world\n")
48 func createMetaInfo(name string, w io.Writer) {
49 builder := metainfo.Builder{}
51 builder.AddAnnounceGroup([]string{"lol://cheezburger"})
52 batch, err := builder.Submit()
56 errs, _ := batch.Start(w, 1)
60 func TestDownloadOnDemand(t *testing.T) {
61 priorNumGoroutines := runtime.NumGoroutine()
63 n := runtime.NumGoroutine()
64 if n != priorNumGoroutines {
65 t.Fatalf("expected %d goroutines, but %d are running", priorNumGoroutines, n)
68 dir, err := ioutil.TempDir("", "torrentfs")
73 if err := os.RemoveAll(dir); err != nil {
77 t.Logf("test directory: %s", dir)
78 finishedDir := filepath.Join(dir, "finished")
79 os.Mkdir(finishedDir, 0777)
80 name := createDummyTorrentData(finishedDir)
81 metaInfoBuf := &bytes.Buffer{}
82 createMetaInfo(name, metaInfoBuf)
83 metaInfo, err := metainfo.Load(metaInfoBuf)
84 seeder := torrent.Client{
86 Listener: func() net.Listener {
87 conn, err := net.Listen("tcp", ":0")
94 defer seeder.Listener.Close()
97 seeder.AddTorrent(metaInfo)
98 leecher := torrent.Client{
99 DataDir: filepath.Join(dir, "download"),
103 leecher.AddTorrent(metaInfo)
104 leecher.AddPeers(torrent.BytesInfoHash(metaInfo.InfoHash), []torrent.Peer{func() torrent.Peer {
105 tcpAddr := seeder.Listener.Addr().(*net.TCPAddr)
111 mountDir := filepath.Join(dir, "mnt")
112 os.Mkdir(mountDir, 0777)
114 fuseConn, err := fuse.Mount(mountDir)
119 if err := fuse.Unmount(mountDir); err != nil {
124 if err := fusefs.Serve(fuseConn, fs); err != nil {
127 if err := fuseConn.Close(); err != nil {
132 if fuseConn.MountError != nil {
133 t.Fatal(fuseConn.MountError)
135 content, err := ioutil.ReadFile(filepath.Join(mountDir, "greeting"))
139 if string(content) != dummyFileContents {