2013-09-29 06:11:24 +08:00
|
|
|
package peer_protocol
|
2013-09-26 11:43:46 +08:00
|
|
|
|
|
|
|
import (
|
2014-03-20 21:42:40 +08:00
|
|
|
"bufio"
|
2014-03-17 22:44:22 +08:00
|
|
|
"bytes"
|
2014-03-20 21:42:40 +08:00
|
|
|
"strings"
|
2013-09-26 11:43:46 +08:00
|
|
|
"testing"
|
2018-02-05 15:00:47 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2013-09-26 11:43:46 +08:00
|
|
|
)
|
|
|
|
|
2014-03-17 22:44:22 +08:00
|
|
|
func TestBinaryReadSliceOfPointers(t *testing.T) {
|
|
|
|
var msg Message
|
|
|
|
r := bytes.NewBufferString("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00")
|
|
|
|
if r.Len() != 12 {
|
|
|
|
t.Fatalf("expected 12 bytes left, but there %d", r.Len())
|
|
|
|
}
|
|
|
|
for _, data := range []*Integer{&msg.Index, &msg.Begin, &msg.Length} {
|
|
|
|
err := data.Read(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.Len() != 0 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 11:43:46 +08:00
|
|
|
func TestConstants(t *testing.T) {
|
2018-02-05 15:00:47 +08:00
|
|
|
assert.EqualValues(t, 3, NotInterested)
|
|
|
|
assert.EqualValues(t, 14, HaveAll)
|
2013-09-26 11:43:46 +08:00
|
|
|
}
|
2013-10-01 16:43:18 +08:00
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
func TestBitfieldEncode(t *testing.T) {
|
2013-10-01 16:43:18 +08:00
|
|
|
bf := make([]bool, 37)
|
|
|
|
bf[2] = true
|
|
|
|
bf[7] = true
|
|
|
|
bf[32] = true
|
|
|
|
s := string(marshalBitfield(bf))
|
2013-09-30 19:51:08 +08:00
|
|
|
const expected = "\x21\x00\x00\x00\x80"
|
|
|
|
if s != expected {
|
|
|
|
t.Fatalf("got %#v, expected %#v", s, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 16:43:18 +08:00
|
|
|
func TestBitfieldUnmarshal(t *testing.T) {
|
|
|
|
bf := unmarshalBitfield([]byte("\x81\x06"))
|
|
|
|
expected := make([]bool, 16)
|
|
|
|
expected[0] = true
|
|
|
|
expected[7] = true
|
|
|
|
expected[13] = true
|
|
|
|
expected[14] = true
|
|
|
|
if len(bf) != len(expected) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for i := range expected {
|
|
|
|
if bf[i] != expected[i] {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
func TestHaveEncode(t *testing.T) {
|
2013-10-01 16:43:18 +08:00
|
|
|
actualBytes, err := Message{
|
|
|
|
Type: Have,
|
|
|
|
Index: 42,
|
|
|
|
}.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actualString := string(actualBytes)
|
2013-10-06 13:05:03 +08:00
|
|
|
expected := "\x00\x00\x00\x05\x04\x00\x00\x00\x2a"
|
2013-10-01 16:43:18 +08:00
|
|
|
if actualString != expected {
|
|
|
|
t.Fatalf("expected %#v, got %#v", expected, actualString)
|
2013-09-30 19:51:08 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-20 21:42:40 +08:00
|
|
|
|
|
|
|
func TestShortRead(t *testing.T) {
|
|
|
|
dec := Decoder{
|
|
|
|
R: bufio.NewReader(bytes.NewBufferString("\x00\x00\x00\x02\x00!")),
|
|
|
|
MaxLength: 2,
|
|
|
|
}
|
|
|
|
msg := new(Message)
|
|
|
|
err := dec.Decode(msg)
|
2014-07-18 00:38:32 +08:00
|
|
|
if !strings.Contains(err.Error(), "1 bytes unused in message type 0") {
|
2014-03-20 21:42:40 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnexpectedEOF(t *testing.T) {
|
|
|
|
msg := new(Message)
|
|
|
|
for _, stream := range []string{
|
|
|
|
"\x00\x00\x00", // Header truncated.
|
|
|
|
"\x00\x00\x00\x01", // Expecting 1 more byte.
|
|
|
|
// Request with wrong length, and too short anyway.
|
|
|
|
"\x00\x00\x00\x06\x06\x00\x00\x00\x00\x00",
|
|
|
|
// Request truncated.
|
|
|
|
"\x00\x00\x00\x0b\x06\x00\x00\x00\x00\x00",
|
|
|
|
} {
|
|
|
|
dec := Decoder{
|
|
|
|
R: bufio.NewReader(bytes.NewBufferString(stream)),
|
|
|
|
MaxLength: 42,
|
|
|
|
}
|
|
|
|
err := dec.Decode(msg)
|
2014-08-22 15:39:18 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error decoding %q", stream)
|
2014-03-20 21:42:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 23:22:51 +08:00
|
|
|
|
|
|
|
func TestMarshalKeepalive(t *testing.T) {
|
|
|
|
b, err := (Message{
|
|
|
|
Keepalive: true,
|
|
|
|
}).MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marshalling keepalive: %s", err)
|
|
|
|
}
|
|
|
|
bs := string(b)
|
|
|
|
const expected = "\x00\x00\x00\x00"
|
|
|
|
if bs != expected {
|
|
|
|
t.Fatalf("marshalled keepalive is %q, expected %q", bs, expected)
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 20:12:16 +08:00
|
|
|
|
|
|
|
func TestMarshalPortMsg(t *testing.T) {
|
|
|
|
b, err := (Message{
|
|
|
|
Type: Port,
|
|
|
|
Port: 0xaabb,
|
|
|
|
}).MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(b) != "\x00\x00\x00\x03\x09\xaa\xbb" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalPortMsg(t *testing.T) {
|
|
|
|
var m Message
|
|
|
|
d := Decoder{
|
|
|
|
R: bufio.NewReader(bytes.NewBufferString("\x00\x00\x00\x03\x09\xaa\xbb")),
|
|
|
|
MaxLength: 8,
|
|
|
|
}
|
|
|
|
err := d.Decode(&m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if m.Type != Port {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if m.Port != 0xaabb {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|