github.com/anacrolix/torrent@v1.61.0/internal/testutil/greeting.go (about) 1 // Package testutil contains stuff for testing torrent-related behaviour. 2 // 3 // "greeting" is a single-file torrent of a file called "greeting" that 4 // "contains "hello, world\n". 5 6 package testutil 7 8 import ( 9 "os" 10 "path/filepath" 11 12 "github.com/anacrolix/torrent/metainfo" 13 ) 14 15 var Greeting = Torrent{ 16 Files: []File{{ 17 Data: GreetingFileContents, 18 }}, 19 Name: GreetingFileName, 20 } 21 22 const ( 23 // A null in the middle triggers an error if SQLite stores data as text instead of blob. 24 GreetingFileContents = "hello,\x00world\n" 25 GreetingFileName = "greeting" 26 ) 27 28 func CreateDummyTorrentData(dirName string) string { 29 f, _ := os.Create(filepath.Join(dirName, "greeting")) 30 defer f.Close() 31 f.WriteString(GreetingFileContents) 32 return f.Name() 33 } 34 35 func GreetingMetaInfo() *metainfo.MetaInfo { 36 mi, _ := Greeting.Generate(5) 37 return &mi 38 } 39 40 // Gives a temporary directory containing the completed "greeting" torrent, 41 // and a corresponding metainfo describing it. The temporary directory can be 42 // cleaned away with os.RemoveAll. 43 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) { 44 tempDir, err := os.MkdirTemp(os.TempDir(), "") 45 if err != nil { 46 panic(err) 47 } 48 CreateDummyTorrentData(tempDir) 49 metaInfo = GreetingMetaInfo() 50 return 51 }