]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/magnet_test.go
Fixes for wasm
[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
73 func TestMagnetize(t *testing.T) {
74         mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
75         require.NoError(t, err)
76
77         info, err := mi.UnmarshalInfo()
78         require.NoError(t, err)
79         m := mi.Magnet(nil, &info)
80
81         assert.EqualValues(t, "bootstrap.dat", m.DisplayName)
82
83         ih := [20]byte{
84                 54, 113, 155, 162, 206, 207, 159, 59, 215, 197,
85                 171, 251, 122, 136, 233, 57, 97, 27, 83, 108,
86         }
87
88         if m.InfoHash != ih {
89                 t.Errorf("Magnet infohash is incorrect")
90         }
91
92         trackers := []string{
93                 "udp://tracker.openbittorrent.com:80",
94                 "udp://tracker.openbittorrent.com:80",
95                 "udp://tracker.publicbt.com:80",
96                 "udp://coppersurfer.tk:6969/announce",
97                 "udp://open.demonii.com:1337",
98                 "http://bttracker.crunchbanglinux.org:6969/announce",
99         }
100
101         for _, expected := range trackers {
102                 if !contains(m.Trackers, expected) {
103                         t.Errorf("Magnet does not contain expected tracker: %s", expected)
104                 }
105         }
106 }
107
108 func contains(haystack []string, needle string) bool {
109         for _, s := range haystack {
110                 if s == needle {
111                         return true
112                 }
113         }
114         return false
115 }