2019-12-24 10:39:22 -05:00
|
|
|
package handshake
|
2019-12-22 16:03:38 -05:00
|
|
|
|
|
|
|
|
import (
|
2019-12-26 21:53:11 -05:00
|
|
|
"bufio"
|
2019-12-22 16:03:38 -05:00
|
|
|
"bytes"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2019-12-24 11:05:22 -05:00
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
|
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}
|
|
|
|
|
h := New(infoHash, peerID)
|
|
|
|
|
expected := &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},
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, expected, h)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 10:39:22 -05:00
|
|
|
func TestSerialize(t *testing.T) {
|
2019-12-22 16:03:38 -05:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 10:39:22 -05:00
|
|
|
func TestRead(t *testing.T) {
|
2019-12-22 16:03:38 -05:00
|
|
|
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 {
|
2019-12-26 21:53:11 -05:00
|
|
|
reader := bufio.NewReader(bytes.NewReader(test.input))
|
2019-12-24 10:39:22 -05:00
|
|
|
m, err := Read(reader)
|
2019-12-22 16:03:38 -05:00
|
|
|
if test.fails {
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
} else {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, test.output, m)
|
|
|
|
|
}
|
|
|
|
|
}
|