]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/magnet_test.go
Drop support for go 1.20
[btrtrc.git] / metainfo / magnet_test.go
1 package metainfo
2
3 import (
4         "encoding/hex"
5         "testing"
6
7         "github.com/stretchr/testify/assert"
8         "github.com/stretchr/testify/require"
9 )
10
11 var (
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",
15                 Trackers: []string{
16                         "http://http.was.great!",
17                         "udp://anti.piracy.honeypot:6969",
18                 },
19         }
20 )
21
22 func init() {
23         hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
24 }
25
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)
31 }
32
33 func TestParseMagnetURI(t *testing.T) {
34         var uri string
35         var m Magnet
36         var err error
37
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)
41         if err != nil {
42                 t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
43         }
44
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)
49
50         // empty string URI case
51         _, err = ParseMagnetUri("")
52         if err == nil {
53                 t.Errorf("Parsing empty string as URI should have returned an error but didn't")
54         }
55
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)
60         if err == nil {
61                 t.Errorf("Magnet URI with non-BTIH URNs (like \"%v\") are not supported and should return an error", uri)
62         }
63
64         // resilience to the broken hash
65         uri = "magnet:?xt=urn:btih:this hash is really broken"
66         _, err = ParseMagnetUri(uri)
67         if err == nil {
68                 t.Errorf("Failed to detect broken Magnet URI: %v", uri)
69         }
70 }
71
72 func TestMagnetize(t *testing.T) {
73         mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
74         require.NoError(t, err)
75
76         info, err := mi.UnmarshalInfo()
77         require.NoError(t, err)
78         m := mi.Magnet(nil, &info)
79
80         assert.EqualValues(t, "bootstrap.dat", m.DisplayName)
81
82         ih := [20]byte{
83                 54, 113, 155, 162, 206, 207, 159, 59, 215, 197,
84                 171, 251, 122, 136, 233, 57, 97, 27, 83, 108,
85         }
86
87         if m.InfoHash != ih {
88                 t.Errorf("Magnet infohash is incorrect")
89         }
90
91         trackers := []string{
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",
98         }
99
100         for _, expected := range trackers {
101                 if !contains(m.Trackers, expected) {
102                         t.Errorf("Magnet does not contain expected tracker: %s", expected)
103                 }
104         }
105 }
106
107 func contains(haystack []string, needle string) bool {
108         for _, s := range haystack {
109                 if s == needle {
110                         return true
111                 }
112         }
113         return false
114 }