7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
12 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`
13 exampleMagnet = Magnet{
14 DisplayName: "Shit Movie (1985) 1337p - Eru",
16 "http://http.was.great!",
17 "udp://anti.piracy.honeypot:6969",
23 hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
26 // Converting from our Magnet type to URL string.
27 func TestMagnetString(t *testing.T) {
28 m, err := ParseMagnetUri(exampleMagnet.String())
29 require.NoError(t, err)
30 assert.EqualValues(t, exampleMagnet, m)
33 func TestParseMagnetURI(t *testing.T) {
38 // parsing the legit Magnet URI with btih-formatted xt should not return errors
39 uri = "magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU"
40 _, err = ParseMagnetUri(uri)
42 t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
45 // Checking if the magnet instance struct is built correctly from parsing
46 m, err = ParseMagnetUri(exampleMagnetURI)
47 assert.EqualValues(t, exampleMagnet, m)
48 assert.NoError(t, err)
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)
72 func TestMagnetize(t *testing.T) {
73 mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
74 require.NoError(t, err)
76 info, err := mi.UnmarshalInfo()
77 require.NoError(t, err)
78 m := mi.Magnet(nil, &info)
80 assert.EqualValues(t, "bootstrap.dat", m.DisplayName)
83 54, 113, 155, 162, 206, 207, 159, 59, 215, 197,
84 171, 251, 122, 136, 233, 57, 97, 27, 83, 108,
88 t.Errorf("Magnet infohash is incorrect")
92 "udp://tracker.openbittorrent.com:80",
93 "udp://tracker.openbittorrent.com:80",
94 "udp://tracker.publicbt.com:80",
95 "udp://coppersurfer.tk:6969/announce",
96 "udp://open.demonii.com:1337",
97 "http://bttracker.crunchbanglinux.org:6969/announce",
100 for _, expected := range trackers {
101 if !contains(m.Trackers, expected) {
102 t.Errorf("Magnet does not contain expected tracker: %s", expected)
107 func contains(haystack []string, needle string) bool {
108 for _, s := range haystack {