8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
13 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`
14 exampleMagnet = Magnet{
15 DisplayName: "Shit Movie (1985) 1337p - Eru",
17 "http://http.was.great!",
18 "udp://anti.piracy.honeypot:6969",
23 // Converting from our Magnet type to URL string.
24 func TestMagnetString(t *testing.T) {
25 hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
26 s := exampleMagnet.String()
27 if s != exampleMagnetURI {
28 t.Fatalf("\nexpected:\n\t%q\nactual\n\t%q", exampleMagnetURI, s)
32 func TestParseMagnetURI(t *testing.T) {
37 // parsing the legit Magnet URI with btih-formatted xt should not return errors
38 uri = "magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU"
39 _, err = ParseMagnetURI(uri)
41 t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
44 // Checking if the magnet instance struct is built correctly from parsing
45 m, err = ParseMagnetURI(exampleMagnetURI)
46 if err != nil || !reflect.DeepEqual(exampleMagnet, m) {
47 t.Errorf("ParseMagnetURI(%s) returned %v, expected %v", uri, m, exampleMagnet)
50 // empty string URI case
51 _, err = ParseMagnetURI("")
53 t.Errorf("Parsing empty string as URI should have returned an error but didn't")
56 // only BTIH (BitTorrent info hash)-formatted magnet links are currently supported
57 // must return error correctly when encountering other URN formats
58 uri = "magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"
59 _, err = ParseMagnetURI(uri)
61 t.Errorf("Magnet URI with non-BTIH URNs (like \"%v\") are not supported and should return an error", uri)
64 // resilience to the broken hash
65 uri = "magnet:?xt=urn:btih:this hash is really broken"
66 _, err = ParseMagnetURI(uri)
68 t.Errorf("Failed to detect broken Magnet URI: %v", uri)
73 func Test_Magnetize(t *testing.T) {
74 mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
75 require.NoError(t, err)
79 assert.EqualValues(t, "bootstrap.dat", m.DisplayName)
82 54, 113, 155, 162, 206, 207, 159, 59, 215, 197,
83 171, 251, 122, 136, 233, 57, 97, 27, 83, 108,
87 t.Errorf("Magnet infohash is incorrect")
91 "udp://tracker.openbittorrent.com:80",
92 "udp://tracker.openbittorrent.com:80",
93 "udp://tracker.publicbt.com:80",
94 "udp://coppersurfer.tk:6969/announce",
95 "udp://open.demonii.com:1337",
96 "http://bttracker.crunchbanglinux.org:6969/announce",
99 for _, expected := range trackers {
100 if !contains(m.Trackers, expected) {
101 t.Errorf("Magnet does not contain expected tracker: %s", expected)
106 func contains(haystack []string, needle string) bool {
107 for _, s := range haystack {