1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00
Files
torrent-client/message/handshake_test.go
2019-12-22 16:03:38 -05:00

74 lines
2.2 KiB
Go

package message
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHandshakeSerialize(t *testing.T) {
tests := map[string]struct {
input *Handshake
output []byte
}{
"serialize message": {
input: &Handshake{
Pstr: "BitTorrent protocol",
InfoHash: [20]byte{134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44, 247, 23, 128, 49, 0, 116},
PeerID: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
},
output: []byte{19, 66, 105, 116, 84, 111, 114, 114, 101, 110, 116, 32, 112, 114, 111, 116, 111, 99, 111, 108, 0, 0, 0, 0, 0, 0, 0, 0, 134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44, 247, 23, 128, 49, 0, 116, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
},
}
for _, test := range tests {
buf := test.input.Serialize()
assert.Equal(t, test.output, buf)
}
}
func TestReadHandshake(t *testing.T) {
tests := map[string]struct {
input []byte
output *Handshake
fails bool
}{
"parse handshake into struct": {
input: []byte{19, 66, 105, 116, 84, 111, 114, 114, 101, 110, 116, 32, 112, 114, 111, 116, 111, 99, 111, 108, 0, 0, 0, 0, 0, 0, 0, 0, 134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44, 247, 23, 128, 49, 0, 116, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
output: &Handshake{
Pstr: "BitTorrent protocol",
InfoHash: [20]byte{134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44, 247, 23, 128, 49, 0, 116},
PeerID: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
},
fails: false,
},
"empty": {
input: []byte{},
output: nil,
fails: true,
},
"Not enough bytes": {
input: []byte{19, 66, 105, 116, 84, 111, 114, 114, 101, 110, 116, 32, 112, 114, 111, 116, 111, 99, 111},
output: nil,
fails: true,
},
"pstrlen is 0": {
input: []byte{0, 0, 0},
output: nil,
fails: true,
},
}
for _, test := range tests {
reader := bytes.NewReader(test.input)
m, err := ReadHandshake(reader)
if test.fails {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
assert.Equal(t, test.output, m)
}
}