2016-04-04 11:48:39 +08:00
|
|
|
package metainfo
|
2015-02-25 11:50:01 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
2016-04-03 21:51:46 +08:00
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-02-25 11:50:01 +08:00
|
|
|
)
|
|
|
|
|
2015-03-23 01:42:43 +08:00
|
|
|
var (
|
|
|
|
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`
|
|
|
|
exampleMagnet = Magnet{
|
2015-02-25 11:50:01 +08:00
|
|
|
DisplayName: "Shit Movie (1985) 1337p - Eru",
|
|
|
|
Trackers: []string{
|
|
|
|
"http://http.was.great!",
|
|
|
|
"udp://anti.piracy.honeypot:6969",
|
|
|
|
},
|
|
|
|
}
|
2015-03-23 01:42:43 +08:00
|
|
|
)
|
|
|
|
|
2019-09-24 13:51:08 +08:00
|
|
|
func init() {
|
|
|
|
hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
|
|
|
|
}
|
|
|
|
|
2015-03-23 01:42:43 +08:00
|
|
|
// Converting from our Magnet type to URL string.
|
|
|
|
func TestMagnetString(t *testing.T) {
|
2020-11-09 07:56:27 +08:00
|
|
|
m, err := ParseMagnetUri(exampleMagnet.String())
|
2019-09-24 13:51:08 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, exampleMagnet, m)
|
2015-03-23 01:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2020-11-09 07:56:27 +08:00
|
|
|
_, err = ParseMagnetUri(uri)
|
2015-03-23 01:42:43 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
|
2015-02-25 11:50:01 +08:00
|
|
|
}
|
2015-03-23 01:42:43 +08:00
|
|
|
|
|
|
|
// Checking if the magnet instance struct is built correctly from parsing
|
2020-11-09 07:56:27 +08:00
|
|
|
m, err = ParseMagnetUri(exampleMagnetURI)
|
2019-09-24 13:51:08 +08:00
|
|
|
assert.EqualValues(t, exampleMagnet, m)
|
2020-02-20 08:09:57 +08:00
|
|
|
assert.NoError(t, err)
|
2015-03-23 01:42:43 +08:00
|
|
|
|
|
|
|
// empty string URI case
|
2020-11-09 07:56:27 +08:00
|
|
|
_, err = ParseMagnetUri("")
|
2015-03-23 01:42:43 +08:00
|
|
|
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"
|
2020-11-09 07:56:27 +08:00
|
|
|
_, err = ParseMagnetUri(uri)
|
2015-03-23 01:42:43 +08:00
|
|
|
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"
|
2020-11-09 07:56:27 +08:00
|
|
|
_, err = ParseMagnetUri(uri)
|
2015-03-23 01:42:43 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed to detect broken Magnet URI: %v", uri)
|
|
|
|
}
|
|
|
|
|
2015-02-25 11:50:01 +08:00
|
|
|
}
|
2016-04-03 21:51:46 +08:00
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
func TestMagnetize(t *testing.T) {
|
2016-04-04 11:48:39 +08:00
|
|
|
mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
|
|
|
|
require.NoError(t, err)
|
2016-04-03 21:51:46 +08:00
|
|
|
|
2016-09-20 10:25:40 +08:00
|
|
|
info, err := mi.UnmarshalInfo()
|
|
|
|
require.NoError(t, err)
|
|
|
|
m := mi.Magnet(info.Name, mi.HashInfoBytes())
|
2016-04-03 21:51:46 +08:00
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
assert.EqualValues(t, "bootstrap.dat", m.DisplayName)
|
2016-04-03 21:51:46 +08:00
|
|
|
|
|
|
|
ih := [20]byte{
|
|
|
|
54, 113, 155, 162, 206, 207, 159, 59, 215, 197,
|
|
|
|
171, 251, 122, 136, 233, 57, 97, 27, 83, 108,
|
|
|
|
}
|
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
if m.InfoHash != ih {
|
2016-04-03 21:51:46 +08:00
|
|
|
t.Errorf("Magnet infohash is incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
trackers := []string{
|
|
|
|
"udp://tracker.openbittorrent.com:80",
|
|
|
|
"udp://tracker.openbittorrent.com:80",
|
|
|
|
"udp://tracker.publicbt.com:80",
|
|
|
|
"udp://coppersurfer.tk:6969/announce",
|
|
|
|
"udp://open.demonii.com:1337",
|
|
|
|
"http://bttracker.crunchbanglinux.org:6969/announce",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, expected := range trackers {
|
2016-04-04 11:48:39 +08:00
|
|
|
if !contains(m.Trackers, expected) {
|
2016-04-03 21:51:46 +08:00
|
|
|
t.Errorf("Magnet does not contain expected tracker: %s", expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(haystack []string, needle string) bool {
|
|
|
|
for _, s := range haystack {
|
|
|
|
if s == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|