]> Sergey Matveev's repositories - btrtrc.git/blob - fs/torrentfs_test.go
Various progress, particularly around the way data readiness is handled
[btrtrc.git] / fs / torrentfs_test.go
1 package torrentfs
2
3 import (
4         "bazil.org/fuse"
5         fusefs "bazil.org/fuse/fs"
6         "bitbucket.org/anacrolix/go.torrent"
7         "bytes"
8         metainfo "github.com/nsf/libtorgo/torrent"
9         "io"
10         "io/ioutil"
11         "net"
12         "os"
13         "path/filepath"
14         "runtime"
15         "testing"
16 )
17
18 func TestTCPAddrString(t *testing.T) {
19         ta := &net.TCPAddr{
20                 IP:   net.IPv4(127, 0, 0, 1),
21                 Port: 3000,
22         }
23         s := ta.String()
24         l, err := net.Listen("tcp4", "localhost:3000")
25         if err != nil {
26                 t.Fatal(err)
27         }
28         defer l.Close()
29         c, err := net.Dial("tcp", l.Addr().String())
30         if err != nil {
31                 t.Fatal(err)
32         }
33         defer c.Close()
34         ras := c.RemoteAddr().String()
35         if ras != s {
36                 t.FailNow()
37         }
38 }
39
40 const dummyFileContents = "hello, world\n"
41
42 func createDummyTorrentData(dirName string) string {
43         f, _ := os.Create(filepath.Join(dirName, "greeting"))
44         f.WriteString("hello, world\n")
45         return f.Name()
46 }
47
48 func createMetaInfo(name string, w io.Writer) {
49         builder := metainfo.Builder{}
50         builder.AddFile(name)
51         builder.AddAnnounceGroup([]string{"lol://cheezburger"})
52         batch, err := builder.Submit()
53         if err != nil {
54                 panic(err)
55         }
56         errs, _ := batch.Start(w, 1)
57         <-errs
58 }
59
60 func TestDownloadOnDemand(t *testing.T) {
61         priorNumGoroutines := runtime.NumGoroutine()
62         defer func() {
63                 n := runtime.NumGoroutine()
64                 if n != priorNumGoroutines {
65                         t.Fatalf("expected %d goroutines, but %d are running", priorNumGoroutines, n)
66                 }
67         }()
68         dir, err := ioutil.TempDir("", "torrentfs")
69         if err != nil {
70                 t.Fatal(err)
71         }
72         defer func() {
73                 if err := os.RemoveAll(dir); err != nil {
74                         t.Error(err)
75                 }
76         }()
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{
85                 DataDir: finishedDir,
86                 Listener: func() net.Listener {
87                         conn, err := net.Listen("tcp", ":0")
88                         if err != nil {
89                                 panic(err)
90                         }
91                         return conn
92                 }(),
93         }
94         defer seeder.Listener.Close()
95         seeder.Start()
96         defer seeder.Stop()
97         seeder.AddTorrent(metaInfo)
98         leecher := torrent.Client{
99                 DataDir: filepath.Join(dir, "download"),
100         }
101         leecher.Start()
102         defer leecher.Stop()
103         leecher.AddTorrent(metaInfo)
104         leecher.AddPeers(torrent.BytesInfoHash(metaInfo.InfoHash), []torrent.Peer{func() torrent.Peer {
105                 tcpAddr := seeder.Listener.Addr().(*net.TCPAddr)
106                 return torrent.Peer{
107                         IP:   tcpAddr.IP,
108                         Port: tcpAddr.Port,
109                 }
110         }()})
111         mountDir := filepath.Join(dir, "mnt")
112         os.Mkdir(mountDir, 0777)
113         fs := New(&leecher)
114         fuseConn, err := fuse.Mount(mountDir)
115         if err != nil {
116                 t.Fatal(err)
117         }
118         defer func() {
119                 if err := fuse.Unmount(mountDir); err != nil {
120                         t.Fatal(err)
121                 }
122         }()
123         go func() {
124                 if err := fusefs.Serve(fuseConn, fs); err != nil {
125                         t.Fatal(err)
126                 }
127                 if err := fuseConn.Close(); err != nil {
128                         t.Fatal(err)
129                 }
130         }()
131         <-fuseConn.Ready
132         if fuseConn.MountError != nil {
133                 t.Fatal(fuseConn.MountError)
134         }
135         content, err := ioutil.ReadFile(filepath.Join(mountDir, "greeting"))
136         if err != nil {
137                 t.Fatal(err)
138         }
139         if string(content) != dummyFileContents {
140                 t.FailNow()
141         }
142 }