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/libtorgo/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 builder := metainfo.Builder{}
31 builder.AddAnnounceGroup([]string{"lol://cheezburger"})
32 builder.SetPieceLength(5)
33 batch, err := builder.Submit()
37 errs, _ := batch.Start(w, 1)
41 // Gives a temporary directory containing the completed "greeting" torrent,
42 // and a corresponding metainfo describing it. The temporary directory can be
43 // cleaned away with os.RemoveAll.
44 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
45 tempDir, err := ioutil.TempDir(os.TempDir(), "")
49 name := CreateDummyTorrentData(tempDir)
51 CreateMetaInfo(name, w)
52 metaInfo, _ = metainfo.Load(w)