9 "github.com/stretchr/testify/assert"
12 func TestBinaryReadSliceOfPointers(t *testing.T) {
14 r := bytes.NewBufferString("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00")
16 t.Fatalf("expected 12 bytes left, but there %d", r.Len())
18 for _, data := range []*Integer{&msg.Index, &msg.Begin, &msg.Length} {
29 func TestConstants(t *testing.T) {
30 assert.EqualValues(t, 3, NotInterested)
31 assert.EqualValues(t, 14, HaveAll)
34 func TestBitfieldEncode(t *testing.T) {
35 bf := make([]bool, 37)
39 s := string(marshalBitfield(bf))
40 const expected = "\x21\x00\x00\x00\x80"
42 t.Fatalf("got %#v, expected %#v", s, expected)
46 func TestBitfieldUnmarshal(t *testing.T) {
47 bf := unmarshalBitfield([]byte("\x81\x06"))
48 expected := make([]bool, 16)
53 if len(bf) != len(expected) {
56 for i := range expected {
57 if bf[i] != expected[i] {
63 func TestHaveEncode(t *testing.T) {
64 actualBytes, err := Message{
71 actualString := string(actualBytes)
72 expected := "\x00\x00\x00\x05\x04\x00\x00\x00\x2a"
73 if actualString != expected {
74 t.Fatalf("expected %#v, got %#v", expected, actualString)
78 func TestShortRead(t *testing.T) {
80 R: bufio.NewReader(bytes.NewBufferString("\x00\x00\x00\x02\x00!")),
84 err := dec.Decode(msg)
85 if !strings.Contains(err.Error(), "1 unused bytes in message type Choke") {
90 func TestUnexpectedEOF(t *testing.T) {
92 for _, stream := range []string{
93 "\x00\x00\x00", // Header truncated.
94 "\x00\x00\x00\x01", // Expecting 1 more byte.
95 // Request with wrong length, and too short anyway.
96 "\x00\x00\x00\x06\x06\x00\x00\x00\x00\x00",
98 "\x00\x00\x00\x0b\x06\x00\x00\x00\x00\x00",
101 R: bufio.NewReader(bytes.NewBufferString(stream)),
104 err := dec.Decode(msg)
106 t.Fatalf("expected an error decoding %q", stream)
111 func TestMarshalKeepalive(t *testing.T) {
116 t.Fatalf("error marshalling keepalive: %s", err)
119 const expected = "\x00\x00\x00\x00"
121 t.Fatalf("marshalled keepalive is %q, expected %q", bs, expected)
125 func TestMarshalPortMsg(t *testing.T) {
133 if string(b) != "\x00\x00\x00\x03\x09\xaa\xbb" {
138 func TestUnmarshalPortMsg(t *testing.T) {
141 R: bufio.NewReader(bytes.NewBufferString("\x00\x00\x00\x03\x09\xaa\xbb")),
151 if m.Port != 0xaabb {