2014-03-20 19:02:15 +08:00
|
|
|
// Package testutil contains stuff for testing torrent-related behaviour.
|
|
|
|
//
|
|
|
|
// "greeting" is a single-file torrent of a file called "greeting" that
|
|
|
|
// "contains "hello, world\n".
|
|
|
|
|
2014-03-20 13:58:09 +08:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2014-03-20 13:58:09 +08:00
|
|
|
)
|
|
|
|
|
2018-01-09 14:29:31 +08:00
|
|
|
var Greeting = Torrent{
|
|
|
|
Files: []File{{
|
|
|
|
Data: GreetingFileContents,
|
|
|
|
}},
|
|
|
|
Name: GreetingFileName,
|
|
|
|
}
|
|
|
|
|
2016-04-30 09:07:29 +08:00
|
|
|
const (
|
2020-10-30 07:42:45 +08:00
|
|
|
// A null in the middle triggers an error if SQLite stores data as text instead of blob.
|
|
|
|
GreetingFileContents = "hello,\x00world\n"
|
2016-04-30 09:07:29 +08:00
|
|
|
GreetingFileName = "greeting"
|
|
|
|
)
|
2014-03-20 13:58:09 +08:00
|
|
|
|
|
|
|
func CreateDummyTorrentData(dirName string) string {
|
|
|
|
f, _ := os.Create(filepath.Join(dirName, "greeting"))
|
2014-08-22 15:40:10 +08:00
|
|
|
defer f.Close()
|
2015-02-09 21:16:01 +08:00
|
|
|
f.WriteString(GreetingFileContents)
|
2014-03-20 13:58:09 +08:00
|
|
|
return f.Name()
|
|
|
|
}
|
2016-08-26 18:29:05 +08:00
|
|
|
|
|
|
|
func GreetingMetaInfo() *metainfo.MetaInfo {
|
2018-01-09 14:29:31 +08:00
|
|
|
return Greeting.Metainfo(5)
|
2014-03-20 13:58:09 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 19:02:15 +08:00
|
|
|
// Gives a temporary directory containing the completed "greeting" torrent,
|
|
|
|
// and a corresponding metainfo describing it. The temporary directory can be
|
|
|
|
// cleaned away with os.RemoveAll.
|
2014-03-20 13:58:09 +08:00
|
|
|
func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
|
|
|
|
tempDir, err := ioutil.TempDir(os.TempDir(), "")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-30 09:07:29 +08:00
|
|
|
CreateDummyTorrentData(tempDir)
|
|
|
|
metaInfo = GreetingMetaInfo()
|
2014-03-20 13:58:09 +08:00
|
|
|
return
|
|
|
|
}
|