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".
18 "github.com/anacrolix/missinggo"
20 "github.com/anacrolix/torrent/bencode"
21 "github.com/anacrolix/torrent/metainfo"
25 GreetingFileContents = "hello, world\n"
26 GreetingFileName = "greeting"
29 func CreateDummyTorrentData(dirName string) string {
30 f, _ := os.Create(filepath.Join(dirName, "greeting"))
32 f.WriteString(GreetingFileContents)
35 func GreetingMetaInfo() (mi *metainfo.MetaInfo) {
36 mi = new(metainfo.MetaInfo)
37 mi.Info.Name = GreetingFileName
38 mi.Info.Length = int64(len(GreetingFileContents))
39 mi.Announce = "lol://cheezburger"
40 mi.Info.PieceLength = 5
41 err := mi.Info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) {
42 return ioutil.NopCloser(strings.NewReader(GreetingFileContents)), nil
47 mi.Info.Bytes, _ = bencode.Marshal(&mi.Info.Info)
49 h.Write(mi.Info.Bytes)
50 missinggo.CopyExact(&mi.Info.Hash, h.Sum(nil))
54 // Gives a temporary directory containing the completed "greeting" torrent,
55 // and a corresponding metainfo describing it. The temporary directory can be
56 // cleaned away with os.RemoveAll.
57 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
58 tempDir, err := ioutil.TempDir(os.TempDir(), "")
62 CreateDummyTorrentData(tempDir)
63 metaInfo = GreetingMetaInfo()
67 type StatusWriter interface {
68 WriteStatus(io.Writer)
71 func ExportStatusWriter(sw StatusWriter, path string) {
73 fmt.Sprintf("/%s/%s", missinggo.GetTestName(), path),
74 func(w http.ResponseWriter, r *http.Request) {