You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package torrent
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestToTorrent(t *testing.T) {
|
|
tests := map[string]struct {
|
|
input *bencodeTorrent
|
|
output *Torrent
|
|
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",
|
|
},
|
|
},
|
|
output: &Torrent{
|
|
Announce: "http://bttracker.debian.org:6969/announce",
|
|
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{
|
|
{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",
|
|
},
|
|
},
|
|
output: nil,
|
|
fails: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
to, err := test.input.toTorrent()
|
|
if test.fails {
|
|
assert.NotNil(t, err)
|
|
} else {
|
|
assert.Nil(t, err)
|
|
}
|
|
assert.Equal(t, test.output, to)
|
|
}
|
|
}
|