internal/testutil/greeting.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ internal/testutil/spec.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ internal/testutil/status_writer.go | 22 ++++++++++++++++++++++ internal/testutil/testutil.go | 79 ----------------------------------------------------- diff --git a/internal/testutil/greeting.go b/internal/testutil/greeting.go new file mode 100644 index 0000000000000000000000000000000000000000..4523a9c20371fa6745385d6894640e30d06fb9fb --- /dev/null +++ b/internal/testutil/greeting.go @@ -0,0 +1,50 @@ +// 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". + +package testutil + +import ( + "io/ioutil" + "os" + "path/filepath" + + "github.com/anacrolix/torrent/metainfo" +) + +var Greeting = Torrent{ + Files: []File{{ + Data: GreetingFileContents, + }}, + Name: GreetingFileName, +} + +const ( + GreetingFileContents = "hello, world\n" + GreetingFileName = "greeting" +) + +func CreateDummyTorrentData(dirName string) string { + f, _ := os.Create(filepath.Join(dirName, "greeting")) + defer f.Close() + f.WriteString(GreetingFileContents) + return f.Name() +} + +func GreetingMetaInfo() *metainfo.MetaInfo { + return Greeting.Metainfo(5) +} + +// 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. +func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) { + tempDir, err := ioutil.TempDir(os.TempDir(), "") + if err != nil { + panic(err) + } + CreateDummyTorrentData(tempDir) + metaInfo = GreetingMetaInfo() + return +} diff --git a/internal/testutil/spec.go b/internal/testutil/spec.go new file mode 100644 index 0000000000000000000000000000000000000000..c3e5cd8821338d87b643770c6f8f663385b46f55 --- /dev/null +++ b/internal/testutil/spec.go @@ -0,0 +1,60 @@ +package testutil + +import ( + "io" + "io/ioutil" + "strings" + + "github.com/anacrolix/missinggo/assert" + "github.com/anacrolix/torrent/bencode" + "github.com/anacrolix/torrent/metainfo" +) + +type File struct { + Name string + Data string +} + +type Torrent struct { + Files []File + Name string +} + +func (t *Torrent) IsDir() bool { + return len(t.Files) == 1 && t.Files[0].Name == "" +} + +func (t *Torrent) GetFile(name string) *File { + if t.IsDir() && t.Name == name { + return &t.Files[0] + } + for _, f := range t.Files { + if f.Name == name { + return &f + } + } + return nil +} + +func (t *Torrent) Info(pieceLength int64) metainfo.Info { + info := metainfo.Info{ + Name: t.Name, + PieceLength: pieceLength, + } + if t.IsDir() { + info.Length = int64(len(t.Files[0].Data)) + } + err := info.GeneratePieces(func(fi metainfo.FileInfo) (io.ReadCloser, error) { + return ioutil.NopCloser(strings.NewReader(t.GetFile(strings.Join(fi.Path, "/")).Data)), nil + }) + assert.Nil(err) + return info +} + +func (t *Torrent) Metainfo(pieceLength int64) *metainfo.MetaInfo { + mi := metainfo.MetaInfo{} + var err error + mi.InfoBytes, err = bencode.Marshal(t.Info(pieceLength)) + assert.Nil(err) + return &mi +} diff --git a/internal/testutil/status_writer.go b/internal/testutil/status_writer.go new file mode 100644 index 0000000000000000000000000000000000000000..06e2f66286487d26b03a033c743c607d006c682f --- /dev/null +++ b/internal/testutil/status_writer.go @@ -0,0 +1,22 @@ +package testutil + +import ( + "fmt" + "io" + "net/http" + + "github.com/anacrolix/missinggo" +) + +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) + }, + ) +} diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go deleted file mode 100644 index 6431a1f187a6f6cab6a867732576136bdda29004..0000000000000000000000000000000000000000 --- a/internal/testutil/testutil.go +++ /dev/null @@ -1,79 +0,0 @@ -// 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". - -package testutil - -import ( - "fmt" - "io" - "io/ioutil" - "net/http" - "os" - "path/filepath" - "strings" - - "github.com/anacrolix/missinggo" - - "github.com/anacrolix/torrent/bencode" - "github.com/anacrolix/torrent/metainfo" -) - -const ( - GreetingFileContents = "hello, world\n" - GreetingFileName = "greeting" -) - -func CreateDummyTorrentData(dirName string) string { - f, _ := os.Create(filepath.Join(dirName, "greeting")) - defer f.Close() - f.WriteString(GreetingFileContents) - return f.Name() -} - -func GreetingMetaInfo() *metainfo.MetaInfo { - info := metainfo.Info{ - Name: GreetingFileName, - Length: int64(len(GreetingFileContents)), - PieceLength: 5, - } - err := info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) { - return ioutil.NopCloser(strings.NewReader(GreetingFileContents)), nil - }) - if err != nil { - panic(err) - } - mi := &metainfo.MetaInfo{} - mi.InfoBytes, err = bencode.Marshal(info) - if err != nil { - panic(err) - } - return mi -} - -// 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. -func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) { - tempDir, err := ioutil.TempDir(os.TempDir(), "") - if err != nil { - panic(err) - } - CreateDummyTorrentData(tempDir) - metaInfo = GreetingMetaInfo() - return -} - -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) - }, - ) -}