]> Sergey Matveev's repositories - btrtrc.git/blobdiff - metainfo/magnet_test.go
Drop support for go 1.20
[btrtrc.git] / metainfo / magnet_test.go
index 065b662ffe6eb65f56f096f59735ac4872fae18a..24ab15b18e7ba88dd4c2fb53d8d3a6dd6443bdcc 100644 (file)
@@ -2,7 +2,6 @@ package metainfo
 
 import (
        "encoding/hex"
-       "reflect"
        "testing"
 
        "github.com/stretchr/testify/assert"
@@ -20,13 +19,15 @@ var (
        }
 )
 
+func init() {
+       hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
+}
+
 // Converting from our Magnet type to URL string.
 func TestMagnetString(t *testing.T) {
-       hex.Decode(exampleMagnet.InfoHash[:], []byte("51340689c960f0778a4387aef9b4b52fd08390cd"))
-       s := exampleMagnet.String()
-       if s != exampleMagnetURI {
-               t.Fatalf("\nexpected:\n\t%q\nactual\n\t%q", exampleMagnetURI, s)
-       }
+       m, err := ParseMagnetUri(exampleMagnet.String())
+       require.NoError(t, err)
+       assert.EqualValues(t, exampleMagnet, m)
 }
 
 func TestParseMagnetURI(t *testing.T) {
@@ -36,19 +37,18 @@ func TestParseMagnetURI(t *testing.T) {
 
        // parsing the legit Magnet URI with btih-formatted xt should not return errors
        uri = "magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU"
-       _, err = ParseMagnetURI(uri)
+       _, err = ParseMagnetUri(uri)
        if err != nil {
                t.Errorf("Attempting parsing the proper Magnet btih URI:\"%v\" failed with err: %v", uri, err)
        }
 
        // Checking if the magnet instance struct is built correctly from parsing
-       m, err = ParseMagnetURI(exampleMagnetURI)
-       if err != nil || !reflect.DeepEqual(exampleMagnet, m) {
-               t.Errorf("ParseMagnetURI(%s) returned %v, expected %v", uri, m, exampleMagnet)
-       }
+       m, err = ParseMagnetUri(exampleMagnetURI)
+       assert.EqualValues(t, exampleMagnet, m)
+       assert.NoError(t, err)
 
        // empty string URI case
-       _, err = ParseMagnetURI("")
+       _, err = ParseMagnetUri("")
        if err == nil {
                t.Errorf("Parsing empty string as URI should have returned an error but didn't")
        }
@@ -56,25 +56,26 @@ func TestParseMagnetURI(t *testing.T) {
        // only BTIH (BitTorrent info hash)-formatted magnet links are currently supported
        // must return error correctly when encountering other URN formats
        uri = "magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"
-       _, err = ParseMagnetURI(uri)
+       _, err = ParseMagnetUri(uri)
        if err == nil {
                t.Errorf("Magnet URI with non-BTIH URNs (like \"%v\") are not supported and should return an error", uri)
        }
 
        // resilience to the broken hash
        uri = "magnet:?xt=urn:btih:this hash is really broken"
-       _, err = ParseMagnetURI(uri)
+       _, err = ParseMagnetUri(uri)
        if err == nil {
                t.Errorf("Failed to detect broken Magnet URI: %v", uri)
        }
-
 }
 
-func Test_Magnetize(t *testing.T) {
+func TestMagnetize(t *testing.T) {
        mi, err := LoadFromFile("../testdata/bootstrap.dat.torrent")
        require.NoError(t, err)
 
-       m := mi.Magnet()
+       info, err := mi.UnmarshalInfo()
+       require.NoError(t, err)
+       m := mi.Magnet(nil, &info)
 
        assert.EqualValues(t, "bootstrap.dat", m.DisplayName)