torrent: comments and starting more tests

This commit is contained in:
Vincent Batts 2016-07-07 10:55:50 -04:00
parent 7f39bac612
commit c9343672f0
3 changed files with 38 additions and 9 deletions

View file

@ -5,30 +5,31 @@ import (
"time" "time"
) )
// https://wiki.theory.org/BitTorrentSpecification#Metainfo_File_Structure // File is a representation of https://wiki.theory.org/BitTorrentSpecification#Metainfo_File_Structure
type File struct { type File struct {
// URL of a main tracker // Announce is the URL of a main tracker
Announce string `bencode:"announce"` Announce string `bencode:"announce"`
// List of additional trackers // AnnounceList lists additional trackers
AnnounceList []string `bencode:"announce-list"` AnnounceList []string `bencode:"announce-list"`
// Epoch of the creation of this torrent // CreationDate is epoch of the creation of this torrent
CreationDate int64 `bencode:"creation date,omitempty"` CreationDate int64 `bencode:"creation date,omitempty"`
// Dictionary about this torrent, including files to be tracked // Info is the dictionary about this torrent, including files to be tracked
Info InfoSection `bencode:"info,omitempty"` Info InfoSection `bencode:"info,omitempty"`
// free-form textual comments of the author // Comment is free-form textual comments of the author
Comment string `bencode:"comment,omitempty"` Comment string `bencode:"comment,omitempty"`
// name and version of the program used to create the .torrent // CreatedBy is the name and version of the program used to create the .torrent
CreatedBy string `bencode:"created by,omitempty"` CreatedBy string `bencode:"created by,omitempty"`
// string encoding used to generate the `pieces` and `info` fields // Encoding string encoding used to generate the `pieces` and `info` fields
Encoding string `bencode:"encoding"` Encoding string `bencode:"encoding"`
} }
// CreationDateTime returns a time from the File's CreationDate
func (f File) CreationDateTime() time.Time { func (f File) CreationDateTime() time.Time {
return time.Unix(f.CreationDate, 0) return time.Unix(f.CreationDate, 0)
} }
@ -84,6 +85,8 @@ var (
ErrNotProperDataInterface = torrentError{"data does not look like map[string]interface{}"} ErrNotProperDataInterface = torrentError{"data does not look like map[string]interface{}"}
) )
// DecocdeTorrentData translates a bencode decoded message from a *.torrent file.
// Assuming a generic map[string]interface{} data.
func DecocdeTorrentData(data interface{}) (*File, error) { func DecocdeTorrentData(data interface{}) (*File, error) {
m, ok := data.(map[string]interface{}) m, ok := data.(map[string]interface{})
if !ok { if !ok {

View file

@ -2,10 +2,34 @@ package torrent
import ( import (
"bytes" "bytes"
"github.com/vbatts/go-bt/bencode" "fmt"
"io/ioutil"
"testing" "testing"
"github.com/vbatts/go-bt/bencode"
) )
func TestDecode(t *testing.T) {
data := new(map[string]interface{})
tData, err := ioutil.ReadFile("./testdata/farts.torrent")
if err != nil {
t.Fatal(err)
}
// currently failing in ./bencode/struct.go:134
if err := bencode.Unmarshal(tData, &data); err != nil {
t.Error(err)
}
f, err := DecocdeTorrentData(data)
if err != nil {
t.Error(err)
}
fmt.Printf("%#v\n", f)
}
func TestFileMarshal(t *testing.T) { func TestFileMarshal(t *testing.T) {
f1 := File{ f1 := File{
Announce: "http://foo.bar.com:9090/announce", Announce: "http://foo.bar.com:9090/announce",

2
torrent/testdata/farts.torrent vendored Normal file
View file

@ -0,0 +1,2 @@
d8:announce41:http://bananaboat.usersys.redhat.com:90907:comment12:farts, y'all10:created by25:Transmission/2.84 (14307)13:creation datei1467900546e8:encoding5:UTF-84:infod5:filesld6:lengthi10e4:pathl10:README.txteee4:name5:farts12:piece lengthi32768e6:pieces20:_¦n"gEU¾l
Ø L97§¸Ž87:privatei0eee