Merge pull request #4 from dborzov/master

Add tests for ParseMagnetURI
This commit is contained in:
Matt Joiner 2015-03-23 12:13:49 +11:00
commit ecda836926
2 changed files with 68 additions and 21 deletions

View File

@ -30,6 +30,7 @@ func (m *Magnet) String() (ret string) {
return return
} }
// ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance
func ParseMagnetURI(uri string) (m Magnet, err error) { func ParseMagnetURI(uri string) (m Magnet, err error) {
u, err := url.Parse(uri) u, err := url.Parse(uri)
if err != nil { if err != nil {
@ -45,22 +46,23 @@ func ParseMagnetURI(uri string) (m Magnet, err error) {
err = fmt.Errorf("bad xt parameter") err = fmt.Errorf("bad xt parameter")
return return
} }
xt = xt[len(xtPrefix):] infoHash := xt[len(xtPrefix):]
decode := func() func(dst, src []byte) (int, error) {
switch len(xt) { // BTIH hash can be in HEX or BASE32 encoding
case 40: // will assign apropriate func judging from symbol length
return hex.Decode var decode func(dst, src []byte) (int, error)
case 32: switch len(infoHash) {
return base32.StdEncoding.Decode case 40:
default: decode = hex.Decode
return nil case 32:
} decode = base32.StdEncoding.Decode
}() }
if decode == nil { if decode == nil {
err = fmt.Errorf("unhandled xt parameter encoding: encoded length %d", len(xt)) err = fmt.Errorf("unhandled xt parameter encoding: encoded length %d", len(infoHash))
return return
} }
n, err := decode(m.InfoHash[:], []byte(xt)) n, err := decode(m.InfoHash[:], []byte(infoHash))
if err != nil { if err != nil {
err = fmt.Errorf("error decoding xt: %s", err) err = fmt.Errorf("error decoding xt: %s", err)
return return

View File

@ -2,22 +2,67 @@ package torrent
import ( import (
"encoding/hex" "encoding/hex"
"reflect"
"testing" "testing"
) )
// Converting from our Magnet type to URL string. var (
func TestMagnetString(t *testing.T) { exampleMagnetURI = `magnet:?xt=urn:btih:51340689c960f0778a4387aef9b4b52fd08390cd&dn=Shit+Movie+%281985%29+1337p+-+Eru&tr=http%3A%2F%2Fhttp.was.great%21&tr=udp%3A%2F%2Fanti.piracy.honeypot%3A6969`
m := Magnet{ exampleMagnet = Magnet{
DisplayName: "Shit Movie (1985) 1337p - Eru", DisplayName: "Shit Movie (1985) 1337p - Eru",
Trackers: []string{ Trackers: []string{
"http://http.was.great!", "http://http.was.great!",
"udp://anti.piracy.honeypot:6969", "udp://anti.piracy.honeypot:6969",
}, },
} }
hex.Decode(m.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd")) )
s := m.String()
const e = `magnet:?xt=urn:btih:51340689c960f0778a4387aef9b4b52fd08390cd&dn=Shit+Movie+%281985%29+1337p+-+Eru&tr=http%3A%2F%2Fhttp.was.great%21&tr=udp%3A%2F%2Fanti.piracy.honeypot%3A6969` // Converting from our Magnet type to URL string.
if s != e { func TestMagnetString(t *testing.T) {
t.Fatalf("\nexpected:\n\t%q\nactual\n\t%q", e, s) hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
s := exampleMagnet.String()
if s != exampleMagnetURI {
t.Fatalf("\nexpected:\n\t%q\nactual\n\t%q", exampleMagnetURI, s)
} }
} }
func TestParseMagnetURI(t *testing.T) {
var uri string
var m Magnet
var err error
// parsing the legit Magnet URI with btih-formatted xt should not return errors
uri = "magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU"
_, err = ParseMagnetURI(uri)
if err != nil {
t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
}
// Checking if the magnet instance struct is built correctly from parsing
m, err = ParseMagnetURI(exampleMagnetURI)
if err != nil || !reflect.DeepEqual(exampleMagnet, m) {
t.Errorf("ParseMagnetURI(%e) returned %v, expected %v", uri, m, exampleMagnet)
}
// empty string URI case
_, err = ParseMagnetURI("")
if err == nil {
t.Errorf("Parsing empty string as URI should have returned an error but didn't")
}
// only BTIH (BitTorrent info hash)-formatted magnet links are currently supported
// must return error correctly when encountering other URN formats
uri = "magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"
_, err = ParseMagnetURI(uri)
if err == nil {
t.Errorf("Magnet URI with non-BTIH URNs (like \"%v\") are not supported and should return an error", uri)
}
// resilience to the broken hash
uri = "magnet:?xt=urn:btih:this hash is really broken"
_, err = ParseMagnetURI(uri)
if err == nil {
t.Errorf("Failed to detect broken Magnet URI: %v", uri)
}
}