11 func TestBinaryReadSliceOfPointers(t *testing.T) {
13 r := bytes.NewBufferString("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00")
15 t.Fatalf("expected 12 bytes left, but there %d", r.Len())
17 for _, data := range []*Integer{&msg.Index, &msg.Begin, &msg.Length} {
28 func TestConstants(t *testing.T) {
29 // check that iota works as expected in the const block
30 if NotInterested != 3 {
35 func TestBitfieldEncode(t *testing.T) {
36 bf := make([]bool, 37)
40 s := string(marshalBitfield(bf))
41 const expected = "\x21\x00\x00\x00\x80"
43 t.Fatalf("got %#v, expected %#v", s, expected)
47 func TestBitfieldUnmarshal(t *testing.T) {
48 bf := unmarshalBitfield([]byte("\x81\x06"))
49 expected := make([]bool, 16)
54 if len(bf) != len(expected) {
57 for i := range expected {
58 if bf[i] != expected[i] {
64 func TestHaveEncode(t *testing.T) {
65 actualBytes, err := Message{
72 actualString := string(actualBytes)
73 expected := "\x00\x00\x00\x05\x04\x00\x00\x00\x2a"
74 if actualString != expected {
75 t.Fatalf("expected %#v, got %#v", expected, actualString)
79 func TestShortRead(t *testing.T) {
81 R: bufio.NewReader(bytes.NewBufferString("\x00\x00\x00\x02\x00!")),
85 err := dec.Decode(msg)
86 if !strings.Contains(err.Error(), "short read") {
91 func TestUnexpectedEOF(t *testing.T) {
93 for _, stream := range []string{
94 "\x00\x00\x00", // Header truncated.
95 "\x00\x00\x00\x01", // Expecting 1 more byte.
96 // Request with wrong length, and too short anyway.
97 "\x00\x00\x00\x06\x06\x00\x00\x00\x00\x00",
99 "\x00\x00\x00\x0b\x06\x00\x00\x00\x00\x00",
102 R: bufio.NewReader(bytes.NewBufferString(stream)),
105 err := dec.Decode(msg)
106 if err != io.ErrUnexpectedEOF {