github.com/anacrolix/torrent@v1.61.0/metainfo/magnet_test.go (about) 1 package metainfo 2 3 import ( 4 "encoding/hex" 5 "testing" 6 7 "github.com/davecgh/go-spew/spew" 8 qt "github.com/go-quicktest/qt" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 var ( 14 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` 15 exampleMagnet = Magnet{ 16 DisplayName: "Shit Movie (1985) 1337p - Eru", 17 Trackers: []string{ 18 "http://http.was.great!", 19 "udp://anti.piracy.honeypot:6969", 20 }, 21 } 22 ) 23 24 func init() { 25 hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd")) 26 } 27 28 // Converting from our Magnet type to URL string. 29 func TestMagnetString(t *testing.T) { 30 m, err := ParseMagnetUri(exampleMagnet.String()) 31 require.NoError(t, err) 32 assert.EqualValues(t, exampleMagnet, m) 33 } 34 35 func TestParseMagnetURI(t *testing.T) { 36 var uri string 37 var m Magnet 38 var err error 39 40 // parsing the legit Magnet URI with btih-formatted xt should not return errors 41 uri = "magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU" 42 _, err = ParseMagnetUri(uri) 43 if err != nil { 44 t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err) 45 } 46 47 // Checking if the magnet instance struct is built correctly from parsing 48 m, err = ParseMagnetUri(exampleMagnetURI) 49 assert.EqualValues(t, exampleMagnet, m) 50 assert.NoError(t, err) 51 52 // empty string URI case 53 _, err = ParseMagnetUri("") 54 if err == nil { 55 t.Errorf("Parsing empty string as URI should have returned an error but didn't") 56 } 57 58 // only BTIH (BitTorrent info hash)-formatted magnet links are currently supported 59 // must return error correctly when encountering other URN formats 60 uri = "magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C" 61 _, err = ParseMagnetUri(uri) 62 if err == nil { 63 t.Errorf("Magnet URI with non-BTIH URNs (like \"%v\") are not supported and should return an error", uri) 64 } 65 66 // resilience to the broken hash 67 uri = "magnet:?xt=urn:btih:this hash is really broken" 68 _, err = ParseMagnetUri(uri) 69 if err == nil { 70 t.Errorf("Failed to detect broken Magnet URI: %v", uri) 71 } 72 } 73 74 func TestMagnetize(t *testing.T) { 75 mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent") 76 require.NoError(t, err) 77 78 info, err := mi.UnmarshalInfo() 79 require.NoError(t, err) 80 m := mi.Magnet(nil, &info) 81 82 assert.EqualValues(t, "bootstrap.dat", m.DisplayName) 83 84 ih := [20]byte{ 85 54, 113, 155, 162, 206, 207, 159, 59, 215, 197, 86 171, 251, 122, 136, 233, 57, 97, 27, 83, 108, 87 } 88 89 if m.InfoHash != ih { 90 t.Errorf("Magnet infohash is incorrect") 91 } 92 93 trackers := []string{ 94 "udp://tracker.openbittorrent.com:80", 95 "udp://tracker.openbittorrent.com:80", 96 "udp://tracker.publicbt.com:80", 97 "udp://coppersurfer.tk:6969/announce", 98 "udp://open.demonii.com:1337", 99 "http://bttracker.crunchbanglinux.org:6969/announce", 100 } 101 102 for _, expected := range trackers { 103 if !contains(m.Trackers, expected) { 104 t.Errorf("Magnet does not contain expected tracker: %s", expected) 105 } 106 } 107 } 108 109 func contains(haystack []string, needle string) bool { 110 for _, s := range haystack { 111 if s == needle { 112 return true 113 } 114 } 115 return false 116 } 117 118 // Check that we can parse the magnet link generated from a real-world torrent. This was added due 119 // to a regression in copyParams. 120 func TestParseSintelMagnet(t *testing.T) { 121 mi, err := LoadFromFile("../testdata/sintel.torrent") 122 qt.Assert(t, qt.IsNil(err)) 123 m := mi.Magnet(nil, nil) 124 ms := m.String() 125 t.Logf("magnet link: %q", ms) 126 m, err = ParseMagnetUri(ms) 127 qt.Check(t, qt.IsNil(err)) 128 spewCfg := spew.NewDefaultConfig() 129 spewCfg.DisableMethods = true 130 spewCfg.Dump(m) 131 m2, err := ParseMagnetV2Uri(ms) 132 spewCfg.Dump(m2) 133 qt.Check(t, qt.IsNil(err)) 134 }