]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/magnet_test.go
metainfo: Improve MetaInfo.AnnounceList handling
[btrtrc.git] / metainfo / magnet_test.go
1 package metainfo
2
3 import (
4         "encoding/hex"
5         "reflect"
6         "testing"
7
8         "github.com/stretchr/testify/assert"
9         "github.com/stretchr/testify/require"
10 )
11
12 var (
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",
16                 Trackers: []string{
17                         "http://http.was.great!",
18                         "udp://anti.piracy.honeypot:6969",
19                 },
20         }
21 )
22
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)
29         }
30 }
31
32 func TestParseMagnetURI(t *testing.T) {
33         var uri string
34         var m Magnet
35         var err error
36
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)
40         if err != nil {
41                 t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
42         }
43
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)
48         }
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(info.Name, mi.HashInfoBytes())
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 }