]> Sergey Matveev's repositories - btrtrc.git/blob - peer_protocol/protocol_test.go
caebd8eff74ad65eb5bbe7f9c4d0cd4004869b5a
[btrtrc.git] / peer_protocol / protocol_test.go
1 package peer_protocol
2
3 import (
4         "testing"
5 )
6
7 func TestConstants(t *testing.T) {
8         // check that iota works as expected in the const block
9         if NotInterested != 3 {
10                 t.FailNow()
11         }
12 }
13
14 func TestBitfieldEncode(t *testing.T) {
15         bf := make([]bool, 37)
16         bf[2] = true
17         bf[7] = true
18         bf[32] = true
19         s := string(marshalBitfield(bf))
20         const expected = "\x21\x00\x00\x00\x80"
21         if s != expected {
22                 t.Fatalf("got %#v, expected %#v", s, expected)
23         }
24 }
25
26 func TestBitfieldUnmarshal(t *testing.T) {
27         bf := unmarshalBitfield([]byte("\x81\x06"))
28         expected := make([]bool, 16)
29         expected[0] = true
30         expected[7] = true
31         expected[13] = true
32         expected[14] = true
33         if len(bf) != len(expected) {
34                 t.FailNow()
35         }
36         for i := range expected {
37                 if bf[i] != expected[i] {
38                         t.FailNow()
39                 }
40         }
41 }
42
43 func TestHaveEncode(t *testing.T) {
44         actualBytes, err := Message{
45                 Type:  Have,
46                 Index: 42,
47         }.MarshalBinary()
48         if err != nil {
49                 t.Fatal(err)
50         }
51         actualString := string(actualBytes)
52         expected := "\x00\x00\x00\x05\x04\x00\x00\x00\x2a"
53         if actualString != expected {
54                 t.Fatalf("expected %#v, got %#v", expected, actualString)
55         }
56 }