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/bencode"
20 "github.com/anacrolix/torrent/metainfo"
24 GreetingFileContents = "hello, world\n"
25 GreetingFileName = "greeting"
28 func CreateDummyTorrentData(dirName string) string {
29 f, _ := os.Create(filepath.Join(dirName, "greeting"))
31 f.WriteString(GreetingFileContents)
35 func GreetingMetaInfo() *metainfo.MetaInfo {
36 info := metainfo.Info{
37 Name: GreetingFileName,
38 Length: int64(len(GreetingFileContents)),
41 err := info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) {
42 return ioutil.NopCloser(strings.NewReader(GreetingFileContents)), nil
47 mi := &metainfo.MetaInfo{}
48 mi.InfoBytes, err = bencode.Marshal(info)
55 // Gives a temporary directory containing the completed "greeting" torrent,
56 // and a corresponding metainfo describing it. The temporary directory can be
57 // cleaned away with os.RemoveAll.
58 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
59 tempDir, err := ioutil.TempDir(os.TempDir(), "")
63 CreateDummyTorrentData(tempDir)
64 metaInfo = GreetingMetaInfo()
68 type StatusWriter interface {
69 WriteStatus(io.Writer)
72 func ExportStatusWriter(sw StatusWriter, path string) {
74 fmt.Sprintf("/%s/%s", missinggo.GetTestName(), path),
75 func(w http.ResponseWriter, r *http.Request) {