]> Sergey Matveev's repositories - btrtrc.git/blob - internal/testutil/greeting.go
954ad9130d55fa644f02c5e946dfc5a249b48d5e
[btrtrc.git] / internal / testutil / greeting.go
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         "io/ioutil"
10         "os"
11         "path/filepath"
12
13         "github.com/anacrolix/torrent/metainfo"
14 )
15
16 var Greeting = Torrent{
17         Files: []File{{
18                 Data: GreetingFileContents,
19         }},
20         Name: GreetingFileName,
21 }
22
23 const (
24         // A null in the middle triggers an error if SQLite stores data as text instead of blob.
25         GreetingFileContents = "hello,\x00world\n"
26         GreetingFileName     = "greeting"
27 )
28
29 func CreateDummyTorrentData(dirName string) string {
30         f, _ := os.Create(filepath.Join(dirName, "greeting"))
31         defer f.Close()
32         f.WriteString(GreetingFileContents)
33         return f.Name()
34 }
35
36 func GreetingMetaInfo() *metainfo.MetaInfo {
37         return Greeting.Metainfo(5)
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 := ioutil.TempDir(os.TempDir(), "")
45         if err != nil {
46                 panic(err)
47         }
48         CreateDummyTorrentData(tempDir)
49         metaInfo = GreetingMetaInfo()
50         return
51 }