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".
15 "github.com/anacrolix/torrent/metainfo"
18 const GreetingFileContents = "hello, world\n"
20 func CreateDummyTorrentData(dirName string) string {
21 f, _ := os.Create(filepath.Join(dirName, "greeting"))
23 f.WriteString(GreetingFileContents)
27 // Writes to w, a metainfo containing the file at name.
28 func CreateMetaInfo(name string, w io.Writer) {
29 var mi metainfo.MetaInfo
30 mi.Info.Name = filepath.Base(name)
31 fi, _ := os.Stat(name)
32 mi.Info.Length = fi.Size()
33 mi.Announce = "lol://cheezburger"
34 mi.Info.PieceLength = 5
35 err := mi.Info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) {
47 // Gives a temporary directory containing the completed "greeting" torrent,
48 // and a corresponding metainfo describing it. The temporary directory can be
49 // cleaned away with os.RemoveAll.
50 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
51 tempDir, err := ioutil.TempDir(os.TempDir(), "")
55 name := CreateDummyTorrentData(tempDir)
57 CreateMetaInfo(name, w)
58 metaInfo, _ = metainfo.Load(w)