1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00
Files
torrent-client/torrentfile/torrentfile_test.go

89 lines
2.3 KiB
Go
Raw Permalink Normal View History

2019-12-29 14:02:50 -05:00
package torrentfile
2019-12-22 13:46:51 -05:00
import (
"encoding/json"
"flag"
"io/ioutil"
2019-12-22 13:46:51 -05:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-12-22 13:46:51 -05:00
)
var update = flag.Bool("update", false, "update .golden.json files")
func TestOpen(t *testing.T) {
2020-01-02 12:20:41 -05:00
torrent, err := Open("testdata/archlinux-2019.12.01-x86_64.iso.torrent")
require.Nil(t, err)
goldenPath := "testdata/archlinux-2019.12.01-x86_64.iso.torrent.golden.json"
if *update {
serialized, err := json.MarshalIndent(torrent, "", " ")
require.Nil(t, err)
ioutil.WriteFile(goldenPath, serialized, 0644)
}
2020-01-02 10:27:00 -05:00
expected := TorrentFile{}
golden, err := ioutil.ReadFile(goldenPath)
require.Nil(t, err)
2020-01-02 10:27:00 -05:00
err = json.Unmarshal(golden, &expected)
require.Nil(t, err)
assert.Equal(t, expected, torrent)
}
2019-12-31 23:36:27 -05:00
func TestToTorrentFile(t *testing.T) {
2019-12-22 13:46:51 -05:00
tests := map[string]struct {
input *bencodeTorrent
2020-01-02 10:27:00 -05:00
output TorrentFile
2019-12-22 13:46:51 -05:00
fails bool
}{
"correct conversion": {
input: &bencodeTorrent{
Announce: "http://bttracker.debian.org:6969/announce",
Info: bencodeInfo{
Pieces: "1234567890abcdefghijabcdefghij1234567890",
PieceLength: 262144,
Length: 351272960,
Name: "debian-10.2.0-amd64-netinst.iso",
},
},
2020-01-02 10:27:00 -05:00
output: TorrentFile{
2019-12-22 13:46:51 -05:00
Announce: "http://bttracker.debian.org:6969/announce",
2019-12-22 17:43:39 -05:00
InfoHash: [20]byte{216, 247, 57, 206, 195, 40, 149, 108, 204, 91, 191, 31, 134, 217, 253, 207, 219, 168, 206, 182},
PieceHashes: [][20]byte{
2019-12-22 13:46:51 -05:00
{49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106},
{97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48},
},
PieceLength: 262144,
Length: 351272960,
Name: "debian-10.2.0-amd64-netinst.iso",
},
fails: false,
},
"not enough bytes in pieces": {
input: &bencodeTorrent{
Announce: "http://bttracker.debian.org:6969/announce",
Info: bencodeInfo{
Pieces: "1234567890abcdefghijabcdef", // Only 26 bytes
PieceLength: 262144,
Length: 351272960,
Name: "debian-10.2.0-amd64-netinst.iso",
},
},
2020-01-02 10:27:00 -05:00
output: TorrentFile{},
2019-12-22 13:46:51 -05:00
fails: true,
},
}
for _, test := range tests {
2019-12-31 23:36:27 -05:00
to, err := test.input.toTorrentFile()
2019-12-22 13:46:51 -05:00
if test.fails {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
assert.Equal(t, test.output, to)
}
}