Begin restructuring testutil to support testing torrents with various properties

This commit is contained in:
Matt Joiner 2018-01-09 17:29:31 +11:00
parent 659be3d366
commit 6239a83bd6
3 changed files with 90 additions and 37 deletions

View File

@ -6,20 +6,20 @@
package testutil package testutil
import ( import (
"fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
) )
var Greeting = Torrent{
Files: []File{{
Data: GreetingFileContents,
}},
Name: GreetingFileName,
}
const ( const (
GreetingFileContents = "hello, world\n" GreetingFileContents = "hello, world\n"
GreetingFileName = "greeting" GreetingFileName = "greeting"
@ -33,23 +33,7 @@ func CreateDummyTorrentData(dirName string) string {
} }
func GreetingMetaInfo() *metainfo.MetaInfo { func GreetingMetaInfo() *metainfo.MetaInfo {
info := metainfo.Info{ return Greeting.Metainfo(5)
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, // Gives a temporary directory containing the completed "greeting" torrent,
@ -64,16 +48,3 @@ func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
metaInfo = GreetingMetaInfo() metaInfo = GreetingMetaInfo()
return 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)
},
)
}

60
internal/testutil/spec.go Normal file
View File

@ -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
}

View File

@ -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)
},
)
}