1 // Package testutil contains stuff for testing torrent-related behaviour.
3 // "greeting" is a single-file torrent of a file called "greeting" that
4 // "contains "hello, world\n".
17 "github.com/anacrolix/missinggo"
19 "github.com/anacrolix/torrent/metainfo"
22 const GreetingFileContents = "hello, world\n"
24 func CreateDummyTorrentData(dirName string) string {
25 f, _ := os.Create(filepath.Join(dirName, "greeting"))
27 f.WriteString(GreetingFileContents)
31 // Writes to w, a metainfo containing the file at name.
32 func CreateMetaInfo(name string, w io.Writer) {
33 var mi metainfo.MetaInfo
34 mi.Info.Name = filepath.Base(name)
35 fi, _ := os.Stat(name)
36 mi.Info.Length = fi.Size()
37 mi.Announce = "lol://cheezburger"
38 mi.Info.PieceLength = 5
39 err := mi.Info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) {
51 // Gives a temporary directory containing the completed "greeting" torrent,
52 // and a corresponding metainfo describing it. The temporary directory can be
53 // cleaned away with os.RemoveAll.
54 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
55 tempDir, err := ioutil.TempDir(os.TempDir(), "")
59 name := CreateDummyTorrentData(tempDir)
61 CreateMetaInfo(name, w)
62 metaInfo, _ = metainfo.Load(w)
66 type StatusWriter interface {
67 WriteStatus(io.Writer)
70 func ExportStatusWriter(sw StatusWriter, path string) {
72 fmt.Sprintf("/%s/%s", missinggo.GetTestName(), path),
73 func(w http.ResponseWriter, r *http.Request) {