2014-03-20 22:02:15 +11: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 16:58:09 +11:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2016-02-22 02:36:41 +11:00
|
|
|
"fmt"
|
2014-03-20 16:58:09 +11:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-02-22 02:36:41 +11:00
|
|
|
"net/http"
|
2014-03-20 16:58:09 +11:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-30 11:07:29 +10:00
|
|
|
"strings"
|
2014-03-20 16:58:09 +11:00
|
|
|
|
2016-02-22 02:36:41 +11:00
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
|
2016-08-26 20:29:05 +10:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-04-27 14:55:01 +10:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2014-03-20 16:58:09 +11:00
|
|
|
)
|
|
|
|
|
2016-04-30 11:07:29 +10:00
|
|
|
const (
|
|
|
|
GreetingFileContents = "hello, world\n"
|
|
|
|
GreetingFileName = "greeting"
|
|
|
|
)
|
2014-03-20 16:58:09 +11:00
|
|
|
|
|
|
|
func CreateDummyTorrentData(dirName string) string {
|
|
|
|
f, _ := os.Create(filepath.Join(dirName, "greeting"))
|
2014-08-22 17:40:10 +10:00
|
|
|
defer f.Close()
|
2015-02-10 00:16:01 +11:00
|
|
|
f.WriteString(GreetingFileContents)
|
2014-03-20 16:58:09 +11:00
|
|
|
return f.Name()
|
|
|
|
}
|
2016-08-26 20:29:05 +10:00
|
|
|
|
|
|
|
func GreetingMetaInfo() *metainfo.MetaInfo {
|
|
|
|
info := metainfo.Info{
|
|
|
|
Name: GreetingFileName,
|
|
|
|
Length: int64(len(GreetingFileContents)),
|
|
|
|
PieceLength: 5,
|
|
|
|
}
|
|
|
|
err := info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) {
|
2016-04-30 11:07:29 +10:00
|
|
|
return ioutil.NopCloser(strings.NewReader(GreetingFileContents)), nil
|
2015-10-30 01:21:09 +11:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-08-26 20:29:05 +10:00
|
|
|
mi := &metainfo.MetaInfo{}
|
|
|
|
mi.InfoBytes, err = bencode.Marshal(info)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return mi
|
2014-03-20 16:58:09 +11:00
|
|
|
}
|
|
|
|
|
2014-03-20 22:02:15 +11: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 16:58:09 +11:00
|
|
|
func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
|
|
|
|
tempDir, err := ioutil.TempDir(os.TempDir(), "")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-30 11:07:29 +10:00
|
|
|
CreateDummyTorrentData(tempDir)
|
|
|
|
metaInfo = GreetingMetaInfo()
|
2014-03-20 16:58:09 +11:00
|
|
|
return
|
|
|
|
}
|
2016-02-22 02:36:41 +11:00
|
|
|
|
|
|
|
type StatusWriter interface {
|
|
|
|
WriteStatus(io.Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExportStatusWriter(sw StatusWriter, path string) {
|
|
|
|
http.HandleFunc(
|
|
|
|
fmt.Sprintf("/%s/%s", missinggo.GetTestName(), path),
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sw.WriteStatus(w)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|